]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
TraceGroup: Implemented stacking
[pulseview.git] / pv / view / view.cpp
CommitLineData
adb4b10c 1/*
b3f22de0 2 * This file is part of the PulseView project.
adb4b10c
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
269528f5 21#ifdef ENABLE_DECODE
4e5a4405 22#include <libsigrokdecode/libsigrokdecode.h>
269528f5 23#endif
4e5a4405 24
c3a740dd
JH
25#include <cassert>
26#include <climits>
27#include <cmath>
28#include <mutex>
7ff0145f 29#include <unordered_set>
adb4b10c 30
adb4b10c 31#include <QEvent>
cbd80f64 32#include <QMouseEvent>
adb4b10c
JH
33#include <QScrollBar>
34
84a0d458 35#include "cursorheader.h"
b9329558 36#include "decodetrace.h"
1d8dca91 37#include "header.h"
ccdd3ef5 38#include "ruler.h"
2e575351 39#include "signal.h"
cdf7bea7
JH
40#include "view.h"
41#include "viewport.h"
adb4b10c 42
1b1ec774
JH
43#include "pv/sigsession.h"
44#include "pv/data/logic.h"
45#include "pv/data/logicsnapshot.h"
adb4b10c 46
aca64cac
JH
47using boost::shared_lock;
48using boost::shared_mutex;
1bc6525b 49using pv::data::SignalData;
a2321722 50using std::back_inserter;
819f4c25
JH
51using std::deque;
52using std::list;
c3a740dd 53using std::lock_guard;
819f4c25 54using std::max;
1bc6525b 55using std::make_pair;
819f4c25 56using std::min;
1bc6525b 57using std::pair;
819f4c25 58using std::set;
f9abf97e 59using std::shared_ptr;
7ff0145f 60using std::unordered_set;
819f4c25 61using std::vector;
f9abf97e 62using std::weak_ptr;
adb4b10c 63
cdf7bea7
JH
64namespace pv {
65namespace view {
adb4b10c 66
cdf7bea7
JH
67const double View::MaxScale = 1e9;
68const double View::MinScale = 1e-15;
adb4b10c 69
f25770e2
JH
70const int View::MaxScrollValue = INT_MAX / 2;
71
f76af637
JH
72const QColor View::CursorAreaColour(220, 231, 243);
73
2e04f9bd
JH
74const QSizeF View::LabelPadding(4, 0);
75
cdf7bea7 76View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
77 QAbstractScrollArea(parent),
78 _session(session),
cdf7bea7 79 _viewport(new Viewport(*this)),
ccdd3ef5 80 _ruler(new Ruler(*this)),
84a0d458 81 _cursorheader(new CursorHeader(*this)),
1d8dca91 82 _header(new Header(*this)),
adb4b10c
JH
83 _scale(1e-6),
84 _offset(0),
cbd80f64 85 _v_offset(0),
528bd8a1 86 _updating_scroll(false),
f76af637 87 _show_cursors(false),
b42d25c4 88 _cursors(*this),
cbd80f64 89 _hover_point(-1, -1)
adb4b10c 90{
b16907d3
JH
91 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
92 this, SLOT(h_scroll_value_changed(int)));
adb4b10c
JH
93 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
94 this, SLOT(v_scroll_value_changed(int)));
69dd2b03
JH
95
96 connect(&_session, SIGNAL(signals_changed()),
97 this, SLOT(signals_changed()));
50f97924
PZ
98 connect(&_session, SIGNAL(capture_state_changed(int)),
99 this, SLOT(data_updated()));
1f374035
JH
100 connect(&_session, SIGNAL(data_received()),
101 this, SLOT(data_updated()));
102 connect(&_session, SIGNAL(frame_ended()),
adb4b10c 103 this, SLOT(data_updated()));
1d8dca91 104
58864c5c 105 connect(_cursors.first().get(), SIGNAL(time_changed()),
ca4ec3ea 106 this, SLOT(marker_time_changed()));
58864c5c 107 connect(_cursors.second().get(), SIGNAL(time_changed()),
ca4ec3ea
JH
108 this, SLOT(marker_time_changed()));
109
54401bbb 110 connect(_header, SIGNAL(signals_moved()),
07204819 111 this, SLOT(on_signals_moved()));
54401bbb 112
17348f85 113 connect(_header, SIGNAL(selection_changed()),
84a0d458
JS
114 _cursorheader, SLOT(clear_selection()));
115 connect(_cursorheader, SIGNAL(selection_changed()),
17348f85
JH
116 _header, SLOT(clear_selection()));
117
8b454527
JH
118 connect(_header, SIGNAL(selection_changed()),
119 this, SIGNAL(selection_changed()));
84a0d458 120 connect(_cursorheader, SIGNAL(selection_changed()),
8b454527
JH
121 this, SIGNAL(selection_changed()));
122
33c62f44
JH
123 connect(this, SIGNAL(hover_point_changed()),
124 this, SLOT(on_hover_point_changed()));
125
32218d3e
JH
126 connect(&_lazy_event_handler, SIGNAL(timeout()),
127 this, SLOT(process_sticky_events()));
128 _lazy_event_handler.setSingleShot(true);
129
adb4b10c 130 setViewport(_viewport);
cbd80f64
JH
131
132 _viewport->installEventFilter(this);
133 _ruler->installEventFilter(this);
84a0d458 134 _cursorheader->installEventFilter(this);
cbd80f64 135 _header->installEventFilter(this);
d873f4d6 136
9f46d905
JH
137 // Trigger the initial event manually. The default device has signals
138 // which were created before this object came into being
d873f4d6 139 signals_changed();
84a0d458 140
512bfc56 141 // make sure the transparent widgets are on the top
84a0d458 142 _cursorheader->raise();
512bfc56 143 _header->raise();
adb4b10c
JH
144}
145
1d19ef83
JH
146SigSession& View::session()
147{
148 return _session;
149}
150
38eeddea
JH
151const SigSession& View::session() const
152{
153 return _session;
154}
155
eae6e30a
JH
156View* View::view()
157{
158 return this;
159}
160
161const View* View::view() const
162{
163 return this;
164}
165
2ae445ba
SA
166Viewport* View::viewport()
167{
168 return _viewport;
169}
170
171const Viewport* View::viewport() const
172{
173 return _viewport;
174}
175
cdf7bea7 176double View::scale() const
adb4b10c
JH
177{
178 return _scale;
179}
180
cdf7bea7 181double View::offset() const
adb4b10c
JH
182{
183 return _offset;
184}
185
7ff0145f 186int View::owner_visual_v_offset() const
adb4b10c 187{
23935421 188 return -_v_offset;
adb4b10c
JH
189}
190
3e769a37
JH
191unsigned int View::depth() const
192{
193 return 0;
194}
195
cdf7bea7 196void View::zoom(double steps)
adb4b10c 197{
f3f98f8f 198 zoom(steps, _viewport->width() / 2);
adb4b10c
JH
199}
200
17c0f398
JH
201void View::zoom(double steps, int offset)
202{
8b2e2a9a 203 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
17c0f398
JH
204}
205
ca46b534
JH
206void View::zoom_fit()
207{
1bc6525b
JH
208 const pair<double, double> extents = get_time_extents();
209 const double delta = extents.second - extents.first;
210 if (delta < 1e-12)
ca46b534
JH
211 return;
212
213 assert(_viewport);
214 const int w = _viewport->width();
215 if (w <= 0)
216 return;
217
78311127
JH
218 const double scale = max(min(delta / w, MaxScale), MinScale);
219 set_scale_offset(scale, extents.first);
ca46b534
JH
220}
221
d1e7d82c
JH
222void View::zoom_one_to_one()
223{
224 using pv::data::SignalData;
225
d1e7d82c 226 // Make a set of all the visible data objects
0fc664a9 227 set< shared_ptr<SignalData> > visible_data = get_visible_data();
d1e7d82c
JH
228 if (visible_data.empty())
229 return;
230
231 double samplerate = 0.0;
d9aecf1f 232 for (const shared_ptr<SignalData> d : visible_data) {
d1e7d82c
JH
233 assert(d);
234 samplerate = max(samplerate, d->samplerate());
235 }
236
237 if (samplerate == 0.0)
238 return;
239
240 assert(_viewport);
241 const int w = _viewport->width();
242 if (w <= 0)
243 return;
244
245 set_zoom(1.0 / samplerate, w / 2);
246}
247
cdf7bea7 248void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
249{
250 _scale = scale;
251 _offset = offset;
ccdd3ef5 252
adb4b10c 253 update_scroll();
ccdd3ef5 254 _ruler->update();
84a0d458 255 _cursorheader->update();
adb4b10c 256 _viewport->update();
e0fc5810 257 scale_offset_changed();
adb4b10c
JH
258}
259
1bc6525b
JH
260set< shared_ptr<SignalData> > View::get_visible_data() const
261{
aca64cac 262 shared_lock<shared_mutex> lock(session().signals_mutex());
c3a740dd 263 const vector< shared_ptr<Signal> > &sigs(session().signals());
1bc6525b
JH
264
265 // Make a set of all the visible data objects
266 set< shared_ptr<SignalData> > visible_data;
d9aecf1f 267 for (const shared_ptr<Signal> sig : sigs)
1bc6525b
JH
268 if (sig->enabled())
269 visible_data.insert(sig->data());
270
271 return visible_data;
272}
273
274pair<double, double> View::get_time_extents() const
275{
276 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
277 if (visible_data.empty())
278 return make_pair(0.0, 0.0);
279
280 double left_time = DBL_MAX, right_time = DBL_MIN;
d9aecf1f 281 for (const shared_ptr<SignalData> d : visible_data)
1bc6525b
JH
282 {
283 const double start_time = d->get_start_time();
a472a884
JH
284 double samplerate = d->samplerate();
285 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
286
1bc6525b
JH
287 left_time = min(left_time, start_time);
288 right_time = max(right_time, start_time +
a472a884 289 d->get_max_sample_count() / samplerate);
1bc6525b
JH
290 }
291
292 assert(left_time < right_time);
293 return make_pair(left_time, right_time);
294}
295
f76af637
JH
296bool View::cursors_shown() const
297{
298 return _show_cursors;
299}
300
301void View::show_cursors(bool show)
302{
303 _show_cursors = show;
84a0d458 304 _cursorheader->update();
f76af637
JH
305 _viewport->update();
306}
307
b4d91e56
JH
308void View::centre_cursors()
309{
310 const double time_width = _scale * _viewport->width();
58864c5c
JH
311 _cursors.first()->set_time(_offset + time_width * 0.4);
312 _cursors.second()->set_time(_offset + time_width * 0.6);
84a0d458 313 _cursorheader->update();
b4d91e56
JH
314 _viewport->update();
315}
316
b42d25c4 317CursorPair& View::cursors()
f76af637
JH
318{
319 return _cursors;
320}
321
58864c5c
JH
322const CursorPair& View::cursors() const
323{
324 return _cursors;
325}
326
cbd80f64
JH
327const QPoint& View::hover_point() const
328{
329 return _hover_point;
330}
331
9cef9567
JH
332void View::update_viewport()
333{
334 assert(_viewport);
335 _viewport->update();
7ff0145f
JH
336 _header->update();
337}
338
339void View::restack_all_row_items()
340{
341 // Make a set of owners
342 unordered_set< RowItemOwner* > owners;
343 for (const auto &r : *this)
344 owners.insert(r->owner());
345
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(); });
351
352 // Restack the items recursively
353 for (auto &o : sorted_owners)
354 o->restack_items();
355
356 // Animate the items to their destination
357 for (const auto &r : *this)
358 r->animate_to_layout_v_offset();
9cef9567
JH
359}
360
f25770e2
JH
361void View::get_scroll_layout(double &length, double &offset) const
362{
1bc6525b
JH
363 const pair<double, double> extents = get_time_extents();
364 length = (extents.second - extents.first) / _scale;
f25770e2
JH
365 offset = _offset / _scale;
366}
367
d1e7d82c
JH
368void View::set_zoom(double scale, int offset)
369{
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);
374}
375
cdf7bea7 376void View::update_scroll()
adb4b10c
JH
377{
378 assert(_viewport);
379
380 const QSize areaSize = _viewport->size();
381
382 // Set the horizontal scroll bar
383 double length = 0, offset = 0;
f25770e2
JH
384 get_scroll_layout(length, offset);
385 length = max(length - areaSize.width(), 0.0);
adb4b10c 386
b4ef7f2a 387 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 388
528bd8a1
JH
389 _updating_scroll = true;
390
333d5bbc 391 if (length < MaxScrollValue) {
f25770e2
JH
392 horizontalScrollBar()->setRange(0, length);
393 horizontalScrollBar()->setSliderPosition(offset);
394 } else {
395 horizontalScrollBar()->setRange(0, MaxScrollValue);
396 horizontalScrollBar()->setSliderPosition(
397 _offset * MaxScrollValue / (_scale * length));
398 }
adb4b10c 399
528bd8a1
JH
400 _updating_scroll = false;
401
adb4b10c
JH
402 // Set the vertical scrollbar
403 verticalScrollBar()->setPageStep(areaSize.height());
a5d93c27
JH
404
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);
adb4b10c
JH
409}
410
d7c0ca4a
JH
411void View::update_layout()
412{
512bfc56
JS
413 setViewportMargins(
414 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
a6c1726e 415 _ruler->sizeHint().height(), 0, 0);
d7c0ca4a
JH
416 _ruler->setGeometry(_viewport->x(), 0,
417 _viewport->width(), _viewport->y());
84a0d458
JS
418 _cursorheader->setGeometry(
419 _viewport->x(),
420 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
421 _viewport->width(), _cursorheader->sizeHint().height());
d7c0ca4a 422 _header->setGeometry(0, _viewport->y(),
512bfc56 423 _header->sizeHint().width(), _viewport->height());
d7c0ca4a
JH
424 update_scroll();
425}
426
eae6e30a
JH
427void View::paint_label(QPainter &p, int right, bool hover)
428{
429 (void)p;
430 (void)right;
431 (void)hover;
432}
433
434QRectF View::label_rect(int right)
435{
436 (void)right;
437 return QRectF();
438}
439
cbd80f64
JH
440bool View::eventFilter(QObject *object, QEvent *event)
441{
442 const QEvent::Type type = event->type();
333d5bbc 443 if (type == QEvent::MouseMove) {
cbd80f64
JH
444
445 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 446 if (object == _viewport)
cbd80f64 447 _hover_point = mouse_event->pos();
84a0d458 448 else if (object == _ruler || object == _cursorheader)
cbd80f64 449 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 450 else if (object == _header)
cbd80f64
JH
451 _hover_point = QPoint(0, mouse_event->y());
452 else
453 _hover_point = QPoint(-1, -1);
454
455 hover_point_changed();
456
333d5bbc 457 } else if (type == QEvent::Leave) {
cbd80f64
JH
458 _hover_point = QPoint(-1, -1);
459 hover_point_changed();
460 }
461
462 return QObject::eventFilter(object, event);
463}
464
cdf7bea7 465bool View::viewportEvent(QEvent *e)
adb4b10c
JH
466{
467 switch(e->type()) {
468 case QEvent::Paint:
469 case QEvent::MouseButtonPress:
470 case QEvent::MouseButtonRelease:
471 case QEvent::MouseButtonDblClick:
472 case QEvent::MouseMove:
473 case QEvent::Wheel:
4b4f1c0d
MC
474 case QEvent::TouchBegin:
475 case QEvent::TouchUpdate:
476 case QEvent::TouchEnd:
adb4b10c
JH
477 return false;
478
479 default:
480 return QAbstractScrollArea::viewportEvent(e);
481 }
482}
483
e314eca4 484void View::resizeEvent(QResizeEvent*)
adb4b10c 485{
d7c0ca4a 486 update_layout();
adb4b10c
JH
487}
488
32218d3e
JH
489void View::appearance_changed(bool label, bool content)
490{
491 if (label)
492 _header->update();
493 if (content)
494 _viewport->update();
495}
496
497void View::extents_changed(bool horz, bool vert)
498{
499 _sticky_events |=
500 (horz ? SelectableItemHExtentsChanged : 0) |
501 (vert ? SelectableItemVExtentsChanged : 0);
502 _lazy_event_handler.start();
503}
504
b16907d3 505void View::h_scroll_value_changed(int value)
adb4b10c 506{
528bd8a1
JH
507 if (_updating_scroll)
508 return;
509
f25770e2 510 const int range = horizontalScrollBar()->maximum();
333d5bbc 511 if (range < MaxScrollValue)
f25770e2
JH
512 _offset = _scale * value;
513 else {
514 double length = 0, offset;
515 get_scroll_layout(length, offset);
516 _offset = _scale * length * value / MaxScrollValue;
517 }
518
ccdd3ef5 519 _ruler->update();
84a0d458 520 _cursorheader->update();
adb4b10c
JH
521 _viewport->update();
522}
523
cdf7bea7 524void View::v_scroll_value_changed(int value)
adb4b10c
JH
525{
526 _v_offset = value;
1d8dca91 527 _header->update();
adb4b10c
JH
528 _viewport->update();
529}
530
69dd2b03
JH
531void View::signals_changed()
532{
68b21a71
JH
533 // Populate the traces
534 clear_child_items();
535
aca64cac 536 shared_lock<shared_mutex> lock(session().signals_mutex());
c3a740dd 537 const vector< shared_ptr<Signal> > &sigs(session().signals());
68b21a71
JH
538 for (auto s : sigs)
539 add_child_item(s);
540
541#ifdef ENABLE_DECODE
542 const vector< shared_ptr<DecodeTrace> > decode_sigs(
543 session().get_decode_signals());
544 for (auto s : decode_sigs)
545 add_child_item(s);
546#endif
547
548 // Create the initial layout
a5d93c27 549 int offset = 0;
aa59d5c2 550 for (shared_ptr<RowItem> r : *this) {
a5d93c27
JH
551 const pair<int, int> extents = r->v_extents();
552 if (r->enabled())
553 offset += -extents.first;
be9e7b4b 554 r->force_to_v_offset(offset);
a5d93c27
JH
555 if (r->enabled())
556 offset += extents.second;
ef8311a4
JH
557 }
558
a6c1726e 559 update_layout();
69dd2b03
JH
560}
561
cdf7bea7 562void View::data_updated()
adb4b10c 563{
adb4b10c
JH
564 // Update the scroll bars
565 update_scroll();
566
567 // Repaint the view
568 _viewport->update();
569}
cdf7bea7 570
ca4ec3ea
JH
571void View::marker_time_changed()
572{
84a0d458 573 _cursorheader->update();
ca4ec3ea
JH
574 _viewport->update();
575}
576
07204819 577void View::on_signals_moved()
54401bbb 578{
07204819
JH
579 update_scroll();
580 signals_moved();
54401bbb
JH
581}
582
32218d3e 583void View::process_sticky_events()
d7c0ca4a 584{
32218d3e
JH
585 if (_sticky_events & SelectableItemHExtentsChanged)
586 update_layout();
7ff0145f
JH
587 if (_sticky_events & SelectableItemVExtentsChanged)
588 restack_all_row_items();
32218d3e
JH
589
590 // Clear the sticky events
591 _sticky_events = 0;
d7c0ca4a
JH
592}
593
33c62f44
JH
594void View::on_hover_point_changed()
595{
aa59d5c2 596 for (shared_ptr<RowItem> r : *this)
eae6e30a 597 r->hover_point_changed();
33c62f44
JH
598}
599
cdf7bea7
JH
600} // namespace view
601} // namespace pv