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