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