]>
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #ifdef ENABLE_DECODE | |
22 | #include <libsigrokdecode/libsigrokdecode.h> | |
23 | #endif | |
24 | ||
25 | #include <extdef.h> | |
26 | ||
27 | #include <algorithm> | |
28 | #include <cassert> | |
29 | #include <climits> | |
30 | #include <cmath> | |
31 | #include <iterator> | |
32 | #include <mutex> | |
33 | #include <unordered_set> | |
34 | ||
35 | #include <boost/thread/locks.hpp> | |
36 | ||
37 | #include <QApplication> | |
38 | #include <QEvent> | |
39 | #include <QFontMetrics> | |
40 | #include <QMouseEvent> | |
41 | #include <QScrollBar> | |
42 | ||
43 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
44 | ||
45 | #include "analogsignal.hpp" | |
46 | #include "header.hpp" | |
47 | #include "logicsignal.hpp" | |
48 | #include "ruler.hpp" | |
49 | #include "signal.hpp" | |
50 | #include "tracegroup.hpp" | |
51 | #include "triggermarker.hpp" | |
52 | #include "view.hpp" | |
53 | #include "viewport.hpp" | |
54 | ||
55 | #include "pv/session.hpp" | |
56 | #include "pv/devices/device.hpp" | |
57 | #include "pv/data/logic.hpp" | |
58 | #include "pv/data/logicsegment.hpp" | |
59 | #include "pv/util.hpp" | |
60 | ||
61 | #ifdef ENABLE_DECODE | |
62 | #include "decodetrace.hpp" | |
63 | #endif | |
64 | ||
65 | using boost::shared_lock; | |
66 | using boost::shared_mutex; | |
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::deque; | |
76 | using std::dynamic_pointer_cast; | |
77 | using std::inserter; | |
78 | using std::list; | |
79 | using std::lock_guard; | |
80 | using std::max; | |
81 | using std::make_pair; | |
82 | using std::make_shared; | |
83 | using std::min; | |
84 | using std::pair; | |
85 | using std::set; | |
86 | using std::set_difference; | |
87 | using std::shared_ptr; | |
88 | using std::unordered_map; | |
89 | using std::unordered_set; | |
90 | using std::vector; | |
91 | using std::weak_ptr; | |
92 | ||
93 | namespace pv { | |
94 | namespace view { | |
95 | ||
96 | const Timestamp View::MaxScale("1e9"); | |
97 | const Timestamp View::MinScale("1e-12"); | |
98 | ||
99 | const int View::MaxScrollValue = INT_MAX / 2; | |
100 | const int View::MaxViewAutoUpdateRate = 25; // No more than 25 Hz with sticky scrolling | |
101 | ||
102 | const int View::ScaleUnits[3] = {1, 2, 5}; | |
103 | ||
104 | View::View(Session &session, QWidget *parent) : | |
105 | QAbstractScrollArea(parent), | |
106 | session_(session), | |
107 | viewport_(new Viewport(*this)), | |
108 | ruler_(new Ruler(*this)), | |
109 | header_(new Header(*this)), | |
110 | scale_(1e-3), | |
111 | offset_(0), | |
112 | updating_scroll_(false), | |
113 | sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui() | |
114 | always_zoom_to_fit_(false), | |
115 | tick_period_(0), | |
116 | tick_prefix_(pv::util::SIPrefix::yocto), | |
117 | tick_precision_(0), | |
118 | time_unit_(util::TimeUnit::Time), | |
119 | show_cursors_(false), | |
120 | cursors_(new CursorPair(*this)), | |
121 | next_flag_text_('A'), | |
122 | trigger_markers_(), | |
123 | hover_point_(-1, -1) | |
124 | { | |
125 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
126 | this, SLOT(h_scroll_value_changed(int))); | |
127 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | |
128 | this, SLOT(v_scroll_value_changed())); | |
129 | ||
130 | connect(&session_, SIGNAL(signals_changed()), | |
131 | this, SLOT(signals_changed())); | |
132 | connect(&session_, SIGNAL(capture_state_changed(int)), | |
133 | this, SLOT(capture_state_updated(int))); | |
134 | connect(&session_, SIGNAL(data_received()), | |
135 | this, SLOT(data_updated())); | |
136 | connect(&session_, SIGNAL(frame_ended()), | |
137 | this, SLOT(data_updated())); | |
138 | ||
139 | connect(header_, SIGNAL(selection_changed()), | |
140 | ruler_, SLOT(clear_selection())); | |
141 | connect(ruler_, SIGNAL(selection_changed()), | |
142 | header_, SLOT(clear_selection())); | |
143 | ||
144 | connect(header_, SIGNAL(selection_changed()), | |
145 | this, SIGNAL(selection_changed())); | |
146 | connect(ruler_, SIGNAL(selection_changed()), | |
147 | this, SIGNAL(selection_changed())); | |
148 | ||
149 | connect(this, SIGNAL(hover_point_changed()), | |
150 | this, SLOT(on_hover_point_changed())); | |
151 | ||
152 | connect(&lazy_event_handler_, SIGNAL(timeout()), | |
153 | this, SLOT(process_sticky_events())); | |
154 | lazy_event_handler_.setSingleShot(true); | |
155 | ||
156 | connect(&delayed_view_updater_, SIGNAL(timeout()), | |
157 | this, SLOT(perform_delayed_view_update())); | |
158 | delayed_view_updater_.setSingleShot(true); | |
159 | delayed_view_updater_.setInterval(1000 / MaxViewAutoUpdateRate); | |
160 | ||
161 | setViewport(viewport_); | |
162 | ||
163 | viewport_->installEventFilter(this); | |
164 | ruler_->installEventFilter(this); | |
165 | header_->installEventFilter(this); | |
166 | ||
167 | // Trigger the initial event manually. The default device has signals | |
168 | // which were created before this object came into being | |
169 | signals_changed(); | |
170 | ||
171 | // make sure the transparent widgets are on the top | |
172 | ruler_->raise(); | |
173 | header_->raise(); | |
174 | ||
175 | // Update the zoom state | |
176 | calculate_tick_spacing(); | |
177 | } | |
178 | ||
179 | Session& View::session() | |
180 | { | |
181 | return session_; | |
182 | } | |
183 | ||
184 | const Session& View::session() const | |
185 | { | |
186 | return session_; | |
187 | } | |
188 | ||
189 | View* View::view() | |
190 | { | |
191 | return this; | |
192 | } | |
193 | ||
194 | const View* View::view() const | |
195 | { | |
196 | return this; | |
197 | } | |
198 | ||
199 | Viewport* View::viewport() | |
200 | { | |
201 | return viewport_; | |
202 | } | |
203 | ||
204 | const Viewport* View::viewport() const | |
205 | { | |
206 | return viewport_; | |
207 | } | |
208 | ||
209 | vector< shared_ptr<TimeItem> > View::time_items() const | |
210 | { | |
211 | const vector<shared_ptr<Flag>> f(flags()); | |
212 | vector<shared_ptr<TimeItem>> items(f.begin(), f.end()); | |
213 | items.push_back(cursors_); | |
214 | items.push_back(cursors_->first()); | |
215 | items.push_back(cursors_->second()); | |
216 | ||
217 | for (auto trigger_marker : trigger_markers_) | |
218 | items.push_back(trigger_marker); | |
219 | ||
220 | return items; | |
221 | } | |
222 | ||
223 | double View::scale() const | |
224 | { | |
225 | return scale_; | |
226 | } | |
227 | ||
228 | void View::set_scale(double scale) | |
229 | { | |
230 | if (scale_ != scale) { | |
231 | scale_ = scale; | |
232 | Q_EMIT scale_changed(); | |
233 | } | |
234 | } | |
235 | ||
236 | const Timestamp& View::offset() const | |
237 | { | |
238 | return offset_; | |
239 | } | |
240 | ||
241 | void View::set_offset(const pv::util::Timestamp& offset) | |
242 | { | |
243 | if (offset_ != offset) { | |
244 | offset_ = offset; | |
245 | Q_EMIT offset_changed(); | |
246 | } | |
247 | } | |
248 | ||
249 | int View::owner_visual_v_offset() const | |
250 | { | |
251 | return -verticalScrollBar()->sliderPosition(); | |
252 | } | |
253 | ||
254 | void View::set_v_offset(int offset) | |
255 | { | |
256 | verticalScrollBar()->setSliderPosition(offset); | |
257 | header_->update(); | |
258 | viewport_->update(); | |
259 | } | |
260 | ||
261 | unsigned int View::depth() const | |
262 | { | |
263 | return 0; | |
264 | } | |
265 | ||
266 | pv::util::SIPrefix View::tick_prefix() const | |
267 | { | |
268 | return tick_prefix_; | |
269 | } | |
270 | ||
271 | void View::set_tick_prefix(pv::util::SIPrefix tick_prefix) | |
272 | { | |
273 | if (tick_prefix_ != tick_prefix) { | |
274 | tick_prefix_ = tick_prefix; | |
275 | Q_EMIT tick_prefix_changed(); | |
276 | } | |
277 | } | |
278 | ||
279 | unsigned int View::tick_precision() const | |
280 | { | |
281 | return tick_precision_; | |
282 | } | |
283 | ||
284 | void View::set_tick_precision(unsigned tick_precision) | |
285 | { | |
286 | if (tick_precision_ != tick_precision) { | |
287 | tick_precision_ = tick_precision; | |
288 | Q_EMIT tick_precision_changed(); | |
289 | } | |
290 | } | |
291 | ||
292 | const pv::util::Timestamp& View::tick_period() const | |
293 | { | |
294 | return tick_period_; | |
295 | } | |
296 | ||
297 | void View::set_tick_period(const pv::util::Timestamp& tick_period) | |
298 | { | |
299 | if (tick_period_ != tick_period) { | |
300 | tick_period_ = tick_period; | |
301 | Q_EMIT tick_period_changed(); | |
302 | } | |
303 | } | |
304 | ||
305 | TimeUnit View::time_unit() const | |
306 | { | |
307 | return time_unit_; | |
308 | } | |
309 | ||
310 | void View::set_time_unit(pv::util::TimeUnit time_unit) | |
311 | { | |
312 | if (time_unit_ != time_unit) { | |
313 | time_unit_ = time_unit; | |
314 | Q_EMIT time_unit_changed(); | |
315 | } | |
316 | } | |
317 | ||
318 | void View::zoom(double steps) | |
319 | { | |
320 | zoom(steps, viewport_->width() / 2); | |
321 | } | |
322 | ||
323 | void View::zoom(double steps, int offset) | |
324 | { | |
325 | set_zoom(scale_ * pow(3.0/2.0, -steps), offset); | |
326 | } | |
327 | ||
328 | void View::zoom_fit(bool gui_state) | |
329 | { | |
330 | // Act as one-shot when stopped, toggle along with the GUI otherwise | |
331 | if (session_.get_capture_state() == Session::Stopped) { | |
332 | always_zoom_to_fit_ = false; | |
333 | always_zoom_to_fit_changed(false); | |
334 | } else { | |
335 | always_zoom_to_fit_ = gui_state; | |
336 | always_zoom_to_fit_changed(gui_state); | |
337 | } | |
338 | ||
339 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
340 | const Timestamp delta = extents.second - extents.first; | |
341 | if (delta < Timestamp("1e-12")) | |
342 | return; | |
343 | ||
344 | assert(viewport_); | |
345 | const int w = viewport_->width(); | |
346 | if (w <= 0) | |
347 | return; | |
348 | ||
349 | const Timestamp scale = max(min(delta / w, MaxScale), MinScale); | |
350 | set_scale_offset(scale.convert_to<double>(), extents.first); | |
351 | } | |
352 | ||
353 | void View::zoom_one_to_one() | |
354 | { | |
355 | using pv::data::SignalData; | |
356 | ||
357 | // Make a set of all the visible data objects | |
358 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
359 | if (visible_data.empty()) | |
360 | return; | |
361 | ||
362 | assert(viewport_); | |
363 | const int w = viewport_->width(); | |
364 | if (w <= 0) | |
365 | return; | |
366 | ||
367 | set_zoom(1.0 / session_.get_samplerate(), w / 2); | |
368 | } | |
369 | ||
370 | void View::set_scale_offset(double scale, const Timestamp& offset) | |
371 | { | |
372 | // Disable sticky scrolling / always zoom to fit when acquisition runs | |
373 | // and user drags the viewport | |
374 | if ((scale_ == scale) && (offset_ != offset) && | |
375 | (session_.get_capture_state() == Session::Running)) { | |
376 | ||
377 | if (sticky_scrolling_) { | |
378 | sticky_scrolling_ = false; | |
379 | sticky_scrolling_changed(false); | |
380 | } | |
381 | ||
382 | if (always_zoom_to_fit_) { | |
383 | always_zoom_to_fit_ = false; | |
384 | always_zoom_to_fit_changed(false); | |
385 | } | |
386 | } | |
387 | ||
388 | set_scale(scale); | |
389 | set_offset(offset); | |
390 | ||
391 | calculate_tick_spacing(); | |
392 | ||
393 | update_scroll(); | |
394 | ruler_->update(); | |
395 | viewport_->update(); | |
396 | } | |
397 | ||
398 | set< shared_ptr<SignalData> > View::get_visible_data() const | |
399 | { | |
400 | const unordered_set< shared_ptr<Signal> > sigs(session().signals()); | |
401 | ||
402 | // Make a set of all the visible data objects | |
403 | set< shared_ptr<SignalData> > visible_data; | |
404 | for (const shared_ptr<Signal> sig : sigs) | |
405 | if (sig->enabled()) | |
406 | visible_data.insert(sig->data()); | |
407 | ||
408 | return visible_data; | |
409 | } | |
410 | ||
411 | pair<Timestamp, Timestamp> View::get_time_extents() const | |
412 | { | |
413 | boost::optional<Timestamp> left_time, right_time; | |
414 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
415 | for (const shared_ptr<SignalData> d : visible_data) { | |
416 | const vector< shared_ptr<Segment> > segments = | |
417 | d->segments(); | |
418 | for (const shared_ptr<Segment> &s : segments) { | |
419 | double samplerate = s->samplerate(); | |
420 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
421 | ||
422 | const Timestamp start_time = s->start_time(); | |
423 | left_time = left_time ? | |
424 | min(*left_time, start_time) : | |
425 | start_time; | |
426 | right_time = right_time ? | |
427 | max(*right_time, start_time + d->max_sample_count() / samplerate) : | |
428 | start_time + d->max_sample_count() / samplerate; | |
429 | } | |
430 | } | |
431 | ||
432 | if (!left_time || !right_time) | |
433 | return make_pair(0, 0); | |
434 | ||
435 | assert(*left_time < *right_time); | |
436 | return make_pair(*left_time, *right_time); | |
437 | } | |
438 | ||
439 | void View::enable_sticky_scrolling(bool state) | |
440 | { | |
441 | sticky_scrolling_ = state; | |
442 | } | |
443 | ||
444 | void View::enable_coloured_bg(bool state) | |
445 | { | |
446 | const vector<shared_ptr<TraceTreeItem>> items( | |
447 | list_by_type<TraceTreeItem>()); | |
448 | ||
449 | for (shared_ptr<TraceTreeItem> i : items) { | |
450 | // Can't cast to Trace because it's abstract, so we need to | |
451 | // check for any derived classes individually | |
452 | ||
453 | shared_ptr<AnalogSignal> a = dynamic_pointer_cast<AnalogSignal>(i); | |
454 | if (a) | |
455 | a->set_coloured_bg(state); | |
456 | ||
457 | shared_ptr<LogicSignal> l = dynamic_pointer_cast<LogicSignal>(i); | |
458 | if (l) | |
459 | l->set_coloured_bg(state); | |
460 | ||
461 | #ifdef ENABLE_DECODE | |
462 | shared_ptr<DecodeTrace> d = dynamic_pointer_cast<DecodeTrace>(i); | |
463 | if (d) | |
464 | d->set_coloured_bg(state); | |
465 | #endif | |
466 | } | |
467 | ||
468 | viewport_->update(); | |
469 | } | |
470 | ||
471 | bool View::cursors_shown() const | |
472 | { | |
473 | return show_cursors_; | |
474 | } | |
475 | ||
476 | void View::show_cursors(bool show) | |
477 | { | |
478 | show_cursors_ = show; | |
479 | ruler_->update(); | |
480 | viewport_->update(); | |
481 | } | |
482 | ||
483 | void View::centre_cursors() | |
484 | { | |
485 | const double time_width = scale_ * viewport_->width(); | |
486 | cursors_->first()->set_time(offset_ + time_width * 0.4); | |
487 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
488 | ruler_->update(); | |
489 | viewport_->update(); | |
490 | } | |
491 | ||
492 | std::shared_ptr<CursorPair> View::cursors() const | |
493 | { | |
494 | return cursors_; | |
495 | } | |
496 | ||
497 | void View::add_flag(const Timestamp& time) | |
498 | { | |
499 | flags_.push_back(shared_ptr<Flag>(new Flag(*this, time, | |
500 | QString("%1").arg(next_flag_text_)))); | |
501 | ||
502 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
503 | (next_flag_text_ + 1); | |
504 | ||
505 | time_item_appearance_changed(true, true); | |
506 | } | |
507 | ||
508 | void View::remove_flag(std::shared_ptr<Flag> flag) | |
509 | { | |
510 | flags_.remove(flag); | |
511 | time_item_appearance_changed(true, true); | |
512 | } | |
513 | ||
514 | vector< std::shared_ptr<Flag> > View::flags() const | |
515 | { | |
516 | vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
517 | stable_sort(flags.begin(), flags.end(), | |
518 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
519 | return a->time() < b->time(); | |
520 | }); | |
521 | ||
522 | return flags; | |
523 | } | |
524 | ||
525 | const QPoint& View::hover_point() const | |
526 | { | |
527 | return hover_point_; | |
528 | } | |
529 | ||
530 | void View::restack_all_trace_tree_items() | |
531 | { | |
532 | // Make a list of owners that is sorted from deepest first | |
533 | const vector<shared_ptr<TraceTreeItem>> items( | |
534 | list_by_type<TraceTreeItem>()); | |
535 | set< TraceTreeItemOwner* > owners; | |
536 | for (const auto &r : items) | |
537 | owners.insert(r->owner()); | |
538 | vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
539 | sort(sorted_owners.begin(), sorted_owners.end(), | |
540 | [](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) { | |
541 | return a->depth() > b->depth(); }); | |
542 | ||
543 | // Restack the items recursively | |
544 | for (auto &o : sorted_owners) | |
545 | o->restack_items(); | |
546 | ||
547 | // Re-assign background colors | |
548 | bool next_bgcolour_state = 0; | |
549 | ||
550 | for (auto &o : sorted_owners) | |
551 | next_bgcolour_state = o->reassign_bgcolour_states(next_bgcolour_state); | |
552 | ||
553 | // Animate the items to their destination | |
554 | for (const auto &i : items) | |
555 | i->animate_to_layout_v_offset(); | |
556 | } | |
557 | ||
558 | void View::trigger_event(util::Timestamp location) | |
559 | { | |
560 | trigger_markers_.push_back(shared_ptr<TriggerMarker>( | |
561 | new TriggerMarker(*this, location))); | |
562 | } | |
563 | ||
564 | void View::get_scroll_layout(double &length, Timestamp &offset) const | |
565 | { | |
566 | const pair<Timestamp, Timestamp> extents = get_time_extents(); | |
567 | length = ((extents.second - extents.first) / scale_).convert_to<double>(); | |
568 | offset = offset_ / scale_; | |
569 | } | |
570 | ||
571 | void View::set_zoom(double scale, int offset) | |
572 | { | |
573 | // Reset the "always zoom to fit" feature as the user changed the zoom | |
574 | always_zoom_to_fit_ = false; | |
575 | always_zoom_to_fit_changed(false); | |
576 | ||
577 | const Timestamp cursor_offset = offset_ + scale_ * offset; | |
578 | const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale); | |
579 | const Timestamp new_offset = cursor_offset - new_scale * offset; | |
580 | set_scale_offset(new_scale.convert_to<double>(), new_offset); | |
581 | } | |
582 | ||
583 | void View::calculate_tick_spacing() | |
584 | { | |
585 | const double SpacingIncrement = 10.0f; | |
586 | const double MinValueSpacing = 40.0f; | |
587 | ||
588 | // Figure out the highest numeric value visible on a label | |
589 | const QSize areaSize = viewport_->size(); | |
590 | const Timestamp max_time = max(fabs(offset_), | |
591 | fabs(offset_ + scale_ * areaSize.width())); | |
592 | ||
593 | double min_width = SpacingIncrement; | |
594 | double label_width, tick_period_width; | |
595 | ||
596 | QFontMetrics m(QApplication::font()); | |
597 | ||
598 | // Copies of the member variables with the same name, used in the calculation | |
599 | // and written back afterwards, so that we don't emit signals all the time | |
600 | // during the calculation. | |
601 | pv::util::Timestamp tick_period = tick_period_; | |
602 | pv::util::SIPrefix tick_prefix = tick_prefix_; | |
603 | unsigned tick_precision = tick_precision_; | |
604 | ||
605 | do { | |
606 | const double min_period = scale_ * min_width; | |
607 | ||
608 | const int order = (int)floorf(log10f(min_period)); | |
609 | const pv::util::Timestamp order_decimal = | |
610 | pow(pv::util::Timestamp(10), order); | |
611 | ||
612 | // Allow for a margin of error so that a scale unit of 1 can be used. | |
613 | // Otherwise, for a SU of 1 the tick period will almost always be below | |
614 | // the min_period by a small amount - and thus skipped in favor of 2. | |
615 | // Note: margin assumes that SU[0] and SU[1] contain the smallest values | |
616 | double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0; | |
617 | double tp_with_margin; | |
618 | unsigned int unit = 0; | |
619 | ||
620 | do { | |
621 | tp_with_margin = order_decimal.convert_to<double>() * | |
622 | (ScaleUnits[unit++] + tp_margin); | |
623 | } while (tp_with_margin < min_period && unit < countof(ScaleUnits)); | |
624 | ||
625 | tick_period = order_decimal * ScaleUnits[unit - 1]; | |
626 | tick_prefix = static_cast<pv::util::SIPrefix>( | |
627 | (order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3); | |
628 | ||
629 | // Precision is the number of fractional digits required, not | |
630 | // taking the prefix into account (and it must never be negative) | |
631 | tick_precision = std::max(ceil(log10(1 / tick_period)).convert_to<int>(), 0); | |
632 | ||
633 | tick_period_width = (tick_period / scale_).convert_to<double>(); | |
634 | ||
635 | const QString label_text = Ruler::format_time_with_distance( | |
636 | tick_period, max_time, tick_prefix, time_unit_, tick_precision); | |
637 | ||
638 | label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
639 | Qt::AlignLeft | Qt::AlignTop, label_text).width() + | |
640 | MinValueSpacing; | |
641 | ||
642 | min_width += SpacingIncrement; | |
643 | } while (tick_period_width < label_width); | |
644 | ||
645 | set_tick_period(tick_period); | |
646 | set_tick_prefix(tick_prefix); | |
647 | set_tick_precision(tick_precision); | |
648 | } | |
649 | ||
650 | void View::update_scroll() | |
651 | { | |
652 | assert(viewport_); | |
653 | ||
654 | const QSize areaSize = viewport_->size(); | |
655 | ||
656 | // Set the horizontal scroll bar | |
657 | double length = 0; | |
658 | Timestamp offset; | |
659 | get_scroll_layout(length, offset); | |
660 | length = max(length - areaSize.width(), 0.0); | |
661 | ||
662 | int major_tick_distance = (tick_period_ / scale_).convert_to<int>(); | |
663 | ||
664 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); | |
665 | horizontalScrollBar()->setSingleStep(major_tick_distance); | |
666 | ||
667 | updating_scroll_ = true; | |
668 | ||
669 | if (length < MaxScrollValue) { | |
670 | horizontalScrollBar()->setRange(0, length); | |
671 | horizontalScrollBar()->setSliderPosition(offset.convert_to<double>()); | |
672 | } else { | |
673 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
674 | horizontalScrollBar()->setSliderPosition( | |
675 | (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>()); | |
676 | } | |
677 | ||
678 | updating_scroll_ = false; | |
679 | ||
680 | // Set the vertical scrollbar | |
681 | verticalScrollBar()->setPageStep(areaSize.height()); | |
682 | verticalScrollBar()->setSingleStep(areaSize.height() / 8); | |
683 | ||
684 | const pair<int, int> extents = v_extents(); | |
685 | ||
686 | // Don't change the scrollbar range if there are no traces | |
687 | if (extents.first != extents.second) | |
688 | verticalScrollBar()->setRange(extents.first - areaSize.height(), | |
689 | extents.second); | |
690 | ||
691 | if (scroll_needs_defaults) | |
692 | set_scroll_default(); | |
693 | } | |
694 | ||
695 | void View::reset_scroll() | |
696 | { | |
697 | verticalScrollBar()->setRange(0, 0); | |
698 | } | |
699 | ||
700 | void View::set_scroll_default() | |
701 | { | |
702 | assert(viewport_); | |
703 | ||
704 | const QSize areaSize = viewport_->size(); | |
705 | ||
706 | // Special case: when starting up and the window isn't visible yet, | |
707 | // areaSize is [0, 0]. In this case we want to be called again later | |
708 | if (areaSize.height() == 0) { | |
709 | scroll_needs_defaults = true; | |
710 | return; | |
711 | } else { | |
712 | scroll_needs_defaults = false; | |
713 | } | |
714 | ||
715 | const pair<int, int> extents = v_extents(); | |
716 | const int trace_height = extents.second - extents.first; | |
717 | ||
718 | // Do all traces fit in the view? | |
719 | if (areaSize.height() >= trace_height) | |
720 | // Center all traces vertically | |
721 | set_v_offset(extents.first - | |
722 | ((areaSize.height() - trace_height) / 2)); | |
723 | else | |
724 | // Put the first trace at the top, letting the bottom ones overflow | |
725 | set_v_offset(extents.first); | |
726 | } | |
727 | ||
728 | void View::update_layout() | |
729 | { | |
730 | setViewportMargins( | |
731 | header_->sizeHint().width() - pv::view::Header::BaselineOffset, | |
732 | ruler_->sizeHint().height(), 0, 0); | |
733 | ruler_->setGeometry(viewport_->x(), 0, | |
734 | viewport_->width(), ruler_->extended_size_hint().height()); | |
735 | header_->setGeometry(0, viewport_->y(), | |
736 | header_->extended_size_hint().width(), viewport_->height()); | |
737 | update_scroll(); | |
738 | } | |
739 | ||
740 | TraceTreeItemOwner* View::find_prevalent_trace_group( | |
741 | const shared_ptr<sigrok::ChannelGroup> &group, | |
742 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
743 | &signal_map) | |
744 | { | |
745 | assert(group); | |
746 | ||
747 | unordered_set<TraceTreeItemOwner*> owners; | |
748 | vector<TraceTreeItemOwner*> owner_list; | |
749 | ||
750 | // Make a set and a list of all the owners | |
751 | for (const auto &channel : group->channels()) { | |
752 | const auto iter = signal_map.find(channel); | |
753 | if (iter == signal_map.end()) | |
754 | continue; | |
755 | ||
756 | TraceTreeItemOwner *const o = (*iter).second->owner(); | |
757 | owner_list.push_back(o); | |
758 | owners.insert(o); | |
759 | } | |
760 | ||
761 | // Iterate through the list of owners, and find the most prevalent | |
762 | size_t max_prevalence = 0; | |
763 | TraceTreeItemOwner *prevalent_owner = nullptr; | |
764 | for (TraceTreeItemOwner *owner : owners) { | |
765 | const size_t prevalence = std::count_if( | |
766 | owner_list.begin(), owner_list.end(), | |
767 | [&](TraceTreeItemOwner *o) { return o == owner; }); | |
768 | if (prevalence > max_prevalence) { | |
769 | max_prevalence = prevalence; | |
770 | prevalent_owner = owner; | |
771 | } | |
772 | } | |
773 | ||
774 | return prevalent_owner; | |
775 | } | |
776 | ||
777 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( | |
778 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
779 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
780 | &signal_map, | |
781 | set< shared_ptr<Trace> > &add_list) | |
782 | { | |
783 | vector< shared_ptr<Trace> > filtered_traces; | |
784 | ||
785 | for (const auto &channel : channels) { | |
786 | const auto map_iter = signal_map.find(channel); | |
787 | if (map_iter == signal_map.end()) | |
788 | continue; | |
789 | ||
790 | shared_ptr<Trace> trace = (*map_iter).second; | |
791 | const auto list_iter = add_list.find(trace); | |
792 | if (list_iter == add_list.end()) | |
793 | continue; | |
794 | ||
795 | filtered_traces.push_back(trace); | |
796 | add_list.erase(list_iter); | |
797 | } | |
798 | ||
799 | return filtered_traces; | |
800 | } | |
801 | ||
802 | void View::determine_time_unit() | |
803 | { | |
804 | // Check whether we know the sample rate and hence can use time as the unit | |
805 | if (time_unit_ == util::TimeUnit::Samples) { | |
806 | const unordered_set< shared_ptr<Signal> > sigs(session().signals()); | |
807 | ||
808 | // Check all signals but... | |
809 | for (const shared_ptr<Signal> signal : sigs) { | |
810 | const shared_ptr<SignalData> data = signal->data(); | |
811 | ||
812 | // ...only check first segment of each | |
813 | const vector< shared_ptr<Segment> > segments = data->segments(); | |
814 | if (!segments.empty()) | |
815 | if (segments[0]->samplerate()) { | |
816 | set_time_unit(util::TimeUnit::Time); | |
817 | break; | |
818 | } | |
819 | } | |
820 | } | |
821 | } | |
822 | ||
823 | bool View::eventFilter(QObject *object, QEvent *event) | |
824 | { | |
825 | const QEvent::Type type = event->type(); | |
826 | if (type == QEvent::MouseMove) { | |
827 | ||
828 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
829 | if (object == viewport_) | |
830 | hover_point_ = mouse_event->pos(); | |
831 | else if (object == ruler_) | |
832 | hover_point_ = QPoint(mouse_event->x(), 0); | |
833 | else if (object == header_) | |
834 | hover_point_ = QPoint(0, mouse_event->y()); | |
835 | else | |
836 | hover_point_ = QPoint(-1, -1); | |
837 | ||
838 | hover_point_changed(); | |
839 | ||
840 | } else if (type == QEvent::Leave) { | |
841 | hover_point_ = QPoint(-1, -1); | |
842 | hover_point_changed(); | |
843 | } | |
844 | ||
845 | return QObject::eventFilter(object, event); | |
846 | } | |
847 | ||
848 | bool View::viewportEvent(QEvent *event) | |
849 | { | |
850 | switch (event->type()) { | |
851 | case QEvent::Paint: | |
852 | case QEvent::MouseButtonPress: | |
853 | case QEvent::MouseButtonRelease: | |
854 | case QEvent::MouseButtonDblClick: | |
855 | case QEvent::MouseMove: | |
856 | case QEvent::Wheel: | |
857 | case QEvent::TouchBegin: | |
858 | case QEvent::TouchUpdate: | |
859 | case QEvent::TouchEnd: | |
860 | return false; | |
861 | default: | |
862 | return QAbstractScrollArea::viewportEvent(event); | |
863 | } | |
864 | } | |
865 | ||
866 | void View::resizeEvent(QResizeEvent*) | |
867 | { | |
868 | update_layout(); | |
869 | } | |
870 | ||
871 | void View::row_item_appearance_changed(bool label, bool content) | |
872 | { | |
873 | if (label) | |
874 | header_->update(); | |
875 | if (content) | |
876 | viewport_->update(); | |
877 | } | |
878 | ||
879 | void View::time_item_appearance_changed(bool label, bool content) | |
880 | { | |
881 | if (label) | |
882 | ruler_->update(); | |
883 | if (content) | |
884 | viewport_->update(); | |
885 | } | |
886 | ||
887 | void View::extents_changed(bool horz, bool vert) | |
888 | { | |
889 | sticky_events_ |= | |
890 | (horz ? TraceTreeItemHExtentsChanged : 0) | | |
891 | (vert ? TraceTreeItemVExtentsChanged : 0); | |
892 | lazy_event_handler_.start(); | |
893 | } | |
894 | ||
895 | void View::h_scroll_value_changed(int value) | |
896 | { | |
897 | if (updating_scroll_) | |
898 | return; | |
899 | ||
900 | // Disable sticky scrolling when user moves the horizontal scroll bar | |
901 | // during a running acquisition | |
902 | if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) { | |
903 | sticky_scrolling_ = false; | |
904 | sticky_scrolling_changed(false); | |
905 | } | |
906 | ||
907 | const int range = horizontalScrollBar()->maximum(); | |
908 | if (range < MaxScrollValue) | |
909 | set_offset(scale_ * value); | |
910 | else { | |
911 | double length = 0; | |
912 | Timestamp offset; | |
913 | get_scroll_layout(length, offset); | |
914 | set_offset(scale_ * length * value / MaxScrollValue); | |
915 | } | |
916 | ||
917 | ruler_->update(); | |
918 | viewport_->update(); | |
919 | } | |
920 | ||
921 | void View::v_scroll_value_changed() | |
922 | { | |
923 | header_->update(); | |
924 | viewport_->update(); | |
925 | } | |
926 | ||
927 | void View::signals_changed() | |
928 | { | |
929 | using sigrok::Channel; | |
930 | ||
931 | vector< shared_ptr<Channel> > channels; | |
932 | shared_ptr<sigrok::Device> sr_dev; | |
933 | ||
934 | // Do we need to set the vertical scrollbar to its default position later? | |
935 | // We do if there are no traces, i.e. the scroll bar has no range set | |
936 | bool reset_scrollbar = | |
937 | verticalScrollBar()->minimum() == verticalScrollBar()->maximum(); | |
938 | ||
939 | if (!session_.device()) { | |
940 | reset_scroll(); | |
941 | } else { | |
942 | assert(sr_dev); | |
943 | sr_dev = session_.device()->device(); | |
944 | channels = sr_dev->channels(); | |
945 | } | |
946 | ||
947 | vector< shared_ptr<TraceTreeItem> > new_top_level_items; | |
948 | ||
949 | // Make a list of traces that are being added, and a list of traces | |
950 | // that are being removed | |
951 | const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>(); | |
952 | const set<shared_ptr<Trace>> prev_traces( | |
953 | prev_trace_list.begin(), prev_trace_list.end()); | |
954 | ||
955 | const unordered_set< shared_ptr<Signal> > sigs(session_.signals()); | |
956 | ||
957 | set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end()); | |
958 | ||
959 | #ifdef ENABLE_DECODE | |
960 | const vector< shared_ptr<DecodeTrace> > decode_traces( | |
961 | session().get_decode_signals()); | |
962 | traces.insert(decode_traces.begin(), decode_traces.end()); | |
963 | #endif | |
964 | ||
965 | set< shared_ptr<Trace> > add_traces; | |
966 | set_difference(traces.begin(), traces.end(), | |
967 | prev_traces.begin(), prev_traces.end(), | |
968 | inserter(add_traces, add_traces.begin())); | |
969 | ||
970 | set< shared_ptr<Trace> > remove_traces; | |
971 | set_difference(prev_traces.begin(), prev_traces.end(), | |
972 | traces.begin(), traces.end(), | |
973 | inserter(remove_traces, remove_traces.begin())); | |
974 | ||
975 | // Make a look-up table of sigrok Channels to pulseview Signals | |
976 | unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
977 | signal_map; | |
978 | for (const shared_ptr<Signal> &sig : sigs) | |
979 | signal_map[sig->channel()] = sig; | |
980 | ||
981 | // Populate channel groups | |
982 | if (sr_dev) | |
983 | for (auto entry : sr_dev->channel_groups()) { | |
984 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
985 | ||
986 | if (group->channels().size() <= 1) | |
987 | continue; | |
988 | ||
989 | // Find best trace group to add to | |
990 | TraceTreeItemOwner *owner = find_prevalent_trace_group( | |
991 | group, signal_map); | |
992 | ||
993 | // If there is no trace group, create one | |
994 | shared_ptr<TraceGroup> new_trace_group; | |
995 | if (!owner) { | |
996 | new_trace_group.reset(new TraceGroup()); | |
997 | owner = new_trace_group.get(); | |
998 | } | |
999 | ||
1000 | // Extract traces for the trace group, removing them from | |
1001 | // the add list | |
1002 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
1003 | extract_new_traces_for_channels(group->channels(), | |
1004 | signal_map, add_traces); | |
1005 | ||
1006 | // Add the traces to the group | |
1007 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
1008 | int offset = prev_v_extents.second - prev_v_extents.first; | |
1009 | for (shared_ptr<Trace> trace : new_traces_in_group) { | |
1010 | assert(trace); | |
1011 | owner->add_child_item(trace); | |
1012 | ||
1013 | const pair<int, int> extents = trace->v_extents(); | |
1014 | if (trace->enabled()) | |
1015 | offset += -extents.first; | |
1016 | trace->force_to_v_offset(offset); | |
1017 | if (trace->enabled()) | |
1018 | offset += extents.second; | |
1019 | } | |
1020 | ||
1021 | if (new_trace_group) { | |
1022 | // Assign proper vertical offsets to each channel in the group | |
1023 | new_trace_group->restack_items(); | |
1024 | ||
1025 | // If this is a new group, enqueue it in the new top level | |
1026 | // items list | |
1027 | if (!new_traces_in_group.empty()) | |
1028 | new_top_level_items.push_back(new_trace_group); | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | // Enqueue the remaining logic channels in a group | |
1033 | vector< shared_ptr<Channel> > logic_channels; | |
1034 | copy_if(channels.begin(), channels.end(), back_inserter(logic_channels), | |
1035 | [](const shared_ptr<Channel>& c) { | |
1036 | return c->type() == sigrok::ChannelType::LOGIC; }); | |
1037 | ||
1038 | const vector< shared_ptr<Trace> > non_grouped_logic_signals = | |
1039 | extract_new_traces_for_channels(logic_channels, signal_map, add_traces); | |
1040 | ||
1041 | if (non_grouped_logic_signals.size() > 0) { | |
1042 | const shared_ptr<TraceGroup> non_grouped_trace_group( | |
1043 | make_shared<TraceGroup>()); | |
1044 | for (shared_ptr<Trace> trace : non_grouped_logic_signals) | |
1045 | non_grouped_trace_group->add_child_item(trace); | |
1046 | ||
1047 | non_grouped_trace_group->restack_items(); | |
1048 | new_top_level_items.push_back(non_grouped_trace_group); | |
1049 | } | |
1050 | ||
1051 | // Enqueue the remaining channels as free ungrouped traces | |
1052 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
1053 | extract_new_traces_for_channels(channels, signal_map, add_traces); | |
1054 | new_top_level_items.insert(new_top_level_items.end(), | |
1055 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
1056 | ||
1057 | // Enqueue any remaining traces i.e. decode traces | |
1058 | new_top_level_items.insert(new_top_level_items.end(), | |
1059 | add_traces.begin(), add_traces.end()); | |
1060 | ||
1061 | // Remove any removed traces | |
1062 | for (shared_ptr<Trace> trace : remove_traces) { | |
1063 | TraceTreeItemOwner *const owner = trace->owner(); | |
1064 | assert(owner); | |
1065 | owner->remove_child_item(trace); | |
1066 | } | |
1067 | ||
1068 | // Remove any empty trace groups | |
1069 | for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>()) | |
1070 | if (group->child_items().size() == 0) { | |
1071 | remove_child_item(group); | |
1072 | group.reset(); | |
1073 | } | |
1074 | ||
1075 | // Add and position the pending top levels items | |
1076 | for (auto item : new_top_level_items) { | |
1077 | add_child_item(item); | |
1078 | ||
1079 | // Position the item after the last item or at the top if there is none | |
1080 | int offset = v_extents().second; | |
1081 | const pair<int, int> extents = item->v_extents(); | |
1082 | ||
1083 | if (item->enabled()) | |
1084 | offset += -extents.first; | |
1085 | ||
1086 | item->force_to_v_offset(offset); | |
1087 | ||
1088 | if (item->enabled()) | |
1089 | offset += extents.second; | |
1090 | } | |
1091 | ||
1092 | update_layout(); | |
1093 | ||
1094 | header_->update(); | |
1095 | viewport_->update(); | |
1096 | ||
1097 | if (reset_scrollbar) | |
1098 | set_scroll_default(); | |
1099 | } | |
1100 | ||
1101 | void View::capture_state_updated(int state) | |
1102 | { | |
1103 | if (state == Session::Running) { | |
1104 | set_time_unit(util::TimeUnit::Samples); | |
1105 | ||
1106 | trigger_markers_.clear(); | |
1107 | } | |
1108 | ||
1109 | if (state == Session::Stopped) { | |
1110 | // After acquisition has stopped we need to re-calculate the ticks once | |
1111 | // as it's otherwise done when the user pans or zooms, which is too late | |
1112 | calculate_tick_spacing(); | |
1113 | ||
1114 | // Reset "always zoom to fit", the acquisition has stopped | |
1115 | if (always_zoom_to_fit_) { | |
1116 | always_zoom_to_fit_ = false; | |
1117 | always_zoom_to_fit_changed(false); | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | void View::data_updated() | |
1123 | { | |
1124 | if (always_zoom_to_fit_ || sticky_scrolling_) { | |
1125 | if (!delayed_view_updater_.isActive()) | |
1126 | delayed_view_updater_.start(); | |
1127 | } else { | |
1128 | determine_time_unit(); | |
1129 | update_scroll(); | |
1130 | ruler_->update(); | |
1131 | viewport_->update(); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | void View::perform_delayed_view_update() | |
1136 | { | |
1137 | if (always_zoom_to_fit_) | |
1138 | zoom_fit(true); | |
1139 | ||
1140 | if (sticky_scrolling_) { | |
1141 | // Make right side of the view sticky | |
1142 | double length = 0; | |
1143 | Timestamp offset; | |
1144 | get_scroll_layout(length, offset); | |
1145 | ||
1146 | const QSize areaSize = viewport_->size(); | |
1147 | length = max(length - areaSize.width(), 0.0); | |
1148 | ||
1149 | set_offset(scale_ * length); | |
1150 | } | |
1151 | ||
1152 | determine_time_unit(); | |
1153 | update_scroll(); | |
1154 | ruler_->update(); | |
1155 | viewport_->update(); | |
1156 | } | |
1157 | ||
1158 | void View::process_sticky_events() | |
1159 | { | |
1160 | if (sticky_events_ & TraceTreeItemHExtentsChanged) | |
1161 | update_layout(); | |
1162 | if (sticky_events_ & TraceTreeItemVExtentsChanged) { | |
1163 | restack_all_trace_tree_items(); | |
1164 | update_scroll(); | |
1165 | } | |
1166 | ||
1167 | // Clear the sticky events | |
1168 | sticky_events_ = 0; | |
1169 | } | |
1170 | ||
1171 | void View::on_hover_point_changed() | |
1172 | { | |
1173 | const vector<shared_ptr<TraceTreeItem>> trace_tree_items( | |
1174 | list_by_type<TraceTreeItem>()); | |
1175 | for (shared_ptr<TraceTreeItem> r : trace_tree_items) | |
1176 | r->hover_point_changed(); | |
1177 | } | |
1178 | ||
1179 | } // namespace view | |
1180 | } // namespace pv |