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