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