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