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