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