]>
Commit | Line | Data |
---|---|---|
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #ifdef ENABLE_DECODE | |
21 | #include <libsigrokdecode/libsigrokdecode.h> | |
22 | #endif | |
23 | ||
24 | #include <extdef.h> | |
25 | ||
26 | #include <algorithm> | |
27 | #include <cassert> | |
28 | #include <climits> | |
29 | #include <cmath> | |
30 | #include <iostream> | |
31 | #include <iterator> | |
32 | #include <mutex> | |
33 | #include <unordered_set> | |
34 | ||
35 | #include <boost/archive/text_iarchive.hpp> | |
36 | #include <boost/archive/text_oarchive.hpp> | |
37 | #include <boost/serialization/serialization.hpp> | |
38 | #include <boost/thread/locks.hpp> | |
39 | ||
40 | #include <QApplication> | |
41 | #include <QHBoxLayout> | |
42 | #include <QEvent> | |
43 | #include <QFontMetrics> | |
44 | #include <QMouseEvent> | |
45 | #include <QScrollBar> | |
46 | ||
47 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
48 | ||
49 | #include "analogsignal.hpp" | |
50 | #include "header.hpp" | |
51 | #include "logicsignal.hpp" | |
52 | #include "ruler.hpp" | |
53 | #include "signal.hpp" | |
54 | #include "tracegroup.hpp" | |
55 | #include "triggermarker.hpp" | |
56 | #include "view.hpp" | |
57 | #include "viewport.hpp" | |
58 | ||
59 | #include "pv/session.hpp" | |
60 | #include "pv/globalsettings.hpp" | |
61 | #include "pv/devices/device.hpp" | |
62 | #include "pv/data/logic.hpp" | |
63 | #include "pv/data/logicsegment.hpp" | |
64 | #include "pv/util.hpp" | |
65 | ||
66 | #ifdef ENABLE_DECODE | |
67 | #include "decodetrace.hpp" | |
68 | #endif | |
69 | ||
70 | using boost::shared_lock; | |
71 | using boost::shared_mutex; | |
72 | ||
73 | using pv::data::SignalData; | |
74 | using pv::data::Segment; | |
75 | using pv::util::TimeUnit; | |
76 | using pv::util::Timestamp; | |
77 | ||
78 | using std::back_inserter; | |
79 | using std::copy_if; | |
80 | using std::deque; | |
81 | using std::dynamic_pointer_cast; | |
82 | using std::inserter; | |
83 | using std::list; | |
84 | using std::lock_guard; | |
85 | using std::max; | |
86 | using std::make_pair; | |
87 | using std::make_shared; | |
88 | using std::min; | |
89 | using std::pair; | |
90 | using std::set; | |
91 | using std::set_difference; | |
92 | using std::shared_ptr; | |
93 | using std::unordered_map; | |
94 | using std::unordered_set; | |
95 | using std::vector; | |
96 | using std::weak_ptr; | |
97 | ||
98 | namespace pv { | |
99 | namespace views { | |
100 | namespace TraceView { | |
101 | ||
102 | const Timestamp View::MaxScale("1e9"); | |
103 | const Timestamp View::MinScale("1e-12"); | |
104 | ||
105 | const int View::MaxScrollValue = INT_MAX / 2; | |
106 | const int View::MaxViewAutoUpdateRate = 25; // No more than 25 Hz with sticky scrolling | |
107 | ||
108 | const int View::ScaleUnits[3] = {1, 2, 5}; | |
109 | ||
110 | ||
111 | CustomAbstractScrollArea::CustomAbstractScrollArea(QWidget *parent) : | |
112 | QAbstractScrollArea(parent) | |
113 | { | |
114 | } | |
115 | ||
116 | void CustomAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom) | |
117 | { | |
118 | QAbstractScrollArea::setViewportMargins(left, top, right, bottom); | |
119 | } | |
120 | ||
121 | bool CustomAbstractScrollArea::viewportEvent(QEvent *event) | |
122 | { | |
123 | switch (event->type()) { | |
124 | case QEvent::Paint: | |
125 | case QEvent::MouseButtonPress: | |
126 | case QEvent::MouseButtonRelease: | |
127 | case QEvent::MouseButtonDblClick: | |
128 | case QEvent::MouseMove: | |
129 | case QEvent::Wheel: | |
130 | case QEvent::TouchBegin: | |
131 | case QEvent::TouchUpdate: | |
132 | case QEvent::TouchEnd: | |
133 | return false; | |
134 | default: | |
135 | return QAbstractScrollArea::viewportEvent(event); | |
136 | } | |
137 | } | |
138 | ||
139 | View::View(Session &session, bool is_main_view, QWidget *parent) : | |
140 | ViewBase(session, is_main_view, parent), | |
141 | viewport_(new Viewport(*this)), | |
142 | ruler_(new Ruler(*this)), | |
143 | header_(new Header(*this)), | |
144 | scrollarea_(this), | |
145 | scale_(1e-3), | |
146 | offset_(0), | |
147 | updating_scroll_(false), | |
148 | sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui() | |
149 | always_zoom_to_fit_(false), | |
150 | tick_period_(0), | |
151 | tick_prefix_(pv::util::SIPrefix::yocto), | |
152 | tick_precision_(0), | |
153 | time_unit_(util::TimeUnit::Time), | |
154 | show_cursors_(false), | |
155 | cursors_(new CursorPair(*this)), | |
156 | next_flag_text_('A'), | |
157 | trigger_markers_(), | |
158 | hover_point_(-1, -1), | |
159 | scroll_needs_defaults_(false), | |
160 | saved_v_offset_(0), | |
161 | size_finalized_(false) | |
162 | { | |
163 | GlobalSettings settings; | |
164 | coloured_bg_ = settings.value(GlobalSettings::Key_View_ColouredBG).toBool(); | |
165 | ||
166 | connect(scrollarea_.horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
167 | this, SLOT(h_scroll_value_changed(int))); | |
168 | connect(scrollarea_.verticalScrollBar(), SIGNAL(valueChanged(int)), | |
169 | this, SLOT(v_scroll_value_changed())); | |
170 | ||
171 | connect(header_, SIGNAL(selection_changed()), | |
172 | ruler_, SLOT(clear_selection())); | |
173 | connect(ruler_, SIGNAL(selection_changed()), | |
174 | header_, SLOT(clear_selection())); | |
175 | ||
176 | connect(header_, SIGNAL(selection_changed()), | |
177 | this, SIGNAL(selection_changed())); | |
178 | connect(ruler_, SIGNAL(selection_changed()), | |
179 | this, SIGNAL(selection_changed())); | |
180 | ||
181 | connect(this, SIGNAL(hover_point_changed()), | |
182 | this, SLOT(on_hover_point_changed())); | |
183 | ||
184 | connect(&lazy_event_handler_, SIGNAL(timeout()), | |
185 | this, SLOT(process_sticky_events())); | |
186 | lazy_event_handler_.setSingleShot(true); | |
187 | ||
188 | connect(&delayed_view_updater_, SIGNAL(timeout()), | |
189 | this, SLOT(perform_delayed_view_update())); | |
190 | delayed_view_updater_.setSingleShot(true); | |
191 | delayed_view_updater_.setInterval(1000 / MaxViewAutoUpdateRate); | |
192 | ||
193 | /* To let the scroll area fill up the parent QWidget (this), we need a layout */ | |
194 | QHBoxLayout *layout = new QHBoxLayout(this); | |
195 | setLayout(layout); | |
196 | layout->setContentsMargins(0, 0, 0, 0); | |
197 | layout->addWidget(&scrollarea_); | |
198 | ||
199 | scrollarea_.setViewport(viewport_); | |
200 | ||
201 | viewport_->installEventFilter(this); | |
202 | ruler_->installEventFilter(this); | |
203 | header_->installEventFilter(this); | |
204 | ||
205 | // Trigger the initial event manually. The default device has signals | |
206 | // which were created before this object came into being | |
207 | signals_changed(); | |
208 | ||
209 | // make sure the transparent widgets are on the top | |
210 | ruler_->raise(); | |
211 | header_->raise(); | |
212 | ||
213 | // Update the zoom state | |
214 | calculate_tick_spacing(); | |
215 | } | |
216 | ||
217 | Session& View::session() | |
218 | { | |
219 | return session_; | |
220 | } | |
221 | ||
222 | const Session& View::session() const | |
223 | { | |
224 | return session_; | |
225 | } | |
226 | ||
227 | std::unordered_set< std::shared_ptr<Signal> > View::signals() const | |
228 | { | |
229 | return signals_; | |
230 | } | |
231 | ||
232 | void View::clear_signals() | |
233 | { | |
234 | signals_.clear(); | |
235 | } | |
236 | ||
237 | void View::add_signal(const shared_ptr<Signal> signal) | |
238 | { | |
239 | signals_.insert(signal); | |
240 | } | |
241 | ||
242 | #ifdef ENABLE_DECODE | |
243 | void View::clear_decode_signals() | |
244 | { | |
245 | decode_traces_.clear(); | |
246 | } | |
247 | ||
248 | void View::add_decode_signal(shared_ptr<data::SignalBase> signalbase) | |
249 | { | |
250 | shared_ptr<DecodeTrace> d( | |
251 | new DecodeTrace(session_, signalbase, decode_traces_.size())); | |
252 | decode_traces_.push_back(d); | |
253 | } | |
254 | ||
255 | void View::remove_decode_signal(shared_ptr<data::SignalBase> signalbase) | |
256 | { | |
257 | for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) | |
258 | if ((*i)->base() == signalbase) { | |
259 | decode_traces_.erase(i); | |
260 | signals_changed(); | |
261 | return; | |
262 | } | |
263 | } | |
264 | #endif | |
265 | ||
266 | View* View::view() | |
267 | { | |
268 | return this; | |
269 | } | |
270 | ||
271 | const View* View::view() const | |
272 | { | |
273 | return this; | |
274 | } | |
275 | ||
276 | Viewport* View::viewport() | |
277 | { | |
278 | return viewport_; | |
279 | } | |
280 | ||
281 | const Viewport* View::viewport() const | |
282 | { | |
283 | return viewport_; | |
284 | } | |
285 | ||
286 | void View::save_settings(QSettings &settings) const | |
287 | { | |
288 | settings.setValue("scale", scale_); | |
289 | settings.setValue("v_offset", | |
290 | scrollarea_.verticalScrollBar()->sliderPosition()); | |
291 | ||
292 | std::stringstream ss; | |
293 | boost::archive::text_oarchive oa(ss); | |
294 | oa << boost::serialization::make_nvp("offset", offset_); | |
295 | settings.setValue("offset", QString::fromStdString(ss.str())); | |
296 | ||
297 | for (shared_ptr<Signal> signal : signals_) { | |
298 | settings.beginGroup(signal->base()->internal_name()); | |
299 | signal->save_settings(settings); | |
300 | settings.endGroup(); | |
301 | } | |
302 | } | |
303 | ||
304 | void View::restore_settings(QSettings &settings) | |
305 | { | |
306 | // Note: It is assumed that this function is only called once, | |
307 | // immediately after restoring a previous session. | |
308 | ||
309 | if (settings.contains("scale")) | |
310 | set_scale(settings.value("scale").toDouble()); | |
311 | ||
312 | if (settings.contains("offset")) { | |
313 | util::Timestamp offset; | |
314 | std::stringstream ss; | |
315 | ss << settings.value("offset").toString().toStdString(); | |
316 | ||
317 | boost::archive::text_iarchive ia(ss); | |
318 | ia >> boost::serialization::make_nvp("offset", offset); | |
319 | ||
320 | set_offset(offset); | |
321 | } | |
322 | ||
323 | for (shared_ptr<Signal> signal : signals_) { | |
324 | settings.beginGroup(signal->base()->internal_name()); | |
325 | signal->restore_settings(settings); | |
326 | settings.endGroup(); | |
327 | } | |
328 | ||
329 | if (settings.contains("v_offset")) { | |
330 | saved_v_offset_ = settings.value("v_offset").toInt(); | |
331 | set_v_offset(saved_v_offset_); | |
332 | scroll_needs_defaults_ = false; | |
333 | // Note: see resizeEvent() for additional information | |
334 | } | |
335 | } | |
336 | ||
337 | vector< shared_ptr<TimeItem> > View::time_items() const | |
338 | { | |
339 | const vector<shared_ptr<Flag>> f(flags()); | |
340 | vector<shared_ptr<TimeItem>> items(f.begin(), f.end()); | |
341 | items.push_back(cursors_); | |
342 | items.push_back(cursors_->first()); | |
343 | items.push_back(cursors_->second()); | |
344 | ||
345 | for (auto trigger_marker : trigger_markers_) | |
346 | items.push_back(trigger_marker); | |
347 | ||
348 | return items; | |
349 | } | |
350 | ||
351 | double View::scale() const | |
352 | { | |
353 | return scale_; | |
354 | } | |
355 | ||
356 | void View::set_scale(double scale) | |
357 | { | |
358 | if (scale_ != scale) { | |
359 | scale_ = scale; | |
360 | Q_EMIT scale_changed(); | |
361 | } | |
362 | } | |
363 | ||
364 | const Timestamp& View::offset() const | |
365 | { | |
366 | return offset_; | |
367 | } | |
368 | ||
369 | void View::set_offset(const pv::util::Timestamp& offset) | |
370 | { | |
371 | if (offset_ != offset) { | |
372 | offset_ = offset; | |
373 | Q_EMIT offset_changed(); | |
374 | } | |
375 | } | |
376 | ||
377 | int View::owner_visual_v_offset() const | |
378 | { | |
379 | return -scrollarea_.verticalScrollBar()->sliderPosition(); | |
380 | } | |
381 | ||
382 | void View::set_v_offset(int offset) | |
383 | { | |
384 | scrollarea_.verticalScrollBar()->setSliderPosition(offset); | |
385 | header_->update(); | |
386 | viewport_->update(); | |
387 | } | |
388 | ||
389 | unsigned int View::depth() const | |
390 | { | |
391 | return 0; | |
392 | } | |
393 | ||
394 | pv::util::SIPrefix View::tick_prefix() const | |
395 | { | |
396 | return tick_prefix_; | |
397 | } | |
398 | ||
399 | void View::set_tick_prefix(pv::util::SIPrefix tick_prefix) | |
400 | { | |
401 | if (tick_prefix_ != tick_prefix) { | |
402 | tick_prefix_ = tick_prefix; | |
403 | Q_EMIT tick_prefix_changed(); | |
404 | } | |
405 | } | |
406 | ||
407 | unsigned int View::tick_precision() const | |
408 | { | |
409 | return tick_precision_; | |
410 | } | |
411 | ||
412 | void View::set_tick_precision(unsigned tick_precision) | |
413 | { | |
414 | if (tick_precision_ != tick_precision) { | |
415 | tick_precision_ = tick_precision; | |
416 | Q_EMIT tick_precision_changed(); | |
417 | } | |
418 | } | |
419 | ||
420 | const pv::util::Timestamp& View::tick_period() const | |
421 | { | |
422 | return tick_period_; | |
423 | } | |
424 | ||
425 | void View::set_tick_period(const pv::util::Timestamp& tick_period) | |
426 | { | |
427 | if (tick_period_ != tick_period) { | |
428 | tick_period_ = tick_period; | |
429 | Q_EMIT tick_period_changed(); | |
430 | } | |
431 | } | |
432 | ||
433 | TimeUnit View::time_unit() const | |
434 | { | |
435 | return time_unit_; | |
436 | } | |
437 | ||
438 | void View::set_time_unit(pv::util::TimeUnit time_unit) | |
439 | { | |
440 | if (time_unit_ != time_unit) { | |
441 | time_unit_ = time_unit; | |
442 | Q_EMIT time_unit_changed(); | |
443 | } | |
444 | } | |
445 | ||
446 | void View::zoom(double steps) | |
447 | { | |
448 | zoom(steps, viewport_->width() / 2); | |
449 | } | |
450 | ||
451 | void View::zoom(double steps, int offset) | |
452 | { | |
453 | set_zoom(scale_ * pow(3.0/2.0, -steps), offset); | |
454 | } | |
455 | ||
456 | void View::zoom_fit(bool gui_state) | |
457 | { | |
458 | // Act as one-shot when stopped, toggle along with the GUI otherwise | |
459 | if (session_.get_capture_state() == Session::Stopped) { | |
460 | always_zoom_to_fit_ = false; | |
461 | always_zoom_to_fit_changed(false); | |
462 | } else { | |
463 | always_zoom_to_fit_ = gui_state; | |
464 | always_zoom_to_fit_changed(gui_state); | |
465 | } | |
466 | ||
467 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
468 | const Timestamp delta = extents.second - extents.first; | |
469 | if (delta < Timestamp("1e-12")) | |
470 | return; | |
471 | ||
472 | assert(viewport_); | |
473 | const int w = viewport_->width(); | |
474 | if (w <= 0) | |
475 | return; | |
476 | ||
477 | const Timestamp scale = max(min(delta / w, MaxScale), MinScale); | |
478 | set_scale_offset(scale.convert_to<double>(), extents.first); | |
479 | } | |
480 | ||
481 | void View::zoom_one_to_one() | |
482 | { | |
483 | using pv::data::SignalData; | |
484 | ||
485 | // Make a set of all the visible data objects | |
486 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
487 | if (visible_data.empty()) | |
488 | return; | |
489 | ||
490 | assert(viewport_); | |
491 | const int w = viewport_->width(); | |
492 | if (w <= 0) | |
493 | return; | |
494 | ||
495 | set_zoom(1.0 / session_.get_samplerate(), w / 2); | |
496 | } | |
497 | ||
498 | void View::set_scale_offset(double scale, const Timestamp& offset) | |
499 | { | |
500 | // Disable sticky scrolling / always zoom to fit when acquisition runs | |
501 | // and user drags the viewport | |
502 | if ((scale_ == scale) && (offset_ != offset) && | |
503 | (session_.get_capture_state() == Session::Running)) { | |
504 | ||
505 | if (sticky_scrolling_) { | |
506 | sticky_scrolling_ = false; | |
507 | sticky_scrolling_changed(false); | |
508 | } | |
509 | ||
510 | if (always_zoom_to_fit_) { | |
511 | always_zoom_to_fit_ = false; | |
512 | always_zoom_to_fit_changed(false); | |
513 | } | |
514 | } | |
515 | ||
516 | set_scale(scale); | |
517 | set_offset(offset); | |
518 | ||
519 | calculate_tick_spacing(); | |
520 | ||
521 | update_scroll(); | |
522 | ruler_->update(); | |
523 | viewport_->update(); | |
524 | } | |
525 | ||
526 | set< shared_ptr<SignalData> > View::get_visible_data() const | |
527 | { | |
528 | // Make a set of all the visible data objects | |
529 | set< shared_ptr<SignalData> > visible_data; | |
530 | for (const shared_ptr<Signal> sig : signals_) | |
531 | if (sig->enabled()) | |
532 | visible_data.insert(sig->data()); | |
533 | ||
534 | return visible_data; | |
535 | } | |
536 | ||
537 | pair<Timestamp, Timestamp> View::get_time_extents() const | |
538 | { | |
539 | boost::optional<Timestamp> left_time, right_time; | |
540 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
541 | for (const shared_ptr<SignalData> d : visible_data) { | |
542 | const vector< shared_ptr<Segment> > segments = | |
543 | d->segments(); | |
544 | for (const shared_ptr<Segment> &s : segments) { | |
545 | double samplerate = s->samplerate(); | |
546 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
547 | ||
548 | const Timestamp start_time = s->start_time(); | |
549 | left_time = left_time ? | |
550 | min(*left_time, start_time) : | |
551 | start_time; | |
552 | right_time = right_time ? | |
553 | max(*right_time, start_time + d->max_sample_count() / samplerate) : | |
554 | start_time + d->max_sample_count() / samplerate; | |
555 | } | |
556 | } | |
557 | ||
558 | if (!left_time || !right_time) | |
559 | return make_pair(0, 0); | |
560 | ||
561 | assert(*left_time < *right_time); | |
562 | return make_pair(*left_time, *right_time); | |
563 | } | |
564 | ||
565 | void View::enable_show_sampling_points(bool state) | |
566 | { | |
567 | (void)state; | |
568 | ||
569 | viewport_->update(); | |
570 | } | |
571 | ||
572 | void View::enable_coloured_bg(bool state) | |
573 | { | |
574 | const vector<shared_ptr<TraceTreeItem>> items( | |
575 | list_by_type<TraceTreeItem>()); | |
576 | ||
577 | for (shared_ptr<TraceTreeItem> i : items) { | |
578 | // Can't cast to Trace because it's abstract, so we need to | |
579 | // check for any derived classes individually | |
580 | ||
581 | shared_ptr<AnalogSignal> a = dynamic_pointer_cast<AnalogSignal>(i); | |
582 | if (a) | |
583 | a->set_coloured_bg(state); | |
584 | ||
585 | shared_ptr<LogicSignal> l = dynamic_pointer_cast<LogicSignal>(i); | |
586 | if (l) | |
587 | l->set_coloured_bg(state); | |
588 | ||
589 | #ifdef ENABLE_DECODE | |
590 | shared_ptr<DecodeTrace> d = dynamic_pointer_cast<DecodeTrace>(i); | |
591 | if (d) | |
592 | d->set_coloured_bg(state); | |
593 | #endif | |
594 | } | |
595 | ||
596 | viewport_->update(); | |
597 | } | |
598 | ||
599 | bool View::cursors_shown() const | |
600 | { | |
601 | return show_cursors_; | |
602 | } | |
603 | ||
604 | void View::show_cursors(bool show) | |
605 | { | |
606 | show_cursors_ = show; | |
607 | ruler_->update(); | |
608 | viewport_->update(); | |
609 | } | |
610 | ||
611 | void View::centre_cursors() | |
612 | { | |
613 | const double time_width = scale_ * viewport_->width(); | |
614 | cursors_->first()->set_time(offset_ + time_width * 0.4); | |
615 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
616 | ruler_->update(); | |
617 | viewport_->update(); | |
618 | } | |
619 | ||
620 | std::shared_ptr<CursorPair> View::cursors() const | |
621 | { | |
622 | return cursors_; | |
623 | } | |
624 | ||
625 | void View::add_flag(const Timestamp& time) | |
626 | { | |
627 | flags_.push_back(make_shared<Flag>(*this, time, | |
628 | QString("%1").arg(next_flag_text_))); | |
629 | ||
630 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
631 | (next_flag_text_ + 1); | |
632 | ||
633 | time_item_appearance_changed(true, true); | |
634 | } | |
635 | ||
636 | void View::remove_flag(std::shared_ptr<Flag> flag) | |
637 | { | |
638 | flags_.remove(flag); | |
639 | time_item_appearance_changed(true, true); | |
640 | } | |
641 | ||
642 | vector< std::shared_ptr<Flag> > View::flags() const | |
643 | { | |
644 | vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
645 | stable_sort(flags.begin(), flags.end(), | |
646 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
647 | return a->time() < b->time(); | |
648 | }); | |
649 | ||
650 | return flags; | |
651 | } | |
652 | ||
653 | const QPoint& View::hover_point() const | |
654 | { | |
655 | return hover_point_; | |
656 | } | |
657 | ||
658 | void View::restack_all_trace_tree_items() | |
659 | { | |
660 | // Make a list of owners that is sorted from deepest first | |
661 | const vector<shared_ptr<TraceTreeItem>> items( | |
662 | list_by_type<TraceTreeItem>()); | |
663 | set< TraceTreeItemOwner* > owners; | |
664 | for (const auto &r : items) | |
665 | owners.insert(r->owner()); | |
666 | vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
667 | sort(sorted_owners.begin(), sorted_owners.end(), | |
668 | [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) { | |
669 | return a->depth() > b->depth(); }); | |
670 | ||
671 | // Restack the items recursively | |
672 | for (auto &o : sorted_owners) | |
673 | o->restack_items(); | |
674 | ||
675 | // Re-assign background colors | |
676 | bool next_bgcolour_state = false; | |
677 | ||
678 | for (auto &o : sorted_owners) | |
679 | next_bgcolour_state = o->reassign_bgcolour_states(next_bgcolour_state); | |
680 | ||
681 | // Animate the items to their destination | |
682 | for (const auto &i : items) | |
683 | i->animate_to_layout_v_offset(); | |
684 | } | |
685 | ||
686 | void View::trigger_event(util::Timestamp location) | |
687 | { | |
688 | trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location)); | |
689 | } | |
690 | ||
691 | void View::get_scroll_layout(double &length, Timestamp &offset) const | |
692 | { | |
693 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
694 | length = ((extents.second - extents.first) / scale_).convert_to<double>(); | |
695 | offset = offset_ / scale_; | |
696 | } | |
697 | ||
698 | void View::set_zoom(double scale, int offset) | |
699 | { | |
700 | // Reset the "always zoom to fit" feature as the user changed the zoom | |
701 | always_zoom_to_fit_ = false; | |
702 | always_zoom_to_fit_changed(false); | |
703 | ||
704 | const Timestamp cursor_offset = offset_ + scale_ * offset; | |
705 | const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale); | |
706 | const Timestamp new_offset = cursor_offset - new_scale * offset; | |
707 | set_scale_offset(new_scale.convert_to<double>(), new_offset); | |
708 | } | |
709 | ||
710 | void View::calculate_tick_spacing() | |
711 | { | |
712 | const double SpacingIncrement = 10.0f; | |
713 | const double MinValueSpacing = 40.0f; | |
714 | ||
715 | // Figure out the highest numeric value visible on a label | |
716 | const QSize areaSize = viewport_->size(); | |
717 | const Timestamp max_time = max(fabs(offset_), | |
718 | fabs(offset_ + scale_ * areaSize.width())); | |
719 | ||
720 | double min_width = SpacingIncrement; | |
721 | double label_width, tick_period_width; | |
722 | ||
723 | QFontMetrics m(QApplication::font()); | |
724 | ||
725 | // Copies of the member variables with the same name, used in the calculation | |
726 | // and written back afterwards, so that we don't emit signals all the time | |
727 | // during the calculation. | |
728 | pv::util::Timestamp tick_period = tick_period_; | |
729 | pv::util::SIPrefix tick_prefix = tick_prefix_; | |
730 | unsigned tick_precision = tick_precision_; | |
731 | ||
732 | do { | |
733 | const double min_period = scale_ * min_width; | |
734 | ||
735 | const int order = (int)floorf(log10f(min_period)); | |
736 | const pv::util::Timestamp order_decimal = | |
737 | pow(pv::util::Timestamp(10), order); | |
738 | ||
739 | // Allow for a margin of error so that a scale unit of 1 can be used. | |
740 | // Otherwise, for a SU of 1 the tick period will almost always be below | |
741 | // the min_period by a small amount - and thus skipped in favor of 2. | |
742 | // Note: margin assumes that SU[0] and SU[1] contain the smallest values | |
743 | double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0; | |
744 | double tp_with_margin; | |
745 | unsigned int unit = 0; | |
746 | ||
747 | do { | |
748 | tp_with_margin = order_decimal.convert_to<double>() * | |
749 | (ScaleUnits[unit++] + tp_margin); | |
750 | } while (tp_with_margin < min_period && unit < countof(ScaleUnits)); | |
751 | ||
752 | tick_period = order_decimal * ScaleUnits[unit - 1]; | |
753 | tick_prefix = static_cast<pv::util::SIPrefix>( | |
754 | (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3); | |
755 | ||
756 | // Precision is the number of fractional digits required, not | |
757 | // taking the prefix into account (and it must never be negative) | |
758 | tick_precision = std::max(ceil(log10(1 / tick_period)).convert_to<int>(), 0); | |
759 | ||
760 | tick_period_width = (tick_period / scale_).convert_to<double>(); | |
761 | ||
762 | const QString label_text = Ruler::format_time_with_distance( | |
763 | tick_period, max_time, tick_prefix, time_unit_, tick_precision); | |
764 | ||
765 | label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
766 | Qt::AlignLeft | Qt::AlignTop, label_text).width() + | |
767 | MinValueSpacing; | |
768 | ||
769 | min_width += SpacingIncrement; | |
770 | } while (tick_period_width < label_width); | |
771 | ||
772 | set_tick_period(tick_period); | |
773 | set_tick_prefix(tick_prefix); | |
774 | set_tick_precision(tick_precision); | |
775 | } | |
776 | ||
777 | void View::update_scroll() | |
778 | { | |
779 | assert(viewport_); | |
780 | QScrollBar *hscrollbar = scrollarea_.horizontalScrollBar(); | |
781 | QScrollBar *vscrollbar = scrollarea_.verticalScrollBar(); | |
782 | ||
783 | const QSize areaSize = viewport_->size(); | |
784 | ||
785 | // Set the horizontal scroll bar | |
786 | double length = 0; | |
787 | Timestamp offset; | |
788 | get_scroll_layout(length, offset); | |
789 | length = max(length - areaSize.width(), 0.0); | |
790 | ||
791 | int major_tick_distance = (tick_period_ / scale_).convert_to<int>(); | |
792 | ||
793 | hscrollbar->setPageStep(areaSize.width() / 2); | |
794 | hscrollbar->setSingleStep(major_tick_distance); | |
795 | ||
796 | updating_scroll_ = true; | |
797 | ||
798 | if (length < MaxScrollValue) { | |
799 | hscrollbar->setRange(0, length); | |
800 | hscrollbar->setSliderPosition(offset.convert_to<double>()); | |
801 | } else { | |
802 | hscrollbar->setRange(0, MaxScrollValue); | |
803 | hscrollbar->setSliderPosition( | |
804 | (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>()); | |
805 | } | |
806 | ||
807 | updating_scroll_ = false; | |
808 | ||
809 | // Set the vertical scrollbar | |
810 | vscrollbar->setPageStep(areaSize.height()); | |
811 | vscrollbar->setSingleStep(areaSize.height() / 8); | |
812 | ||
813 | const pair<int, int> extents = v_extents(); | |
814 | ||
815 | // Don't change the scrollbar range if there are no traces | |
816 | if (extents.first != extents.second) | |
817 | vscrollbar->setRange(extents.first - areaSize.height(), | |
818 | extents.second); | |
819 | ||
820 | if (scroll_needs_defaults_) | |
821 | set_scroll_default(); | |
822 | } | |
823 | ||
824 | void View::reset_scroll() | |
825 | { | |
826 | scrollarea_.verticalScrollBar()->setRange(0, 0); | |
827 | } | |
828 | ||
829 | void View::set_scroll_default() | |
830 | { | |
831 | assert(viewport_); | |
832 | ||
833 | const QSize areaSize = viewport_->size(); | |
834 | ||
835 | const pair<int, int> extents = v_extents(); | |
836 | const int trace_height = extents.second - extents.first; | |
837 | ||
838 | // Do all traces fit in the view? | |
839 | if (areaSize.height() >= trace_height) | |
840 | // Center all traces vertically | |
841 | set_v_offset(extents.first - | |
842 | ((areaSize.height() - trace_height) / 2)); | |
843 | else | |
844 | // Put the first trace at the top, letting the bottom ones overflow | |
845 | set_v_offset(extents.first); | |
846 | ||
847 | // If we're not sure whether setting the defaults worked as | |
848 | // the window wasn't set up entirely yet, we want to be called | |
849 | // again later to make sure | |
850 | scroll_needs_defaults_ = !size_finalized_; | |
851 | } | |
852 | ||
853 | void View::update_layout() | |
854 | { | |
855 | scrollarea_.setViewportMargins( | |
856 | header_->sizeHint().width() - Header::BaselineOffset, | |
857 | ruler_->sizeHint().height(), 0, 0); | |
858 | ruler_->setGeometry(viewport_->x(), 0, | |
859 | viewport_->width(), ruler_->extended_size_hint().height()); | |
860 | header_->setGeometry(0, viewport_->y(), | |
861 | header_->extended_size_hint().width(), viewport_->height()); | |
862 | update_scroll(); | |
863 | } | |
864 | ||
865 | TraceTreeItemOwner* View::find_prevalent_trace_group( | |
866 | const shared_ptr<sigrok::ChannelGroup> &group, | |
867 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
868 | &signal_map) | |
869 | { | |
870 | assert(group); | |
871 | ||
872 | unordered_set<TraceTreeItemOwner*> owners; | |
873 | vector<TraceTreeItemOwner*> owner_list; | |
874 | ||
875 | // Make a set and a list of all the owners | |
876 | for (const auto &channel : group->channels()) { | |
877 | for (auto entry : signal_map) { | |
878 | if (entry.first->channel() == channel) { | |
879 | TraceTreeItemOwner *const o = (entry.second)->owner(); | |
880 | owner_list.push_back(o); | |
881 | owners.insert(o); | |
882 | } | |
883 | } | |
884 | } | |
885 | ||
886 | // Iterate through the list of owners, and find the most prevalent | |
887 | size_t max_prevalence = 0; | |
888 | TraceTreeItemOwner *prevalent_owner = nullptr; | |
889 | for (TraceTreeItemOwner *owner : owners) { | |
890 | const size_t prevalence = std::count_if( | |
891 | owner_list.begin(), owner_list.end(), | |
892 | [&](TraceTreeItemOwner *o) { return o == owner; }); | |
893 | if (prevalence > max_prevalence) { | |
894 | max_prevalence = prevalence; | |
895 | prevalent_owner = owner; | |
896 | } | |
897 | } | |
898 | ||
899 | return prevalent_owner; | |
900 | } | |
901 | ||
902 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( | |
903 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
904 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
905 | &signal_map, | |
906 | set< shared_ptr<Trace> > &add_list) | |
907 | { | |
908 | vector< shared_ptr<Trace> > filtered_traces; | |
909 | ||
910 | for (const auto &channel : channels) { | |
911 | for (auto entry : signal_map) { | |
912 | if (entry.first->channel() == channel) { | |
913 | shared_ptr<Trace> trace = entry.second; | |
914 | const auto list_iter = add_list.find(trace); | |
915 | if (list_iter == add_list.end()) | |
916 | continue; | |
917 | ||
918 | filtered_traces.push_back(trace); | |
919 | add_list.erase(list_iter); | |
920 | } | |
921 | } | |
922 | } | |
923 | ||
924 | return filtered_traces; | |
925 | } | |
926 | ||
927 | void View::determine_time_unit() | |
928 | { | |
929 | // Check whether we know the sample rate and hence can use time as the unit | |
930 | if (time_unit_ == util::TimeUnit::Samples) { | |
931 | // Check all signals but... | |
932 | for (const shared_ptr<Signal> signal : signals_) { | |
933 | const shared_ptr<SignalData> data = signal->data(); | |
934 | ||
935 | // ...only check first segment of each | |
936 | const vector< shared_ptr<Segment> > segments = data->segments(); | |
937 | if (!segments.empty()) | |
938 | if (segments[0]->samplerate()) { | |
939 | set_time_unit(util::TimeUnit::Time); | |
940 | break; | |
941 | } | |
942 | } | |
943 | } | |
944 | } | |
945 | ||
946 | bool View::eventFilter(QObject *object, QEvent *event) | |
947 | { | |
948 | const QEvent::Type type = event->type(); | |
949 | if (type == QEvent::MouseMove) { | |
950 | ||
951 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
952 | if (object == viewport_) | |
953 | hover_point_ = mouse_event->pos(); | |
954 | else if (object == ruler_) | |
955 | hover_point_ = QPoint(mouse_event->x(), 0); | |
956 | else if (object == header_) | |
957 | hover_point_ = QPoint(0, mouse_event->y()); | |
958 | else | |
959 | hover_point_ = QPoint(-1, -1); | |
960 | ||
961 | hover_point_changed(); | |
962 | ||
963 | } else if (type == QEvent::Leave) { | |
964 | hover_point_ = QPoint(-1, -1); | |
965 | hover_point_changed(); | |
966 | } | |
967 | ||
968 | return QObject::eventFilter(object, event); | |
969 | } | |
970 | ||
971 | void View::resizeEvent(QResizeEvent*) | |
972 | { | |
973 | update_layout(); | |
974 | ||
975 | // This is somewhat of a hack, unfortunately. We cannot use | |
976 | // set_v_offset() from within restore_settings() as the view | |
977 | // at that point is neither visible nor properly sized. | |
978 | // This is the least intrusive workaround I could come up | |
979 | // with: set the vertical offset (or scroll defaults) when | |
980 | // the view is visible and resized to its final size. | |
981 | // Resize events that are sent when the view is not visible | |
982 | // must be ignored as they have wrong sizes, potentially | |
983 | // preventing the v offset from being set successfully. | |
984 | ||
985 | if (isVisible()) | |
986 | size_finalized_ = true; | |
987 | ||
988 | if (size_finalized_ && saved_v_offset_) { | |
989 | set_v_offset(saved_v_offset_); | |
990 | saved_v_offset_ = 0; | |
991 | } | |
992 | ||
993 | if (size_finalized_ && scroll_needs_defaults_) | |
994 | set_scroll_default(); | |
995 | } | |
996 | ||
997 | void View::row_item_appearance_changed(bool label, bool content) | |
998 | { | |
999 | if (label) | |
1000 | header_->update(); | |
1001 | if (content) | |
1002 | viewport_->update(); | |
1003 | } | |
1004 | ||
1005 | void View::time_item_appearance_changed(bool label, bool content) | |
1006 | { | |
1007 | if (label) | |
1008 | ruler_->update(); | |
1009 | if (content) | |
1010 | viewport_->update(); | |
1011 | } | |
1012 | ||
1013 | void View::extents_changed(bool horz, bool vert) | |
1014 | { | |
1015 | sticky_events_ |= | |
1016 | (horz ? TraceTreeItemHExtentsChanged : 0) | | |
1017 | (vert ? TraceTreeItemVExtentsChanged : 0); | |
1018 | lazy_event_handler_.start(); | |
1019 | } | |
1020 | ||
1021 | void View::h_scroll_value_changed(int value) | |
1022 | { | |
1023 | if (updating_scroll_) | |
1024 | return; | |
1025 | ||
1026 | // Disable sticky scrolling when user moves the horizontal scroll bar | |
1027 | // during a running acquisition | |
1028 | if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) { | |
1029 | sticky_scrolling_ = false; | |
1030 | sticky_scrolling_changed(false); | |
1031 | } | |
1032 | ||
1033 | const int range = scrollarea_.horizontalScrollBar()->maximum(); | |
1034 | if (range < MaxScrollValue) | |
1035 | set_offset(scale_ * value); | |
1036 | else { | |
1037 | double length = 0; | |
1038 | Timestamp offset; | |
1039 | get_scroll_layout(length, offset); | |
1040 | set_offset(scale_ * length * value / MaxScrollValue); | |
1041 | } | |
1042 | ||
1043 | ruler_->update(); | |
1044 | viewport_->update(); | |
1045 | } | |
1046 | ||
1047 | void View::v_scroll_value_changed() | |
1048 | { | |
1049 | header_->update(); | |
1050 | viewport_->update(); | |
1051 | } | |
1052 | ||
1053 | void View::signals_changed() | |
1054 | { | |
1055 | using sigrok::Channel; | |
1056 | ||
1057 | vector< shared_ptr<Channel> > channels; | |
1058 | shared_ptr<sigrok::Device> sr_dev; | |
1059 | ||
1060 | // Do we need to set the vertical scrollbar to its default position later? | |
1061 | // We do if there are no traces, i.e. the scroll bar has no range set | |
1062 | bool reset_scrollbar = | |
1063 | (scrollarea_.verticalScrollBar()->minimum() == | |
1064 | scrollarea_.verticalScrollBar()->maximum()); | |
1065 | ||
1066 | if (!session_.device()) { | |
1067 | reset_scroll(); | |
1068 | signals_.clear(); | |
1069 | } else { | |
1070 | sr_dev = session_.device()->device(); | |
1071 | assert(sr_dev); | |
1072 | channels = sr_dev->channels(); | |
1073 | } | |
1074 | ||
1075 | vector< shared_ptr<TraceTreeItem> > new_top_level_items; | |
1076 | ||
1077 | // Make a list of traces that are being added, and a list of traces | |
1078 | // that are being removed | |
1079 | const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>(); | |
1080 | const set<shared_ptr<Trace>> prev_traces( | |
1081 | prev_trace_list.begin(), prev_trace_list.end()); | |
1082 | ||
1083 | set< shared_ptr<Trace> > traces(signals_.begin(), signals_.end()); | |
1084 | ||
1085 | #ifdef ENABLE_DECODE | |
1086 | traces.insert(decode_traces_.begin(), decode_traces_.end()); | |
1087 | #endif | |
1088 | ||
1089 | set< shared_ptr<Trace> > add_traces; | |
1090 | set_difference(traces.begin(), traces.end(), | |
1091 | prev_traces.begin(), prev_traces.end(), | |
1092 | inserter(add_traces, add_traces.begin())); | |
1093 | ||
1094 | set< shared_ptr<Trace> > remove_traces; | |
1095 | set_difference(prev_traces.begin(), prev_traces.end(), | |
1096 | traces.begin(), traces.end(), | |
1097 | inserter(remove_traces, remove_traces.begin())); | |
1098 | ||
1099 | // Make a look-up table of sigrok Channels to pulseview Signals | |
1100 | unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
1101 | signal_map; | |
1102 | for (const shared_ptr<Signal> &sig : signals_) | |
1103 | signal_map[sig->base()] = sig; | |
1104 | ||
1105 | // Populate channel groups | |
1106 | if (sr_dev) | |
1107 | for (auto entry : sr_dev->channel_groups()) { | |
1108 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
1109 | ||
1110 | if (group->channels().size() <= 1) | |
1111 | continue; | |
1112 | ||
1113 | // Find best trace group to add to | |
1114 | TraceTreeItemOwner *owner = find_prevalent_trace_group( | |
1115 | group, signal_map); | |
1116 | ||
1117 | // If there is no trace group, create one | |
1118 | shared_ptr<TraceGroup> new_trace_group; | |
1119 | if (!owner) { | |
1120 | new_trace_group.reset(new TraceGroup()); | |
1121 | owner = new_trace_group.get(); | |
1122 | } | |
1123 | ||
1124 | // Extract traces for the trace group, removing them from | |
1125 | // the add list | |
1126 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
1127 | extract_new_traces_for_channels(group->channels(), | |
1128 | signal_map, add_traces); | |
1129 | ||
1130 | // Add the traces to the group | |
1131 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
1132 | int offset = prev_v_extents.second - prev_v_extents.first; | |
1133 | for (shared_ptr<Trace> trace : new_traces_in_group) { | |
1134 | assert(trace); | |
1135 | owner->add_child_item(trace); | |
1136 | ||
1137 | const pair<int, int> extents = trace->v_extents(); | |
1138 | if (trace->enabled()) | |
1139 | offset += -extents.first; | |
1140 | trace->force_to_v_offset(offset); | |
1141 | if (trace->enabled()) | |
1142 | offset += extents.second; | |
1143 | } | |
1144 | ||
1145 | if (new_trace_group) { | |
1146 | // Assign proper vertical offsets to each channel in the group | |
1147 | new_trace_group->restack_items(); | |
1148 | ||
1149 | // If this is a new group, enqueue it in the new top level | |
1150 | // items list | |
1151 | if (!new_traces_in_group.empty()) | |
1152 | new_top_level_items.push_back(new_trace_group); | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | // Enqueue the remaining logic channels in a group | |
1157 | vector< shared_ptr<Channel> > logic_channels; | |
1158 | copy_if(channels.begin(), channels.end(), back_inserter(logic_channels), | |
1159 | [](const shared_ptr<Channel>& c) { | |
1160 | return c->type() == sigrok::ChannelType::LOGIC; }); | |
1161 | ||
1162 | const vector< shared_ptr<Trace> > non_grouped_logic_signals = | |
1163 | extract_new_traces_for_channels(logic_channels, signal_map, add_traces); | |
1164 | ||
1165 | if (non_grouped_logic_signals.size() > 0) { | |
1166 | const shared_ptr<TraceGroup> non_grouped_trace_group( | |
1167 | make_shared<TraceGroup>()); | |
1168 | for (shared_ptr<Trace> trace : non_grouped_logic_signals) | |
1169 | non_grouped_trace_group->add_child_item(trace); | |
1170 | ||
1171 | non_grouped_trace_group->restack_items(); | |
1172 | new_top_level_items.push_back(non_grouped_trace_group); | |
1173 | } | |
1174 | ||
1175 | // Enqueue the remaining channels as free ungrouped traces | |
1176 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
1177 | extract_new_traces_for_channels(channels, signal_map, add_traces); | |
1178 | new_top_level_items.insert(new_top_level_items.end(), | |
1179 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
1180 | ||
1181 | // Enqueue any remaining traces i.e. decode traces | |
1182 | new_top_level_items.insert(new_top_level_items.end(), | |
1183 | add_traces.begin(), add_traces.end()); | |
1184 | ||
1185 | // Remove any removed traces | |
1186 | for (shared_ptr<Trace> trace : remove_traces) { | |
1187 | TraceTreeItemOwner *const owner = trace->owner(); | |
1188 | assert(owner); | |
1189 | owner->remove_child_item(trace); | |
1190 | } | |
1191 | ||
1192 | // Remove any empty trace groups | |
1193 | for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>()) | |
1194 | if (group->child_items().size() == 0) { | |
1195 | remove_child_item(group); | |
1196 | group.reset(); | |
1197 | } | |
1198 | ||
1199 | // Add and position the pending top levels items | |
1200 | int offset = v_extents().second; | |
1201 | for (auto item : new_top_level_items) { | |
1202 | add_child_item(item); | |
1203 | ||
1204 | // Position the item after the last item or at the top if there is none | |
1205 | const pair<int, int> extents = item->v_extents(); | |
1206 | ||
1207 | if (item->enabled()) | |
1208 | offset += -extents.first; | |
1209 | ||
1210 | item->force_to_v_offset(offset); | |
1211 | ||
1212 | if (item->enabled()) | |
1213 | offset += extents.second; | |
1214 | } | |
1215 | ||
1216 | update_layout(); | |
1217 | ||
1218 | header_->update(); | |
1219 | viewport_->update(); | |
1220 | ||
1221 | if (reset_scrollbar) | |
1222 | set_scroll_default(); | |
1223 | } | |
1224 | ||
1225 | void View::capture_state_updated(int state) | |
1226 | { | |
1227 | if (state == Session::Running) { | |
1228 | set_time_unit(util::TimeUnit::Samples); | |
1229 | ||
1230 | trigger_markers_.clear(); | |
1231 | ||
1232 | // Activate "always zoom to fit" if the setting is enabled and we're | |
1233 | // the main view of this session (other trace views may be used for | |
1234 | // zooming and we don't want to mess them up) | |
1235 | GlobalSettings settings; | |
1236 | bool state = settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool(); | |
1237 | if (is_main_view_ && state) { | |
1238 | always_zoom_to_fit_ = true; | |
1239 | always_zoom_to_fit_changed(always_zoom_to_fit_); | |
1240 | } | |
1241 | ||
1242 | // Enable sticky scrolling if the setting is enabled | |
1243 | sticky_scrolling_ = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool(); | |
1244 | } | |
1245 | ||
1246 | if (state == Session::Stopped) { | |
1247 | // After acquisition has stopped we need to re-calculate the ticks once | |
1248 | // as it's otherwise done when the user pans or zooms, which is too late | |
1249 | calculate_tick_spacing(); | |
1250 | ||
1251 | // Reset "always zoom to fit", the acquisition has stopped | |
1252 | if (always_zoom_to_fit_) { | |
1253 | // Perform a final zoom-to-fit before disabling | |
1254 | zoom_fit(always_zoom_to_fit_); | |
1255 | always_zoom_to_fit_ = false; | |
1256 | always_zoom_to_fit_changed(always_zoom_to_fit_); | |
1257 | } | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | void View::data_updated() | |
1262 | { | |
1263 | if (always_zoom_to_fit_ || sticky_scrolling_) { | |
1264 | if (!delayed_view_updater_.isActive()) | |
1265 | delayed_view_updater_.start(); | |
1266 | } else { | |
1267 | determine_time_unit(); | |
1268 | update_scroll(); | |
1269 | ruler_->update(); | |
1270 | viewport_->update(); | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | void View::perform_delayed_view_update() | |
1275 | { | |
1276 | if (always_zoom_to_fit_) { | |
1277 | zoom_fit(true); | |
1278 | } else if (sticky_scrolling_) { | |
1279 | // Make right side of the view sticky | |
1280 | double length = 0; | |
1281 | Timestamp offset; | |
1282 | get_scroll_layout(length, offset); | |
1283 | ||
1284 | const QSize areaSize = viewport_->size(); | |
1285 | length = max(length - areaSize.width(), 0.0); | |
1286 | ||
1287 | set_offset(scale_ * length); | |
1288 | } | |
1289 | ||
1290 | determine_time_unit(); | |
1291 | update_scroll(); | |
1292 | ruler_->update(); | |
1293 | viewport_->update(); | |
1294 | } | |
1295 | ||
1296 | void View::process_sticky_events() | |
1297 | { | |
1298 | if (sticky_events_ & TraceTreeItemHExtentsChanged) | |
1299 | update_layout(); | |
1300 | if (sticky_events_ & TraceTreeItemVExtentsChanged) { | |
1301 | restack_all_trace_tree_items(); | |
1302 | update_scroll(); | |
1303 | } | |
1304 | ||
1305 | // Clear the sticky events | |
1306 | sticky_events_ = 0; | |
1307 | } | |
1308 | ||
1309 | void View::on_hover_point_changed() | |
1310 | { | |
1311 | const vector<shared_ptr<TraceTreeItem>> trace_tree_items( | |
1312 | list_by_type<TraceTreeItem>()); | |
1313 | for (shared_ptr<TraceTreeItem> r : trace_tree_items) | |
1314 | r->hover_point_changed(); | |
1315 | } | |
1316 | ||
1317 | } // namespace TraceView | |
1318 | } // namespace views | |
1319 | } // namespace pv |