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