]>
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_sticky_scrolling(bool state) | |
566 | { | |
567 | sticky_scrolling_ = state; | |
568 | } | |
569 | ||
570 | void View::toggle_sticky_scrolling() | |
571 | { | |
572 | sticky_scrolling_ = !sticky_scrolling_; | |
573 | } | |
574 | ||
575 | void View::enable_coloured_bg(bool state) | |
576 | { | |
577 | const vector<shared_ptr<TraceTreeItem>> items( | |
578 | list_by_type<TraceTreeItem>()); | |
579 | ||
580 | for (shared_ptr<TraceTreeItem> i : items) { | |
581 | // Can't cast to Trace because it's abstract, so we need to | |
582 | // check for any derived classes individually | |
583 | ||
584 | shared_ptr<AnalogSignal> a = dynamic_pointer_cast<AnalogSignal>(i); | |
585 | if (a) | |
586 | a->set_coloured_bg(state); | |
587 | ||
588 | shared_ptr<LogicSignal> l = dynamic_pointer_cast<LogicSignal>(i); | |
589 | if (l) | |
590 | l->set_coloured_bg(state); | |
591 | ||
592 | #ifdef ENABLE_DECODE | |
593 | shared_ptr<DecodeTrace> d = dynamic_pointer_cast<DecodeTrace>(i); | |
594 | if (d) | |
595 | d->set_coloured_bg(state); | |
596 | #endif | |
597 | } | |
598 | ||
599 | viewport_->update(); | |
600 | } | |
601 | ||
602 | bool View::cursors_shown() const | |
603 | { | |
604 | return show_cursors_; | |
605 | } | |
606 | ||
607 | void View::show_cursors(bool show) | |
608 | { | |
609 | show_cursors_ = show; | |
610 | ruler_->update(); | |
611 | viewport_->update(); | |
612 | } | |
613 | ||
614 | void View::centre_cursors() | |
615 | { | |
616 | const double time_width = scale_ * viewport_->width(); | |
617 | cursors_->first()->set_time(offset_ + time_width * 0.4); | |
618 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
619 | ruler_->update(); | |
620 | viewport_->update(); | |
621 | } | |
622 | ||
623 | std::shared_ptr<CursorPair> View::cursors() const | |
624 | { | |
625 | return cursors_; | |
626 | } | |
627 | ||
628 | void View::add_flag(const Timestamp& time) | |
629 | { | |
630 | flags_.push_back(make_shared<Flag>(*this, time, | |
631 | QString("%1").arg(next_flag_text_))); | |
632 | ||
633 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
634 | (next_flag_text_ + 1); | |
635 | ||
636 | time_item_appearance_changed(true, true); | |
637 | } | |
638 | ||
639 | void View::remove_flag(std::shared_ptr<Flag> flag) | |
640 | { | |
641 | flags_.remove(flag); | |
642 | time_item_appearance_changed(true, true); | |
643 | } | |
644 | ||
645 | vector< std::shared_ptr<Flag> > View::flags() const | |
646 | { | |
647 | vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
648 | stable_sort(flags.begin(), flags.end(), | |
649 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
650 | return a->time() < b->time(); | |
651 | }); | |
652 | ||
653 | return flags; | |
654 | } | |
655 | ||
656 | const QPoint& View::hover_point() const | |
657 | { | |
658 | return hover_point_; | |
659 | } | |
660 | ||
661 | void View::restack_all_trace_tree_items() | |
662 | { | |
663 | // Make a list of owners that is sorted from deepest first | |
664 | const vector<shared_ptr<TraceTreeItem>> items( | |
665 | list_by_type<TraceTreeItem>()); | |
666 | set< TraceTreeItemOwner* > owners; | |
667 | for (const auto &r : items) | |
668 | owners.insert(r->owner()); | |
669 | vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
670 | sort(sorted_owners.begin(), sorted_owners.end(), | |
671 | [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) { | |
672 | return a->depth() > b->depth(); }); | |
673 | ||
674 | // Restack the items recursively | |
675 | for (auto &o : sorted_owners) | |
676 | o->restack_items(); | |
677 | ||
678 | // Re-assign background colors | |
679 | bool next_bgcolour_state = 0; | |
680 | ||
681 | for (auto &o : sorted_owners) | |
682 | next_bgcolour_state = o->reassign_bgcolour_states(next_bgcolour_state); | |
683 | ||
684 | // Animate the items to their destination | |
685 | for (const auto &i : items) | |
686 | i->animate_to_layout_v_offset(); | |
687 | } | |
688 | ||
689 | void View::trigger_event(util::Timestamp location) | |
690 | { | |
691 | trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location)); | |
692 | } | |
693 | ||
694 | void View::get_scroll_layout(double &length, Timestamp &offset) const | |
695 | { | |
696 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
697 | length = ((extents.second - extents.first) / scale_).convert_to<double>(); | |
698 | offset = offset_ / scale_; | |
699 | } | |
700 | ||
701 | void View::set_zoom(double scale, int offset) | |
702 | { | |
703 | // Reset the "always zoom to fit" feature as the user changed the zoom | |
704 | always_zoom_to_fit_ = false; | |
705 | always_zoom_to_fit_changed(false); | |
706 | ||
707 | const Timestamp cursor_offset = offset_ + scale_ * offset; | |
708 | const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale); | |
709 | const Timestamp new_offset = cursor_offset - new_scale * offset; | |
710 | set_scale_offset(new_scale.convert_to<double>(), new_offset); | |
711 | } | |
712 | ||
713 | void View::calculate_tick_spacing() | |
714 | { | |
715 | const double SpacingIncrement = 10.0f; | |
716 | const double MinValueSpacing = 40.0f; | |
717 | ||
718 | // Figure out the highest numeric value visible on a label | |
719 | const QSize areaSize = viewport_->size(); | |
720 | const Timestamp max_time = max(fabs(offset_), | |
721 | fabs(offset_ + scale_ * areaSize.width())); | |
722 | ||
723 | double min_width = SpacingIncrement; | |
724 | double label_width, tick_period_width; | |
725 | ||
726 | QFontMetrics m(QApplication::font()); | |
727 | ||
728 | // Copies of the member variables with the same name, used in the calculation | |
729 | // and written back afterwards, so that we don't emit signals all the time | |
730 | // during the calculation. | |
731 | pv::util::Timestamp tick_period = tick_period_; | |
732 | pv::util::SIPrefix tick_prefix = tick_prefix_; | |
733 | unsigned tick_precision = tick_precision_; | |
734 | ||
735 | do { | |
736 | const double min_period = scale_ * min_width; | |
737 | ||
738 | const int order = (int)floorf(log10f(min_period)); | |
739 | const pv::util::Timestamp order_decimal = | |
740 | pow(pv::util::Timestamp(10), order); | |
741 | ||
742 | // Allow for a margin of error so that a scale unit of 1 can be used. | |
743 | // Otherwise, for a SU of 1 the tick period will almost always be below | |
744 | // the min_period by a small amount - and thus skipped in favor of 2. | |
745 | // Note: margin assumes that SU[0] and SU[1] contain the smallest values | |
746 | double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0; | |
747 | double tp_with_margin; | |
748 | unsigned int unit = 0; | |
749 | ||
750 | do { | |
751 | tp_with_margin = order_decimal.convert_to<double>() * | |
752 | (ScaleUnits[unit++] + tp_margin); | |
753 | } while (tp_with_margin < min_period && unit < countof(ScaleUnits)); | |
754 | ||
755 | tick_period = order_decimal * ScaleUnits[unit - 1]; | |
756 | tick_prefix = static_cast<pv::util::SIPrefix>( | |
757 | (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3); | |
758 | ||
759 | // Precision is the number of fractional digits required, not | |
760 | // taking the prefix into account (and it must never be negative) | |
761 | tick_precision = std::max(ceil(log10(1 / tick_period)).convert_to<int>(), 0); | |
762 | ||
763 | tick_period_width = (tick_period / scale_).convert_to<double>(); | |
764 | ||
765 | const QString label_text = Ruler::format_time_with_distance( | |
766 | tick_period, max_time, tick_prefix, time_unit_, tick_precision); | |
767 | ||
768 | label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
769 | Qt::AlignLeft | Qt::AlignTop, label_text).width() + | |
770 | MinValueSpacing; | |
771 | ||
772 | min_width += SpacingIncrement; | |
773 | } while (tick_period_width < label_width); | |
774 | ||
775 | set_tick_period(tick_period); | |
776 | set_tick_prefix(tick_prefix); | |
777 | set_tick_precision(tick_precision); | |
778 | } | |
779 | ||
780 | void View::update_scroll() | |
781 | { | |
782 | assert(viewport_); | |
783 | QScrollBar *hscrollbar = scrollarea_.horizontalScrollBar(); | |
784 | QScrollBar *vscrollbar = scrollarea_.verticalScrollBar(); | |
785 | ||
786 | const QSize areaSize = viewport_->size(); | |
787 | ||
788 | // Set the horizontal scroll bar | |
789 | double length = 0; | |
790 | Timestamp offset; | |
791 | get_scroll_layout(length, offset); | |
792 | length = max(length - areaSize.width(), 0.0); | |
793 | ||
794 | int major_tick_distance = (tick_period_ / scale_).convert_to<int>(); | |
795 | ||
796 | hscrollbar->setPageStep(areaSize.width() / 2); | |
797 | hscrollbar->setSingleStep(major_tick_distance); | |
798 | ||
799 | updating_scroll_ = true; | |
800 | ||
801 | if (length < MaxScrollValue) { | |
802 | hscrollbar->setRange(0, length); | |
803 | hscrollbar->setSliderPosition(offset.convert_to<double>()); | |
804 | } else { | |
805 | hscrollbar->setRange(0, MaxScrollValue); | |
806 | hscrollbar->setSliderPosition( | |
807 | (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>()); | |
808 | } | |
809 | ||
810 | updating_scroll_ = false; | |
811 | ||
812 | // Set the vertical scrollbar | |
813 | vscrollbar->setPageStep(areaSize.height()); | |
814 | vscrollbar->setSingleStep(areaSize.height() / 8); | |
815 | ||
816 | const pair<int, int> extents = v_extents(); | |
817 | ||
818 | // Don't change the scrollbar range if there are no traces | |
819 | if (extents.first != extents.second) | |
820 | vscrollbar->setRange(extents.first - areaSize.height(), | |
821 | extents.second); | |
822 | ||
823 | if (scroll_needs_defaults_) | |
824 | set_scroll_default(); | |
825 | } | |
826 | ||
827 | void View::reset_scroll() | |
828 | { | |
829 | scrollarea_.verticalScrollBar()->setRange(0, 0); | |
830 | } | |
831 | ||
832 | void View::set_scroll_default() | |
833 | { | |
834 | assert(viewport_); | |
835 | ||
836 | const QSize areaSize = viewport_->size(); | |
837 | ||
838 | const pair<int, int> extents = v_extents(); | |
839 | const int trace_height = extents.second - extents.first; | |
840 | ||
841 | // Do all traces fit in the view? | |
842 | if (areaSize.height() >= trace_height) | |
843 | // Center all traces vertically | |
844 | set_v_offset(extents.first - | |
845 | ((areaSize.height() - trace_height) / 2)); | |
846 | else | |
847 | // Put the first trace at the top, letting the bottom ones overflow | |
848 | set_v_offset(extents.first); | |
849 | ||
850 | // If we're not sure whether setting the defaults worked as | |
851 | // the window wasn't set up entirely yet, we want to be called | |
852 | // again later to make sure | |
853 | scroll_needs_defaults_ = !size_finalized_; | |
854 | } | |
855 | ||
856 | void View::update_layout() | |
857 | { | |
858 | scrollarea_.setViewportMargins( | |
859 | header_->sizeHint().width() - Header::BaselineOffset, | |
860 | ruler_->sizeHint().height(), 0, 0); | |
861 | ruler_->setGeometry(viewport_->x(), 0, | |
862 | viewport_->width(), ruler_->extended_size_hint().height()); | |
863 | header_->setGeometry(0, viewport_->y(), | |
864 | header_->extended_size_hint().width(), viewport_->height()); | |
865 | update_scroll(); | |
866 | } | |
867 | ||
868 | TraceTreeItemOwner* View::find_prevalent_trace_group( | |
869 | const shared_ptr<sigrok::ChannelGroup> &group, | |
870 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
871 | &signal_map) | |
872 | { | |
873 | assert(group); | |
874 | ||
875 | unordered_set<TraceTreeItemOwner*> owners; | |
876 | vector<TraceTreeItemOwner*> owner_list; | |
877 | ||
878 | // Make a set and a list of all the owners | |
879 | for (const auto &channel : group->channels()) { | |
880 | for (auto entry : signal_map) { | |
881 | if (entry.first->channel() == channel) { | |
882 | TraceTreeItemOwner *const o = (entry.second)->owner(); | |
883 | owner_list.push_back(o); | |
884 | owners.insert(o); | |
885 | } | |
886 | } | |
887 | } | |
888 | ||
889 | // Iterate through the list of owners, and find the most prevalent | |
890 | size_t max_prevalence = 0; | |
891 | TraceTreeItemOwner *prevalent_owner = nullptr; | |
892 | for (TraceTreeItemOwner *owner : owners) { | |
893 | const size_t prevalence = std::count_if( | |
894 | owner_list.begin(), owner_list.end(), | |
895 | [&](TraceTreeItemOwner *o) { return o == owner; }); | |
896 | if (prevalence > max_prevalence) { | |
897 | max_prevalence = prevalence; | |
898 | prevalent_owner = owner; | |
899 | } | |
900 | } | |
901 | ||
902 | return prevalent_owner; | |
903 | } | |
904 | ||
905 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( | |
906 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
907 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
908 | &signal_map, | |
909 | set< shared_ptr<Trace> > &add_list) | |
910 | { | |
911 | vector< shared_ptr<Trace> > filtered_traces; | |
912 | ||
913 | for (const auto &channel : channels) { | |
914 | for (auto entry : signal_map) { | |
915 | if (entry.first->channel() == channel) { | |
916 | shared_ptr<Trace> trace = entry.second; | |
917 | const auto list_iter = add_list.find(trace); | |
918 | if (list_iter == add_list.end()) | |
919 | continue; | |
920 | ||
921 | filtered_traces.push_back(trace); | |
922 | add_list.erase(list_iter); | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
927 | return filtered_traces; | |
928 | } | |
929 | ||
930 | void View::determine_time_unit() | |
931 | { | |
932 | // Check whether we know the sample rate and hence can use time as the unit | |
933 | if (time_unit_ == util::TimeUnit::Samples) { | |
934 | // Check all signals but... | |
935 | for (const shared_ptr<Signal> signal : signals_) { | |
936 | const shared_ptr<SignalData> data = signal->data(); | |
937 | ||
938 | // ...only check first segment of each | |
939 | const vector< shared_ptr<Segment> > segments = data->segments(); | |
940 | if (!segments.empty()) | |
941 | if (segments[0]->samplerate()) { | |
942 | set_time_unit(util::TimeUnit::Time); | |
943 | break; | |
944 | } | |
945 | } | |
946 | } | |
947 | } | |
948 | ||
949 | bool View::eventFilter(QObject *object, QEvent *event) | |
950 | { | |
951 | const QEvent::Type type = event->type(); | |
952 | if (type == QEvent::MouseMove) { | |
953 | ||
954 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
955 | if (object == viewport_) | |
956 | hover_point_ = mouse_event->pos(); | |
957 | else if (object == ruler_) | |
958 | hover_point_ = QPoint(mouse_event->x(), 0); | |
959 | else if (object == header_) | |
960 | hover_point_ = QPoint(0, mouse_event->y()); | |
961 | else | |
962 | hover_point_ = QPoint(-1, -1); | |
963 | ||
964 | hover_point_changed(); | |
965 | ||
966 | } else if (type == QEvent::Leave) { | |
967 | hover_point_ = QPoint(-1, -1); | |
968 | hover_point_changed(); | |
969 | } | |
970 | ||
971 | return QObject::eventFilter(object, event); | |
972 | } | |
973 | ||
974 | void View::resizeEvent(QResizeEvent*) | |
975 | { | |
976 | update_layout(); | |
977 | ||
978 | // This is somewhat of a hack, unfortunately. We cannot use | |
979 | // set_v_offset() from within restore_settings() as the view | |
980 | // at that point is neither visible nor properly sized. | |
981 | // This is the least intrusive workaround I could come up | |
982 | // with: set the vertical offset (or scroll defaults) when | |
983 | // the view is visible and resized to its final size. | |
984 | // Resize events that are sent when the view is not visible | |
985 | // must be ignored as they have wrong sizes, potentially | |
986 | // preventing the v offset from being set successfully. | |
987 | ||
988 | if (isVisible()) | |
989 | size_finalized_ = true; | |
990 | ||
991 | if (size_finalized_ && saved_v_offset_) { | |
992 | set_v_offset(saved_v_offset_); | |
993 | saved_v_offset_ = 0; | |
994 | } | |
995 | ||
996 | if (size_finalized_ && scroll_needs_defaults_) | |
997 | set_scroll_default(); | |
998 | } | |
999 | ||
1000 | void View::row_item_appearance_changed(bool label, bool content) | |
1001 | { | |
1002 | if (label) | |
1003 | header_->update(); | |
1004 | if (content) | |
1005 | viewport_->update(); | |
1006 | } | |
1007 | ||
1008 | void View::time_item_appearance_changed(bool label, bool content) | |
1009 | { | |
1010 | if (label) | |
1011 | ruler_->update(); | |
1012 | if (content) | |
1013 | viewport_->update(); | |
1014 | } | |
1015 | ||
1016 | void View::extents_changed(bool horz, bool vert) | |
1017 | { | |
1018 | sticky_events_ |= | |
1019 | (horz ? TraceTreeItemHExtentsChanged : 0) | | |
1020 | (vert ? TraceTreeItemVExtentsChanged : 0); | |
1021 | lazy_event_handler_.start(); | |
1022 | } | |
1023 | ||
1024 | void View::h_scroll_value_changed(int value) | |
1025 | { | |
1026 | if (updating_scroll_) | |
1027 | return; | |
1028 | ||
1029 | // Disable sticky scrolling when user moves the horizontal scroll bar | |
1030 | // during a running acquisition | |
1031 | if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) { | |
1032 | sticky_scrolling_ = false; | |
1033 | sticky_scrolling_changed(false); | |
1034 | } | |
1035 | ||
1036 | const int range = scrollarea_.horizontalScrollBar()->maximum(); | |
1037 | if (range < MaxScrollValue) | |
1038 | set_offset(scale_ * value); | |
1039 | else { | |
1040 | double length = 0; | |
1041 | Timestamp offset; | |
1042 | get_scroll_layout(length, offset); | |
1043 | set_offset(scale_ * length * value / MaxScrollValue); | |
1044 | } | |
1045 | ||
1046 | ruler_->update(); | |
1047 | viewport_->update(); | |
1048 | } | |
1049 | ||
1050 | void View::v_scroll_value_changed() | |
1051 | { | |
1052 | header_->update(); | |
1053 | viewport_->update(); | |
1054 | } | |
1055 | ||
1056 | void View::signals_changed() | |
1057 | { | |
1058 | using sigrok::Channel; | |
1059 | ||
1060 | vector< shared_ptr<Channel> > channels; | |
1061 | shared_ptr<sigrok::Device> sr_dev; | |
1062 | ||
1063 | // Do we need to set the vertical scrollbar to its default position later? | |
1064 | // We do if there are no traces, i.e. the scroll bar has no range set | |
1065 | bool reset_scrollbar = | |
1066 | (scrollarea_.verticalScrollBar()->minimum() == | |
1067 | scrollarea_.verticalScrollBar()->maximum()); | |
1068 | ||
1069 | if (!session_.device()) { | |
1070 | reset_scroll(); | |
1071 | signals_.clear(); | |
1072 | } else { | |
1073 | sr_dev = session_.device()->device(); | |
1074 | assert(sr_dev); | |
1075 | channels = sr_dev->channels(); | |
1076 | } | |
1077 | ||
1078 | vector< shared_ptr<TraceTreeItem> > new_top_level_items; | |
1079 | ||
1080 | // Make a list of traces that are being added, and a list of traces | |
1081 | // that are being removed | |
1082 | const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>(); | |
1083 | const set<shared_ptr<Trace>> prev_traces( | |
1084 | prev_trace_list.begin(), prev_trace_list.end()); | |
1085 | ||
1086 | set< shared_ptr<Trace> > traces(signals_.begin(), signals_.end()); | |
1087 | ||
1088 | #ifdef ENABLE_DECODE | |
1089 | traces.insert(decode_traces_.begin(), decode_traces_.end()); | |
1090 | #endif | |
1091 | ||
1092 | set< shared_ptr<Trace> > add_traces; | |
1093 | set_difference(traces.begin(), traces.end(), | |
1094 | prev_traces.begin(), prev_traces.end(), | |
1095 | inserter(add_traces, add_traces.begin())); | |
1096 | ||
1097 | set< shared_ptr<Trace> > remove_traces; | |
1098 | set_difference(prev_traces.begin(), prev_traces.end(), | |
1099 | traces.begin(), traces.end(), | |
1100 | inserter(remove_traces, remove_traces.begin())); | |
1101 | ||
1102 | // Make a look-up table of sigrok Channels to pulseview Signals | |
1103 | unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
1104 | signal_map; | |
1105 | for (const shared_ptr<Signal> &sig : signals_) | |
1106 | signal_map[sig->base()] = sig; | |
1107 | ||
1108 | // Populate channel groups | |
1109 | if (sr_dev) | |
1110 | for (auto entry : sr_dev->channel_groups()) { | |
1111 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
1112 | ||
1113 | if (group->channels().size() <= 1) | |
1114 | continue; | |
1115 | ||
1116 | // Find best trace group to add to | |
1117 | TraceTreeItemOwner *owner = find_prevalent_trace_group( | |
1118 | group, signal_map); | |
1119 | ||
1120 | // If there is no trace group, create one | |
1121 | shared_ptr<TraceGroup> new_trace_group; | |
1122 | if (!owner) { | |
1123 | new_trace_group.reset(new TraceGroup()); | |
1124 | owner = new_trace_group.get(); | |
1125 | } | |
1126 | ||
1127 | // Extract traces for the trace group, removing them from | |
1128 | // the add list | |
1129 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
1130 | extract_new_traces_for_channels(group->channels(), | |
1131 | signal_map, add_traces); | |
1132 | ||
1133 | // Add the traces to the group | |
1134 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
1135 | int offset = prev_v_extents.second - prev_v_extents.first; | |
1136 | for (shared_ptr<Trace> trace : new_traces_in_group) { | |
1137 | assert(trace); | |
1138 | owner->add_child_item(trace); | |
1139 | ||
1140 | const pair<int, int> extents = trace->v_extents(); | |
1141 | if (trace->enabled()) | |
1142 | offset += -extents.first; | |
1143 | trace->force_to_v_offset(offset); | |
1144 | if (trace->enabled()) | |
1145 | offset += extents.second; | |
1146 | } | |
1147 | ||
1148 | if (new_trace_group) { | |
1149 | // Assign proper vertical offsets to each channel in the group | |
1150 | new_trace_group->restack_items(); | |
1151 | ||
1152 | // If this is a new group, enqueue it in the new top level | |
1153 | // items list | |
1154 | if (!new_traces_in_group.empty()) | |
1155 | new_top_level_items.push_back(new_trace_group); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | // Enqueue the remaining logic channels in a group | |
1160 | vector< shared_ptr<Channel> > logic_channels; | |
1161 | copy_if(channels.begin(), channels.end(), back_inserter(logic_channels), | |
1162 | [](const shared_ptr<Channel>& c) { | |
1163 | return c->type() == sigrok::ChannelType::LOGIC; }); | |
1164 | ||
1165 | const vector< shared_ptr<Trace> > non_grouped_logic_signals = | |
1166 | extract_new_traces_for_channels(logic_channels, signal_map, add_traces); | |
1167 | ||
1168 | if (non_grouped_logic_signals.size() > 0) { | |
1169 | const shared_ptr<TraceGroup> non_grouped_trace_group( | |
1170 | make_shared<TraceGroup>()); | |
1171 | for (shared_ptr<Trace> trace : non_grouped_logic_signals) | |
1172 | non_grouped_trace_group->add_child_item(trace); | |
1173 | ||
1174 | non_grouped_trace_group->restack_items(); | |
1175 | new_top_level_items.push_back(non_grouped_trace_group); | |
1176 | } | |
1177 | ||
1178 | // Enqueue the remaining channels as free ungrouped traces | |
1179 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
1180 | extract_new_traces_for_channels(channels, signal_map, add_traces); | |
1181 | new_top_level_items.insert(new_top_level_items.end(), | |
1182 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
1183 | ||
1184 | // Enqueue any remaining traces i.e. decode traces | |
1185 | new_top_level_items.insert(new_top_level_items.end(), | |
1186 | add_traces.begin(), add_traces.end()); | |
1187 | ||
1188 | // Remove any removed traces | |
1189 | for (shared_ptr<Trace> trace : remove_traces) { | |
1190 | TraceTreeItemOwner *const owner = trace->owner(); | |
1191 | assert(owner); | |
1192 | owner->remove_child_item(trace); | |
1193 | } | |
1194 | ||
1195 | // Remove any empty trace groups | |
1196 | for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>()) | |
1197 | if (group->child_items().size() == 0) { | |
1198 | remove_child_item(group); | |
1199 | group.reset(); | |
1200 | } | |
1201 | ||
1202 | // Add and position the pending top levels items | |
1203 | int offset = v_extents().second; | |
1204 | for (auto item : new_top_level_items) { | |
1205 | add_child_item(item); | |
1206 | ||
1207 | // Position the item after the last item or at the top if there is none | |
1208 | const pair<int, int> extents = item->v_extents(); | |
1209 | ||
1210 | if (item->enabled()) | |
1211 | offset += -extents.first; | |
1212 | ||
1213 | item->force_to_v_offset(offset); | |
1214 | ||
1215 | if (item->enabled()) | |
1216 | offset += extents.second; | |
1217 | } | |
1218 | ||
1219 | update_layout(); | |
1220 | ||
1221 | header_->update(); | |
1222 | viewport_->update(); | |
1223 | ||
1224 | if (reset_scrollbar) | |
1225 | set_scroll_default(); | |
1226 | } | |
1227 | ||
1228 | void View::capture_state_updated(int state) | |
1229 | { | |
1230 | if (state == Session::Running) { | |
1231 | set_time_unit(util::TimeUnit::Samples); | |
1232 | ||
1233 | trigger_markers_.clear(); | |
1234 | ||
1235 | // Activate "always zoom to fit" if the setting is enabled and we're | |
1236 | // the main view of this session (other trace views may be used for | |
1237 | // zooming and we don't want to mess them up) | |
1238 | GlobalSettings settings; | |
1239 | bool state = settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool(); | |
1240 | if (is_main_view_ && state) { | |
1241 | always_zoom_to_fit_ = true; | |
1242 | always_zoom_to_fit_changed(always_zoom_to_fit_); | |
1243 | } | |
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 |