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