]>
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 <unordered_set> | |
33 | ||
34 | #include <QApplication> | |
35 | #include <QDebug> | |
36 | #include <QEvent> | |
37 | #include <QFontMetrics> | |
38 | #include <QMenu> | |
39 | #include <QMouseEvent> | |
40 | #include <QScrollBar> | |
41 | #include <QVBoxLayout> | |
42 | ||
43 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
44 | ||
45 | #include "analogsignal.hpp" | |
46 | #include "header.hpp" | |
47 | #include "logicsignal.hpp" | |
48 | #include "ruler.hpp" | |
49 | #include "signal.hpp" | |
50 | #include "tracegroup.hpp" | |
51 | #include "triggermarker.hpp" | |
52 | #include "view.hpp" | |
53 | #include "viewport.hpp" | |
54 | ||
55 | #include "pv/data/logic.hpp" | |
56 | #include "pv/data/logicsegment.hpp" | |
57 | #include "pv/devices/device.hpp" | |
58 | #include "pv/globalsettings.hpp" | |
59 | #include "pv/session.hpp" | |
60 | #include "pv/util.hpp" | |
61 | ||
62 | #ifdef ENABLE_DECODE | |
63 | #include "decodetrace.hpp" | |
64 | #endif | |
65 | ||
66 | using pv::data::SignalData; | |
67 | using pv::data::Segment; | |
68 | using pv::util::TimeUnit; | |
69 | using pv::util::Timestamp; | |
70 | ||
71 | using std::back_inserter; | |
72 | using std::copy_if; | |
73 | using std::count_if; | |
74 | using std::inserter; | |
75 | using std::max; | |
76 | using std::make_pair; | |
77 | using std::make_shared; | |
78 | using std::min; | |
79 | using std::numeric_limits; | |
80 | using std::pair; | |
81 | using std::set; | |
82 | using std::set_difference; | |
83 | using std::shared_ptr; | |
84 | using std::unordered_map; | |
85 | using std::unordered_set; | |
86 | using std::vector; | |
87 | ||
88 | namespace pv { | |
89 | namespace views { | |
90 | namespace trace { | |
91 | ||
92 | const Timestamp View::MaxScale("1e9"); | |
93 | const Timestamp View::MinScale("1e-12"); | |
94 | ||
95 | const int View::MaxScrollValue = INT_MAX / 2; | |
96 | ||
97 | /* Area at the top and bottom of the view that can't be scrolled out of sight */ | |
98 | const int View::ViewScrollMargin = 50; | |
99 | ||
100 | const int View::ScaleUnits[3] = {1, 2, 5}; | |
101 | ||
102 | CustomScrollArea::CustomScrollArea(QWidget *parent) : | |
103 | QAbstractScrollArea(parent) | |
104 | { | |
105 | } | |
106 | ||
107 | bool CustomScrollArea::viewportEvent(QEvent *event) | |
108 | { | |
109 | switch (event->type()) { | |
110 | case QEvent::Paint: | |
111 | case QEvent::MouseButtonPress: | |
112 | case QEvent::MouseButtonRelease: | |
113 | case QEvent::MouseButtonDblClick: | |
114 | case QEvent::MouseMove: | |
115 | case QEvent::Wheel: | |
116 | case QEvent::TouchBegin: | |
117 | case QEvent::TouchUpdate: | |
118 | case QEvent::TouchEnd: | |
119 | return false; | |
120 | default: | |
121 | return QAbstractScrollArea::viewportEvent(event); | |
122 | } | |
123 | } | |
124 | ||
125 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
126 | ViewBase(session, is_main_view, parent), | |
127 | ||
128 | // Note: Place defaults in View::reset_view_state(), not here | |
129 | splitter_(new QSplitter()), | |
130 | header_was_shrunk_(false), // The splitter remains unchanged after a reset, so this goes here | |
131 | sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui() | |
132 | scroll_needs_defaults_(true) | |
133 | { | |
134 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
135 | root_layout->setContentsMargins(0, 0, 0, 0); | |
136 | root_layout->addWidget(splitter_); | |
137 | ||
138 | viewport_ = new Viewport(*this); | |
139 | scrollarea_ = new CustomScrollArea(splitter_); | |
140 | scrollarea_->setViewport(viewport_); | |
141 | scrollarea_->setFrameShape(QFrame::NoFrame); | |
142 | ||
143 | ruler_ = new Ruler(*this); | |
144 | ||
145 | header_ = new Header(*this); | |
146 | header_->setMinimumWidth(10); // So that the arrow tips show at least | |
147 | ||
148 | // We put the header into a simple layout so that we can add the top margin, | |
149 | // allowing us to make it line up with the bottom of the ruler | |
150 | QWidget *header_container = new QWidget(); | |
151 | header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
152 | QVBoxLayout *header_layout = new QVBoxLayout(header_container); | |
153 | header_layout->setContentsMargins(0, ruler_->sizeHint().height(), 0, 0); | |
154 | header_layout->addWidget(header_); | |
155 | ||
156 | // To let the ruler and scrollarea be on the same split pane, we need a layout | |
157 | QWidget *trace_container = new QWidget(); | |
158 | trace_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
159 | QVBoxLayout *trace_layout = new QVBoxLayout(trace_container); | |
160 | trace_layout->setSpacing(0); // We don't want space between the ruler and scrollarea | |
161 | trace_layout->setContentsMargins(0, 0, 0, 0); | |
162 | trace_layout->addWidget(ruler_); | |
163 | trace_layout->addWidget(scrollarea_); | |
164 | ||
165 | splitter_->addWidget(header_container); | |
166 | splitter_->addWidget(trace_container); | |
167 | splitter_->setHandleWidth(1); // Don't show a visible rubber band | |
168 | splitter_->setCollapsible(0, false); // Prevent the header from collapsing | |
169 | splitter_->setCollapsible(1, false); // Prevent the traces from collapsing | |
170 | splitter_->setStretchFactor(0, 0); // Prevent the panes from being resized | |
171 | splitter_->setStretchFactor(1, 1); // when the entire view is resized | |
172 | splitter_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
173 | ||
174 | viewport_->installEventFilter(this); | |
175 | ruler_->installEventFilter(this); | |
176 | header_->installEventFilter(this); | |
177 | ||
178 | // Set up settings and event handlers | |
179 | GlobalSettings settings; | |
180 | colored_bg_ = settings.value(GlobalSettings::Key_View_ColoredBG).toBool(); | |
181 | snap_distance_ = settings.value(GlobalSettings::Key_View_SnapDistance).toInt(); | |
182 | ||
183 | GlobalSettings::add_change_handler(this); | |
184 | ||
185 | connect(scrollarea_->horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
186 | this, SLOT(h_scroll_value_changed(int))); | |
187 | connect(scrollarea_->verticalScrollBar(), SIGNAL(valueChanged(int)), | |
188 | this, SLOT(v_scroll_value_changed())); | |
189 | ||
190 | connect(header_, SIGNAL(selection_changed()), | |
191 | ruler_, SLOT(clear_selection())); | |
192 | connect(ruler_, SIGNAL(selection_changed()), | |
193 | header_, SLOT(clear_selection())); | |
194 | ||
195 | connect(header_, SIGNAL(selection_changed()), | |
196 | this, SIGNAL(selection_changed())); | |
197 | connect(ruler_, SIGNAL(selection_changed()), | |
198 | this, SIGNAL(selection_changed())); | |
199 | ||
200 | connect(splitter_, SIGNAL(splitterMoved(int, int)), | |
201 | this, SLOT(on_splitter_moved())); | |
202 | ||
203 | connect(&lazy_event_handler_, SIGNAL(timeout()), | |
204 | this, SLOT(process_sticky_events())); | |
205 | lazy_event_handler_.setSingleShot(true); | |
206 | lazy_event_handler_.setInterval(1000 / ViewBase::MaxViewAutoUpdateRate); | |
207 | ||
208 | // Set up local keyboard shortcuts | |
209 | zoom_in_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Plus), this, | |
210 | SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
211 | zoom_in_shortcut_->setAutoRepeat(false); | |
212 | ||
213 | zoom_out_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Minus), this, | |
214 | SLOT(on_zoom_out_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
215 | zoom_out_shortcut_->setAutoRepeat(false); | |
216 | ||
217 | zoom_in_shortcut_2_ = new QShortcut(QKeySequence(Qt::Key_Up), this, | |
218 | SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
219 | zoom_out_shortcut_2_ = new QShortcut(QKeySequence(Qt::Key_Down), this, | |
220 | SLOT(on_zoom_out_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
221 | ||
222 | home_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Home), this, | |
223 | SLOT(on_scroll_to_start_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
224 | home_shortcut_->setAutoRepeat(false); | |
225 | ||
226 | end_shortcut_ = new QShortcut(QKeySequence(Qt::Key_End), this, | |
227 | SLOT(on_scroll_to_end_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut); | |
228 | end_shortcut_->setAutoRepeat(false); | |
229 | ||
230 | grab_ruler_left_shortcut_ = new QShortcut(QKeySequence(Qt::Key_1), this, | |
231 | nullptr, nullptr, Qt::WidgetWithChildrenShortcut); | |
232 | connect(grab_ruler_left_shortcut_, &QShortcut::activated, | |
233 | this, [=]{on_grab_ruler(1);}); | |
234 | grab_ruler_left_shortcut_->setAutoRepeat(false); | |
235 | ||
236 | grab_ruler_right_shortcut_ = new QShortcut(QKeySequence(Qt::Key_2), this, | |
237 | nullptr, nullptr, Qt::WidgetWithChildrenShortcut); | |
238 | connect(grab_ruler_right_shortcut_, &QShortcut::activated, | |
239 | this, [=]{on_grab_ruler(2);}); | |
240 | grab_ruler_right_shortcut_->setAutoRepeat(false); | |
241 | ||
242 | cancel_grab_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Escape), this, | |
243 | nullptr, nullptr, Qt::WidgetWithChildrenShortcut); | |
244 | connect(cancel_grab_shortcut_, &QShortcut::activated, | |
245 | this, [=]{grabbed_widget_ = nullptr;}); | |
246 | cancel_grab_shortcut_->setAutoRepeat(false); | |
247 | ||
248 | // Trigger the initial event manually. The default device has signals | |
249 | // which were created before this object came into being | |
250 | signals_changed(); | |
251 | ||
252 | // Make sure the transparent widgets are on the top | |
253 | ruler_->raise(); | |
254 | header_->raise(); | |
255 | ||
256 | reset_view_state(); | |
257 | } | |
258 | ||
259 | View::~View() | |
260 | { | |
261 | GlobalSettings::remove_change_handler(this); | |
262 | } | |
263 | ||
264 | ViewType View::get_type() const | |
265 | { | |
266 | return ViewTypeTrace; | |
267 | } | |
268 | ||
269 | void View::reset_view_state() | |
270 | { | |
271 | ViewBase::reset_view_state(); | |
272 | ||
273 | segment_display_mode_ = Trace::ShowLastSegmentOnly; | |
274 | segment_selectable_ = false; | |
275 | scale_ = 1e-3; | |
276 | offset_ = 0; | |
277 | ruler_offset_ = 0; | |
278 | zero_offset_ = 0; | |
279 | custom_zero_offset_set_ = false; | |
280 | updating_scroll_ = false; | |
281 | settings_restored_ = false; | |
282 | always_zoom_to_fit_ = false; | |
283 | tick_period_ = 0; | |
284 | tick_prefix_ = pv::util::SIPrefix::yocto; | |
285 | tick_precision_ = 0; | |
286 | time_unit_ = util::TimeUnit::Time; | |
287 | show_cursors_ = false; | |
288 | cursors_ = make_shared<CursorPair>(*this); | |
289 | next_flag_text_ = 'A'; | |
290 | trigger_markers_.clear(); | |
291 | hover_widget_ = nullptr; | |
292 | grabbed_widget_ = nullptr; | |
293 | hover_point_ = QPoint(-1, -1); | |
294 | scroll_needs_defaults_ = true; | |
295 | saved_v_offset_ = 0; | |
296 | scale_at_acq_start_ = 0; | |
297 | offset_at_acq_start_ = 0; | |
298 | suppress_zoom_to_fit_after_acq_ = false; | |
299 | ||
300 | show_cursors_ = false; | |
301 | cursor_state_changed(show_cursors_); | |
302 | flags_.clear(); | |
303 | ||
304 | // Update the zoom state | |
305 | calculate_tick_spacing(); | |
306 | ||
307 | // Make sure the standard bar's segment selector is in sync | |
308 | set_segment_display_mode(segment_display_mode_); | |
309 | } | |
310 | ||
311 | Session& View::session() | |
312 | { | |
313 | return session_; | |
314 | } | |
315 | ||
316 | const Session& View::session() const | |
317 | { | |
318 | return session_; | |
319 | } | |
320 | ||
321 | unordered_set< shared_ptr<Signal> > View::signals() const | |
322 | { | |
323 | return signals_; | |
324 | } | |
325 | ||
326 | shared_ptr<Signal> View::get_signal_by_signalbase(shared_ptr<data::SignalBase> base) const | |
327 | { | |
328 | shared_ptr<Signal> ret_val; | |
329 | ||
330 | for (const shared_ptr<Signal>& s : signals_) | |
331 | if (s->base() == base) { | |
332 | ret_val = s; | |
333 | break; | |
334 | } | |
335 | ||
336 | return ret_val; | |
337 | } | |
338 | ||
339 | void View::clear_signals() | |
340 | { | |
341 | ViewBase::clear_signals(); | |
342 | signals_.clear(); | |
343 | } | |
344 | ||
345 | void View::add_signal(const shared_ptr<Signal> signal) | |
346 | { | |
347 | ViewBase::add_signalbase(signal->base()); | |
348 | signals_.insert(signal); | |
349 | ||
350 | signal->set_segment_display_mode(segment_display_mode_); | |
351 | signal->set_current_segment(current_segment_); | |
352 | ||
353 | connect(signal->base().get(), SIGNAL(name_changed(const QString&)), | |
354 | this, SLOT(on_signal_name_changed())); | |
355 | } | |
356 | ||
357 | #ifdef ENABLE_DECODE | |
358 | void View::clear_decode_signals() | |
359 | { | |
360 | ViewBase::clear_decode_signals(); | |
361 | decode_traces_.clear(); | |
362 | } | |
363 | ||
364 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
365 | { | |
366 | ViewBase::add_decode_signal(signal); | |
367 | ||
368 | shared_ptr<DecodeTrace> d( | |
369 | new DecodeTrace(session_, signal, decode_traces_.size())); | |
370 | decode_traces_.push_back(d); | |
371 | ||
372 | d->set_segment_display_mode(segment_display_mode_); | |
373 | d->set_current_segment(current_segment_); | |
374 | ||
375 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
376 | this, SLOT(on_signal_name_changed())); | |
377 | } | |
378 | ||
379 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
380 | { | |
381 | for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++) | |
382 | if ((*i)->base() == signal) { | |
383 | decode_traces_.erase(i); | |
384 | signals_changed(); | |
385 | return; | |
386 | } | |
387 | ||
388 | ViewBase::remove_decode_signal(signal); | |
389 | } | |
390 | #endif | |
391 | ||
392 | shared_ptr<Signal> View::get_signal_under_mouse_cursor() const | |
393 | { | |
394 | return signal_under_mouse_cursor_; | |
395 | } | |
396 | ||
397 | View* View::view() | |
398 | { | |
399 | return this; | |
400 | } | |
401 | ||
402 | const View* View::view() const | |
403 | { | |
404 | return this; | |
405 | } | |
406 | ||
407 | Viewport* View::viewport() | |
408 | { | |
409 | return viewport_; | |
410 | } | |
411 | ||
412 | const Viewport* View::viewport() const | |
413 | { | |
414 | return viewport_; | |
415 | } | |
416 | ||
417 | QAbstractScrollArea* View::scrollarea() const | |
418 | { | |
419 | return scrollarea_; | |
420 | } | |
421 | ||
422 | const Ruler* View::ruler() const | |
423 | { | |
424 | return ruler_; | |
425 | } | |
426 | ||
427 | void View::save_settings(QSettings &settings) const | |
428 | { | |
429 | settings.setValue("scale", scale_); | |
430 | settings.setValue("v_offset", | |
431 | scrollarea_->verticalScrollBar()->sliderPosition()); | |
432 | ||
433 | settings.setValue("splitter_state", splitter_->saveState()); | |
434 | settings.setValue("segment_display_mode", segment_display_mode_); | |
435 | ||
436 | GlobalSettings::store_timestamp(settings, "offset", offset_); | |
437 | ||
438 | if (custom_zero_offset_set_) | |
439 | GlobalSettings::store_timestamp(settings, "zero_offset", -zero_offset_); | |
440 | else | |
441 | settings.remove("zero_offset"); | |
442 | ||
443 | for (const shared_ptr<Signal>& signal : signals_) { | |
444 | settings.beginGroup(signal->base()->internal_name()); | |
445 | signal->save_settings(settings); | |
446 | settings.endGroup(); | |
447 | } | |
448 | } | |
449 | ||
450 | void View::restore_settings(QSettings &settings) | |
451 | { | |
452 | // Note: It is assumed that this function is only called once, | |
453 | // immediately after restoring a previous session. | |
454 | ||
455 | if (settings.contains("scale")) | |
456 | set_scale(settings.value("scale").toDouble()); | |
457 | ||
458 | if (settings.contains("offset")) { | |
459 | // This also updates ruler_offset_ | |
460 | set_offset(GlobalSettings::restore_timestamp(settings, "offset")); | |
461 | } | |
462 | ||
463 | if (settings.contains("zero_offset")) | |
464 | set_zero_position(GlobalSettings::restore_timestamp(settings, "zero_offset")); | |
465 | ||
466 | if (settings.contains("splitter_state")) | |
467 | splitter_->restoreState(settings.value("splitter_state").toByteArray()); | |
468 | ||
469 | if (settings.contains("segment_display_mode")) | |
470 | set_segment_display_mode( | |
471 | (Trace::SegmentDisplayMode)(settings.value("segment_display_mode").toInt())); | |
472 | ||
473 | for (shared_ptr<Signal> signal : signals_) { | |
474 | settings.beginGroup(signal->base()->internal_name()); | |
475 | signal->restore_settings(settings); | |
476 | settings.endGroup(); | |
477 | } | |
478 | ||
479 | if (settings.contains("v_offset")) { | |
480 | saved_v_offset_ = settings.value("v_offset").toInt(); | |
481 | set_v_offset(saved_v_offset_); | |
482 | scroll_needs_defaults_ = false; | |
483 | // Note: see eventFilter() for additional information | |
484 | } | |
485 | ||
486 | settings_restored_ = true; | |
487 | suppress_zoom_to_fit_after_acq_ = true; | |
488 | ||
489 | // Update the ruler so that it uses the new scale | |
490 | calculate_tick_spacing(); | |
491 | } | |
492 | ||
493 | vector< shared_ptr<TimeItem> > View::time_items() const | |
494 | { | |
495 | const vector<shared_ptr<Flag>> f(flags()); | |
496 | vector<shared_ptr<TimeItem>> items(f.begin(), f.end()); | |
497 | ||
498 | if (cursors_) { | |
499 | items.push_back(cursors_); | |
500 | items.push_back(cursors_->first()); | |
501 | items.push_back(cursors_->second()); | |
502 | } | |
503 | ||
504 | for (auto& trigger_marker : trigger_markers_) | |
505 | items.push_back(trigger_marker); | |
506 | ||
507 | return items; | |
508 | } | |
509 | ||
510 | double View::scale() const | |
511 | { | |
512 | return scale_; | |
513 | } | |
514 | ||
515 | void View::set_scale(double scale) | |
516 | { | |
517 | if (scale_ != scale) { | |
518 | scale_ = scale; | |
519 | scale_changed(); | |
520 | } | |
521 | } | |
522 | ||
523 | void View::set_offset(const pv::util::Timestamp& offset, bool force_update) | |
524 | { | |
525 | if ((offset_ != offset) || force_update) { | |
526 | offset_ = offset; | |
527 | ruler_offset_ = offset_ + zero_offset_; | |
528 | offset_changed(); | |
529 | } | |
530 | } | |
531 | ||
532 | const Timestamp& View::offset() const | |
533 | { | |
534 | return offset_; | |
535 | } | |
536 | ||
537 | const Timestamp& View::ruler_offset() const | |
538 | { | |
539 | return ruler_offset_; | |
540 | } | |
541 | ||
542 | void View::set_zero_position(const pv::util::Timestamp& position) | |
543 | { | |
544 | zero_offset_ = -position; | |
545 | custom_zero_offset_set_ = true; | |
546 | ||
547 | // Force an immediate update of the offsets | |
548 | set_offset(offset_, true); | |
549 | ruler_->update(); | |
550 | } | |
551 | ||
552 | void View::reset_zero_position() | |
553 | { | |
554 | zero_offset_ = 0; | |
555 | ||
556 | // When enabled, the first trigger for this segment is used as the zero position | |
557 | GlobalSettings settings; | |
558 | bool trigger_is_zero_time = settings.value(GlobalSettings::Key_View_TriggerIsZeroTime).toBool(); | |
559 | ||
560 | if (trigger_is_zero_time) { | |
561 | vector<util::Timestamp> triggers = session_.get_triggers(current_segment_); | |
562 | ||
563 | if (triggers.size() > 0) | |
564 | zero_offset_ = triggers.front(); | |
565 | } | |
566 | ||
567 | custom_zero_offset_set_ = false; | |
568 | ||
569 | // Force an immediate update of the offsets | |
570 | set_offset(offset_, true); | |
571 | ruler_->update(); | |
572 | } | |
573 | ||
574 | pv::util::Timestamp View::zero_offset() const | |
575 | { | |
576 | return zero_offset_; | |
577 | } | |
578 | ||
579 | int View::owner_visual_v_offset() const | |
580 | { | |
581 | return -scrollarea_->verticalScrollBar()->sliderPosition(); | |
582 | } | |
583 | ||
584 | void View::set_v_offset(int offset) | |
585 | { | |
586 | scrollarea_->verticalScrollBar()->setSliderPosition(offset); | |
587 | header_->update(); | |
588 | viewport_->update(); | |
589 | } | |
590 | ||
591 | void View::set_h_offset(int offset) | |
592 | { | |
593 | scrollarea_->horizontalScrollBar()->setSliderPosition(offset); | |
594 | header_->update(); | |
595 | viewport_->update(); | |
596 | } | |
597 | ||
598 | int View::get_h_scrollbar_maximum() const | |
599 | { | |
600 | return scrollarea_->horizontalScrollBar()->maximum(); | |
601 | } | |
602 | ||
603 | unsigned int View::depth() const | |
604 | { | |
605 | return 0; | |
606 | } | |
607 | ||
608 | uint32_t View::current_segment() const | |
609 | { | |
610 | return current_segment_; | |
611 | } | |
612 | ||
613 | pv::util::SIPrefix View::tick_prefix() const | |
614 | { | |
615 | return tick_prefix_; | |
616 | } | |
617 | ||
618 | void View::set_tick_prefix(pv::util::SIPrefix tick_prefix) | |
619 | { | |
620 | if (tick_prefix_ != tick_prefix) { | |
621 | tick_prefix_ = tick_prefix; | |
622 | tick_prefix_changed(); | |
623 | } | |
624 | } | |
625 | ||
626 | unsigned int View::tick_precision() const | |
627 | { | |
628 | return tick_precision_; | |
629 | } | |
630 | ||
631 | void View::set_tick_precision(unsigned tick_precision) | |
632 | { | |
633 | if (tick_precision_ != tick_precision) { | |
634 | tick_precision_ = tick_precision; | |
635 | tick_precision_changed(); | |
636 | } | |
637 | } | |
638 | ||
639 | const pv::util::Timestamp& View::tick_period() const | |
640 | { | |
641 | return tick_period_; | |
642 | } | |
643 | ||
644 | unsigned int View::minor_tick_count() const | |
645 | { | |
646 | return minor_tick_count_; | |
647 | } | |
648 | ||
649 | void View::set_tick_period(const pv::util::Timestamp& tick_period) | |
650 | { | |
651 | if (tick_period_ != tick_period) { | |
652 | tick_period_ = tick_period; | |
653 | tick_period_changed(); | |
654 | } | |
655 | } | |
656 | ||
657 | TimeUnit View::time_unit() const | |
658 | { | |
659 | return time_unit_; | |
660 | } | |
661 | ||
662 | void View::set_time_unit(pv::util::TimeUnit time_unit) | |
663 | { | |
664 | if (time_unit_ != time_unit) { | |
665 | time_unit_ = time_unit; | |
666 | time_unit_changed(); | |
667 | } | |
668 | } | |
669 | ||
670 | void View::set_current_segment(uint32_t segment_id) | |
671 | { | |
672 | current_segment_ = segment_id; | |
673 | ||
674 | for (const shared_ptr<Signal>& signal : signals_) | |
675 | signal->set_current_segment(current_segment_); | |
676 | #ifdef ENABLE_DECODE | |
677 | for (shared_ptr<DecodeTrace>& dt : decode_traces_) | |
678 | dt->set_current_segment(current_segment_); | |
679 | #endif | |
680 | ||
681 | vector<util::Timestamp> triggers = session_.get_triggers(current_segment_); | |
682 | ||
683 | trigger_markers_.clear(); | |
684 | for (util::Timestamp timestamp : triggers) | |
685 | trigger_markers_.push_back(make_shared<TriggerMarker>(*this, timestamp)); | |
686 | ||
687 | if (!custom_zero_offset_set_) | |
688 | reset_zero_position(); | |
689 | ||
690 | viewport_->update(); | |
691 | ||
692 | segment_changed(segment_id); | |
693 | } | |
694 | ||
695 | bool View::segment_is_selectable() const | |
696 | { | |
697 | return segment_selectable_; | |
698 | } | |
699 | ||
700 | Trace::SegmentDisplayMode View::segment_display_mode() const | |
701 | { | |
702 | return segment_display_mode_; | |
703 | } | |
704 | ||
705 | void View::set_segment_display_mode(Trace::SegmentDisplayMode mode) | |
706 | { | |
707 | segment_display_mode_ = mode; | |
708 | ||
709 | for (const shared_ptr<Signal>& signal : signals_) | |
710 | signal->set_segment_display_mode(mode); | |
711 | ||
712 | uint32_t last_segment = session_.get_segment_count() - 1; | |
713 | ||
714 | switch (mode) { | |
715 | case Trace::ShowLastSegmentOnly: | |
716 | if (current_segment_ != last_segment) | |
717 | set_current_segment(last_segment); | |
718 | break; | |
719 | ||
720 | case Trace::ShowLastCompleteSegmentOnly: | |
721 | // Do nothing if we only have one segment so far | |
722 | if (last_segment > 0) { | |
723 | // If the last segment isn't complete, the previous one must be | |
724 | uint32_t segment_id = | |
725 | (session_.all_segments_complete(last_segment)) ? | |
726 | last_segment : last_segment - 1; | |
727 | ||
728 | if (current_segment_ != segment_id) | |
729 | set_current_segment(segment_id); | |
730 | } | |
731 | break; | |
732 | ||
733 | case Trace::ShowSingleSegmentOnly: | |
734 | case Trace::ShowAllSegments: | |
735 | case Trace::ShowAccumulatedIntensity: | |
736 | default: | |
737 | // Current segment remains as-is | |
738 | break; | |
739 | } | |
740 | ||
741 | segment_selectable_ = true; | |
742 | ||
743 | if ((mode == Trace::ShowAllSegments) || (mode == Trace::ShowAccumulatedIntensity)) | |
744 | segment_selectable_ = false; | |
745 | ||
746 | viewport_->update(); | |
747 | ||
748 | segment_display_mode_changed((int)mode, segment_selectable_); | |
749 | } | |
750 | ||
751 | void View::zoom(double steps) | |
752 | { | |
753 | zoom(steps, viewport_->width() / 2); | |
754 | } | |
755 | ||
756 | void View::zoom(double steps, int offset) | |
757 | { | |
758 | set_zoom(scale_ * pow(3.0 / 2.0, -steps), offset); | |
759 | } | |
760 | ||
761 | void View::zoom_fit(bool gui_state) | |
762 | { | |
763 | // Act as one-shot when stopped, toggle along with the GUI otherwise | |
764 | if (session_.get_capture_state() == Session::Stopped) { | |
765 | always_zoom_to_fit_ = false; | |
766 | always_zoom_to_fit_changed(false); | |
767 | } else { | |
768 | always_zoom_to_fit_ = gui_state; | |
769 | always_zoom_to_fit_changed(gui_state); | |
770 | } | |
771 | ||
772 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
773 | const Timestamp delta = extents.second - extents.first; | |
774 | if (delta < Timestamp("1e-12")) | |
775 | return; | |
776 | ||
777 | assert(viewport_); | |
778 | const int w = viewport_->width(); | |
779 | if (w <= 0) | |
780 | return; | |
781 | ||
782 | const Timestamp scale = max(min(delta / w, MaxScale), MinScale); | |
783 | set_scale_offset(scale.convert_to<double>(), extents.first); | |
784 | } | |
785 | ||
786 | void View::set_scale_offset(double scale, const Timestamp& offset) | |
787 | { | |
788 | // Disable sticky scrolling / always zoom to fit when acquisition runs | |
789 | // and user drags the viewport | |
790 | if ((scale_ == scale) && (offset_ != offset) && | |
791 | (session_.get_capture_state() == Session::Running)) { | |
792 | ||
793 | if (sticky_scrolling_) { | |
794 | sticky_scrolling_ = false; | |
795 | sticky_scrolling_changed(false); | |
796 | } | |
797 | ||
798 | if (always_zoom_to_fit_) { | |
799 | always_zoom_to_fit_ = false; | |
800 | always_zoom_to_fit_changed(false); | |
801 | } | |
802 | } | |
803 | ||
804 | set_scale(scale); | |
805 | set_offset(offset); | |
806 | ||
807 | calculate_tick_spacing(); | |
808 | ||
809 | update_scroll(); | |
810 | ruler_->update(); | |
811 | viewport_->update(); | |
812 | } | |
813 | ||
814 | set< shared_ptr<SignalData> > View::get_visible_data() const | |
815 | { | |
816 | // Make a set of all the visible data objects | |
817 | set< shared_ptr<SignalData> > visible_data; | |
818 | for (const shared_ptr<Signal>& sig : signals_) | |
819 | if (sig->enabled()) | |
820 | visible_data.insert(sig->data()); | |
821 | ||
822 | return visible_data; | |
823 | } | |
824 | ||
825 | pair<Timestamp, Timestamp> View::get_time_extents() const | |
826 | { | |
827 | boost::optional<Timestamp> left_time, right_time; | |
828 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
829 | for (const shared_ptr<SignalData>& d : visible_data) { | |
830 | const vector< shared_ptr<Segment> > segments = d->segments(); | |
831 | for (const shared_ptr<Segment>& s : segments) { | |
832 | double samplerate = s->samplerate(); | |
833 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
834 | ||
835 | const Timestamp start_time = s->start_time(); | |
836 | left_time = left_time ? | |
837 | min(*left_time, start_time) : | |
838 | start_time; | |
839 | right_time = right_time ? | |
840 | max(*right_time, start_time + d->max_sample_count() / samplerate) : | |
841 | start_time + d->max_sample_count() / samplerate; | |
842 | } | |
843 | } | |
844 | ||
845 | if (!left_time || !right_time) | |
846 | return make_pair(0, 0); | |
847 | ||
848 | assert(*left_time < *right_time); | |
849 | return make_pair(*left_time, *right_time); | |
850 | } | |
851 | ||
852 | bool View::colored_bg() const | |
853 | { | |
854 | return colored_bg_; | |
855 | } | |
856 | ||
857 | bool View::cursors_shown() const | |
858 | { | |
859 | return show_cursors_; | |
860 | } | |
861 | ||
862 | void View::show_cursors(bool show) | |
863 | { | |
864 | if (show_cursors_ != show) { | |
865 | show_cursors_ = show; | |
866 | ||
867 | cursor_state_changed(show); | |
868 | ruler_->update(); | |
869 | viewport_->update(); | |
870 | } | |
871 | } | |
872 | ||
873 | void View::set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second) | |
874 | { | |
875 | assert(cursors_); | |
876 | ||
877 | cursors_->first()->set_time(first); | |
878 | cursors_->second()->set_time(second); | |
879 | ||
880 | ruler_->update(); | |
881 | viewport_->update(); | |
882 | } | |
883 | ||
884 | void View::center_cursors() | |
885 | { | |
886 | assert(cursors_); | |
887 | ||
888 | const double time_width = scale_ * viewport_->width(); | |
889 | cursors_->first()->set_time(offset_ + time_width * 0.4); | |
890 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
891 | ||
892 | ruler_->update(); | |
893 | viewport_->update(); | |
894 | } | |
895 | ||
896 | shared_ptr<CursorPair> View::cursors() const | |
897 | { | |
898 | return cursors_; | |
899 | } | |
900 | ||
901 | shared_ptr<Flag> View::add_flag(const Timestamp& time) | |
902 | { | |
903 | shared_ptr<Flag> flag = | |
904 | make_shared<Flag>(*this, time, QString("%1").arg(next_flag_text_)); | |
905 | flags_.push_back(flag); | |
906 | ||
907 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
908 | (next_flag_text_ + 1); | |
909 | ||
910 | time_item_appearance_changed(true, true); | |
911 | return flag; | |
912 | } | |
913 | ||
914 | void View::remove_flag(shared_ptr<Flag> flag) | |
915 | { | |
916 | flags_.remove(flag); | |
917 | time_item_appearance_changed(true, true); | |
918 | } | |
919 | ||
920 | vector< shared_ptr<Flag> > View::flags() const | |
921 | { | |
922 | vector< shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
923 | stable_sort(flags.begin(), flags.end(), | |
924 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
925 | return a->time() < b->time(); | |
926 | }); | |
927 | ||
928 | return flags; | |
929 | } | |
930 | ||
931 | const QPoint& View::hover_point() const | |
932 | { | |
933 | return hover_point_; | |
934 | } | |
935 | ||
936 | const QWidget* View::hover_widget() const | |
937 | { | |
938 | return hover_widget_; | |
939 | } | |
940 | ||
941 | int64_t View::get_nearest_level_change(const QPoint &p) | |
942 | { | |
943 | // Is snapping disabled? | |
944 | if (snap_distance_ == 0) | |
945 | return -1; | |
946 | ||
947 | struct entry_t { | |
948 | entry_t(shared_ptr<Signal> s) : | |
949 | signal(s), delta(numeric_limits<int64_t>::max()), sample(-1), is_dense(false) {} | |
950 | shared_ptr<Signal> signal; | |
951 | int64_t delta; | |
952 | int64_t sample; | |
953 | bool is_dense; | |
954 | }; | |
955 | ||
956 | vector<entry_t> list; | |
957 | ||
958 | // Create list of signals to consider | |
959 | if (signal_under_mouse_cursor_) | |
960 | list.emplace_back(signal_under_mouse_cursor_); | |
961 | else | |
962 | for (shared_ptr<Signal> s : signals_) { | |
963 | if (!s->enabled()) | |
964 | continue; | |
965 | ||
966 | list.emplace_back(s); | |
967 | } | |
968 | ||
969 | // Get data for listed signals | |
970 | for (entry_t &e : list) { | |
971 | // Calculate sample number from cursor position | |
972 | const double samples_per_pixel = e.signal->base()->get_samplerate() * scale(); | |
973 | const int64_t x_offset = offset().convert_to<double>() / scale(); | |
974 | const int64_t sample_num = max(((x_offset + p.x()) * samples_per_pixel), 0.0); | |
975 | ||
976 | vector<data::LogicSegment::EdgePair> edges = | |
977 | e.signal->get_nearest_level_changes(sample_num); | |
978 | ||
979 | if (edges.empty()) | |
980 | continue; | |
981 | ||
982 | // Check first edge | |
983 | const int64_t first_sample_delta = abs(sample_num - edges.front().first); | |
984 | const int64_t first_delta = first_sample_delta / samples_per_pixel; | |
985 | e.delta = first_delta; | |
986 | e.sample = edges.front().first; | |
987 | ||
988 | // Check second edge if available | |
989 | if (edges.size() == 2) { | |
990 | // Note: -1 because this is usually the right edge and sample points are left-aligned | |
991 | const int64_t second_sample_delta = abs(sample_num - edges.back().first - 1); | |
992 | const int64_t second_delta = second_sample_delta / samples_per_pixel; | |
993 | ||
994 | // If both edges are too close, we mark this signal as being dense | |
995 | if ((first_delta + second_delta) <= snap_distance_) | |
996 | e.is_dense = true; | |
997 | ||
998 | if (second_delta < first_delta) { | |
999 | e.delta = second_delta; | |
1000 | e.sample = edges.back().first; | |
1001 | } | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | // Look for the best match: non-dense first, then dense | |
1006 | entry_t *match = nullptr; | |
1007 | ||
1008 | for (entry_t &e : list) { | |
1009 | if (e.delta > snap_distance_ || e.is_dense) | |
1010 | continue; | |
1011 | ||
1012 | if (match) { | |
1013 | if (e.delta < match->delta) | |
1014 | match = &e; | |
1015 | } else | |
1016 | match = &e; | |
1017 | } | |
1018 | ||
1019 | if (!match) { | |
1020 | for (entry_t &e : list) { | |
1021 | if (!e.is_dense) | |
1022 | continue; | |
1023 | ||
1024 | if (match) { | |
1025 | if (e.delta < match->delta) | |
1026 | match = &e; | |
1027 | } else | |
1028 | match = &e; | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | if (match) { | |
1033 | // Somewhat ugly hack to make TimeItem::drag_by() work | |
1034 | signal_under_mouse_cursor_ = match->signal; | |
1035 | ||
1036 | return match->sample; | |
1037 | } | |
1038 | ||
1039 | return -1; | |
1040 | } | |
1041 | ||
1042 | void View::restack_all_trace_tree_items() | |
1043 | { | |
1044 | // Make a list of owners that is sorted from deepest first | |
1045 | const vector<shared_ptr<TraceTreeItem>> items( | |
1046 | list_by_type<TraceTreeItem>()); | |
1047 | set< TraceTreeItemOwner* > owners; | |
1048 | for (const auto &r : items) | |
1049 | owners.insert(r->owner()); | |
1050 | vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
1051 | sort(sorted_owners.begin(), sorted_owners.end(), | |
1052 | [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) { | |
1053 | return a->depth() > b->depth(); }); | |
1054 | ||
1055 | // Restack the items recursively | |
1056 | for (auto &o : sorted_owners) | |
1057 | o->restack_items(); | |
1058 | ||
1059 | // Animate the items to their destination | |
1060 | for (const auto &i : items) | |
1061 | i->animate_to_layout_v_offset(); | |
1062 | } | |
1063 | ||
1064 | int View::header_width() const | |
1065 | { | |
1066 | return header_->extended_size_hint().width(); | |
1067 | } | |
1068 | ||
1069 | void View::on_setting_changed(const QString &key, const QVariant &value) | |
1070 | { | |
1071 | GlobalSettings settings; | |
1072 | ||
1073 | if (key == GlobalSettings::Key_View_ColoredBG) { | |
1074 | colored_bg_ = settings.value(GlobalSettings::Key_View_ColoredBG).toBool(); | |
1075 | viewport_->update(); | |
1076 | } | |
1077 | ||
1078 | if ((key == GlobalSettings::Key_View_ShowSamplingPoints) || | |
1079 | (key == GlobalSettings::Key_View_ShowAnalogMinorGrid)) | |
1080 | viewport_->update(); | |
1081 | ||
1082 | if (key == GlobalSettings::Key_View_TriggerIsZeroTime) | |
1083 | on_settingViewTriggerIsZeroTime_changed(value); | |
1084 | ||
1085 | if (key == GlobalSettings::Key_View_SnapDistance) | |
1086 | snap_distance_ = settings.value(GlobalSettings::Key_View_SnapDistance).toInt(); | |
1087 | } | |
1088 | ||
1089 | void View::trigger_event(int segment_id, util::Timestamp location) | |
1090 | { | |
1091 | // TODO This doesn't work if we're showing multiple segments at once | |
1092 | if ((uint32_t)segment_id != current_segment_) | |
1093 | return; | |
1094 | ||
1095 | if (!custom_zero_offset_set_) | |
1096 | reset_zero_position(); | |
1097 | ||
1098 | trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location)); | |
1099 | } | |
1100 | ||
1101 | void View::get_scroll_layout(double &length, Timestamp &offset) const | |
1102 | { | |
1103 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
1104 | length = ((extents.second - extents.first) / scale_).convert_to<double>(); | |
1105 | offset = offset_ / scale_; | |
1106 | } | |
1107 | ||
1108 | void View::set_zoom(double scale, int offset) | |
1109 | { | |
1110 | // Reset the "always zoom to fit" feature as the user changed the zoom | |
1111 | always_zoom_to_fit_ = false; | |
1112 | always_zoom_to_fit_changed(false); | |
1113 | ||
1114 | const Timestamp cursor_offset = offset_ + scale_ * offset; | |
1115 | const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale); | |
1116 | const Timestamp new_offset = cursor_offset - new_scale * offset; | |
1117 | set_scale_offset(new_scale.convert_to<double>(), new_offset); | |
1118 | } | |
1119 | ||
1120 | void View::calculate_tick_spacing() | |
1121 | { | |
1122 | const double SpacingIncrement = 10.0f; | |
1123 | const double MinValueSpacing = 40.0f; | |
1124 | ||
1125 | // Figure out the highest numeric value visible on a label | |
1126 | const QSize areaSize = viewport_->size(); | |
1127 | const Timestamp max_time = max(fabs(offset_), | |
1128 | fabs(offset_ + scale_ * areaSize.width())); | |
1129 | ||
1130 | double min_width = SpacingIncrement; | |
1131 | double label_width, tick_period_width; | |
1132 | ||
1133 | QFontMetrics m(QApplication::font()); | |
1134 | ||
1135 | // Copies of the member variables with the same name, used in the calculation | |
1136 | // and written back afterwards, so that we don't emit signals all the time | |
1137 | // during the calculation. | |
1138 | pv::util::Timestamp tick_period = tick_period_; | |
1139 | pv::util::SIPrefix tick_prefix = tick_prefix_; | |
1140 | unsigned tick_precision = tick_precision_; | |
1141 | ||
1142 | do { | |
1143 | const double min_period = scale_ * min_width; | |
1144 | ||
1145 | const int order = (int)floorf(log10f(min_period)); | |
1146 | const pv::util::Timestamp order_decimal = | |
1147 | pow(pv::util::Timestamp(10), order); | |
1148 | ||
1149 | // Allow for a margin of error so that a scale unit of 1 can be used. | |
1150 | // Otherwise, for a SU of 1 the tick period will almost always be below | |
1151 | // the min_period by a small amount - and thus skipped in favor of 2. | |
1152 | // Note: margin assumes that SU[0] and SU[1] contain the smallest values | |
1153 | double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0; | |
1154 | double tp_with_margin; | |
1155 | unsigned int unit = 0; | |
1156 | ||
1157 | do { | |
1158 | tp_with_margin = order_decimal.convert_to<double>() * | |
1159 | (ScaleUnits[unit++] + tp_margin); | |
1160 | } while (tp_with_margin < min_period && unit < countof(ScaleUnits)); | |
1161 | ||
1162 | minor_tick_count_ = (unit == 2) ? 4 : 5; | |
1163 | tick_period = order_decimal * ScaleUnits[unit - 1]; | |
1164 | tick_prefix = static_cast<pv::util::SIPrefix>( | |
1165 | (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3); | |
1166 | ||
1167 | // Precision is the number of fractional digits required, not | |
1168 | // taking the prefix into account (and it must never be negative) | |
1169 | tick_precision = max(ceil(log10(1 / tick_period)).convert_to<int>(), 0); | |
1170 | ||
1171 | tick_period_width = (tick_period / scale_).convert_to<double>(); | |
1172 | ||
1173 | const QString label_text = Ruler::format_time_with_distance( | |
1174 | tick_period, max_time, tick_prefix, time_unit_, tick_precision); | |
1175 | ||
1176 | label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
1177 | Qt::AlignLeft | Qt::AlignTop, label_text).width() + | |
1178 | MinValueSpacing; | |
1179 | ||
1180 | min_width += SpacingIncrement; | |
1181 | } while (tick_period_width < label_width); | |
1182 | ||
1183 | set_tick_period(tick_period); | |
1184 | set_tick_prefix(tick_prefix); | |
1185 | set_tick_precision(tick_precision); | |
1186 | } | |
1187 | ||
1188 | void View::adjust_top_margin() | |
1189 | { | |
1190 | assert(viewport_); | |
1191 | ||
1192 | const QSize areaSize = viewport_->size(); | |
1193 | ||
1194 | const pair<int, int> extents = v_extents(); | |
1195 | const int top_margin = owner_visual_v_offset() + extents.first; | |
1196 | const int trace_bottom = owner_visual_v_offset() + extents.first + extents.second; | |
1197 | ||
1198 | // Do we have empty space at the top while the last trace goes out of screen? | |
1199 | if ((top_margin > 0) && (trace_bottom > areaSize.height())) { | |
1200 | const int trace_height = extents.second - extents.first; | |
1201 | ||
1202 | // Center everything vertically if there is enough space | |
1203 | if (areaSize.height() >= trace_height) | |
1204 | set_v_offset(extents.first - | |
1205 | ((areaSize.height() - trace_height) / 2)); | |
1206 | else | |
1207 | // Remove the top margin to make as many traces fit on screen as possible | |
1208 | set_v_offset(extents.first); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | void View::update_scroll() | |
1213 | { | |
1214 | assert(viewport_); | |
1215 | QScrollBar *hscrollbar = scrollarea_->horizontalScrollBar(); | |
1216 | QScrollBar *vscrollbar = scrollarea_->verticalScrollBar(); | |
1217 | ||
1218 | const QSize areaSize = viewport_->size(); | |
1219 | ||
1220 | // Set the horizontal scroll bar | |
1221 | double length = 0; | |
1222 | Timestamp offset; | |
1223 | get_scroll_layout(length, offset); | |
1224 | length = max(length - areaSize.width(), 0.0); | |
1225 | ||
1226 | int major_tick_distance = (tick_period_ / scale_).convert_to<int>(); | |
1227 | ||
1228 | hscrollbar->setPageStep(areaSize.width() / 2); | |
1229 | hscrollbar->setSingleStep(major_tick_distance); | |
1230 | ||
1231 | updating_scroll_ = true; | |
1232 | ||
1233 | if (length < MaxScrollValue) { | |
1234 | hscrollbar->setRange(0, length); | |
1235 | hscrollbar->setSliderPosition(offset.convert_to<double>()); | |
1236 | } else { | |
1237 | hscrollbar->setRange(0, MaxScrollValue); | |
1238 | hscrollbar->setSliderPosition( | |
1239 | (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>()); | |
1240 | } | |
1241 | ||
1242 | updating_scroll_ = false; | |
1243 | ||
1244 | // Set the vertical scrollbar | |
1245 | vscrollbar->setPageStep(areaSize.height()); | |
1246 | vscrollbar->setSingleStep(areaSize.height() / 8); | |
1247 | ||
1248 | const pair<int, int> extents = v_extents(); | |
1249 | ||
1250 | // Don't change the scrollbar range if there are no traces | |
1251 | if (extents.first != extents.second) { | |
1252 | int top_margin = ViewScrollMargin; | |
1253 | int btm_margin = ViewScrollMargin; | |
1254 | ||
1255 | vscrollbar->setRange(extents.first - areaSize.height() + top_margin, | |
1256 | extents.second - btm_margin); | |
1257 | } | |
1258 | ||
1259 | if (scroll_needs_defaults_) { | |
1260 | set_scroll_default(); | |
1261 | scroll_needs_defaults_ = false; | |
1262 | } | |
1263 | } | |
1264 | ||
1265 | void View::reset_scroll() | |
1266 | { | |
1267 | scrollarea_->verticalScrollBar()->setRange(0, 0); | |
1268 | } | |
1269 | ||
1270 | void View::set_scroll_default() | |
1271 | { | |
1272 | assert(viewport_); | |
1273 | ||
1274 | const QSize areaSize = viewport_->size(); | |
1275 | ||
1276 | const pair<int, int> extents = v_extents(); | |
1277 | const int trace_height = extents.second - extents.first; | |
1278 | ||
1279 | // Do all traces fit in the view? | |
1280 | if (areaSize.height() >= trace_height) | |
1281 | // Center all traces vertically | |
1282 | set_v_offset(extents.first - | |
1283 | ((areaSize.height() - trace_height) / 2)); | |
1284 | else | |
1285 | // Put the first trace at the top, letting the bottom ones overflow | |
1286 | set_v_offset(extents.first); | |
1287 | } | |
1288 | ||
1289 | void View::determine_if_header_was_shrunk() | |
1290 | { | |
1291 | const int header_pane_width = | |
1292 | splitter_->sizes().front(); // clazy:exclude=detaching-temporary | |
1293 | ||
1294 | // Allow for a slight margin of error so that we also accept | |
1295 | // slight differences when e.g. a label name change increased | |
1296 | // the overall width | |
1297 | header_was_shrunk_ = (header_pane_width < (header_width() - 10)); | |
1298 | } | |
1299 | ||
1300 | void View::resize_header_to_fit() | |
1301 | { | |
1302 | // Setting the maximum width of the header widget doesn't work as | |
1303 | // expected because the splitter would allow the user to make the | |
1304 | // pane wider than that, creating empty space as a result. | |
1305 | // To make this work, we stricly enforce the maximum width by | |
1306 | // expanding the header unless the user shrunk it on purpose. | |
1307 | // As we're then setting the width of the header pane, we set the | |
1308 | // splitter to the maximum allowed position. | |
1309 | ||
1310 | int splitter_area_width = 0; | |
1311 | for (int w : splitter_->sizes()) // clazy:exclude=range-loop | |
1312 | splitter_area_width += w; | |
1313 | ||
1314 | // Make sure the header has enough horizontal space to show all labels fully | |
1315 | QList<int> pane_sizes; | |
1316 | pane_sizes.push_back(header_->extended_size_hint().width()); | |
1317 | pane_sizes.push_back(splitter_area_width - header_->extended_size_hint().width()); | |
1318 | splitter_->setSizes(pane_sizes); | |
1319 | } | |
1320 | ||
1321 | void View::update_layout() | |
1322 | { | |
1323 | update_scroll(); | |
1324 | } | |
1325 | ||
1326 | TraceTreeItemOwner* View::find_prevalent_trace_group( | |
1327 | const shared_ptr<sigrok::ChannelGroup> &group, | |
1328 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
1329 | &signal_map) | |
1330 | { | |
1331 | assert(group); | |
1332 | ||
1333 | unordered_set<TraceTreeItemOwner*> owners; | |
1334 | vector<TraceTreeItemOwner*> owner_list; | |
1335 | ||
1336 | // Make a set and a list of all the owners | |
1337 | for (const auto& channel : group->channels()) { | |
1338 | for (auto& entry : signal_map) { | |
1339 | if (entry.first->channel() == channel) { | |
1340 | TraceTreeItemOwner *const o = (entry.second)->owner(); | |
1341 | owner_list.push_back(o); | |
1342 | owners.insert(o); | |
1343 | } | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | // Iterate through the list of owners, and find the most prevalent | |
1348 | size_t max_prevalence = 0; | |
1349 | TraceTreeItemOwner *prevalent_owner = nullptr; | |
1350 | for (TraceTreeItemOwner *owner : owners) { | |
1351 | const size_t prevalence = count_if( | |
1352 | owner_list.begin(), owner_list.end(), | |
1353 | [&](TraceTreeItemOwner *o) { return o == owner; }); | |
1354 | if (prevalence > max_prevalence) { | |
1355 | max_prevalence = prevalence; | |
1356 | prevalent_owner = owner; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | return prevalent_owner; | |
1361 | } | |
1362 | ||
1363 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( | |
1364 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
1365 | const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
1366 | &signal_map, | |
1367 | set< shared_ptr<Trace> > &add_list) | |
1368 | { | |
1369 | vector< shared_ptr<Trace> > filtered_traces; | |
1370 | ||
1371 | for (const auto& channel : channels) { | |
1372 | for (auto& entry : signal_map) { | |
1373 | if (entry.first->channel() == channel) { | |
1374 | shared_ptr<Trace> trace = entry.second; | |
1375 | const auto list_iter = add_list.find(trace); | |
1376 | if (list_iter == add_list.end()) | |
1377 | continue; | |
1378 | ||
1379 | filtered_traces.push_back(trace); | |
1380 | add_list.erase(list_iter); | |
1381 | } | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | return filtered_traces; | |
1386 | } | |
1387 | ||
1388 | void View::determine_time_unit() | |
1389 | { | |
1390 | // Check whether we know the sample rate and hence can use time as the unit | |
1391 | if (time_unit_ == util::TimeUnit::Samples) { | |
1392 | // Check all signals but... | |
1393 | for (const shared_ptr<Signal>& signal : signals_) { | |
1394 | const shared_ptr<SignalData> data = signal->data(); | |
1395 | ||
1396 | // ...only check first segment of each | |
1397 | const vector< shared_ptr<Segment> > segments = data->segments(); | |
1398 | if (!segments.empty()) | |
1399 | if (segments[0]->samplerate()) { | |
1400 | set_time_unit(util::TimeUnit::Time); | |
1401 | break; | |
1402 | } | |
1403 | } | |
1404 | } | |
1405 | } | |
1406 | ||
1407 | bool View::eventFilter(QObject *object, QEvent *event) | |
1408 | { | |
1409 | const QEvent::Type type = event->type(); | |
1410 | ||
1411 | if (type == QEvent::MouseMove) { | |
1412 | ||
1413 | if (object) | |
1414 | hover_widget_ = qobject_cast<QWidget*>(object); | |
1415 | ||
1416 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
1417 | if (object == viewport_) | |
1418 | hover_point_ = mouse_event->pos(); | |
1419 | else if (object == ruler_) | |
1420 | hover_point_ = mouse_event->pos(); | |
1421 | else if (object == header_) | |
1422 | hover_point_ = QPoint(0, mouse_event->y()); | |
1423 | else | |
1424 | hover_point_ = QPoint(-1, -1); | |
1425 | ||
1426 | update_hover_point(); | |
1427 | ||
1428 | if (grabbed_widget_) { | |
1429 | int64_t nearest = get_nearest_level_change(hover_point_); | |
1430 | pv::util::Timestamp mouse_time = offset_ + hover_point_.x() * scale_; | |
1431 | ||
1432 | if (nearest == -1) { | |
1433 | grabbed_widget_->set_time(mouse_time); | |
1434 | } else { | |
1435 | grabbed_widget_->set_time(nearest / get_signal_under_mouse_cursor()->base()->get_samplerate()); | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | } else if (type == QEvent::MouseButtonPress) { | |
1440 | grabbed_widget_ = nullptr; | |
1441 | ||
1442 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
1443 | if ((object == viewport_) && (mouse_event->button() & Qt::LeftButton)) { | |
1444 | // Send event to all trace tree items | |
1445 | const vector<shared_ptr<TraceTreeItem>> trace_tree_items( | |
1446 | list_by_type<TraceTreeItem>()); | |
1447 | for (const shared_ptr<TraceTreeItem>& r : trace_tree_items) | |
1448 | r->mouse_left_press_event(mouse_event); | |
1449 | } | |
1450 | } else if (type == QEvent::Leave) { | |
1451 | hover_point_ = QPoint(-1, -1); | |
1452 | update_hover_point(); | |
1453 | } else if (type == QEvent::Show) { | |
1454 | ||
1455 | // This is somewhat of a hack, unfortunately. We cannot use | |
1456 | // set_v_offset() from within restore_settings() as the view | |
1457 | // at that point is neither visible nor properly sized. | |
1458 | // This is the least intrusive workaround I could come up | |
1459 | // with: set the vertical offset (or scroll defaults) when | |
1460 | // the view is shown, which happens after all widgets were | |
1461 | // resized to their final sizes. | |
1462 | update_layout(); | |
1463 | ||
1464 | if (settings_restored_) | |
1465 | determine_if_header_was_shrunk(); | |
1466 | else | |
1467 | resize_header_to_fit(); | |
1468 | ||
1469 | if (scroll_needs_defaults_) { | |
1470 | set_scroll_default(); | |
1471 | scroll_needs_defaults_ = false; | |
1472 | } | |
1473 | ||
1474 | if (saved_v_offset_) { | |
1475 | set_v_offset(saved_v_offset_); | |
1476 | saved_v_offset_ = 0; | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | return QObject::eventFilter(object, event); | |
1481 | } | |
1482 | ||
1483 | void View::contextMenuEvent(QContextMenuEvent *event) | |
1484 | { | |
1485 | QPoint pos = event->pos() - QPoint(0, ruler_->sizeHint().height()); | |
1486 | ||
1487 | const shared_ptr<ViewItem> r = viewport_->get_mouse_over_item(pos); | |
1488 | if (!r) | |
1489 | return; | |
1490 | ||
1491 | QMenu *menu = r->create_view_context_menu(this, pos); | |
1492 | if (menu) | |
1493 | menu->popup(event->globalPos()); | |
1494 | } | |
1495 | ||
1496 | void View::resizeEvent(QResizeEvent* event) | |
1497 | { | |
1498 | // Only adjust the top margin if we shrunk vertically | |
1499 | if (event->size().height() < event->oldSize().height()) | |
1500 | adjust_top_margin(); | |
1501 | ||
1502 | update_layout(); | |
1503 | } | |
1504 | ||
1505 | void View::update_hover_point() | |
1506 | { | |
1507 | // Determine signal that the mouse cursor is hovering over | |
1508 | signal_under_mouse_cursor_.reset(); | |
1509 | if (hover_widget_ == this) { | |
1510 | for (const shared_ptr<Signal>& s : signals_) { | |
1511 | const pair<int, int> extents = s->v_extents(); | |
1512 | const int top = s->get_visual_y() + extents.first; | |
1513 | const int btm = s->get_visual_y() + extents.second; | |
1514 | if ((hover_point_.y() >= top) && (hover_point_.y() <= btm) | |
1515 | && s->base()->enabled()) | |
1516 | signal_under_mouse_cursor_ = s; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | // Update all trace tree items | |
1521 | const vector<shared_ptr<TraceTreeItem>> trace_tree_items( | |
1522 | list_by_type<TraceTreeItem>()); | |
1523 | for (const shared_ptr<TraceTreeItem>& r : trace_tree_items) | |
1524 | r->hover_point_changed(hover_point_); | |
1525 | ||
1526 | // Notify any other listeners | |
1527 | hover_point_changed(hover_widget_, hover_point_); | |
1528 | } | |
1529 | ||
1530 | void View::row_item_appearance_changed(bool label, bool content) | |
1531 | { | |
1532 | if (label) | |
1533 | header_->update(); | |
1534 | if (content) | |
1535 | viewport_->update(); | |
1536 | } | |
1537 | ||
1538 | void View::time_item_appearance_changed(bool label, bool content) | |
1539 | { | |
1540 | if (label) { | |
1541 | ruler_->update(); | |
1542 | ||
1543 | // Make sure the header pane width is updated, too | |
1544 | update_layout(); | |
1545 | } | |
1546 | ||
1547 | if (content) | |
1548 | viewport_->update(); | |
1549 | } | |
1550 | ||
1551 | void View::extents_changed(bool horz, bool vert) | |
1552 | { | |
1553 | sticky_events_ |= | |
1554 | (horz ? TraceTreeItemHExtentsChanged : 0) | | |
1555 | (vert ? TraceTreeItemVExtentsChanged : 0); | |
1556 | ||
1557 | if (!lazy_event_handler_.isActive()) | |
1558 | lazy_event_handler_.start(); | |
1559 | } | |
1560 | ||
1561 | void View::on_signal_name_changed() | |
1562 | { | |
1563 | if (!header_was_shrunk_) | |
1564 | resize_header_to_fit(); | |
1565 | } | |
1566 | ||
1567 | void View::on_splitter_moved() | |
1568 | { | |
1569 | // The header can only shrink when the splitter is moved manually | |
1570 | determine_if_header_was_shrunk(); | |
1571 | ||
1572 | if (!header_was_shrunk_) | |
1573 | resize_header_to_fit(); | |
1574 | } | |
1575 | ||
1576 | void View::on_zoom_in_shortcut_triggered() | |
1577 | { | |
1578 | zoom(1); | |
1579 | } | |
1580 | ||
1581 | void View::on_zoom_out_shortcut_triggered() | |
1582 | { | |
1583 | zoom(-1); | |
1584 | } | |
1585 | ||
1586 | void View::on_scroll_to_start_shortcut_triggered() | |
1587 | { | |
1588 | set_h_offset(0); | |
1589 | } | |
1590 | ||
1591 | void View::on_scroll_to_end_shortcut_triggered() | |
1592 | { | |
1593 | set_h_offset(get_h_scrollbar_maximum()); | |
1594 | } | |
1595 | ||
1596 | void View::h_scroll_value_changed(int value) | |
1597 | { | |
1598 | if (updating_scroll_) | |
1599 | return; | |
1600 | ||
1601 | // Disable sticky scrolling when user moves the horizontal scroll bar | |
1602 | // during a running acquisition | |
1603 | if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) { | |
1604 | sticky_scrolling_ = false; | |
1605 | sticky_scrolling_changed(false); | |
1606 | } | |
1607 | ||
1608 | const int range = scrollarea_->horizontalScrollBar()->maximum(); | |
1609 | if (range < MaxScrollValue) | |
1610 | set_offset(scale_ * value); | |
1611 | else { | |
1612 | double length = 0; | |
1613 | Timestamp offset; | |
1614 | get_scroll_layout(length, offset); | |
1615 | set_offset(scale_ * length * value / MaxScrollValue); | |
1616 | } | |
1617 | ||
1618 | ruler_->update(); | |
1619 | viewport_->update(); | |
1620 | } | |
1621 | ||
1622 | void View::v_scroll_value_changed() | |
1623 | { | |
1624 | header_->update(); | |
1625 | viewport_->update(); | |
1626 | } | |
1627 | ||
1628 | void View::on_grab_ruler(int ruler_id) | |
1629 | { | |
1630 | if (!cursors_shown()) { | |
1631 | center_cursors(); | |
1632 | show_cursors(); | |
1633 | } | |
1634 | ||
1635 | // Release the grabbed widget if its trigger hotkey was pressed twice | |
1636 | if (ruler_id == 1) | |
1637 | grabbed_widget_ = (grabbed_widget_ == cursors_->first().get()) ? | |
1638 | nullptr : cursors_->first().get(); | |
1639 | else | |
1640 | grabbed_widget_ = (grabbed_widget_ == cursors_->second().get()) ? | |
1641 | nullptr : cursors_->second().get(); | |
1642 | ||
1643 | if (grabbed_widget_) | |
1644 | grabbed_widget_->set_time(ruler_->get_absolute_time_from_x_pos( | |
1645 | mapFromGlobal(QCursor::pos()).x() - header_width())); | |
1646 | } | |
1647 | ||
1648 | void View::signals_changed() | |
1649 | { | |
1650 | using sigrok::Channel; | |
1651 | ||
1652 | vector< shared_ptr<Channel> > channels; | |
1653 | shared_ptr<sigrok::Device> sr_dev; | |
1654 | bool signals_added_or_removed = false; | |
1655 | ||
1656 | // Do we need to set the vertical scrollbar to its default position later? | |
1657 | // We do if there are no traces, i.e. the scroll bar has no range set | |
1658 | bool reset_scrollbar = | |
1659 | (scrollarea_->verticalScrollBar()->minimum() == | |
1660 | scrollarea_->verticalScrollBar()->maximum()); | |
1661 | ||
1662 | if (!session_.device()) { | |
1663 | reset_scroll(); | |
1664 | signals_.clear(); | |
1665 | } else { | |
1666 | sr_dev = session_.device()->device(); | |
1667 | assert(sr_dev); | |
1668 | channels = sr_dev->channels(); | |
1669 | } | |
1670 | ||
1671 | vector< shared_ptr<TraceTreeItem> > new_top_level_items; | |
1672 | ||
1673 | // Make a list of traces that are being added, and a list of traces | |
1674 | // that are being removed | |
1675 | const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>(); | |
1676 | const set<shared_ptr<Trace>> prev_traces( | |
1677 | prev_trace_list.begin(), prev_trace_list.end()); | |
1678 | ||
1679 | set< shared_ptr<Trace> > traces(signals_.begin(), signals_.end()); | |
1680 | ||
1681 | #ifdef ENABLE_DECODE | |
1682 | traces.insert(decode_traces_.begin(), decode_traces_.end()); | |
1683 | #endif | |
1684 | ||
1685 | set< shared_ptr<Trace> > add_traces; | |
1686 | set_difference(traces.begin(), traces.end(), | |
1687 | prev_traces.begin(), prev_traces.end(), | |
1688 | inserter(add_traces, add_traces.begin())); | |
1689 | ||
1690 | set< shared_ptr<Trace> > remove_traces; | |
1691 | set_difference(prev_traces.begin(), prev_traces.end(), | |
1692 | traces.begin(), traces.end(), | |
1693 | inserter(remove_traces, remove_traces.begin())); | |
1694 | ||
1695 | // Make a look-up table of sigrok Channels to pulseview Signals | |
1696 | unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> > | |
1697 | signal_map; | |
1698 | for (const shared_ptr<Signal>& sig : signals_) | |
1699 | signal_map[sig->base()] = sig; | |
1700 | ||
1701 | // Populate channel groups | |
1702 | if (sr_dev) | |
1703 | for (auto& entry : sr_dev->channel_groups()) { | |
1704 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
1705 | ||
1706 | if (group->channels().size() <= 1) | |
1707 | continue; | |
1708 | ||
1709 | // Find best trace group to add to | |
1710 | TraceTreeItemOwner *owner = find_prevalent_trace_group( | |
1711 | group, signal_map); | |
1712 | ||
1713 | // If there is no trace group, create one | |
1714 | shared_ptr<TraceGroup> new_trace_group; | |
1715 | if (!owner) { | |
1716 | new_trace_group.reset(new TraceGroup()); | |
1717 | owner = new_trace_group.get(); | |
1718 | } | |
1719 | ||
1720 | // Extract traces for the trace group, removing them from | |
1721 | // the add list | |
1722 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
1723 | extract_new_traces_for_channels(group->channels(), | |
1724 | signal_map, add_traces); | |
1725 | ||
1726 | // Add the traces to the group | |
1727 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
1728 | int offset = prev_v_extents.second - prev_v_extents.first; | |
1729 | for (const shared_ptr<Trace>& trace : new_traces_in_group) { | |
1730 | assert(trace); | |
1731 | owner->add_child_item(trace); | |
1732 | ||
1733 | const pair<int, int> extents = trace->v_extents(); | |
1734 | if (trace->enabled()) | |
1735 | offset += -extents.first; | |
1736 | trace->force_to_v_offset(offset); | |
1737 | if (trace->enabled()) | |
1738 | offset += extents.second; | |
1739 | } | |
1740 | ||
1741 | if (new_trace_group) { | |
1742 | // Assign proper vertical offsets to each channel in the group | |
1743 | new_trace_group->restack_items(); | |
1744 | ||
1745 | // If this is a new group, enqueue it in the new top level | |
1746 | // items list | |
1747 | if (!new_traces_in_group.empty()) | |
1748 | new_top_level_items.push_back(new_trace_group); | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | // Enqueue the remaining logic channels in a group | |
1753 | vector< shared_ptr<Channel> > logic_channels; | |
1754 | copy_if(channels.begin(), channels.end(), back_inserter(logic_channels), | |
1755 | [](const shared_ptr<Channel>& c) { | |
1756 | return c->type() == sigrok::ChannelType::LOGIC; }); | |
1757 | ||
1758 | const vector< shared_ptr<Trace> > non_grouped_logic_signals = | |
1759 | extract_new_traces_for_channels(logic_channels, signal_map, add_traces); | |
1760 | ||
1761 | if (non_grouped_logic_signals.size() > 0) { | |
1762 | const shared_ptr<TraceGroup> non_grouped_trace_group( | |
1763 | make_shared<TraceGroup>()); | |
1764 | for (const shared_ptr<Trace>& trace : non_grouped_logic_signals) | |
1765 | non_grouped_trace_group->add_child_item(trace); | |
1766 | ||
1767 | non_grouped_trace_group->restack_items(); | |
1768 | new_top_level_items.push_back(non_grouped_trace_group); | |
1769 | } | |
1770 | ||
1771 | // Enqueue the remaining channels as free ungrouped traces | |
1772 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
1773 | extract_new_traces_for_channels(channels, signal_map, add_traces); | |
1774 | new_top_level_items.insert(new_top_level_items.end(), | |
1775 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
1776 | ||
1777 | // Enqueue any remaining traces i.e. decode traces | |
1778 | new_top_level_items.insert(new_top_level_items.end(), | |
1779 | add_traces.begin(), add_traces.end()); | |
1780 | ||
1781 | // Remove any removed traces | |
1782 | for (const shared_ptr<Trace>& trace : remove_traces) { | |
1783 | TraceTreeItemOwner *const owner = trace->owner(); | |
1784 | assert(owner); | |
1785 | owner->remove_child_item(trace); | |
1786 | signals_added_or_removed = true; | |
1787 | } | |
1788 | ||
1789 | // Remove any empty trace groups | |
1790 | for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>()) | |
1791 | if (group->child_items().size() == 0) { | |
1792 | remove_child_item(group); | |
1793 | group.reset(); | |
1794 | } | |
1795 | ||
1796 | // Add and position the pending top levels items | |
1797 | int offset = v_extents().second; | |
1798 | for (auto item : new_top_level_items) { | |
1799 | add_child_item(item); | |
1800 | ||
1801 | // Position the item after the last item or at the top if there is none | |
1802 | const pair<int, int> extents = item->v_extents(); | |
1803 | ||
1804 | if (item->enabled()) | |
1805 | offset += -extents.first; | |
1806 | ||
1807 | item->force_to_v_offset(offset); | |
1808 | ||
1809 | if (item->enabled()) | |
1810 | offset += extents.second; | |
1811 | signals_added_or_removed = true; | |
1812 | } | |
1813 | ||
1814 | ||
1815 | if (signals_added_or_removed && !header_was_shrunk_) | |
1816 | resize_header_to_fit(); | |
1817 | ||
1818 | update_layout(); | |
1819 | ||
1820 | header_->update(); | |
1821 | viewport_->update(); | |
1822 | ||
1823 | if (reset_scrollbar) | |
1824 | set_scroll_default(); | |
1825 | } | |
1826 | ||
1827 | void View::capture_state_updated(int state) | |
1828 | { | |
1829 | GlobalSettings settings; | |
1830 | ||
1831 | if (state == Session::Running) { | |
1832 | set_time_unit(util::TimeUnit::Samples); | |
1833 | ||
1834 | trigger_markers_.clear(); | |
1835 | if (!custom_zero_offset_set_) | |
1836 | set_zero_position(0); | |
1837 | ||
1838 | scale_at_acq_start_ = scale_; | |
1839 | offset_at_acq_start_ = offset_; | |
1840 | ||
1841 | // Activate "always zoom to fit" if the setting is enabled and we're | |
1842 | // the main view of this session (other trace views may be used for | |
1843 | // zooming and we don't want to mess them up) | |
1844 | bool state = settings.value(GlobalSettings::Key_View_ZoomToFitDuringAcq).toBool(); | |
1845 | if (is_main_view_ && state) { | |
1846 | always_zoom_to_fit_ = true; | |
1847 | always_zoom_to_fit_changed(always_zoom_to_fit_); | |
1848 | } | |
1849 | ||
1850 | // Enable sticky scrolling if the setting is enabled | |
1851 | sticky_scrolling_ = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool(); | |
1852 | ||
1853 | // Reset all traces to segment 0 | |
1854 | current_segment_ = 0; | |
1855 | set_current_segment(current_segment_); | |
1856 | } | |
1857 | ||
1858 | if (state == Session::Stopped) { | |
1859 | // After acquisition has stopped we need to re-calculate the ticks once | |
1860 | // as it's otherwise done when the user pans or zooms, which is too late | |
1861 | calculate_tick_spacing(); | |
1862 | ||
1863 | // Reset "always zoom to fit", the acquisition has stopped | |
1864 | if (always_zoom_to_fit_) { | |
1865 | // Perform a final zoom-to-fit before disabling | |
1866 | zoom_fit(always_zoom_to_fit_); | |
1867 | always_zoom_to_fit_ = false; | |
1868 | always_zoom_to_fit_changed(always_zoom_to_fit_); | |
1869 | } | |
1870 | ||
1871 | bool zoom_to_fit_after_acq = | |
1872 | settings.value(GlobalSettings::Key_View_ZoomToFitAfterAcq).toBool(); | |
1873 | ||
1874 | // Only perform zoom-to-fit if the user hasn't altered the viewport and | |
1875 | // we didn't restore settings in the meanwhile | |
1876 | if (zoom_to_fit_after_acq && | |
1877 | !suppress_zoom_to_fit_after_acq_ && | |
1878 | (scale_ == scale_at_acq_start_) && | |
1879 | (offset_ == offset_at_acq_start_)) | |
1880 | zoom_fit(false); // We're stopped, so the GUI state doesn't matter | |
1881 | ||
1882 | suppress_zoom_to_fit_after_acq_ = false; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | void View::on_new_segment(int new_segment_id) | |
1887 | { | |
1888 | on_segment_changed(new_segment_id); | |
1889 | } | |
1890 | ||
1891 | void View::on_segment_completed(int segment_id) | |
1892 | { | |
1893 | on_segment_changed(segment_id); | |
1894 | } | |
1895 | ||
1896 | void View::on_segment_changed(int segment) | |
1897 | { | |
1898 | switch (segment_display_mode_) { | |
1899 | case Trace::ShowLastSegmentOnly: | |
1900 | case Trace::ShowSingleSegmentOnly: | |
1901 | set_current_segment(segment); | |
1902 | break; | |
1903 | ||
1904 | case Trace::ShowLastCompleteSegmentOnly: | |
1905 | // Only update if all segments are complete | |
1906 | if (session_.all_segments_complete(segment)) | |
1907 | set_current_segment(segment); | |
1908 | break; | |
1909 | ||
1910 | case Trace::ShowAllSegments: | |
1911 | case Trace::ShowAccumulatedIntensity: | |
1912 | default: | |
1913 | break; | |
1914 | } | |
1915 | } | |
1916 | ||
1917 | void View::on_settingViewTriggerIsZeroTime_changed(const QVariant new_value) | |
1918 | { | |
1919 | (void)new_value; | |
1920 | ||
1921 | if (!custom_zero_offset_set_) | |
1922 | reset_zero_position(); | |
1923 | } | |
1924 | ||
1925 | void View::perform_delayed_view_update() | |
1926 | { | |
1927 | if (always_zoom_to_fit_) { | |
1928 | zoom_fit(true); | |
1929 | } else if (sticky_scrolling_) { | |
1930 | // Make right side of the view sticky | |
1931 | double length = 0; | |
1932 | Timestamp offset; | |
1933 | get_scroll_layout(length, offset); | |
1934 | ||
1935 | const QSize areaSize = viewport_->size(); | |
1936 | length = max(length - areaSize.width(), 0.0); | |
1937 | ||
1938 | set_offset(scale_ * length); | |
1939 | } | |
1940 | ||
1941 | determine_time_unit(); | |
1942 | update_scroll(); | |
1943 | ruler_->update(); | |
1944 | viewport_->update(); | |
1945 | } | |
1946 | ||
1947 | void View::process_sticky_events() | |
1948 | { | |
1949 | if (sticky_events_ & TraceTreeItemHExtentsChanged) | |
1950 | update_layout(); | |
1951 | if (sticky_events_ & TraceTreeItemVExtentsChanged) { | |
1952 | restack_all_trace_tree_items(); | |
1953 | update_scroll(); | |
1954 | } | |
1955 | ||
1956 | // Clear the sticky events | |
1957 | sticky_events_ = 0; | |
1958 | } | |
1959 | ||
1960 | } // namespace trace | |
1961 | } // namespace views | |
1962 | } // namespace pv |