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