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