]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
Refactoring: move samplerate algo from View to Session
[pulseview.git] / pv / view / view.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#ifdef ENABLE_DECODE
22#include <libsigrokdecode/libsigrokdecode.h>
23#endif
24
25#include <extdef.h>
26
27#include <algorithm>
28#include <cassert>
29#include <climits>
30#include <cmath>
31#include <iterator>
32#include <mutex>
33#include <unordered_set>
34
35#include <boost/thread/locks.hpp>
36
37#include <QApplication>
38#include <QEvent>
39#include <QFontMetrics>
40#include <QMouseEvent>
41#include <QScrollBar>
42
43#include <libsigrokcxx/libsigrokcxx.hpp>
44
45#include "decodetrace.hpp"
46#include "header.hpp"
47#include "logicsignal.hpp"
48#include "ruler.hpp"
49#include "signal.hpp"
50#include "tracegroup.hpp"
51#include "view.hpp"
52#include "viewport.hpp"
53
54#include "pv/session.hpp"
55#include "pv/devices/device.hpp"
56#include "pv/data/logic.hpp"
57#include "pv/data/logicsegment.hpp"
58#include "pv/util.hpp"
59
60using boost::shared_lock;
61using boost::shared_mutex;
62
63using pv::data::SignalData;
64using pv::data::Segment;
65using pv::util::TimeUnit;
66using pv::util::Timestamp;
67
68using std::back_inserter;
69using std::copy_if;
70using std::deque;
71using std::dynamic_pointer_cast;
72using std::inserter;
73using std::list;
74using std::lock_guard;
75using std::max;
76using std::make_pair;
77using std::make_shared;
78using std::min;
79using std::pair;
80using std::set;
81using std::set_difference;
82using std::shared_ptr;
83using std::unordered_map;
84using std::unordered_set;
85using std::vector;
86using std::weak_ptr;
87
88namespace pv {
89namespace view {
90
91const Timestamp View::MaxScale("1e9");
92const Timestamp View::MinScale("1e-12");
93
94const int View::MaxScrollValue = INT_MAX / 2;
95const int View::MaxViewAutoUpdateRate = 25; // No more than 25 Hz with sticky scrolling
96
97const int View::ScaleUnits[3] = {1, 2, 5};
98
99View::View(Session &session, QWidget *parent) :
100 QAbstractScrollArea(parent),
101 session_(session),
102 viewport_(new Viewport(*this)),
103 ruler_(new Ruler(*this)),
104 header_(new Header(*this)),
105 scale_(1e-3),
106 offset_(0),
107 updating_scroll_(false),
108 sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui()
109 always_zoom_to_fit_(false),
110 tick_period_(0),
111 tick_prefix_(pv::util::SIPrefix::yocto),
112 tick_precision_(0),
113 time_unit_(util::TimeUnit::Time),
114 show_cursors_(false),
115 cursors_(new CursorPair(*this)),
116 next_flag_text_('A'),
117 hover_point_(-1, -1)
118{
119 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
120 this, SLOT(h_scroll_value_changed(int)));
121 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
122 this, SLOT(v_scroll_value_changed()));
123
124 connect(&session_, SIGNAL(signals_changed()),
125 this, SLOT(signals_changed()));
126 connect(&session_, SIGNAL(capture_state_changed(int)),
127 this, SLOT(capture_state_updated(int)));
128 connect(&session_, SIGNAL(data_received()),
129 this, SLOT(data_updated()));
130 connect(&session_, SIGNAL(frame_ended()),
131 this, SLOT(data_updated()));
132
133 connect(header_, SIGNAL(selection_changed()),
134 ruler_, SLOT(clear_selection()));
135 connect(ruler_, SIGNAL(selection_changed()),
136 header_, SLOT(clear_selection()));
137
138 connect(header_, SIGNAL(selection_changed()),
139 this, SIGNAL(selection_changed()));
140 connect(ruler_, SIGNAL(selection_changed()),
141 this, SIGNAL(selection_changed()));
142
143 connect(this, SIGNAL(hover_point_changed()),
144 this, SLOT(on_hover_point_changed()));
145
146 connect(&lazy_event_handler_, SIGNAL(timeout()),
147 this, SLOT(process_sticky_events()));
148 lazy_event_handler_.setSingleShot(true);
149
150 connect(&delayed_view_updater_, SIGNAL(timeout()),
151 this, SLOT(perform_delayed_view_update()));
152 delayed_view_updater_.setSingleShot(true);
153 delayed_view_updater_.setInterval(1000 / MaxViewAutoUpdateRate);
154
155 setViewport(viewport_);
156
157 viewport_->installEventFilter(this);
158 ruler_->installEventFilter(this);
159 header_->installEventFilter(this);
160
161 // Trigger the initial event manually. The default device has signals
162 // which were created before this object came into being
163 signals_changed();
164
165 // make sure the transparent widgets are on the top
166 ruler_->raise();
167 header_->raise();
168
169 // Update the zoom state
170 calculate_tick_spacing();
171}
172
173Session& View::session()
174{
175 return session_;
176}
177
178const Session& View::session() const
179{
180 return session_;
181}
182
183View* View::view()
184{
185 return this;
186}
187
188const View* View::view() const
189{
190 return this;
191}
192
193Viewport* View::viewport()
194{
195 return viewport_;
196}
197
198const Viewport* View::viewport() const
199{
200 return viewport_;
201}
202
203vector< shared_ptr<TimeItem> > View::time_items() const
204{
205 const vector<shared_ptr<Flag>> f(flags());
206 vector<shared_ptr<TimeItem>> items(f.begin(), f.end());
207 items.push_back(cursors_);
208 items.push_back(cursors_->first());
209 items.push_back(cursors_->second());
210 return items;
211}
212
213double View::scale() const
214{
215 return scale_;
216}
217
218void View::set_scale(double scale)
219{
220 if (scale_ != scale) {
221 scale_ = scale;
222 Q_EMIT scale_changed();
223 }
224}
225
226const Timestamp& View::offset() const
227{
228 return offset_;
229}
230
231void View::set_offset(const pv::util::Timestamp& offset)
232{
233 if (offset_ != offset) {
234 offset_ = offset;
235 Q_EMIT offset_changed();
236 }
237}
238
239int View::owner_visual_v_offset() const
240{
241 return -verticalScrollBar()->sliderPosition();
242}
243
244void View::set_v_offset(int offset)
245{
246 verticalScrollBar()->setSliderPosition(offset);
247 header_->update();
248 viewport_->update();
249}
250
251unsigned int View::depth() const
252{
253 return 0;
254}
255
256pv::util::SIPrefix View::tick_prefix() const
257{
258 return tick_prefix_;
259}
260
261void View::set_tick_prefix(pv::util::SIPrefix tick_prefix)
262{
263 if (tick_prefix_ != tick_prefix) {
264 tick_prefix_ = tick_prefix;
265 Q_EMIT tick_prefix_changed();
266 }
267}
268
269unsigned int View::tick_precision() const
270{
271 return tick_precision_;
272}
273
274void View::set_tick_precision(unsigned tick_precision)
275{
276 if (tick_precision_ != tick_precision) {
277 tick_precision_ = tick_precision;
278 Q_EMIT tick_precision_changed();
279 }
280}
281
282const pv::util::Timestamp& View::tick_period() const
283{
284 return tick_period_;
285}
286
287void View::set_tick_period(const pv::util::Timestamp& tick_period)
288{
289 if (tick_period_ != tick_period) {
290 tick_period_ = tick_period;
291 Q_EMIT tick_period_changed();
292 }
293}
294
295TimeUnit View::time_unit() const
296{
297 return time_unit_;
298}
299
300void View::set_time_unit(pv::util::TimeUnit time_unit)
301{
302 if (time_unit_ != time_unit) {
303 time_unit_ = time_unit;
304 Q_EMIT time_unit_changed();
305 }
306}
307
308void View::zoom(double steps)
309{
310 zoom(steps, viewport_->width() / 2);
311}
312
313void View::zoom(double steps, int offset)
314{
315 set_zoom(scale_ * pow(3.0/2.0, -steps), offset);
316}
317
318void View::zoom_fit(bool gui_state)
319{
320 // Act as one-shot when stopped, toggle along with the GUI otherwise
321 if (session_.get_capture_state() == Session::Stopped) {
322 always_zoom_to_fit_ = false;
323 always_zoom_to_fit_changed(false);
324 } else {
325 always_zoom_to_fit_ = gui_state;
326 always_zoom_to_fit_changed(gui_state);
327 }
328
329 const pair<Timestamp, Timestamp> extents = get_time_extents();
330 const Timestamp delta = extents.second - extents.first;
331 if (delta < Timestamp("1e-12"))
332 return;
333
334 assert(viewport_);
335 const int w = viewport_->width();
336 if (w <= 0)
337 return;
338
339 const Timestamp scale = max(min(delta / w, MaxScale), MinScale);
340 set_scale_offset(scale.convert_to<double>(), extents.first);
341}
342
343void View::zoom_one_to_one()
344{
345 using pv::data::SignalData;
346
347 // Make a set of all the visible data objects
348 set< shared_ptr<SignalData> > visible_data = get_visible_data();
349 if (visible_data.empty())
350 return;
351
352 assert(viewport_);
353 const int w = viewport_->width();
354 if (w <= 0)
355 return;
356
357 set_zoom(1.0 / session_.get_samplerate(), w / 2);
358}
359
360void View::set_scale_offset(double scale, const Timestamp& offset)
361{
362 // Disable sticky scrolling / always zoom to fit when acquisition runs
363 // and user drags the viewport
364 if ((scale_ == scale) && (offset_ != offset) &&
365 (session_.get_capture_state() == Session::Running)) {
366
367 if (sticky_scrolling_) {
368 sticky_scrolling_ = false;
369 sticky_scrolling_changed(false);
370 }
371
372 if (always_zoom_to_fit_) {
373 always_zoom_to_fit_ = false;
374 always_zoom_to_fit_changed(false);
375 }
376 }
377
378 set_scale(scale);
379 set_offset(offset);
380
381 calculate_tick_spacing();
382
383 update_scroll();
384 ruler_->update();
385 viewport_->update();
386}
387
388set< shared_ptr<SignalData> > View::get_visible_data() const
389{
390 const unordered_set< shared_ptr<Signal> > sigs(session().signals());
391
392 // Make a set of all the visible data objects
393 set< shared_ptr<SignalData> > visible_data;
394 for (const shared_ptr<Signal> sig : sigs)
395 if (sig->enabled())
396 visible_data.insert(sig->data());
397
398 return visible_data;
399}
400
401pair<Timestamp, Timestamp> View::get_time_extents() const
402{
403 boost::optional<Timestamp> left_time, right_time;
404 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
405 for (const shared_ptr<SignalData> d : visible_data)
406 {
407 const vector< shared_ptr<Segment> > segments =
408 d->segments();
409 for (const shared_ptr<Segment> &s : segments) {
410 double samplerate = s->samplerate();
411 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
412
413 const Timestamp start_time = s->start_time();
414 left_time = left_time ?
415 min(*left_time, start_time) :
416 start_time;
417 right_time = right_time ?
418 max(*right_time, start_time + d->max_sample_count() / samplerate) :
419 start_time + d->max_sample_count() / samplerate;
420 }
421 }
422
423 if (!left_time || !right_time)
424 return make_pair(0, 0);
425
426 assert(*left_time < *right_time);
427 return make_pair(*left_time, *right_time);
428}
429
430void View::enable_sticky_scrolling(bool state)
431{
432 sticky_scrolling_ = state;
433}
434
435bool View::cursors_shown() const
436{
437 return show_cursors_;
438}
439
440void View::show_cursors(bool show)
441{
442 show_cursors_ = show;
443 ruler_->update();
444 viewport_->update();
445}
446
447void View::centre_cursors()
448{
449 const double time_width = scale_ * viewport_->width();
450 cursors_->first()->set_time(offset_ + time_width * 0.4);
451 cursors_->second()->set_time(offset_ + time_width * 0.6);
452 ruler_->update();
453 viewport_->update();
454}
455
456std::shared_ptr<CursorPair> View::cursors() const
457{
458 return cursors_;
459}
460
461void View::add_flag(const Timestamp& time)
462{
463 flags_.push_back(shared_ptr<Flag>(new Flag(*this, time,
464 QString("%1").arg(next_flag_text_))));
465 next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
466 (next_flag_text_ + 1);
467 time_item_appearance_changed(true, true);
468}
469
470void View::remove_flag(std::shared_ptr<Flag> flag)
471{
472 flags_.remove(flag);
473 time_item_appearance_changed(true, true);
474}
475
476vector< std::shared_ptr<Flag> > View::flags() const
477{
478 vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end());
479 stable_sort(flags.begin(), flags.end(),
480 [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) {
481 return a->time() < b->time();
482 });
483
484 return flags;
485}
486
487const QPoint& View::hover_point() const
488{
489 return hover_point_;
490}
491
492void View::update_viewport()
493{
494 assert(viewport_);
495 viewport_->update();
496 header_->update();
497}
498
499void View::restack_all_trace_tree_items()
500{
501 // Make a list of owners that is sorted from deepest first
502 const vector<shared_ptr<TraceTreeItem>> items(
503 list_by_type<TraceTreeItem>());
504 set< TraceTreeItemOwner* > owners;
505 for (const auto &r : items)
506 owners.insert(r->owner());
507 vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end());
508 sort(sorted_owners.begin(), sorted_owners.end(),
509 [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) {
510 return a->depth() > b->depth(); });
511
512 // Restack the items recursively
513 for (auto &o : sorted_owners)
514 o->restack_items();
515
516 // Animate the items to their destination
517 for (const auto &i : items)
518 i->animate_to_layout_v_offset();
519}
520
521void View::get_scroll_layout(double &length, Timestamp &offset) const
522{
523 const pair<Timestamp, Timestamp> extents = get_time_extents();
524 length = ((extents.second - extents.first) / scale_).convert_to<double>();
525 offset = offset_ / scale_;
526}
527
528void View::set_zoom(double scale, int offset)
529{
530 // Reset the "always zoom to fit" feature as the user changed the zoom
531 always_zoom_to_fit_ = false;
532 always_zoom_to_fit_changed(false);
533
534 const Timestamp cursor_offset = offset_ + scale_ * offset;
535 const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale);
536 const Timestamp new_offset = cursor_offset - new_scale * offset;
537 set_scale_offset(new_scale.convert_to<double>(), new_offset);
538}
539
540void View::calculate_tick_spacing()
541{
542 const double SpacingIncrement = 10.0f;
543 const double MinValueSpacing = 40.0f;
544
545 // Figure out the highest numeric value visible on a label
546 const QSize areaSize = viewport_->size();
547 const Timestamp max_time = max(fabs(offset_),
548 fabs(offset_ + scale_ * areaSize.width()));
549
550 double min_width = SpacingIncrement;
551 double label_width, tick_period_width;
552
553 QFontMetrics m(QApplication::font());
554
555 // Copies of the member variables with the same name, used in the calculation
556 // and written back afterwards, so that we don't emit signals all the time
557 // during the calculation.
558 pv::util::Timestamp tick_period = tick_period_;
559 pv::util::SIPrefix tick_prefix = tick_prefix_;
560 unsigned tick_precision = tick_precision_;
561
562 do {
563 const double min_period = scale_ * min_width;
564
565 const int order = (int)floorf(log10f(min_period));
566 const pv::util::Timestamp order_decimal =
567 pow(pv::util::Timestamp(10), order);
568
569 // Allow for a margin of error so that a scale unit of 1 can be used.
570 // Otherwise, for a SU of 1 the tick period will almost always be below
571 // the min_period by a small amount - and thus skipped in favor of 2.
572 // Note: margin assumes that SU[0] and SU[1] contain the smallest values
573 double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0;
574 double tp_with_margin;
575 unsigned int unit = 0;
576
577 do {
578 tp_with_margin = order_decimal.convert_to<double>() *
579 (ScaleUnits[unit++] + tp_margin);
580 } while (tp_with_margin < min_period && unit < countof(ScaleUnits));
581
582 tick_period = order_decimal * ScaleUnits[unit - 1];
583 tick_prefix = static_cast<pv::util::SIPrefix>(
584 (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3);
585
586 // Precision is the number of fractional digits required, not
587 // taking the prefix into account (and it must never be negative)
588 tick_precision = std::max(ceil(log10(1 / tick_period)).convert_to<int>(), 0);
589
590 tick_period_width = (tick_period / scale_).convert_to<double>();
591
592 const QString label_text = Ruler::format_time_with_distance(
593 tick_period, max_time, tick_prefix, time_unit_, tick_precision);
594
595 label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
596 Qt::AlignLeft | Qt::AlignTop, label_text).width() +
597 MinValueSpacing;
598
599 min_width += SpacingIncrement;
600 } while (tick_period_width < label_width);
601
602 set_tick_period(tick_period);
603 set_tick_prefix(tick_prefix);
604 set_tick_precision(tick_precision);
605}
606
607void View::update_scroll()
608{
609 assert(viewport_);
610
611 const QSize areaSize = viewport_->size();
612
613 // Set the horizontal scroll bar
614 double length = 0;
615 Timestamp offset;
616 get_scroll_layout(length, offset);
617 length = max(length - areaSize.width(), 0.0);
618
619 int major_tick_distance = (tick_period_ / scale_).convert_to<int>();
620
621 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
622 horizontalScrollBar()->setSingleStep(major_tick_distance);
623
624 updating_scroll_ = true;
625
626 if (length < MaxScrollValue) {
627 horizontalScrollBar()->setRange(0, length);
628 horizontalScrollBar()->setSliderPosition(offset.convert_to<double>());
629 } else {
630 horizontalScrollBar()->setRange(0, MaxScrollValue);
631 horizontalScrollBar()->setSliderPosition(
632 (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>());
633 }
634
635 updating_scroll_ = false;
636
637 // Set the vertical scrollbar
638 verticalScrollBar()->setPageStep(areaSize.height());
639 verticalScrollBar()->setSingleStep(areaSize.height() / 8);
640
641 const pair<int, int> extents = v_extents();
642 verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2),
643 extents.second - (areaSize.height() / 2));
644}
645
646void View::update_layout()
647{
648 setViewportMargins(
649 header_->sizeHint().width() - pv::view::Header::BaselineOffset,
650 ruler_->sizeHint().height(), 0, 0);
651 ruler_->setGeometry(viewport_->x(), 0,
652 viewport_->width(), ruler_->extended_size_hint().height());
653 header_->setGeometry(0, viewport_->y(),
654 header_->extended_size_hint().width(), viewport_->height());
655 update_scroll();
656}
657
658void View::paint_label(QPainter &p, const QRect &rect, bool hover)
659{
660 (void)p;
661 (void)rect;
662 (void)hover;
663}
664
665QRectF View::label_rect(const QRectF &rect)
666{
667 (void)rect;
668 return QRectF();
669}
670
671TraceTreeItemOwner* View::find_prevalent_trace_group(
672 const shared_ptr<sigrok::ChannelGroup> &group,
673 const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
674 &signal_map)
675{
676 assert(group);
677
678 unordered_set<TraceTreeItemOwner*> owners;
679 vector<TraceTreeItemOwner*> owner_list;
680
681 // Make a set and a list of all the owners
682 for (const auto &channel : group->channels()) {
683 const auto iter = signal_map.find(channel);
684 if (iter == signal_map.end())
685 continue;
686
687 TraceTreeItemOwner *const o = (*iter).second->owner();
688 owner_list.push_back(o);
689 owners.insert(o);
690 }
691
692 // Iterate through the list of owners, and find the most prevalent
693 size_t max_prevalence = 0;
694 TraceTreeItemOwner *prevalent_owner = nullptr;
695 for (TraceTreeItemOwner *owner : owners) {
696 const size_t prevalence = std::count_if(
697 owner_list.begin(), owner_list.end(),
698 [&](TraceTreeItemOwner *o) { return o == owner; });
699 if (prevalence > max_prevalence) {
700 max_prevalence = prevalence;
701 prevalent_owner = owner;
702 }
703 }
704
705 return prevalent_owner;
706}
707
708vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
709 const vector< shared_ptr<sigrok::Channel> > &channels,
710 const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
711 &signal_map,
712 set< shared_ptr<Trace> > &add_list)
713{
714 vector< shared_ptr<Trace> > filtered_traces;
715
716 for (const auto &channel : channels)
717 {
718 const auto map_iter = signal_map.find(channel);
719 if (map_iter == signal_map.end())
720 continue;
721
722 shared_ptr<Trace> trace = (*map_iter).second;
723 const auto list_iter = add_list.find(trace);
724 if (list_iter == add_list.end())
725 continue;
726
727 filtered_traces.push_back(trace);
728 add_list.erase(list_iter);
729 }
730
731 return filtered_traces;
732}
733
734void View::determine_time_unit()
735{
736 // Check whether we know the sample rate and hence can use time as the unit
737 if (time_unit_ == util::TimeUnit::Samples) {
738 const unordered_set< shared_ptr<Signal> > sigs(session().signals());
739
740 // Check all signals but...
741 for (const shared_ptr<Signal> signal : sigs) {
742 const shared_ptr<SignalData> data = signal->data();
743
744 // ...only check first segment of each
745 const vector< shared_ptr<Segment> > segments = data->segments();
746 if (!segments.empty())
747 if (segments[0]->samplerate()) {
748 set_time_unit(util::TimeUnit::Time);
749 break;
750 }
751 }
752 }
753}
754
755bool View::eventFilter(QObject *object, QEvent *event)
756{
757 const QEvent::Type type = event->type();
758 if (type == QEvent::MouseMove) {
759
760 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
761 if (object == viewport_)
762 hover_point_ = mouse_event->pos();
763 else if (object == ruler_)
764 hover_point_ = QPoint(mouse_event->x(), 0);
765 else if (object == header_)
766 hover_point_ = QPoint(0, mouse_event->y());
767 else
768 hover_point_ = QPoint(-1, -1);
769
770 hover_point_changed();
771
772 } else if (type == QEvent::Leave) {
773 hover_point_ = QPoint(-1, -1);
774 hover_point_changed();
775 }
776
777 return QObject::eventFilter(object, event);
778}
779
780bool View::viewportEvent(QEvent *e)
781{
782 switch(e->type()) {
783 case QEvent::Paint:
784 case QEvent::MouseButtonPress:
785 case QEvent::MouseButtonRelease:
786 case QEvent::MouseButtonDblClick:
787 case QEvent::MouseMove:
788 case QEvent::Wheel:
789 case QEvent::TouchBegin:
790 case QEvent::TouchUpdate:
791 case QEvent::TouchEnd:
792 return false;
793
794 default:
795 return QAbstractScrollArea::viewportEvent(e);
796 }
797}
798
799void View::resizeEvent(QResizeEvent*)
800{
801 update_layout();
802}
803
804void View::row_item_appearance_changed(bool label, bool content)
805{
806 if (label)
807 header_->update();
808 if (content)
809 viewport_->update();
810}
811
812void View::time_item_appearance_changed(bool label, bool content)
813{
814 if (label)
815 ruler_->update();
816 if (content)
817 viewport_->update();
818}
819
820void View::extents_changed(bool horz, bool vert)
821{
822 sticky_events_ |=
823 (horz ? TraceTreeItemHExtentsChanged : 0) |
824 (vert ? TraceTreeItemVExtentsChanged : 0);
825 lazy_event_handler_.start();
826}
827
828void View::h_scroll_value_changed(int value)
829{
830 if (updating_scroll_)
831 return;
832
833 // Disable sticky scrolling when user moves the horizontal scroll bar
834 // during a running acquisition
835 if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) {
836 sticky_scrolling_ = false;
837 sticky_scrolling_changed(false);
838 }
839
840 const int range = horizontalScrollBar()->maximum();
841 if (range < MaxScrollValue)
842 set_offset(scale_ * value);
843 else {
844 double length = 0;
845 Timestamp offset;
846 get_scroll_layout(length, offset);
847 set_offset(scale_ * length * value / MaxScrollValue);
848 }
849
850 ruler_->update();
851 viewport_->update();
852}
853
854void View::v_scroll_value_changed()
855{
856 header_->update();
857 viewport_->update();
858}
859
860void View::signals_changed()
861{
862 using sigrok::Channel;
863
864 vector< shared_ptr<TraceTreeItem> > new_top_level_items;
865
866 const auto device = session_.device();
867 if (!device)
868 return;
869
870 shared_ptr<sigrok::Device> sr_dev = device->device();
871 assert(sr_dev);
872
873 const vector< shared_ptr<Channel> > channels(
874 sr_dev->channels());
875
876 // Make a list of traces that are being added, and a list of traces
877 // that are being removed
878 const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>();
879 const set<shared_ptr<Trace>> prev_traces(
880 prev_trace_list.begin(), prev_trace_list.end());
881
882 const unordered_set< shared_ptr<Signal> > sigs(session_.signals());
883
884 set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end());
885
886#ifdef ENABLE_DECODE
887 const vector< shared_ptr<DecodeTrace> > decode_traces(
888 session().get_decode_signals());
889 traces.insert(decode_traces.begin(), decode_traces.end());
890#endif
891
892 set< shared_ptr<Trace> > add_traces;
893 set_difference(traces.begin(), traces.end(),
894 prev_traces.begin(), prev_traces.end(),
895 inserter(add_traces, add_traces.begin()));
896
897 set< shared_ptr<Trace> > remove_traces;
898 set_difference(prev_traces.begin(), prev_traces.end(),
899 traces.begin(), traces.end(),
900 inserter(remove_traces, remove_traces.begin()));
901
902 // Make a look-up table of sigrok Channels to pulseview Signals
903 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
904 signal_map;
905 for (const shared_ptr<Signal> &sig : sigs)
906 signal_map[sig->channel()] = sig;
907
908 // Populate channel groups
909 for (auto entry : sr_dev->channel_groups())
910 {
911 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
912
913 if (group->channels().size() <= 1)
914 continue;
915
916 // Find best trace group to add to
917 TraceTreeItemOwner *owner = find_prevalent_trace_group(
918 group, signal_map);
919
920 // If there is no trace group, create one
921 shared_ptr<TraceGroup> new_trace_group;
922 if (!owner) {
923 new_trace_group.reset(new TraceGroup());
924 owner = new_trace_group.get();
925 }
926
927 // Extract traces for the trace group, removing them from
928 // the add list
929 const vector< shared_ptr<Trace> > new_traces_in_group =
930 extract_new_traces_for_channels(group->channels(),
931 signal_map, add_traces);
932
933 // Add the traces to the group
934 const pair<int, int> prev_v_extents = owner->v_extents();
935 int offset = prev_v_extents.second - prev_v_extents.first;
936 for (shared_ptr<Trace> trace : new_traces_in_group) {
937 assert(trace);
938 owner->add_child_item(trace);
939
940 const pair<int, int> extents = trace->v_extents();
941 if (trace->enabled())
942 offset += -extents.first;
943 trace->force_to_v_offset(offset);
944 if (trace->enabled())
945 offset += extents.second;
946 }
947
948 // If this is a new group, enqueue it in the new top level
949 // items list
950 if (!new_traces_in_group.empty() && new_trace_group)
951 new_top_level_items.push_back(new_trace_group);
952 }
953
954 // Enqueue the remaining logic channels in a group
955 vector< shared_ptr<Channel> > logic_channels;
956 copy_if(channels.begin(), channels.end(), back_inserter(logic_channels),
957 [](const shared_ptr<Channel>& c) {
958 return c->type() == sigrok::ChannelType::LOGIC; });
959 const vector< shared_ptr<Trace> > non_grouped_logic_signals =
960 extract_new_traces_for_channels(logic_channels,
961 signal_map, add_traces);
962 const shared_ptr<TraceGroup> non_grouped_trace_group(
963 make_shared<TraceGroup>());
964 for (shared_ptr<Trace> trace : non_grouped_logic_signals)
965 non_grouped_trace_group->add_child_item(trace);
966 new_top_level_items.push_back(non_grouped_trace_group);
967
968 // Enqueue the remaining channels as free ungrouped traces
969 const vector< shared_ptr<Trace> > new_top_level_signals =
970 extract_new_traces_for_channels(channels,
971 signal_map, add_traces);
972 new_top_level_items.insert(new_top_level_items.end(),
973 new_top_level_signals.begin(), new_top_level_signals.end());
974
975 // Enqueue any remaining traces i.e. decode traces
976 new_top_level_items.insert(new_top_level_items.end(),
977 add_traces.begin(), add_traces.end());
978
979 // Remove any removed traces
980 for (shared_ptr<Trace> trace : remove_traces) {
981 TraceTreeItemOwner *const owner = trace->owner();
982 assert(owner);
983 owner->remove_child_item(trace);
984 }
985
986 // Add and position the pending top levels items
987 for (auto item : new_top_level_items) {
988 add_child_item(item);
989
990 // Position the item after the last present item
991 int offset = v_extents().second;
992 const pair<int, int> extents = item->v_extents();
993 if (item->enabled())
994 offset += -extents.first;
995 item->force_to_v_offset(offset);
996 if (item->enabled())
997 offset += extents.second;
998 }
999
1000 update_layout();
1001
1002 header_->update();
1003 viewport_->update();
1004}
1005
1006void View::capture_state_updated(int state)
1007{
1008 if (state == Session::Running)
1009 set_time_unit(util::TimeUnit::Samples);
1010
1011 if (state == Session::Stopped) {
1012 // After acquisition has stopped we need to re-calculate the ticks once
1013 // as it's otherwise done when the user pans or zooms, which is too late
1014 calculate_tick_spacing();
1015
1016 // Reset "always zoom to fit", the acquisition has stopped
1017 if (always_zoom_to_fit_) {
1018 always_zoom_to_fit_ = false;
1019 always_zoom_to_fit_changed(false);
1020 }
1021 }
1022}
1023
1024void View::data_updated()
1025{
1026 if (always_zoom_to_fit_ || sticky_scrolling_) {
1027 if (!delayed_view_updater_.isActive())
1028 delayed_view_updater_.start();
1029 } else {
1030 determine_time_unit();
1031 update_scroll();
1032 ruler_->update();
1033 viewport_->update();
1034 }
1035}
1036
1037void View::perform_delayed_view_update()
1038{
1039 if (always_zoom_to_fit_)
1040 zoom_fit(true);
1041
1042 if (sticky_scrolling_) {
1043 // Make right side of the view sticky
1044 double length = 0;
1045 Timestamp offset;
1046 get_scroll_layout(length, offset);
1047
1048 const QSize areaSize = viewport_->size();
1049 length = max(length - areaSize.width(), 0.0);
1050
1051 set_offset(scale_ * length);
1052 }
1053
1054 determine_time_unit();
1055 update_scroll();
1056 ruler_->update();
1057 viewport_->update();
1058}
1059
1060void View::process_sticky_events()
1061{
1062 if (sticky_events_ & TraceTreeItemHExtentsChanged)
1063 update_layout();
1064 if (sticky_events_ & TraceTreeItemVExtentsChanged) {
1065 restack_all_trace_tree_items();
1066 update_scroll();
1067 }
1068
1069 // Clear the sticky events
1070 sticky_events_ = 0;
1071}
1072
1073void View::on_hover_point_changed()
1074{
1075 const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
1076 list_by_type<TraceTreeItem>());
1077 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
1078 r->hover_point_changed();
1079}
1080
1081} // namespace view
1082} // namespace pv