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