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
22 #include <libsigrokdecode/libsigrokdecode.h>
31 #include <QMouseEvent>
34 #include "cursorheader.h"
35 #include "decodetrace.h"
42 #include "pv/sigsession.h"
43 #include "pv/data/logic.h"
44 #include "pv/data/logicsnapshot.h"
46 using boost::shared_lock;
47 using boost::shared_mutex;
48 using pv::data::SignalData;
49 using std::back_inserter;
52 using std::lock_guard;
58 using std::shared_ptr;
65 const double View::MaxScale = 1e9;
66 const double View::MinScale = 1e-15;
68 const int View::MaxScrollValue = INT_MAX / 2;
70 const int View::SignalHeight = 30;
71 const int View::SignalMargin = 10;
73 const QColor View::CursorAreaColour(220, 231, 243);
75 const QSizeF View::LabelPadding(4, 0);
77 View::View(SigSession &session, QWidget *parent) :
78 QAbstractScrollArea(parent),
80 _viewport(new Viewport(*this)),
81 _ruler(new Ruler(*this)),
82 _cursorheader(new CursorHeader(*this)),
83 _header(new Header(*this)),
87 _updating_scroll(false),
92 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
93 this, SLOT(h_scroll_value_changed(int)));
94 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
95 this, SLOT(v_scroll_value_changed(int)));
97 connect(&_session, SIGNAL(signals_changed()),
98 this, SLOT(signals_changed()));
99 connect(&_session, SIGNAL(capture_state_changed(int)),
100 this, SLOT(data_updated()));
101 connect(&_session, SIGNAL(data_received()),
102 this, SLOT(data_updated()));
103 connect(&_session, SIGNAL(frame_ended()),
104 this, SLOT(data_updated()));
106 connect(_cursors.first().get(), SIGNAL(time_changed()),
107 this, SLOT(marker_time_changed()));
108 connect(_cursors.second().get(), SIGNAL(time_changed()),
109 this, SLOT(marker_time_changed()));
111 connect(_header, SIGNAL(geometry_updated()),
112 this, SLOT(on_geometry_updated()));
113 connect(_header, SIGNAL(signals_moved()),
114 this, SLOT(on_signals_moved()));
116 connect(_header, SIGNAL(selection_changed()),
117 _cursorheader, SLOT(clear_selection()));
118 connect(_cursorheader, SIGNAL(selection_changed()),
119 _header, SLOT(clear_selection()));
121 connect(_header, SIGNAL(selection_changed()),
122 this, SIGNAL(selection_changed()));
123 connect(_cursorheader, SIGNAL(selection_changed()),
124 this, SIGNAL(selection_changed()));
126 connect(this, SIGNAL(hover_point_changed()),
127 this, SLOT(on_hover_point_changed()));
129 setViewport(_viewport);
131 _viewport->installEventFilter(this);
132 _ruler->installEventFilter(this);
133 _cursorheader->installEventFilter(this);
134 _header->installEventFilter(this);
136 // Trigger the initial event manually. The default device has signals
137 // which were created before this object came into being
140 // make sure the transparent widgets are on the top
141 _cursorheader->raise();
145 SigSession& View::session()
150 const SigSession& View::session() const
160 const View* View::view() const
165 Viewport* View::viewport()
170 const Viewport* View::viewport() const
175 double View::scale() const
180 double View::offset() const
185 int View::owner_v_offset() const
190 void View::zoom(double steps)
192 zoom(steps, _viewport->width() / 2);
195 void View::zoom(double steps, int offset)
197 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
200 void View::zoom_fit()
202 const pair<double, double> extents = get_time_extents();
203 const double delta = extents.second - extents.first;
208 const int w = _viewport->width();
212 const double scale = max(min(delta / w, MaxScale), MinScale);
213 set_scale_offset(scale, extents.first);
216 void View::zoom_one_to_one()
218 using pv::data::SignalData;
220 // Make a set of all the visible data objects
221 set< shared_ptr<SignalData> > visible_data = get_visible_data();
222 if (visible_data.empty())
225 double samplerate = 0.0;
226 for (const shared_ptr<SignalData> d : visible_data) {
228 samplerate = max(samplerate, d->samplerate());
231 if (samplerate == 0.0)
235 const int w = _viewport->width();
239 set_zoom(1.0 / samplerate, w / 2);
242 void View::set_scale_offset(double scale, double offset)
249 _cursorheader->update();
251 scale_offset_changed();
254 set< shared_ptr<SignalData> > View::get_visible_data() const
256 shared_lock<shared_mutex> lock(session().signals_mutex());
257 const vector< shared_ptr<Signal> > &sigs(session().signals());
259 // Make a set of all the visible data objects
260 set< shared_ptr<SignalData> > visible_data;
261 for (const shared_ptr<Signal> sig : sigs)
263 visible_data.insert(sig->data());
268 pair<double, double> View::get_time_extents() const
270 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
271 if (visible_data.empty())
272 return make_pair(0.0, 0.0);
274 double left_time = DBL_MAX, right_time = DBL_MIN;
275 for (const shared_ptr<SignalData> d : visible_data)
277 const double start_time = d->get_start_time();
278 double samplerate = d->samplerate();
279 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
281 left_time = min(left_time, start_time);
282 right_time = max(right_time, start_time +
283 d->get_max_sample_count() / samplerate);
286 assert(left_time < right_time);
287 return make_pair(left_time, right_time);
290 bool View::cursors_shown() const
292 return _show_cursors;
295 void View::show_cursors(bool show)
297 _show_cursors = show;
298 _cursorheader->update();
302 void View::centre_cursors()
304 const double time_width = _scale * _viewport->width();
305 _cursors.first()->set_time(_offset + time_width * 0.4);
306 _cursors.second()->set_time(_offset + time_width * 0.6);
307 _cursorheader->update();
311 CursorPair& View::cursors()
316 const CursorPair& View::cursors() const
321 const QPoint& View::hover_point() const
326 void View::normalize_layout()
329 for (const shared_ptr<RowItem> r : *this)
330 v_min = min(r->v_offset(), v_min);
332 const int delta = -min(v_min, 0);
333 for (shared_ptr<RowItem> r : *this)
334 r->set_v_offset(r->v_offset() + delta);
336 verticalScrollBar()->setSliderPosition(_v_offset + delta);
337 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
340 void View::update_viewport()
346 void View::get_scroll_layout(double &length, double &offset) const
348 const pair<double, double> extents = get_time_extents();
349 length = (extents.second - extents.first) / _scale;
350 offset = _offset / _scale;
353 void View::set_zoom(double scale, int offset)
355 const double cursor_offset = _offset + _scale * offset;
356 const double new_scale = max(min(scale, MaxScale), MinScale);
357 const double new_offset = cursor_offset - new_scale * offset;
358 set_scale_offset(new_scale, new_offset);
361 void View::update_scroll()
365 const QSize areaSize = _viewport->size();
367 // Set the horizontal scroll bar
368 double length = 0, offset = 0;
369 get_scroll_layout(length, offset);
370 length = max(length - areaSize.width(), 0.0);
372 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
374 _updating_scroll = true;
376 if (length < MaxScrollValue) {
377 horizontalScrollBar()->setRange(0, length);
378 horizontalScrollBar()->setSliderPosition(offset);
380 horizontalScrollBar()->setRange(0, MaxScrollValue);
381 horizontalScrollBar()->setSliderPosition(
382 _offset * MaxScrollValue / (_scale * length));
385 _updating_scroll = false;
387 // Set the vertical scrollbar
388 verticalScrollBar()->setPageStep(areaSize.height());
389 verticalScrollBar()->setRange(0,
390 _viewport->get_total_height() + SignalMargin -
394 void View::update_layout()
397 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
398 _ruler->sizeHint().height(), 0, 0);
399 _ruler->setGeometry(_viewport->x(), 0,
400 _viewport->width(), _viewport->y());
401 _cursorheader->setGeometry(
403 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
404 _viewport->width(), _cursorheader->sizeHint().height());
405 _header->setGeometry(0, _viewport->y(),
406 _header->sizeHint().width(), _viewport->height());
410 void View::paint_label(QPainter &p, int right, bool hover)
417 QRectF View::label_rect(int right)
423 bool View::eventFilter(QObject *object, QEvent *event)
425 const QEvent::Type type = event->type();
426 if (type == QEvent::MouseMove) {
428 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
429 if (object == _viewport)
430 _hover_point = mouse_event->pos();
431 else if (object == _ruler || object == _cursorheader)
432 _hover_point = QPoint(mouse_event->x(), 0);
433 else if (object == _header)
434 _hover_point = QPoint(0, mouse_event->y());
436 _hover_point = QPoint(-1, -1);
438 hover_point_changed();
440 } else if (type == QEvent::Leave) {
441 _hover_point = QPoint(-1, -1);
442 hover_point_changed();
445 return QObject::eventFilter(object, event);
448 bool View::viewportEvent(QEvent *e)
452 case QEvent::MouseButtonPress:
453 case QEvent::MouseButtonRelease:
454 case QEvent::MouseButtonDblClick:
455 case QEvent::MouseMove:
457 case QEvent::TouchBegin:
458 case QEvent::TouchUpdate:
459 case QEvent::TouchEnd:
463 return QAbstractScrollArea::viewportEvent(e);
467 void View::resizeEvent(QResizeEvent*)
472 void View::h_scroll_value_changed(int value)
474 if (_updating_scroll)
477 const int range = horizontalScrollBar()->maximum();
478 if (range < MaxScrollValue)
479 _offset = _scale * value;
481 double length = 0, offset;
482 get_scroll_layout(length, offset);
483 _offset = _scale * length * value / MaxScrollValue;
487 _cursorheader->update();
491 void View::v_scroll_value_changed(int value)
498 void View::signals_changed()
500 // Populate the traces
503 shared_lock<shared_mutex> lock(session().signals_mutex());
504 const vector< shared_ptr<Signal> > &sigs(session().signals());
509 const vector< shared_ptr<DecodeTrace> > decode_sigs(
510 session().get_decode_signals());
511 for (auto s : decode_sigs)
515 // Create the initial layout
516 int offset = SignalMargin + SignalHeight;
517 for (shared_ptr<RowItem> r : *this) {
518 r->set_v_offset(offset);
519 offset += SignalHeight + 2 * SignalMargin;
525 // Update the child widgets
526 _header->signals_updated();
527 _viewport->signals_updated();
530 void View::data_updated()
532 // Update the scroll bars
539 void View::marker_time_changed()
541 _cursorheader->update();
545 void View::on_signals_moved()
551 void View::on_geometry_updated()
556 void View::on_hover_point_changed()
558 for (shared_ptr<RowItem> r : *this)
559 r->hover_point_changed();