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