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