]>
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 | ||
65 | using std::deque; | |
66 | using std::dynamic_pointer_cast; | |
67 | using std::inserter; | |
68 | using std::list; | |
69 | using std::lock_guard; | |
70 | using std::max; | |
71 | using std::make_pair; | |
72 | using std::min; | |
73 | using std::pair; | |
74 | using std::set; | |
75 | using std::set_difference; | |
76 | using std::shared_ptr; | |
77 | using std::unordered_map; | |
78 | using std::unordered_set; | |
79 | using std::vector; | |
80 | using std::weak_ptr; | |
81 | ||
82 | namespace pv { | |
83 | namespace view { | |
84 | ||
85 | const double View::MaxScale = 1e9; | |
86 | const double View::MinScale = 1e-15; | |
87 | ||
88 | const int View::MaxScrollValue = INT_MAX / 2; | |
89 | ||
90 | const int View::ScaleUnits[3] = {1, 2, 5}; | |
91 | ||
92 | View::View(Session &session, QWidget *parent) : | |
93 | QAbstractScrollArea(parent), | |
94 | session_(session), | |
95 | viewport_(new Viewport(*this)), | |
96 | ruler_(new Ruler(*this)), | |
97 | header_(new Header(*this)), | |
98 | scale_(1e-3), | |
99 | offset_(0), | |
100 | updating_scroll_(false), | |
101 | tick_period_(0.0), | |
102 | tick_prefix_(0), | |
103 | show_cursors_(false), | |
104 | cursors_(new CursorPair(*this)), | |
105 | next_flag_text_('A'), | |
106 | hover_point_(-1, -1) | |
107 | { | |
108 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
109 | this, SLOT(h_scroll_value_changed(int))); | |
110 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | |
111 | this, SLOT(v_scroll_value_changed())); | |
112 | ||
113 | connect(&session_, SIGNAL(signals_changed()), | |
114 | this, SLOT(signals_changed())); | |
115 | connect(&session_, SIGNAL(capture_state_changed(int)), | |
116 | this, SLOT(data_updated())); | |
117 | connect(&session_, SIGNAL(data_received()), | |
118 | this, SLOT(data_updated())); | |
119 | connect(&session_, SIGNAL(frame_ended()), | |
120 | this, SLOT(data_updated())); | |
121 | ||
122 | connect(header_, SIGNAL(selection_changed()), | |
123 | ruler_, SLOT(clear_selection())); | |
124 | connect(ruler_, SIGNAL(selection_changed()), | |
125 | header_, SLOT(clear_selection())); | |
126 | ||
127 | connect(header_, SIGNAL(selection_changed()), | |
128 | this, SIGNAL(selection_changed())); | |
129 | connect(ruler_, SIGNAL(selection_changed()), | |
130 | this, SIGNAL(selection_changed())); | |
131 | ||
132 | connect(this, SIGNAL(hover_point_changed()), | |
133 | this, SLOT(on_hover_point_changed())); | |
134 | ||
135 | connect(&lazy_event_handler_, SIGNAL(timeout()), | |
136 | this, SLOT(process_sticky_events())); | |
137 | lazy_event_handler_.setSingleShot(true); | |
138 | ||
139 | setViewport(viewport_); | |
140 | ||
141 | viewport_->installEventFilter(this); | |
142 | ruler_->installEventFilter(this); | |
143 | header_->installEventFilter(this); | |
144 | ||
145 | // Trigger the initial event manually. The default device has signals | |
146 | // which were created before this object came into being | |
147 | signals_changed(); | |
148 | ||
149 | // make sure the transparent widgets are on the top | |
150 | ruler_->raise(); | |
151 | header_->raise(); | |
152 | ||
153 | // Update the zoom state | |
154 | calculate_tick_spacing(); | |
155 | } | |
156 | ||
157 | Session& View::session() | |
158 | { | |
159 | return session_; | |
160 | } | |
161 | ||
162 | const Session& View::session() const | |
163 | { | |
164 | return session_; | |
165 | } | |
166 | ||
167 | View* View::view() | |
168 | { | |
169 | return this; | |
170 | } | |
171 | ||
172 | const View* View::view() const | |
173 | { | |
174 | return this; | |
175 | } | |
176 | ||
177 | Viewport* View::viewport() | |
178 | { | |
179 | return viewport_; | |
180 | } | |
181 | ||
182 | const Viewport* View::viewport() const | |
183 | { | |
184 | return viewport_; | |
185 | } | |
186 | ||
187 | vector< shared_ptr<TimeItem> > View::time_items() const | |
188 | { | |
189 | const vector<shared_ptr<Flag>> f(flags()); | |
190 | vector<shared_ptr<TimeItem>> items(f.begin(), f.end()); | |
191 | items.push_back(cursors_); | |
192 | items.push_back(cursors_->first()); | |
193 | items.push_back(cursors_->second()); | |
194 | return items; | |
195 | } | |
196 | ||
197 | double View::scale() const | |
198 | { | |
199 | return scale_; | |
200 | } | |
201 | ||
202 | double View::offset() const | |
203 | { | |
204 | return offset_; | |
205 | } | |
206 | ||
207 | int View::owner_visual_v_offset() const | |
208 | { | |
209 | return -verticalScrollBar()->sliderPosition(); | |
210 | } | |
211 | ||
212 | void View::set_v_offset(int offset) | |
213 | { | |
214 | verticalScrollBar()->setSliderPosition(offset); | |
215 | header_->update(); | |
216 | viewport_->update(); | |
217 | } | |
218 | ||
219 | unsigned int View::depth() const | |
220 | { | |
221 | return 0; | |
222 | } | |
223 | ||
224 | unsigned int View::tick_prefix() const | |
225 | { | |
226 | return tick_prefix_; | |
227 | } | |
228 | ||
229 | double View::tick_period() const | |
230 | { | |
231 | return tick_period_; | |
232 | } | |
233 | ||
234 | void View::zoom(double steps) | |
235 | { | |
236 | zoom(steps, viewport_->width() / 2); | |
237 | } | |
238 | ||
239 | void View::zoom(double steps, int offset) | |
240 | { | |
241 | set_zoom(scale_ * pow(3.0/2.0, -steps), offset); | |
242 | } | |
243 | ||
244 | void View::zoom_fit() | |
245 | { | |
246 | const pair<double, double> extents = get_time_extents(); | |
247 | const double delta = extents.second - extents.first; | |
248 | if (delta < 1e-12) | |
249 | return; | |
250 | ||
251 | assert(viewport_); | |
252 | const int w = viewport_->width(); | |
253 | if (w <= 0) | |
254 | return; | |
255 | ||
256 | const double scale = max(min(delta / w, MaxScale), MinScale); | |
257 | set_scale_offset(scale, extents.first); | |
258 | } | |
259 | ||
260 | void View::zoom_one_to_one() | |
261 | { | |
262 | using pv::data::SignalData; | |
263 | ||
264 | // Make a set of all the visible data objects | |
265 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
266 | if (visible_data.empty()) | |
267 | return; | |
268 | ||
269 | double samplerate = 0.0; | |
270 | for (const shared_ptr<SignalData> d : visible_data) { | |
271 | assert(d); | |
272 | const vector< shared_ptr<Segment> > segments = | |
273 | d->segments(); | |
274 | for (const shared_ptr<Segment> &s : segments) | |
275 | samplerate = max(samplerate, s->samplerate()); | |
276 | } | |
277 | ||
278 | if (samplerate == 0.0) | |
279 | return; | |
280 | ||
281 | assert(viewport_); | |
282 | const int w = viewport_->width(); | |
283 | if (w <= 0) | |
284 | return; | |
285 | ||
286 | set_zoom(1.0 / samplerate, w / 2); | |
287 | } | |
288 | ||
289 | void View::set_scale_offset(double scale, double offset) | |
290 | { | |
291 | scale_ = scale; | |
292 | offset_ = offset; | |
293 | ||
294 | calculate_tick_spacing(); | |
295 | ||
296 | update_scroll(); | |
297 | ruler_->update(); | |
298 | viewport_->update(); | |
299 | scale_offset_changed(); | |
300 | } | |
301 | ||
302 | set< shared_ptr<SignalData> > View::get_visible_data() const | |
303 | { | |
304 | shared_lock<shared_mutex> lock(session().signals_mutex()); | |
305 | const unordered_set< shared_ptr<Signal> > &sigs(session().signals()); | |
306 | ||
307 | // Make a set of all the visible data objects | |
308 | set< shared_ptr<SignalData> > visible_data; | |
309 | for (const shared_ptr<Signal> sig : sigs) | |
310 | if (sig->enabled()) | |
311 | visible_data.insert(sig->data()); | |
312 | ||
313 | return visible_data; | |
314 | } | |
315 | ||
316 | pair<double, double> View::get_time_extents() const | |
317 | { | |
318 | double left_time = DBL_MAX, right_time = DBL_MIN; | |
319 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); | |
320 | for (const shared_ptr<SignalData> d : visible_data) | |
321 | { | |
322 | const vector< shared_ptr<Segment> > segments = | |
323 | d->segments(); | |
324 | for (const shared_ptr<Segment> &s : segments) { | |
325 | double samplerate = s->samplerate(); | |
326 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
327 | ||
328 | const double start_time = s->start_time(); | |
329 | left_time = min(left_time, start_time); | |
330 | right_time = max(right_time, start_time + | |
331 | d->max_sample_count() / samplerate); | |
332 | } | |
333 | } | |
334 | ||
335 | if (left_time == DBL_MAX && right_time == DBL_MIN) | |
336 | return make_pair(0.0, 0.0); | |
337 | ||
338 | assert(left_time < right_time); | |
339 | return make_pair(left_time, right_time); | |
340 | } | |
341 | ||
342 | bool View::cursors_shown() const | |
343 | { | |
344 | return show_cursors_; | |
345 | } | |
346 | ||
347 | void View::show_cursors(bool show) | |
348 | { | |
349 | show_cursors_ = show; | |
350 | ruler_->update(); | |
351 | viewport_->update(); | |
352 | } | |
353 | ||
354 | void View::centre_cursors() | |
355 | { | |
356 | const double time_width = scale_ * viewport_->width(); | |
357 | cursors_->first()->set_time(offset_ + time_width * 0.4); | |
358 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
359 | ruler_->update(); | |
360 | viewport_->update(); | |
361 | } | |
362 | ||
363 | std::shared_ptr<CursorPair> View::cursors() const | |
364 | { | |
365 | return cursors_; | |
366 | } | |
367 | ||
368 | void View::add_flag(double time) | |
369 | { | |
370 | flags_.push_back(shared_ptr<Flag>(new Flag(*this, time, | |
371 | QString("%1").arg(next_flag_text_)))); | |
372 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
373 | (next_flag_text_ + 1); | |
374 | time_item_appearance_changed(true, true); | |
375 | } | |
376 | ||
377 | void View::remove_flag(std::shared_ptr<Flag> flag) | |
378 | { | |
379 | flags_.remove(flag); | |
380 | time_item_appearance_changed(true, true); | |
381 | } | |
382 | ||
383 | vector< std::shared_ptr<Flag> > View::flags() const | |
384 | { | |
385 | vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
386 | stable_sort(flags.begin(), flags.end(), | |
387 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
388 | return a->time() < b->time(); | |
389 | }); | |
390 | ||
391 | return flags; | |
392 | } | |
393 | ||
394 | const QPoint& View::hover_point() const | |
395 | { | |
396 | return hover_point_; | |
397 | } | |
398 | ||
399 | void View::update_viewport() | |
400 | { | |
401 | assert(viewport_); | |
402 | viewport_->update(); | |
403 | header_->update(); | |
404 | } | |
405 | ||
406 | void View::restack_all_row_items() | |
407 | { | |
408 | // Make a list of owners that is sorted from deepest first | |
409 | const auto owners = list_row_item_owners(); | |
410 | vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
411 | sort(sorted_owners.begin(), sorted_owners.end(), | |
412 | [](const RowItemOwner* a, const RowItemOwner *b) { | |
413 | return a->depth() > b->depth(); }); | |
414 | ||
415 | // Restack the items recursively | |
416 | for (auto &o : sorted_owners) | |
417 | o->restack_items(); | |
418 | ||
419 | // Animate the items to their destination | |
420 | for (const auto &r : *this) | |
421 | r->animate_to_layout_v_offset(); | |
422 | } | |
423 | ||
424 | void View::get_scroll_layout(double &length, double &offset) const | |
425 | { | |
426 | const pair<double, double> extents = get_time_extents(); | |
427 | length = (extents.second - extents.first) / scale_; | |
428 | offset = offset_ / scale_; | |
429 | } | |
430 | ||
431 | void View::set_zoom(double scale, int offset) | |
432 | { | |
433 | const double cursor_offset = offset_ + scale_ * offset; | |
434 | const double new_scale = max(min(scale, MaxScale), MinScale); | |
435 | const double new_offset = cursor_offset - new_scale * offset; | |
436 | set_scale_offset(new_scale, new_offset); | |
437 | } | |
438 | ||
439 | void View::calculate_tick_spacing() | |
440 | { | |
441 | const double SpacingIncrement = 32.0f; | |
442 | const double MinValueSpacing = 32.0f; | |
443 | ||
444 | double min_width = SpacingIncrement, typical_width; | |
445 | ||
446 | QFontMetrics m(QApplication::font()); | |
447 | ||
448 | do { | |
449 | const double min_period = scale_ * min_width; | |
450 | ||
451 | const int order = (int)floorf(log10f(min_period)); | |
452 | const double order_decimal = pow(10.0, order); | |
453 | ||
454 | unsigned int unit = 0; | |
455 | ||
456 | do { | |
457 | tick_period_ = order_decimal * ScaleUnits[unit++]; | |
458 | } while (tick_period_ < min_period && | |
459 | unit < countof(ScaleUnits)); | |
460 | ||
461 | tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3; | |
462 | ||
463 | typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
464 | Qt::AlignLeft | Qt::AlignTop, | |
465 | format_time(offset_, tick_prefix_)).width() + | |
466 | MinValueSpacing; | |
467 | ||
468 | min_width += SpacingIncrement; | |
469 | ||
470 | } while(typical_width > tick_period_ / scale_); | |
471 | } | |
472 | ||
473 | void View::update_scroll() | |
474 | { | |
475 | assert(viewport_); | |
476 | ||
477 | const QSize areaSize = viewport_->size(); | |
478 | ||
479 | // Set the horizontal scroll bar | |
480 | double length = 0, offset = 0; | |
481 | get_scroll_layout(length, offset); | |
482 | length = max(length - areaSize.width(), 0.0); | |
483 | ||
484 | int major_tick_distance = tick_period_ / scale_; | |
485 | ||
486 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); | |
487 | horizontalScrollBar()->setSingleStep(major_tick_distance); | |
488 | ||
489 | updating_scroll_ = true; | |
490 | ||
491 | if (length < MaxScrollValue) { | |
492 | horizontalScrollBar()->setRange(0, length); | |
493 | horizontalScrollBar()->setSliderPosition(offset); | |
494 | } else { | |
495 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
496 | horizontalScrollBar()->setSliderPosition( | |
497 | offset_ * MaxScrollValue / (scale_ * length)); | |
498 | } | |
499 | ||
500 | updating_scroll_ = false; | |
501 | ||
502 | // Set the vertical scrollbar | |
503 | verticalScrollBar()->setPageStep(areaSize.height()); | |
504 | verticalScrollBar()->setSingleStep(areaSize.height() / 8); | |
505 | ||
506 | const pair<int, int> extents = v_extents(); | |
507 | verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2), | |
508 | extents.second - (areaSize.height() / 2)); | |
509 | } | |
510 | ||
511 | void View::update_layout() | |
512 | { | |
513 | setViewportMargins( | |
514 | header_->sizeHint().width() - pv::view::Header::BaselineOffset, | |
515 | ruler_->sizeHint().height(), 0, 0); | |
516 | ruler_->setGeometry(viewport_->x(), 0, | |
517 | viewport_->width(), ruler_->extended_size_hint().height()); | |
518 | header_->setGeometry(0, viewport_->y(), | |
519 | header_->extended_size_hint().width(), viewport_->height()); | |
520 | update_scroll(); | |
521 | } | |
522 | ||
523 | void View::paint_label(QPainter &p, const QRect &rect, bool hover) | |
524 | { | |
525 | (void)p; | |
526 | (void)rect; | |
527 | (void)hover; | |
528 | } | |
529 | ||
530 | QRectF View::label_rect(const QRectF &rect) | |
531 | { | |
532 | (void)rect; | |
533 | return QRectF(); | |
534 | } | |
535 | ||
536 | RowItemOwner* View::find_prevalent_trace_group( | |
537 | const shared_ptr<sigrok::ChannelGroup> &group, | |
538 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
539 | &signal_map) | |
540 | { | |
541 | assert(group); | |
542 | ||
543 | unordered_set<RowItemOwner*> owners; | |
544 | vector<RowItemOwner*> owner_list; | |
545 | ||
546 | // Make a set and a list of all the owners | |
547 | for (const auto &channel : group->channels()) { | |
548 | const auto iter = signal_map.find(channel); | |
549 | if (iter == signal_map.end()) | |
550 | continue; | |
551 | ||
552 | RowItemOwner *const o = (*iter).second->owner(); | |
553 | owner_list.push_back(o); | |
554 | owners.insert(o); | |
555 | } | |
556 | ||
557 | // Iterate through the list of owners, and find the most prevalent | |
558 | size_t max_prevalence = 0; | |
559 | RowItemOwner *prevalent_owner = nullptr; | |
560 | for (RowItemOwner *owner : owners) { | |
561 | const size_t prevalence = std::count_if( | |
562 | owner_list.begin(), owner_list.end(), | |
563 | [&](RowItemOwner *o) { return o == owner; }); | |
564 | if (prevalence > max_prevalence) { | |
565 | max_prevalence = prevalence; | |
566 | prevalent_owner = owner; | |
567 | } | |
568 | } | |
569 | ||
570 | return prevalent_owner; | |
571 | } | |
572 | ||
573 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( | |
574 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
575 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
576 | &signal_map, | |
577 | set< shared_ptr<Trace> > &add_list) | |
578 | { | |
579 | vector< shared_ptr<Trace> > filtered_traces; | |
580 | ||
581 | for (const auto &channel : channels) | |
582 | { | |
583 | const auto map_iter = signal_map.find(channel); | |
584 | if (map_iter == signal_map.end()) | |
585 | continue; | |
586 | ||
587 | shared_ptr<Trace> trace = (*map_iter).second; | |
588 | const auto list_iter = add_list.find(trace); | |
589 | if (list_iter == add_list.end()) | |
590 | continue; | |
591 | ||
592 | filtered_traces.push_back(trace); | |
593 | add_list.erase(list_iter); | |
594 | } | |
595 | ||
596 | return filtered_traces; | |
597 | } | |
598 | ||
599 | bool View::eventFilter(QObject *object, QEvent *event) | |
600 | { | |
601 | const QEvent::Type type = event->type(); | |
602 | if (type == QEvent::MouseMove) { | |
603 | ||
604 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
605 | if (object == viewport_) | |
606 | hover_point_ = mouse_event->pos(); | |
607 | else if (object == ruler_) | |
608 | hover_point_ = QPoint(mouse_event->x(), 0); | |
609 | else if (object == header_) | |
610 | hover_point_ = QPoint(0, mouse_event->y()); | |
611 | else | |
612 | hover_point_ = QPoint(-1, -1); | |
613 | ||
614 | hover_point_changed(); | |
615 | ||
616 | } else if (type == QEvent::Leave) { | |
617 | hover_point_ = QPoint(-1, -1); | |
618 | hover_point_changed(); | |
619 | } | |
620 | ||
621 | return QObject::eventFilter(object, event); | |
622 | } | |
623 | ||
624 | bool View::viewportEvent(QEvent *e) | |
625 | { | |
626 | switch(e->type()) { | |
627 | case QEvent::Paint: | |
628 | case QEvent::MouseButtonPress: | |
629 | case QEvent::MouseButtonRelease: | |
630 | case QEvent::MouseButtonDblClick: | |
631 | case QEvent::MouseMove: | |
632 | case QEvent::Wheel: | |
633 | case QEvent::TouchBegin: | |
634 | case QEvent::TouchUpdate: | |
635 | case QEvent::TouchEnd: | |
636 | return false; | |
637 | ||
638 | default: | |
639 | return QAbstractScrollArea::viewportEvent(e); | |
640 | } | |
641 | } | |
642 | ||
643 | void View::resizeEvent(QResizeEvent*) | |
644 | { | |
645 | update_layout(); | |
646 | } | |
647 | ||
648 | void View::row_item_appearance_changed(bool label, bool content) | |
649 | { | |
650 | if (label) | |
651 | header_->update(); | |
652 | if (content) | |
653 | viewport_->update(); | |
654 | } | |
655 | ||
656 | void View::time_item_appearance_changed(bool label, bool content) | |
657 | { | |
658 | if (label) | |
659 | ruler_->update(); | |
660 | if (content) | |
661 | viewport_->update(); | |
662 | } | |
663 | ||
664 | void View::extents_changed(bool horz, bool vert) | |
665 | { | |
666 | sticky_events_ |= | |
667 | (horz ? RowItemHExtentsChanged : 0) | | |
668 | (vert ? RowItemVExtentsChanged : 0); | |
669 | lazy_event_handler_.start(); | |
670 | } | |
671 | ||
672 | void View::h_scroll_value_changed(int value) | |
673 | { | |
674 | if (updating_scroll_) | |
675 | return; | |
676 | ||
677 | const int range = horizontalScrollBar()->maximum(); | |
678 | if (range < MaxScrollValue) | |
679 | offset_ = scale_ * value; | |
680 | else { | |
681 | double length = 0, offset; | |
682 | get_scroll_layout(length, offset); | |
683 | offset_ = scale_ * length * value / MaxScrollValue; | |
684 | } | |
685 | ||
686 | ruler_->update(); | |
687 | viewport_->update(); | |
688 | } | |
689 | ||
690 | void View::v_scroll_value_changed() | |
691 | { | |
692 | header_->update(); | |
693 | viewport_->update(); | |
694 | } | |
695 | ||
696 | void View::signals_changed() | |
697 | { | |
698 | vector< shared_ptr<RowItem> > new_top_level_items; | |
699 | ||
700 | const auto device = session_.device(); | |
701 | if (!device) | |
702 | return; | |
703 | ||
704 | shared_ptr<sigrok::Device> sr_dev = device->device(); | |
705 | assert(sr_dev); | |
706 | ||
707 | // Make a list of traces that are being added, and a list of traces | |
708 | // that are being removed | |
709 | const set<shared_ptr<Trace>> prev_traces = list_by_type<Trace>(); | |
710 | ||
711 | shared_lock<shared_mutex> lock(session_.signals_mutex()); | |
712 | const unordered_set< shared_ptr<Signal> > &sigs(session_.signals()); | |
713 | ||
714 | set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end()); | |
715 | ||
716 | #ifdef ENABLE_DECODE | |
717 | const vector< shared_ptr<DecodeTrace> > decode_traces( | |
718 | session().get_decode_signals()); | |
719 | traces.insert(decode_traces.begin(), decode_traces.end()); | |
720 | #endif | |
721 | ||
722 | set< shared_ptr<Trace> > add_traces; | |
723 | set_difference(traces.begin(), traces.end(), | |
724 | prev_traces.begin(), prev_traces.end(), | |
725 | inserter(add_traces, add_traces.begin())); | |
726 | ||
727 | set< shared_ptr<Trace> > remove_traces; | |
728 | set_difference(prev_traces.begin(), prev_traces.end(), | |
729 | traces.begin(), traces.end(), | |
730 | inserter(remove_traces, remove_traces.begin())); | |
731 | ||
732 | // Make a look-up table of sigrok Channels to pulseview Signals | |
733 | unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
734 | signal_map; | |
735 | for (const shared_ptr<Signal> &sig : sigs) | |
736 | signal_map[sig->channel()] = sig; | |
737 | ||
738 | // Populate channel groups | |
739 | for (auto entry : sr_dev->channel_groups()) | |
740 | { | |
741 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
742 | ||
743 | if (group->channels().size() <= 1) | |
744 | continue; | |
745 | ||
746 | // Find best trace group to add to | |
747 | RowItemOwner *owner = find_prevalent_trace_group( | |
748 | group, signal_map); | |
749 | ||
750 | // If there is no trace group, create one | |
751 | shared_ptr<TraceGroup> new_trace_group; | |
752 | if (!owner) { | |
753 | new_trace_group.reset(new TraceGroup()); | |
754 | owner = new_trace_group.get(); | |
755 | } | |
756 | ||
757 | // Extract traces for the trace group, removing them from | |
758 | // the add list | |
759 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
760 | extract_new_traces_for_channels(group->channels(), | |
761 | signal_map, add_traces); | |
762 | ||
763 | // Add the traces to the group | |
764 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
765 | int offset = prev_v_extents.second - prev_v_extents.first; | |
766 | for (shared_ptr<Trace> trace : new_traces_in_group) { | |
767 | assert(trace); | |
768 | owner->add_child_item(trace); | |
769 | ||
770 | const pair<int, int> extents = trace->v_extents(); | |
771 | if (trace->enabled()) | |
772 | offset += -extents.first; | |
773 | trace->force_to_v_offset(offset); | |
774 | if (trace->enabled()) | |
775 | offset += extents.second; | |
776 | } | |
777 | ||
778 | // If this is a new group, enqueue it in the new top level | |
779 | // items list | |
780 | if (!new_traces_in_group.empty() && new_trace_group) | |
781 | new_top_level_items.push_back(new_trace_group); | |
782 | } | |
783 | ||
784 | // Enqueue the remaining channels as free ungrouped traces | |
785 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
786 | extract_new_traces_for_channels(sr_dev->channels(), | |
787 | signal_map, add_traces); | |
788 | new_top_level_items.insert(new_top_level_items.end(), | |
789 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
790 | ||
791 | // Enqueue any remaining traces i.e. decode traces | |
792 | new_top_level_items.insert(new_top_level_items.end(), | |
793 | add_traces.begin(), add_traces.end()); | |
794 | ||
795 | // Remove any removed traces | |
796 | for (shared_ptr<Trace> trace : remove_traces) { | |
797 | RowItemOwner *const owner = trace->owner(); | |
798 | assert(owner); | |
799 | owner->remove_child_item(trace); | |
800 | } | |
801 | ||
802 | // Add and position the pending top levels items | |
803 | for (auto item : new_top_level_items) { | |
804 | add_child_item(item); | |
805 | ||
806 | // Position the item after the last present item | |
807 | int offset = v_extents().second; | |
808 | const pair<int, int> extents = item->v_extents(); | |
809 | if (item->enabled()) | |
810 | offset += -extents.first; | |
811 | item->force_to_v_offset(offset); | |
812 | if (item->enabled()) | |
813 | offset += extents.second; | |
814 | } | |
815 | ||
816 | update_layout(); | |
817 | ||
818 | header_->update(); | |
819 | viewport_->update(); | |
820 | } | |
821 | ||
822 | void View::data_updated() | |
823 | { | |
824 | // Update the scroll bars | |
825 | update_scroll(); | |
826 | ||
827 | // Repaint the view | |
828 | viewport_->update(); | |
829 | } | |
830 | ||
831 | void View::process_sticky_events() | |
832 | { | |
833 | if (sticky_events_ & RowItemHExtentsChanged) | |
834 | update_layout(); | |
835 | if (sticky_events_ & RowItemVExtentsChanged) { | |
836 | restack_all_row_items(); | |
837 | update_scroll(); | |
838 | } | |
839 | ||
840 | // Clear the sticky events | |
841 | sticky_events_ = 0; | |
842 | } | |
843 | ||
844 | void View::on_hover_point_changed() | |
845 | { | |
846 | for (shared_ptr<RowItem> r : *this) | |
847 | r->hover_point_changed(); | |
848 | } | |
849 | ||
850 | } // namespace view | |
851 | } // namespace pv |