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