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