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