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