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>
29 #include <unordered_set>
32 #include <QMouseEvent>
35 #include "cursorheader.h"
36 #include "decodetrace.h"
43 #include "pv/sigsession.h"
44 #include "pv/data/logic.h"
45 #include "pv/data/logicsnapshot.h"
47 using boost::shared_lock;
48 using boost::shared_mutex;
49 using pv::data::SignalData;
50 using std::back_inserter;
53 using std::lock_guard;
59 using std::shared_ptr;
60 using std::unordered_set;
67 const double View::MaxScale = 1e9;
68 const double View::MinScale = 1e-15;
70 const int View::MaxScrollValue = INT_MAX / 2;
72 const QColor View::CursorAreaColour(220, 231, 243);
74 const QSizeF View::LabelPadding(4, 0);
76 View::View(SigSession &session, QWidget *parent) :
77 QAbstractScrollArea(parent),
79 _viewport(new Viewport(*this)),
80 _ruler(new Ruler(*this)),
81 _cursorheader(new CursorHeader(*this)),
82 _header(new Header(*this)),
86 _updating_scroll(false),
91 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
92 this, SLOT(h_scroll_value_changed(int)));
93 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
94 this, SLOT(v_scroll_value_changed(int)));
96 connect(&_session, SIGNAL(signals_changed()),
97 this, SLOT(signals_changed()));
98 connect(&_session, SIGNAL(capture_state_changed(int)),
99 this, SLOT(data_updated()));
100 connect(&_session, SIGNAL(data_received()),
101 this, SLOT(data_updated()));
102 connect(&_session, SIGNAL(frame_ended()),
103 this, SLOT(data_updated()));
105 connect(_cursors.first().get(), SIGNAL(time_changed()),
106 this, SLOT(marker_time_changed()));
107 connect(_cursors.second().get(), SIGNAL(time_changed()),
108 this, SLOT(marker_time_changed()));
110 connect(_header, SIGNAL(signals_moved()),
111 this, SLOT(on_signals_moved()));
113 connect(_header, SIGNAL(selection_changed()),
114 _cursorheader, SLOT(clear_selection()));
115 connect(_cursorheader, SIGNAL(selection_changed()),
116 _header, SLOT(clear_selection()));
118 connect(_header, SIGNAL(selection_changed()),
119 this, SIGNAL(selection_changed()));
120 connect(_cursorheader, SIGNAL(selection_changed()),
121 this, SIGNAL(selection_changed()));
123 connect(this, SIGNAL(hover_point_changed()),
124 this, SLOT(on_hover_point_changed()));
126 connect(&_lazy_event_handler, SIGNAL(timeout()),
127 this, SLOT(process_sticky_events()));
128 _lazy_event_handler.setSingleShot(true);
130 setViewport(_viewport);
132 _viewport->installEventFilter(this);
133 _ruler->installEventFilter(this);
134 _cursorheader->installEventFilter(this);
135 _header->installEventFilter(this);
137 // Trigger the initial event manually. The default device has signals
138 // which were created before this object came into being
141 // make sure the transparent widgets are on the top
142 _cursorheader->raise();
146 SigSession& View::session()
151 const SigSession& View::session() const
161 const View* View::view() const
166 Viewport* View::viewport()
171 const Viewport* View::viewport() const
176 double View::scale() const
181 double View::offset() const
186 int View::owner_visual_v_offset() const
191 unsigned int View::depth() const
196 void View::zoom(double steps)
198 zoom(steps, _viewport->width() / 2);
201 void View::zoom(double steps, int offset)
203 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
206 void View::zoom_fit()
208 const pair<double, double> extents = get_time_extents();
209 const double delta = extents.second - extents.first;
214 const int w = _viewport->width();
218 const double scale = max(min(delta / w, MaxScale), MinScale);
219 set_scale_offset(scale, extents.first);
222 void View::zoom_one_to_one()
224 using pv::data::SignalData;
226 // Make a set of all the visible data objects
227 set< shared_ptr<SignalData> > visible_data = get_visible_data();
228 if (visible_data.empty())
231 double samplerate = 0.0;
232 for (const shared_ptr<SignalData> d : visible_data) {
234 samplerate = max(samplerate, d->samplerate());
237 if (samplerate == 0.0)
241 const int w = _viewport->width();
245 set_zoom(1.0 / samplerate, w / 2);
248 void View::set_scale_offset(double scale, double offset)
255 _cursorheader->update();
257 scale_offset_changed();
260 set< shared_ptr<SignalData> > View::get_visible_data() const
262 shared_lock<shared_mutex> lock(session().signals_mutex());
263 const vector< shared_ptr<Signal> > &sigs(session().signals());
265 // Make a set of all the visible data objects
266 set< shared_ptr<SignalData> > visible_data;
267 for (const shared_ptr<Signal> sig : sigs)
269 visible_data.insert(sig->data());
274 pair<double, double> View::get_time_extents() const
276 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
277 if (visible_data.empty())
278 return make_pair(0.0, 0.0);
280 double left_time = DBL_MAX, right_time = DBL_MIN;
281 for (const shared_ptr<SignalData> d : visible_data)
283 const double start_time = d->get_start_time();
284 double samplerate = d->samplerate();
285 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
287 left_time = min(left_time, start_time);
288 right_time = max(right_time, start_time +
289 d->get_max_sample_count() / samplerate);
292 assert(left_time < right_time);
293 return make_pair(left_time, right_time);
296 bool View::cursors_shown() const
298 return _show_cursors;
301 void View::show_cursors(bool show)
303 _show_cursors = show;
304 _cursorheader->update();
308 void View::centre_cursors()
310 const double time_width = _scale * _viewport->width();
311 _cursors.first()->set_time(_offset + time_width * 0.4);
312 _cursors.second()->set_time(_offset + time_width * 0.6);
313 _cursorheader->update();
317 CursorPair& View::cursors()
322 const CursorPair& View::cursors() const
327 const QPoint& View::hover_point() const
332 void View::update_viewport()
339 void View::restack_all_row_items()
341 // Make a set of owners
342 unordered_set< RowItemOwner* > owners;
343 for (const auto &r : *this)
344 owners.insert(r->owner());
346 // Make a list that is sorted from deepest first
347 vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end());
348 sort(sorted_owners.begin(), sorted_owners.end(),
349 [](const RowItemOwner* a, const RowItemOwner *b) {
350 return a->depth() > b->depth(); });
352 // Restack the items recursively
353 for (auto &o : sorted_owners)
356 // Animate the items to their destination
357 for (const auto &r : *this)
358 r->animate_to_layout_v_offset();
361 void View::get_scroll_layout(double &length, double &offset) const
363 const pair<double, double> extents = get_time_extents();
364 length = (extents.second - extents.first) / _scale;
365 offset = _offset / _scale;
368 void View::set_zoom(double scale, int offset)
370 const double cursor_offset = _offset + _scale * offset;
371 const double new_scale = max(min(scale, MaxScale), MinScale);
372 const double new_offset = cursor_offset - new_scale * offset;
373 set_scale_offset(new_scale, new_offset);
376 void View::update_scroll()
380 const QSize areaSize = _viewport->size();
382 // Set the horizontal scroll bar
383 double length = 0, offset = 0;
384 get_scroll_layout(length, offset);
385 length = max(length - areaSize.width(), 0.0);
387 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
389 _updating_scroll = true;
391 if (length < MaxScrollValue) {
392 horizontalScrollBar()->setRange(0, length);
393 horizontalScrollBar()->setSliderPosition(offset);
395 horizontalScrollBar()->setRange(0, MaxScrollValue);
396 horizontalScrollBar()->setSliderPosition(
397 _offset * MaxScrollValue / (_scale * length));
400 _updating_scroll = false;
402 // Set the vertical scrollbar
403 verticalScrollBar()->setPageStep(areaSize.height());
405 const pair<int, int> extents = v_extents();
406 const int extra_scroll_height = (extents.second - extents.first) / 4;
407 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
408 extents.first + extra_scroll_height);
411 void View::update_layout()
414 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
415 _ruler->sizeHint().height(), 0, 0);
416 _ruler->setGeometry(_viewport->x(), 0,
417 _viewport->width(), _viewport->y());
418 _cursorheader->setGeometry(
420 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
421 _viewport->width(), _cursorheader->sizeHint().height());
422 _header->setGeometry(0, _viewport->y(),
423 _header->sizeHint().width(), _viewport->height());
427 void View::paint_label(QPainter &p, int right, bool hover)
434 QRectF View::label_rect(int right)
440 bool View::eventFilter(QObject *object, QEvent *event)
442 const QEvent::Type type = event->type();
443 if (type == QEvent::MouseMove) {
445 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
446 if (object == _viewport)
447 _hover_point = mouse_event->pos();
448 else if (object == _ruler || object == _cursorheader)
449 _hover_point = QPoint(mouse_event->x(), 0);
450 else if (object == _header)
451 _hover_point = QPoint(0, mouse_event->y());
453 _hover_point = QPoint(-1, -1);
455 hover_point_changed();
457 } else if (type == QEvent::Leave) {
458 _hover_point = QPoint(-1, -1);
459 hover_point_changed();
462 return QObject::eventFilter(object, event);
465 bool View::viewportEvent(QEvent *e)
469 case QEvent::MouseButtonPress:
470 case QEvent::MouseButtonRelease:
471 case QEvent::MouseButtonDblClick:
472 case QEvent::MouseMove:
474 case QEvent::TouchBegin:
475 case QEvent::TouchUpdate:
476 case QEvent::TouchEnd:
480 return QAbstractScrollArea::viewportEvent(e);
484 void View::resizeEvent(QResizeEvent*)
489 void View::appearance_changed(bool label, bool content)
497 void View::extents_changed(bool horz, bool vert)
500 (horz ? SelectableItemHExtentsChanged : 0) |
501 (vert ? SelectableItemVExtentsChanged : 0);
502 _lazy_event_handler.start();
505 void View::h_scroll_value_changed(int value)
507 if (_updating_scroll)
510 const int range = horizontalScrollBar()->maximum();
511 if (range < MaxScrollValue)
512 _offset = _scale * value;
514 double length = 0, offset;
515 get_scroll_layout(length, offset);
516 _offset = _scale * length * value / MaxScrollValue;
520 _cursorheader->update();
524 void View::v_scroll_value_changed(int value)
531 void View::signals_changed()
533 // Populate the traces
536 shared_lock<shared_mutex> lock(session().signals_mutex());
537 const vector< shared_ptr<Signal> > &sigs(session().signals());
542 const vector< shared_ptr<DecodeTrace> > decode_sigs(
543 session().get_decode_signals());
544 for (auto s : decode_sigs)
548 // Create the initial layout
550 for (shared_ptr<RowItem> r : *this) {
551 const pair<int, int> extents = r->v_extents();
553 offset += -extents.first;
554 r->force_to_v_offset(offset);
556 offset += extents.second;
562 void View::data_updated()
564 // Update the scroll bars
571 void View::marker_time_changed()
573 _cursorheader->update();
577 void View::on_signals_moved()
583 void View::process_sticky_events()
585 if (_sticky_events & SelectableItemHExtentsChanged)
587 if (_sticky_events & SelectableItemVExtentsChanged)
588 restack_all_row_items();
590 // Clear the sticky events
594 void View::on_hover_point_changed()
596 for (shared_ptr<RowItem> r : *this)
597 r->hover_point_changed();