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