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