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