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