]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/view.cpp
View: support multiple trigger markers in one acquisition.
[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_markers_(),
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 for (auto trigger_marker : trigger_markers_)
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 trigger_markers_.push_back(shared_ptr<TriggerMarker>(
532 new TriggerMarker(*this, location)));
533}
534
535void View::get_scroll_layout(double &length, Timestamp &offset) const
536{
537 const pair<Timestamp, Timestamp> extents = get_time_extents();
538 length = ((extents.second - extents.first) / scale_).convert_to<double>();
539 offset = offset_ / scale_;
540}
541
542void View::set_zoom(double scale, int offset)
543{
544 // Reset the "always zoom to fit" feature as the user changed the zoom
545 always_zoom_to_fit_ = false;
546 always_zoom_to_fit_changed(false);
547
548 const Timestamp cursor_offset = offset_ + scale_ * offset;
549 const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale);
550 const Timestamp new_offset = cursor_offset - new_scale * offset;
551 set_scale_offset(new_scale.convert_to<double>(), new_offset);
552}
553
554void View::calculate_tick_spacing()
555{
556 const double SpacingIncrement = 10.0f;
557 const double MinValueSpacing = 40.0f;
558
559 // Figure out the highest numeric value visible on a label
560 const QSize areaSize = viewport_->size();
561 const Timestamp max_time = max(fabs(offset_),
562 fabs(offset_ + scale_ * areaSize.width()));
563
564 double min_width = SpacingIncrement;
565 double label_width, tick_period_width;
566
567 QFontMetrics m(QApplication::font());
568
569 // Copies of the member variables with the same name, used in the calculation
570 // and written back afterwards, so that we don't emit signals all the time
571 // during the calculation.
572 pv::util::Timestamp tick_period = tick_period_;
573 pv::util::SIPrefix tick_prefix = tick_prefix_;
574 unsigned tick_precision = tick_precision_;
575
576 do {
577 const double min_period = scale_ * min_width;
578
579 const int order = (int)floorf(log10f(min_period));
580 const pv::util::Timestamp order_decimal =
581 pow(pv::util::Timestamp(10), order);
582
583 // Allow for a margin of error so that a scale unit of 1 can be used.
584 // Otherwise, for a SU of 1 the tick period will almost always be below
585 // the min_period by a small amount - and thus skipped in favor of 2.
586 // Note: margin assumes that SU[0] and SU[1] contain the smallest values
587 double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0;
588 double tp_with_margin;
589 unsigned int unit = 0;
590
591 do {
592 tp_with_margin = order_decimal.convert_to<double>() *
593 (ScaleUnits[unit++] + tp_margin);
594 } while (tp_with_margin < min_period && unit < countof(ScaleUnits));
595
596 tick_period = order_decimal * ScaleUnits[unit - 1];
597 tick_prefix = static_cast<pv::util::SIPrefix>(
598 (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3);
599
600 // Precision is the number of fractional digits required, not
601 // taking the prefix into account (and it must never be negative)
602 tick_precision = std::max(ceil(log10(1 / tick_period)).convert_to<int>(), 0);
603
604 tick_period_width = (tick_period / scale_).convert_to<double>();
605
606 const QString label_text = Ruler::format_time_with_distance(
607 tick_period, max_time, tick_prefix, time_unit_, tick_precision);
608
609 label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
610 Qt::AlignLeft | Qt::AlignTop, label_text).width() +
611 MinValueSpacing;
612
613 min_width += SpacingIncrement;
614 } while (tick_period_width < label_width);
615
616 set_tick_period(tick_period);
617 set_tick_prefix(tick_prefix);
618 set_tick_precision(tick_precision);
619}
620
621void View::update_scroll()
622{
623 assert(viewport_);
624
625 const QSize areaSize = viewport_->size();
626
627 // Set the horizontal scroll bar
628 double length = 0;
629 Timestamp offset;
630 get_scroll_layout(length, offset);
631 length = max(length - areaSize.width(), 0.0);
632
633 int major_tick_distance = (tick_period_ / scale_).convert_to<int>();
634
635 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
636 horizontalScrollBar()->setSingleStep(major_tick_distance);
637
638 updating_scroll_ = true;
639
640 if (length < MaxScrollValue) {
641 horizontalScrollBar()->setRange(0, length);
642 horizontalScrollBar()->setSliderPosition(offset.convert_to<double>());
643 } else {
644 horizontalScrollBar()->setRange(0, MaxScrollValue);
645 horizontalScrollBar()->setSliderPosition(
646 (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>());
647 }
648
649 updating_scroll_ = false;
650
651 // Set the vertical scrollbar
652 verticalScrollBar()->setPageStep(areaSize.height());
653 verticalScrollBar()->setSingleStep(areaSize.height() / 8);
654
655 const pair<int, int> extents = v_extents();
656 verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2),
657 extents.second - (areaSize.height() / 2));
658}
659
660void View::update_layout()
661{
662 setViewportMargins(
663 header_->sizeHint().width() - pv::view::Header::BaselineOffset,
664 ruler_->sizeHint().height(), 0, 0);
665 ruler_->setGeometry(viewport_->x(), 0,
666 viewport_->width(), ruler_->extended_size_hint().height());
667 header_->setGeometry(0, viewport_->y(),
668 header_->extended_size_hint().width(), viewport_->height());
669 update_scroll();
670}
671
672void View::paint_label(QPainter &p, const QRect &rect, bool hover)
673{
674 (void)p;
675 (void)rect;
676 (void)hover;
677}
678
679QRectF View::label_rect(const QRectF &rect)
680{
681 (void)rect;
682 return QRectF();
683}
684
685TraceTreeItemOwner* View::find_prevalent_trace_group(
686 const shared_ptr<sigrok::ChannelGroup> &group,
687 const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
688 &signal_map)
689{
690 assert(group);
691
692 unordered_set<TraceTreeItemOwner*> owners;
693 vector<TraceTreeItemOwner*> owner_list;
694
695 // Make a set and a list of all the owners
696 for (const auto &channel : group->channels()) {
697 const auto iter = signal_map.find(channel);
698 if (iter == signal_map.end())
699 continue;
700
701 TraceTreeItemOwner *const o = (*iter).second->owner();
702 owner_list.push_back(o);
703 owners.insert(o);
704 }
705
706 // Iterate through the list of owners, and find the most prevalent
707 size_t max_prevalence = 0;
708 TraceTreeItemOwner *prevalent_owner = nullptr;
709 for (TraceTreeItemOwner *owner : owners) {
710 const size_t prevalence = std::count_if(
711 owner_list.begin(), owner_list.end(),
712 [&](TraceTreeItemOwner *o) { return o == owner; });
713 if (prevalence > max_prevalence) {
714 max_prevalence = prevalence;
715 prevalent_owner = owner;
716 }
717 }
718
719 return prevalent_owner;
720}
721
722vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
723 const vector< shared_ptr<sigrok::Channel> > &channels,
724 const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
725 &signal_map,
726 set< shared_ptr<Trace> > &add_list)
727{
728 vector< shared_ptr<Trace> > filtered_traces;
729
730 for (const auto &channel : channels)
731 {
732 const auto map_iter = signal_map.find(channel);
733 if (map_iter == signal_map.end())
734 continue;
735
736 shared_ptr<Trace> trace = (*map_iter).second;
737 const auto list_iter = add_list.find(trace);
738 if (list_iter == add_list.end())
739 continue;
740
741 filtered_traces.push_back(trace);
742 add_list.erase(list_iter);
743 }
744
745 return filtered_traces;
746}
747
748void View::determine_time_unit()
749{
750 // Check whether we know the sample rate and hence can use time as the unit
751 if (time_unit_ == util::TimeUnit::Samples) {
752 const unordered_set< shared_ptr<Signal> > sigs(session().signals());
753
754 // Check all signals but...
755 for (const shared_ptr<Signal> signal : sigs) {
756 const shared_ptr<SignalData> data = signal->data();
757
758 // ...only check first segment of each
759 const vector< shared_ptr<Segment> > segments = data->segments();
760 if (!segments.empty())
761 if (segments[0]->samplerate()) {
762 set_time_unit(util::TimeUnit::Time);
763 break;
764 }
765 }
766 }
767}
768
769bool View::eventFilter(QObject *object, QEvent *event)
770{
771 const QEvent::Type type = event->type();
772 if (type == QEvent::MouseMove) {
773
774 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
775 if (object == viewport_)
776 hover_point_ = mouse_event->pos();
777 else if (object == ruler_)
778 hover_point_ = QPoint(mouse_event->x(), 0);
779 else if (object == header_)
780 hover_point_ = QPoint(0, mouse_event->y());
781 else
782 hover_point_ = QPoint(-1, -1);
783
784 hover_point_changed();
785
786 } else if (type == QEvent::Leave) {
787 hover_point_ = QPoint(-1, -1);
788 hover_point_changed();
789 }
790
791 return QObject::eventFilter(object, event);
792}
793
794bool View::viewportEvent(QEvent *e)
795{
796 switch(e->type()) {
797 case QEvent::Paint:
798 case QEvent::MouseButtonPress:
799 case QEvent::MouseButtonRelease:
800 case QEvent::MouseButtonDblClick:
801 case QEvent::MouseMove:
802 case QEvent::Wheel:
803 case QEvent::TouchBegin:
804 case QEvent::TouchUpdate:
805 case QEvent::TouchEnd:
806 return false;
807
808 default:
809 return QAbstractScrollArea::viewportEvent(e);
810 }
811}
812
813void View::resizeEvent(QResizeEvent*)
814{
815 update_layout();
816}
817
818void View::row_item_appearance_changed(bool label, bool content)
819{
820 if (label)
821 header_->update();
822 if (content)
823 viewport_->update();
824}
825
826void View::time_item_appearance_changed(bool label, bool content)
827{
828 if (label)
829 ruler_->update();
830 if (content)
831 viewport_->update();
832}
833
834void View::extents_changed(bool horz, bool vert)
835{
836 sticky_events_ |=
837 (horz ? TraceTreeItemHExtentsChanged : 0) |
838 (vert ? TraceTreeItemVExtentsChanged : 0);
839 lazy_event_handler_.start();
840}
841
842void View::h_scroll_value_changed(int value)
843{
844 if (updating_scroll_)
845 return;
846
847 // Disable sticky scrolling when user moves the horizontal scroll bar
848 // during a running acquisition
849 if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) {
850 sticky_scrolling_ = false;
851 sticky_scrolling_changed(false);
852 }
853
854 const int range = horizontalScrollBar()->maximum();
855 if (range < MaxScrollValue)
856 set_offset(scale_ * value);
857 else {
858 double length = 0;
859 Timestamp offset;
860 get_scroll_layout(length, offset);
861 set_offset(scale_ * length * value / MaxScrollValue);
862 }
863
864 ruler_->update();
865 viewport_->update();
866}
867
868void View::v_scroll_value_changed()
869{
870 header_->update();
871 viewport_->update();
872}
873
874void View::signals_changed()
875{
876 using sigrok::Channel;
877
878 vector< shared_ptr<TraceTreeItem> > new_top_level_items;
879
880 const auto device = session_.device();
881 if (!device)
882 return;
883
884 shared_ptr<sigrok::Device> sr_dev = device->device();
885 assert(sr_dev);
886
887 const vector< shared_ptr<Channel> > channels(
888 sr_dev->channels());
889
890 // Make a list of traces that are being added, and a list of traces
891 // that are being removed
892 const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>();
893 const set<shared_ptr<Trace>> prev_traces(
894 prev_trace_list.begin(), prev_trace_list.end());
895
896 const unordered_set< shared_ptr<Signal> > sigs(session_.signals());
897
898 set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end());
899
900#ifdef ENABLE_DECODE
901 const vector< shared_ptr<DecodeTrace> > decode_traces(
902 session().get_decode_signals());
903 traces.insert(decode_traces.begin(), decode_traces.end());
904#endif
905
906 set< shared_ptr<Trace> > add_traces;
907 set_difference(traces.begin(), traces.end(),
908 prev_traces.begin(), prev_traces.end(),
909 inserter(add_traces, add_traces.begin()));
910
911 set< shared_ptr<Trace> > remove_traces;
912 set_difference(prev_traces.begin(), prev_traces.end(),
913 traces.begin(), traces.end(),
914 inserter(remove_traces, remove_traces.begin()));
915
916 // Make a look-up table of sigrok Channels to pulseview Signals
917 unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> >
918 signal_map;
919 for (const shared_ptr<Signal> &sig : sigs)
920 signal_map[sig->channel()] = sig;
921
922 // Populate channel groups
923 for (auto entry : sr_dev->channel_groups())
924 {
925 const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
926
927 if (group->channels().size() <= 1)
928 continue;
929
930 // Find best trace group to add to
931 TraceTreeItemOwner *owner = find_prevalent_trace_group(
932 group, signal_map);
933
934 // If there is no trace group, create one
935 shared_ptr<TraceGroup> new_trace_group;
936 if (!owner) {
937 new_trace_group.reset(new TraceGroup());
938 owner = new_trace_group.get();
939 }
940
941 // Extract traces for the trace group, removing them from
942 // the add list
943 const vector< shared_ptr<Trace> > new_traces_in_group =
944 extract_new_traces_for_channels(group->channels(),
945 signal_map, add_traces);
946
947 // Add the traces to the group
948 const pair<int, int> prev_v_extents = owner->v_extents();
949 int offset = prev_v_extents.second - prev_v_extents.first;
950 for (shared_ptr<Trace> trace : new_traces_in_group) {
951 assert(trace);
952 owner->add_child_item(trace);
953
954 const pair<int, int> extents = trace->v_extents();
955 if (trace->enabled())
956 offset += -extents.first;
957 trace->force_to_v_offset(offset);
958 if (trace->enabled())
959 offset += extents.second;
960 }
961
962 // If this is a new group, enqueue it in the new top level
963 // items list
964 if (!new_traces_in_group.empty() && new_trace_group)
965 new_top_level_items.push_back(new_trace_group);
966 }
967
968 // Enqueue the remaining logic channels in a group
969 vector< shared_ptr<Channel> > logic_channels;
970 copy_if(channels.begin(), channels.end(), back_inserter(logic_channels),
971 [](const shared_ptr<Channel>& c) {
972 return c->type() == sigrok::ChannelType::LOGIC; });
973 const vector< shared_ptr<Trace> > non_grouped_logic_signals =
974 extract_new_traces_for_channels(logic_channels,
975 signal_map, add_traces);
976 const shared_ptr<TraceGroup> non_grouped_trace_group(
977 make_shared<TraceGroup>());
978 for (shared_ptr<Trace> trace : non_grouped_logic_signals)
979 non_grouped_trace_group->add_child_item(trace);
980 new_top_level_items.push_back(non_grouped_trace_group);
981
982 // Enqueue the remaining channels as free ungrouped traces
983 const vector< shared_ptr<Trace> > new_top_level_signals =
984 extract_new_traces_for_channels(channels,
985 signal_map, add_traces);
986 new_top_level_items.insert(new_top_level_items.end(),
987 new_top_level_signals.begin(), new_top_level_signals.end());
988
989 // Enqueue any remaining traces i.e. decode traces
990 new_top_level_items.insert(new_top_level_items.end(),
991 add_traces.begin(), add_traces.end());
992
993 // Remove any removed traces
994 for (shared_ptr<Trace> trace : remove_traces) {
995 TraceTreeItemOwner *const owner = trace->owner();
996 assert(owner);
997 owner->remove_child_item(trace);
998 }
999
1000 // Add and position the pending top levels items
1001 for (auto item : new_top_level_items) {
1002 add_child_item(item);
1003
1004 // Position the item after the last present item
1005 int offset = v_extents().second;
1006 const pair<int, int> extents = item->v_extents();
1007 if (item->enabled())
1008 offset += -extents.first;
1009 item->force_to_v_offset(offset);
1010 if (item->enabled())
1011 offset += extents.second;
1012 }
1013
1014 update_layout();
1015
1016 header_->update();
1017 viewport_->update();
1018}
1019
1020void View::capture_state_updated(int state)
1021{
1022 if (state == Session::Running) {
1023 set_time_unit(util::TimeUnit::Samples);
1024
1025 trigger_markers_.clear();
1026 }
1027
1028 if (state == Session::Stopped) {
1029 // After acquisition has stopped we need to re-calculate the ticks once
1030 // as it's otherwise done when the user pans or zooms, which is too late
1031 calculate_tick_spacing();
1032
1033 // Reset "always zoom to fit", the acquisition has stopped
1034 if (always_zoom_to_fit_) {
1035 always_zoom_to_fit_ = false;
1036 always_zoom_to_fit_changed(false);
1037 }
1038 }
1039}
1040
1041void View::data_updated()
1042{
1043 if (always_zoom_to_fit_ || sticky_scrolling_) {
1044 if (!delayed_view_updater_.isActive())
1045 delayed_view_updater_.start();
1046 } else {
1047 determine_time_unit();
1048 update_scroll();
1049 ruler_->update();
1050 viewport_->update();
1051 }
1052}
1053
1054void View::perform_delayed_view_update()
1055{
1056 if (always_zoom_to_fit_)
1057 zoom_fit(true);
1058
1059 if (sticky_scrolling_) {
1060 // Make right side of the view sticky
1061 double length = 0;
1062 Timestamp offset;
1063 get_scroll_layout(length, offset);
1064
1065 const QSize areaSize = viewport_->size();
1066 length = max(length - areaSize.width(), 0.0);
1067
1068 set_offset(scale_ * length);
1069 }
1070
1071 determine_time_unit();
1072 update_scroll();
1073 ruler_->update();
1074 viewport_->update();
1075}
1076
1077void View::process_sticky_events()
1078{
1079 if (sticky_events_ & TraceTreeItemHExtentsChanged)
1080 update_layout();
1081 if (sticky_events_ & TraceTreeItemVExtentsChanged) {
1082 restack_all_trace_tree_items();
1083 update_scroll();
1084 }
1085
1086 // Clear the sticky events
1087 sticky_events_ = 0;
1088}
1089
1090void View::on_hover_point_changed()
1091{
1092 const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
1093 list_by_type<TraceTreeItem>());
1094 for (shared_ptr<TraceTreeItem> r : trace_tree_items)
1095 r->hover_point_changed();
1096}
1097
1098} // namespace view
1099} // namespace pv