2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <boost/foreach.hpp>
28 #include <QMouseEvent>
37 #include "pv/sigsession.h"
38 #include "pv/data/logic.h"
39 #include "pv/data/logicsnapshot.h"
41 using namespace boost;
47 const double View::MaxScale = 1e9;
48 const double View::MinScale = 1e-15;
50 const int View::LabelMarginWidth = 70;
51 const int View::RulerHeight = 30;
53 const int View::MaxScrollValue = INT_MAX / 2;
55 const int View::SignalHeight = 30;
56 const int View::SignalMargin = 10;
57 const int View::SignalSnapGridSize = 10;
59 const QColor View::CursorAreaColour(220, 231, 243);
61 const QSizeF View::LabelPadding(4, 0);
63 View::View(SigSession &session, QWidget *parent) :
64 QAbstractScrollArea(parent),
66 _viewport(new Viewport(*this)),
67 _ruler(new Ruler(*this)),
68 _header(new Header(*this)),
73 _updating_scroll(false),
78 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
79 this, SLOT(h_scroll_value_changed(int)));
80 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
81 this, SLOT(v_scroll_value_changed(int)));
83 connect(&_session, SIGNAL(signals_changed()),
84 this, SLOT(signals_changed()));
85 connect(&_session, SIGNAL(data_updated()),
86 this, SLOT(data_updated()));
88 connect(&_cursors.first(), SIGNAL(time_changed()),
89 this, SLOT(marker_time_changed()));
90 connect(&_cursors.second(), SIGNAL(time_changed()),
91 this, SLOT(marker_time_changed()));
93 connect(_header, SIGNAL(signals_moved()),
94 this, SLOT(on_signals_moved()));
96 connect(_header, SIGNAL(selection_changed()),
97 _ruler, SLOT(clear_selection()));
98 connect(_ruler, SIGNAL(selection_changed()),
99 _header, SLOT(clear_selection()));
101 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
102 setViewport(_viewport);
104 _viewport->installEventFilter(this);
105 _ruler->installEventFilter(this);
106 _header->installEventFilter(this);
109 SigSession& View::session()
114 double View::scale() const
119 double View::offset() const
124 int View::v_offset() const
129 void View::zoom(double steps)
131 zoom(steps, (width() - LabelMarginWidth) / 2);
134 void View::zoom(double steps, int offset)
136 const double cursor_offset = _offset + _scale * offset;
137 _scale *= pow(3.0/2.0, -steps);
138 _scale = max(min(_scale, MaxScale), MinScale);
139 _offset = cursor_offset - _scale * offset;
147 void View::set_scale_offset(double scale, double offset)
157 bool View::cursors_shown() const
159 return _show_cursors;
162 void View::show_cursors(bool show)
164 _show_cursors = show;
169 void View::centre_cursors()
171 const double time_width = _scale * _viewport->width();
172 _cursors.first().set_time(_offset + time_width * 0.4);
173 _cursors.second().set_time(_offset + time_width * 0.6);
178 CursorPair& View::cursors()
183 const QPoint& View::hover_point() const
188 void View::normalize_layout()
190 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
193 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
194 v_min = min(s->get_v_offset(), v_min);
196 const int delta = -min(v_min, 0);
197 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
198 s->set_v_offset(s->get_v_offset() + delta);
200 verticalScrollBar()->setSliderPosition(_v_offset + delta);
201 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
204 void View::get_scroll_layout(double &length, double &offset) const
206 const shared_ptr<data::SignalData> sig_data = _session.get_data();
210 length = _data_length / (sig_data->get_samplerate() * _scale);
211 offset = _offset / _scale;
214 void View::update_scroll()
218 const QSize areaSize = _viewport->size();
220 // Set the horizontal scroll bar
221 double length = 0, offset = 0;
222 get_scroll_layout(length, offset);
223 length = max(length - areaSize.width(), 0.0);
225 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
227 _updating_scroll = true;
229 if (length < MaxScrollValue) {
230 horizontalScrollBar()->setRange(0, length);
231 horizontalScrollBar()->setSliderPosition(offset);
233 horizontalScrollBar()->setRange(0, MaxScrollValue);
234 horizontalScrollBar()->setSliderPosition(
235 _offset * MaxScrollValue / (_scale * length));
238 _updating_scroll = false;
240 // Set the vertical scrollbar
241 verticalScrollBar()->setPageStep(areaSize.height());
242 verticalScrollBar()->setRange(0,
243 _viewport->get_total_height() + SignalMargin -
247 void View::reset_signal_layout()
249 int offset = SignalMargin + SignalHeight;
250 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
251 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
252 s->set_v_offset(offset);
253 offset += SignalHeight + 2 * SignalMargin;
259 bool View::eventFilter(QObject *object, QEvent *event)
261 const QEvent::Type type = event->type();
262 if (type == QEvent::MouseMove) {
264 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
265 if (object == _viewport)
266 _hover_point = mouse_event->pos();
267 else if (object == _ruler)
268 _hover_point = QPoint(mouse_event->x(), 0);
269 else if (object == _header)
270 _hover_point = QPoint(0, mouse_event->y());
272 _hover_point = QPoint(-1, -1);
274 hover_point_changed();
276 } else if (type == QEvent::Leave) {
277 _hover_point = QPoint(-1, -1);
278 hover_point_changed();
281 return QObject::eventFilter(object, event);
284 bool View::viewportEvent(QEvent *e)
288 case QEvent::MouseButtonPress:
289 case QEvent::MouseButtonRelease:
290 case QEvent::MouseButtonDblClick:
291 case QEvent::MouseMove:
296 return QAbstractScrollArea::viewportEvent(e);
300 void View::resizeEvent(QResizeEvent*)
302 _ruler->setGeometry(_viewport->x(), 0,
303 _viewport->width(), _viewport->y());
304 _header->setGeometry(0, _viewport->y(),
305 _viewport->x(), _viewport->height());
309 void View::h_scroll_value_changed(int value)
311 if (_updating_scroll)
314 const int range = horizontalScrollBar()->maximum();
315 if (range < MaxScrollValue)
316 _offset = _scale * value;
318 double length = 0, offset;
319 get_scroll_layout(length, offset);
320 _offset = _scale * length * value / MaxScrollValue;
327 void View::v_scroll_value_changed(int value)
334 void View::signals_changed()
336 reset_signal_layout();
339 void View::data_updated()
341 // Get the new data length
343 shared_ptr<data::Logic> sig_data = _session.get_data();
345 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
346 sig_data->get_snapshots();
347 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
349 _data_length = max(_data_length,
350 s->get_sample_count());
353 // Update the scroll bars
360 void View::marker_time_changed()
366 void View::on_signals_moved()