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