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