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