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