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