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