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