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