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