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