]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
Fix #685 by adding a special T marker when SR_DT_TRIGGER arrives
[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
466 next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
467 (next_flag_text_ + 1);
468
469 // Skip 'T' (for trigger) as it's treated special
470 if (next_flag_text_ == 'T')
471 next_flag_text_ += 1;
472
473 time_item_appearance_changed(true, true);
474}
475
476void View::remove_flag(std::shared_ptr<Flag> flag)
477{
478 flags_.remove(flag);
479 time_item_appearance_changed(true, true);
480}
481
482vector< std::shared_ptr<Flag> > View::flags() const
483{
484 vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end());
485 stable_sort(flags.begin(), flags.end(),
486 [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) {
487 return a->time() < b->time();
488 });
489
490 return flags;
491}
492
493const QPoint& View::hover_point() const
494{
495 return hover_point_;
496}
497
498void View::update_viewport()
499{
500 assert(viewport_);
501 viewport_->update();
502 header_->update();
503}
504
505void View::restack_all_trace_tree_items()
506{
507 // Make a list of owners that is sorted from deepest first
508 const vector<shared_ptr<TraceTreeItem>> items(
509 list_by_type<TraceTreeItem>());
510 set< TraceTreeItemOwner* > owners;
511 for (const auto &r : items)
512 owners.insert(r->owner());
513 vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end());
514 sort(sorted_owners.begin(), sorted_owners.end(),
515 [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) {
516 return a->depth() > b->depth(); });
517
518 // Restack the items recursively
519 for (auto &o : sorted_owners)
520 o->restack_items();
521
522 // Animate the items to their destination
523 for (const auto &i : items)
524 i->animate_to_layout_v_offset();
525}
526
527void View::trigger_event(util::Timestamp location)
528{
529 char next_flag_text = next_flag_text_;
530
531 next_flag_text_ = 'T';
532 add_flag(location);
533
534 next_flag_text_ = next_flag_text;
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