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