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