]>
Commit | Line | Data |
---|---|---|
adb4b10c | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
adb4b10c JH |
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 | ||
269528f5 | 21 | #ifdef ENABLE_DECODE |
4e5a4405 | 22 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 23 | #endif |
4e5a4405 | 24 | |
361c560e JH |
25 | #include <extdef.h> |
26 | ||
c3a740dd JH |
27 | #include <cassert> |
28 | #include <climits> | |
29 | #include <cmath> | |
30 | #include <mutex> | |
7ff0145f | 31 | #include <unordered_set> |
adb4b10c | 32 | |
361c560e | 33 | #include <QApplication> |
adb4b10c | 34 | #include <QEvent> |
361c560e | 35 | #include <QFontMetrics> |
cbd80f64 | 36 | #include <QMouseEvent> |
adb4b10c JH |
37 | #include <QScrollBar> |
38 | ||
448a72cf JH |
39 | #include <libsigrok/libsigrok.hpp> |
40 | ||
2acdb232 JH |
41 | #include "cursorheader.hpp" |
42 | #include "decodetrace.hpp" | |
43 | #include "header.hpp" | |
44 | #include "logicsignal.hpp" | |
45 | #include "ruler.hpp" | |
46 | #include "signal.hpp" | |
47 | #include "tracegroup.hpp" | |
48 | #include "view.hpp" | |
49 | #include "viewport.hpp" | |
adb4b10c | 50 | |
f65cd27b | 51 | #include "pv/session.hpp" |
2acdb232 JH |
52 | #include "pv/data/logic.hpp" |
53 | #include "pv/data/logicsnapshot.hpp" | |
361c560e | 54 | #include "pv/util.hpp" |
adb4b10c | 55 | |
aca64cac JH |
56 | using boost::shared_lock; |
57 | using boost::shared_mutex; | |
361c560e | 58 | |
1bc6525b | 59 | using pv::data::SignalData; |
7f4038d6 | 60 | using pv::data::Snapshot; |
361c560e JH |
61 | using pv::util::format_time; |
62 | ||
a2321722 | 63 | using std::back_inserter; |
819f4c25 | 64 | using std::deque; |
448a72cf | 65 | using std::dynamic_pointer_cast; |
819f4c25 | 66 | using std::list; |
c3a740dd | 67 | using std::lock_guard; |
819f4c25 | 68 | using std::max; |
1bc6525b | 69 | using std::make_pair; |
819f4c25 | 70 | using std::min; |
1bc6525b | 71 | using std::pair; |
819f4c25 | 72 | using std::set; |
f9abf97e | 73 | using std::shared_ptr; |
448a72cf | 74 | using std::unordered_map; |
7ff0145f | 75 | using std::unordered_set; |
819f4c25 | 76 | using std::vector; |
f9abf97e | 77 | using std::weak_ptr; |
adb4b10c | 78 | |
cdf7bea7 JH |
79 | namespace pv { |
80 | namespace view { | |
adb4b10c | 81 | |
cdf7bea7 JH |
82 | const double View::MaxScale = 1e9; |
83 | const double View::MinScale = 1e-15; | |
adb4b10c | 84 | |
f25770e2 JH |
85 | const int View::MaxScrollValue = INT_MAX / 2; |
86 | ||
361c560e JH |
87 | const int View::ScaleUnits[3] = {1, 2, 5}; |
88 | ||
f76af637 JH |
89 | const QColor View::CursorAreaColour(220, 231, 243); |
90 | ||
2e04f9bd JH |
91 | const QSizeF View::LabelPadding(4, 0); |
92 | ||
2b81ae46 | 93 | View::View(Session &session, QWidget *parent) : |
adb4b10c | 94 | QAbstractScrollArea(parent), |
8dbbc7f0 JH |
95 | session_(session), |
96 | viewport_(new Viewport(*this)), | |
97 | ruler_(new Ruler(*this)), | |
98 | cursorheader_(new CursorHeader(*this)), | |
99 | header_(new Header(*this)), | |
100 | scale_(1e-6), | |
101 | offset_(0), | |
102 | v_offset_(0), | |
103 | updating_scroll_(false), | |
361c560e JH |
104 | tick_period_(0.0), |
105 | tick_prefix_(0), | |
8dbbc7f0 JH |
106 | show_cursors_(false), |
107 | cursors_(*this), | |
108 | hover_point_(-1, -1) | |
adb4b10c | 109 | { |
b16907d3 JH |
110 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), |
111 | this, SLOT(h_scroll_value_changed(int))); | |
adb4b10c JH |
112 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), |
113 | this, SLOT(v_scroll_value_changed(int))); | |
69dd2b03 | 114 | |
8dbbc7f0 | 115 | connect(&session_, SIGNAL(signals_changed()), |
69dd2b03 | 116 | this, SLOT(signals_changed())); |
8dbbc7f0 | 117 | connect(&session_, SIGNAL(capture_state_changed(int)), |
50f97924 | 118 | this, SLOT(data_updated())); |
8dbbc7f0 | 119 | connect(&session_, SIGNAL(data_received()), |
1f374035 | 120 | this, SLOT(data_updated())); |
8dbbc7f0 | 121 | connect(&session_, SIGNAL(frame_ended()), |
adb4b10c | 122 | this, SLOT(data_updated())); |
1d8dca91 | 123 | |
8dbbc7f0 | 124 | connect(cursors_.first().get(), SIGNAL(time_changed()), |
ca4ec3ea | 125 | this, SLOT(marker_time_changed())); |
8dbbc7f0 | 126 | connect(cursors_.second().get(), SIGNAL(time_changed()), |
ca4ec3ea JH |
127 | this, SLOT(marker_time_changed())); |
128 | ||
8dbbc7f0 | 129 | connect(header_, SIGNAL(signals_moved()), |
07204819 | 130 | this, SLOT(on_signals_moved())); |
54401bbb | 131 | |
8dbbc7f0 JH |
132 | connect(header_, SIGNAL(selection_changed()), |
133 | cursorheader_, SLOT(clear_selection())); | |
134 | connect(cursorheader_, SIGNAL(selection_changed()), | |
135 | header_, SLOT(clear_selection())); | |
17348f85 | 136 | |
8dbbc7f0 | 137 | connect(header_, SIGNAL(selection_changed()), |
8b454527 | 138 | this, SIGNAL(selection_changed())); |
8dbbc7f0 | 139 | connect(cursorheader_, SIGNAL(selection_changed()), |
8b454527 JH |
140 | this, SIGNAL(selection_changed())); |
141 | ||
33c62f44 JH |
142 | connect(this, SIGNAL(hover_point_changed()), |
143 | this, SLOT(on_hover_point_changed())); | |
144 | ||
8dbbc7f0 | 145 | connect(&lazy_event_handler_, SIGNAL(timeout()), |
32218d3e | 146 | this, SLOT(process_sticky_events())); |
8dbbc7f0 | 147 | lazy_event_handler_.setSingleShot(true); |
32218d3e | 148 | |
8dbbc7f0 | 149 | setViewport(viewport_); |
cbd80f64 | 150 | |
8dbbc7f0 JH |
151 | viewport_->installEventFilter(this); |
152 | ruler_->installEventFilter(this); | |
153 | cursorheader_->installEventFilter(this); | |
154 | header_->installEventFilter(this); | |
d873f4d6 | 155 | |
9f46d905 JH |
156 | // Trigger the initial event manually. The default device has signals |
157 | // which were created before this object came into being | |
d873f4d6 | 158 | signals_changed(); |
84a0d458 | 159 | |
512bfc56 | 160 | // make sure the transparent widgets are on the top |
8dbbc7f0 JH |
161 | cursorheader_->raise(); |
162 | header_->raise(); | |
361c560e JH |
163 | |
164 | // Update the zoom state | |
165 | calculate_tick_spacing(); | |
adb4b10c JH |
166 | } |
167 | ||
2b81ae46 | 168 | Session& View::session() |
1d19ef83 | 169 | { |
8dbbc7f0 | 170 | return session_; |
1d19ef83 JH |
171 | } |
172 | ||
2b81ae46 | 173 | const Session& View::session() const |
38eeddea | 174 | { |
8dbbc7f0 | 175 | return session_; |
38eeddea JH |
176 | } |
177 | ||
eae6e30a JH |
178 | View* View::view() |
179 | { | |
180 | return this; | |
181 | } | |
182 | ||
183 | const View* View::view() const | |
184 | { | |
185 | return this; | |
186 | } | |
187 | ||
2ae445ba SA |
188 | Viewport* View::viewport() |
189 | { | |
8dbbc7f0 | 190 | return viewport_; |
2ae445ba SA |
191 | } |
192 | ||
193 | const Viewport* View::viewport() const | |
194 | { | |
8dbbc7f0 | 195 | return viewport_; |
2ae445ba SA |
196 | } |
197 | ||
cdf7bea7 | 198 | double View::scale() const |
adb4b10c | 199 | { |
8dbbc7f0 | 200 | return scale_; |
adb4b10c JH |
201 | } |
202 | ||
cdf7bea7 | 203 | double View::offset() const |
adb4b10c | 204 | { |
8dbbc7f0 | 205 | return offset_; |
adb4b10c JH |
206 | } |
207 | ||
7ff0145f | 208 | int View::owner_visual_v_offset() const |
adb4b10c | 209 | { |
8dbbc7f0 | 210 | return -v_offset_; |
adb4b10c JH |
211 | } |
212 | ||
3e769a37 JH |
213 | unsigned int View::depth() const |
214 | { | |
215 | return 0; | |
216 | } | |
217 | ||
361c560e JH |
218 | unsigned int View::tick_prefix() const |
219 | { | |
220 | return tick_prefix_; | |
221 | } | |
222 | ||
223 | double View::tick_period() const | |
224 | { | |
225 | return tick_period_; | |
226 | } | |
227 | ||
cdf7bea7 | 228 | void View::zoom(double steps) |
adb4b10c | 229 | { |
8dbbc7f0 | 230 | zoom(steps, viewport_->width() / 2); |
adb4b10c JH |
231 | } |
232 | ||
17c0f398 JH |
233 | void View::zoom(double steps, int offset) |
234 | { | |
8dbbc7f0 | 235 | set_zoom(scale_ * pow(3.0/2.0, -steps), offset); |
17c0f398 JH |
236 | } |
237 | ||
ca46b534 JH |
238 | void View::zoom_fit() |
239 | { | |
1bc6525b JH |
240 | const pair<double, double> extents = get_time_extents(); |
241 | const double delta = extents.second - extents.first; | |
242 | if (delta < 1e-12) | |
ca46b534 JH |
243 | return; |
244 | ||
8dbbc7f0 JH |
245 | assert(viewport_); |
246 | const int w = viewport_->width(); | |
ca46b534 JH |
247 | if (w <= 0) |
248 | return; | |
249 | ||
78311127 JH |
250 | const double scale = max(min(delta / w, MaxScale), MinScale); |
251 | set_scale_offset(scale, extents.first); | |
ca46b534 JH |
252 | } |
253 | ||
d1e7d82c JH |
254 | void View::zoom_one_to_one() |
255 | { | |
256 | using pv::data::SignalData; | |
257 | ||
d1e7d82c | 258 | // Make a set of all the visible data objects |
0fc664a9 | 259 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); |
d1e7d82c JH |
260 | if (visible_data.empty()) |
261 | return; | |
262 | ||
263 | double samplerate = 0.0; | |
d9aecf1f | 264 | for (const shared_ptr<SignalData> d : visible_data) { |
d1e7d82c JH |
265 | assert(d); |
266 | samplerate = max(samplerate, d->samplerate()); | |
267 | } | |
268 | ||
269 | if (samplerate == 0.0) | |
270 | return; | |
271 | ||
8dbbc7f0 JH |
272 | assert(viewport_); |
273 | const int w = viewport_->width(); | |
d1e7d82c JH |
274 | if (w <= 0) |
275 | return; | |
276 | ||
277 | set_zoom(1.0 / samplerate, w / 2); | |
278 | } | |
279 | ||
cdf7bea7 | 280 | void View::set_scale_offset(double scale, double offset) |
adb4b10c | 281 | { |
8dbbc7f0 JH |
282 | scale_ = scale; |
283 | offset_ = offset; | |
ccdd3ef5 | 284 | |
361c560e JH |
285 | calculate_tick_spacing(); |
286 | ||
adb4b10c | 287 | update_scroll(); |
8dbbc7f0 JH |
288 | ruler_->update(); |
289 | cursorheader_->update(); | |
290 | viewport_->update(); | |
e0fc5810 | 291 | scale_offset_changed(); |
adb4b10c JH |
292 | } |
293 | ||
1bc6525b JH |
294 | set< shared_ptr<SignalData> > View::get_visible_data() const |
295 | { | |
aca64cac | 296 | shared_lock<shared_mutex> lock(session().signals_mutex()); |
c3a740dd | 297 | const vector< shared_ptr<Signal> > &sigs(session().signals()); |
1bc6525b JH |
298 | |
299 | // Make a set of all the visible data objects | |
300 | set< shared_ptr<SignalData> > visible_data; | |
d9aecf1f | 301 | for (const shared_ptr<Signal> sig : sigs) |
1bc6525b JH |
302 | if (sig->enabled()) |
303 | visible_data.insert(sig->data()); | |
304 | ||
305 | return visible_data; | |
306 | } | |
307 | ||
308 | pair<double, double> View::get_time_extents() const | |
309 | { | |
1bc6525b | 310 | double left_time = DBL_MAX, right_time = DBL_MIN; |
7f4038d6 | 311 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); |
d9aecf1f | 312 | for (const shared_ptr<SignalData> d : visible_data) |
1bc6525b | 313 | { |
a472a884 JH |
314 | double samplerate = d->samplerate(); |
315 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
316 | ||
7f4038d6 JH |
317 | const vector< shared_ptr<Snapshot> > snapshots = |
318 | d->snapshots(); | |
319 | for (const shared_ptr<Snapshot> &s : snapshots) { | |
320 | const double start_time = s->start_time(); | |
321 | left_time = min(left_time, start_time); | |
322 | right_time = max(right_time, start_time + | |
323 | d->get_max_sample_count() / samplerate); | |
324 | } | |
1bc6525b JH |
325 | } |
326 | ||
7f4038d6 JH |
327 | if (left_time == DBL_MAX && right_time == DBL_MIN) |
328 | return make_pair(0.0, 0.0); | |
329 | ||
1bc6525b JH |
330 | assert(left_time < right_time); |
331 | return make_pair(left_time, right_time); | |
332 | } | |
333 | ||
f76af637 JH |
334 | bool View::cursors_shown() const |
335 | { | |
8dbbc7f0 | 336 | return show_cursors_; |
f76af637 JH |
337 | } |
338 | ||
339 | void View::show_cursors(bool show) | |
340 | { | |
8dbbc7f0 JH |
341 | show_cursors_ = show; |
342 | cursorheader_->update(); | |
343 | viewport_->update(); | |
f76af637 JH |
344 | } |
345 | ||
b4d91e56 JH |
346 | void View::centre_cursors() |
347 | { | |
8dbbc7f0 JH |
348 | const double time_width = scale_ * viewport_->width(); |
349 | cursors_.first()->set_time(offset_ + time_width * 0.4); | |
350 | cursors_.second()->set_time(offset_ + time_width * 0.6); | |
351 | cursorheader_->update(); | |
352 | viewport_->update(); | |
b4d91e56 JH |
353 | } |
354 | ||
b42d25c4 | 355 | CursorPair& View::cursors() |
f76af637 | 356 | { |
8dbbc7f0 | 357 | return cursors_; |
f76af637 JH |
358 | } |
359 | ||
58864c5c JH |
360 | const CursorPair& View::cursors() const |
361 | { | |
8dbbc7f0 | 362 | return cursors_; |
58864c5c JH |
363 | } |
364 | ||
cbd80f64 JH |
365 | const QPoint& View::hover_point() const |
366 | { | |
8dbbc7f0 | 367 | return hover_point_; |
cbd80f64 JH |
368 | } |
369 | ||
9cef9567 JH |
370 | void View::update_viewport() |
371 | { | |
8dbbc7f0 JH |
372 | assert(viewport_); |
373 | viewport_->update(); | |
374 | header_->update(); | |
7ff0145f JH |
375 | } |
376 | ||
377 | void View::restack_all_row_items() | |
378 | { | |
379 | // Make a set of owners | |
380 | unordered_set< RowItemOwner* > owners; | |
381 | for (const auto &r : *this) | |
382 | owners.insert(r->owner()); | |
383 | ||
384 | // Make a list that is sorted from deepest first | |
385 | vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end()); | |
386 | sort(sorted_owners.begin(), sorted_owners.end(), | |
387 | [](const RowItemOwner* a, const RowItemOwner *b) { | |
388 | return a->depth() > b->depth(); }); | |
389 | ||
390 | // Restack the items recursively | |
391 | for (auto &o : sorted_owners) | |
392 | o->restack_items(); | |
393 | ||
394 | // Animate the items to their destination | |
395 | for (const auto &r : *this) | |
396 | r->animate_to_layout_v_offset(); | |
9cef9567 JH |
397 | } |
398 | ||
f25770e2 JH |
399 | void View::get_scroll_layout(double &length, double &offset) const |
400 | { | |
1bc6525b | 401 | const pair<double, double> extents = get_time_extents(); |
8dbbc7f0 JH |
402 | length = (extents.second - extents.first) / scale_; |
403 | offset = offset_ / scale_; | |
f25770e2 JH |
404 | } |
405 | ||
d1e7d82c JH |
406 | void View::set_zoom(double scale, int offset) |
407 | { | |
8dbbc7f0 | 408 | const double cursor_offset = offset_ + scale_ * offset; |
d1e7d82c JH |
409 | const double new_scale = max(min(scale, MaxScale), MinScale); |
410 | const double new_offset = cursor_offset - new_scale * offset; | |
411 | set_scale_offset(new_scale, new_offset); | |
412 | } | |
413 | ||
361c560e JH |
414 | void View::calculate_tick_spacing() |
415 | { | |
416 | const double SpacingIncrement = 32.0f; | |
417 | const double MinValueSpacing = 32.0f; | |
418 | ||
419 | double min_width = SpacingIncrement, typical_width; | |
420 | ||
421 | QFontMetrics m(QApplication::font()); | |
422 | ||
423 | do { | |
424 | const double min_period = scale_ * min_width; | |
425 | ||
426 | const int order = (int)floorf(log10f(min_period)); | |
427 | const double order_decimal = pow(10.0, order); | |
428 | ||
429 | unsigned int unit = 0; | |
430 | ||
431 | do { | |
432 | tick_period_ = order_decimal * ScaleUnits[unit++]; | |
433 | } while (tick_period_ < min_period && | |
434 | unit < countof(ScaleUnits)); | |
435 | ||
436 | tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3; | |
437 | ||
438 | typical_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
439 | Qt::AlignLeft | Qt::AlignTop, | |
440 | format_time(offset_, tick_prefix_)).width() + | |
441 | MinValueSpacing; | |
442 | ||
443 | min_width += SpacingIncrement; | |
444 | ||
445 | } while(typical_width > tick_period_ / scale_); | |
446 | } | |
447 | ||
cdf7bea7 | 448 | void View::update_scroll() |
adb4b10c | 449 | { |
8dbbc7f0 | 450 | assert(viewport_); |
adb4b10c | 451 | |
8dbbc7f0 | 452 | const QSize areaSize = viewport_->size(); |
adb4b10c JH |
453 | |
454 | // Set the horizontal scroll bar | |
455 | double length = 0, offset = 0; | |
f25770e2 JH |
456 | get_scroll_layout(length, offset); |
457 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c | 458 | |
d7aae647 SA |
459 | int major_tick_distance = tick_period_ / scale_; |
460 | ||
b4ef7f2a | 461 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); |
d7aae647 | 462 | horizontalScrollBar()->setSingleStep(major_tick_distance); |
f25770e2 | 463 | |
8dbbc7f0 | 464 | updating_scroll_ = true; |
528bd8a1 | 465 | |
333d5bbc | 466 | if (length < MaxScrollValue) { |
f25770e2 JH |
467 | horizontalScrollBar()->setRange(0, length); |
468 | horizontalScrollBar()->setSliderPosition(offset); | |
469 | } else { | |
470 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
471 | horizontalScrollBar()->setSliderPosition( | |
8dbbc7f0 | 472 | offset_ * MaxScrollValue / (scale_ * length)); |
f25770e2 | 473 | } |
adb4b10c | 474 | |
8dbbc7f0 | 475 | updating_scroll_ = false; |
528bd8a1 | 476 | |
adb4b10c JH |
477 | // Set the vertical scrollbar |
478 | verticalScrollBar()->setPageStep(areaSize.height()); | |
a5d93c27 JH |
479 | |
480 | const pair<int, int> extents = v_extents(); | |
481 | const int extra_scroll_height = (extents.second - extents.first) / 4; | |
482 | verticalScrollBar()->setRange(extents.first - extra_scroll_height, | |
483 | extents.first + extra_scroll_height); | |
adb4b10c JH |
484 | } |
485 | ||
d7c0ca4a JH |
486 | void View::update_layout() |
487 | { | |
512bfc56 | 488 | setViewportMargins( |
8dbbc7f0 JH |
489 | header_->sizeHint().width() - pv::view::Header::BaselineOffset, |
490 | ruler_->sizeHint().height(), 0, 0); | |
491 | ruler_->setGeometry(viewport_->x(), 0, | |
492 | viewport_->width(), viewport_->y()); | |
493 | cursorheader_->setGeometry( | |
494 | viewport_->x(), | |
495 | ruler_->sizeHint().height() - cursorheader_->sizeHint().height() / 2, | |
496 | viewport_->width(), cursorheader_->sizeHint().height()); | |
497 | header_->setGeometry(0, viewport_->y(), | |
498 | header_->sizeHint().width(), viewport_->height()); | |
d7c0ca4a JH |
499 | update_scroll(); |
500 | } | |
501 | ||
eae6e30a JH |
502 | void View::paint_label(QPainter &p, int right, bool hover) |
503 | { | |
504 | (void)p; | |
505 | (void)right; | |
506 | (void)hover; | |
507 | } | |
508 | ||
509 | QRectF View::label_rect(int right) | |
510 | { | |
511 | (void)right; | |
512 | return QRectF(); | |
513 | } | |
514 | ||
448a72cf JH |
515 | bool View::add_channels_to_owner( |
516 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
517 | RowItemOwner *owner, int &offset, | |
518 | unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
519 | &signal_map, | |
520 | std::function<bool (shared_ptr<RowItem>)> filter_func) | |
521 | { | |
522 | bool any_added = false; | |
523 | ||
524 | assert(owner); | |
525 | ||
526 | for (const auto &channel : channels) | |
527 | { | |
528 | const auto iter = signal_map.find(channel); | |
529 | if (iter == signal_map.end() || | |
530 | (filter_func && !filter_func((*iter).second))) | |
531 | continue; | |
532 | ||
533 | shared_ptr<RowItem> row_item = (*iter).second; | |
534 | owner->add_child_item(row_item); | |
535 | apply_offset(row_item, offset); | |
536 | signal_map.erase(iter); | |
537 | ||
538 | any_added = true; | |
539 | } | |
540 | ||
541 | return any_added; | |
542 | } | |
543 | ||
544 | void View::apply_offset(shared_ptr<RowItem> row_item, int &offset) { | |
545 | assert(row_item); | |
546 | const pair<int, int> extents = row_item->v_extents(); | |
547 | if (row_item->enabled()) | |
548 | offset += -extents.first; | |
549 | row_item->force_to_v_offset(offset); | |
550 | if (row_item->enabled()) | |
551 | offset += extents.second; | |
552 | } | |
553 | ||
cbd80f64 JH |
554 | bool View::eventFilter(QObject *object, QEvent *event) |
555 | { | |
556 | const QEvent::Type type = event->type(); | |
333d5bbc | 557 | if (type == QEvent::MouseMove) { |
cbd80f64 JH |
558 | |
559 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
8dbbc7f0 JH |
560 | if (object == viewport_) |
561 | hover_point_ = mouse_event->pos(); | |
562 | else if (object == ruler_ || object == cursorheader_) | |
563 | hover_point_ = QPoint(mouse_event->x(), 0); | |
564 | else if (object == header_) | |
565 | hover_point_ = QPoint(0, mouse_event->y()); | |
cbd80f64 | 566 | else |
8dbbc7f0 | 567 | hover_point_ = QPoint(-1, -1); |
cbd80f64 JH |
568 | |
569 | hover_point_changed(); | |
570 | ||
333d5bbc | 571 | } else if (type == QEvent::Leave) { |
8dbbc7f0 | 572 | hover_point_ = QPoint(-1, -1); |
cbd80f64 JH |
573 | hover_point_changed(); |
574 | } | |
575 | ||
576 | return QObject::eventFilter(object, event); | |
577 | } | |
578 | ||
cdf7bea7 | 579 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
580 | { |
581 | switch(e->type()) { | |
582 | case QEvent::Paint: | |
583 | case QEvent::MouseButtonPress: | |
584 | case QEvent::MouseButtonRelease: | |
585 | case QEvent::MouseButtonDblClick: | |
586 | case QEvent::MouseMove: | |
587 | case QEvent::Wheel: | |
4b4f1c0d MC |
588 | case QEvent::TouchBegin: |
589 | case QEvent::TouchUpdate: | |
590 | case QEvent::TouchEnd: | |
adb4b10c JH |
591 | return false; |
592 | ||
593 | default: | |
594 | return QAbstractScrollArea::viewportEvent(e); | |
595 | } | |
596 | } | |
597 | ||
e314eca4 | 598 | void View::resizeEvent(QResizeEvent*) |
adb4b10c | 599 | { |
d7c0ca4a | 600 | update_layout(); |
adb4b10c JH |
601 | } |
602 | ||
32218d3e JH |
603 | void View::appearance_changed(bool label, bool content) |
604 | { | |
605 | if (label) | |
8dbbc7f0 | 606 | header_->update(); |
32218d3e | 607 | if (content) |
8dbbc7f0 | 608 | viewport_->update(); |
32218d3e JH |
609 | } |
610 | ||
611 | void View::extents_changed(bool horz, bool vert) | |
612 | { | |
8dbbc7f0 | 613 | sticky_events_ |= |
32218d3e JH |
614 | (horz ? SelectableItemHExtentsChanged : 0) | |
615 | (vert ? SelectableItemVExtentsChanged : 0); | |
8dbbc7f0 | 616 | lazy_event_handler_.start(); |
32218d3e JH |
617 | } |
618 | ||
b16907d3 | 619 | void View::h_scroll_value_changed(int value) |
adb4b10c | 620 | { |
8dbbc7f0 | 621 | if (updating_scroll_) |
528bd8a1 JH |
622 | return; |
623 | ||
f25770e2 | 624 | const int range = horizontalScrollBar()->maximum(); |
333d5bbc | 625 | if (range < MaxScrollValue) |
8dbbc7f0 | 626 | offset_ = scale_ * value; |
f25770e2 JH |
627 | else { |
628 | double length = 0, offset; | |
629 | get_scroll_layout(length, offset); | |
8dbbc7f0 | 630 | offset_ = scale_ * length * value / MaxScrollValue; |
f25770e2 JH |
631 | } |
632 | ||
8dbbc7f0 JH |
633 | ruler_->update(); |
634 | cursorheader_->update(); | |
635 | viewport_->update(); | |
adb4b10c JH |
636 | } |
637 | ||
cdf7bea7 | 638 | void View::v_scroll_value_changed(int value) |
adb4b10c | 639 | { |
8dbbc7f0 JH |
640 | v_offset_ = value; |
641 | header_->update(); | |
642 | viewport_->update(); | |
adb4b10c JH |
643 | } |
644 | ||
69dd2b03 JH |
645 | void View::signals_changed() |
646 | { | |
448a72cf JH |
647 | int offset = 0; |
648 | ||
68b21a71 JH |
649 | // Populate the traces |
650 | clear_child_items(); | |
651 | ||
8dbbc7f0 | 652 | shared_ptr<sigrok::Device> device = session_.device(); |
448a72cf JH |
653 | assert(device); |
654 | ||
655 | // Collect a set of signals | |
656 | unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
657 | signal_map; | |
658 | ||
8dbbc7f0 JH |
659 | shared_lock<shared_mutex> lock(session_.signals_mutex()); |
660 | const vector< shared_ptr<Signal> > &sigs(session_.signals()); | |
448a72cf JH |
661 | |
662 | for (const shared_ptr<Signal> &sig : sigs) | |
663 | signal_map[sig->channel()] = sig; | |
664 | ||
665 | // Populate channel groups | |
666 | for (auto entry : device->channel_groups()) | |
667 | { | |
668 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
669 | ||
670 | if (group->channels().size() <= 1) | |
671 | continue; | |
672 | ||
673 | shared_ptr<TraceGroup> trace_group(new TraceGroup()); | |
674 | int child_offset = 0; | |
675 | if (add_channels_to_owner(group->channels(), | |
676 | trace_group.get(), child_offset, signal_map)) | |
677 | { | |
678 | add_child_item(trace_group); | |
679 | apply_offset(trace_group, offset); | |
680 | } | |
681 | } | |
682 | ||
683 | // Add the remaining logic channels | |
684 | shared_ptr<TraceGroup> logic_trace_group(new TraceGroup()); | |
685 | int child_offset = 0; | |
686 | ||
687 | if (add_channels_to_owner(device->channels(), | |
688 | logic_trace_group.get(), child_offset, signal_map, | |
689 | [](shared_ptr<RowItem> r) -> bool { | |
690 | return dynamic_pointer_cast<LogicSignal>(r) != nullptr; | |
691 | })) | |
68b21a71 | 692 | |
448a72cf JH |
693 | { |
694 | add_child_item(logic_trace_group); | |
695 | apply_offset(logic_trace_group, offset); | |
696 | } | |
697 | ||
698 | // Add the remaining channels | |
699 | add_channels_to_owner(device->channels(), this, offset, signal_map); | |
700 | assert(signal_map.empty()); | |
701 | ||
702 | // Add decode signals | |
68b21a71 JH |
703 | #ifdef ENABLE_DECODE |
704 | const vector< shared_ptr<DecodeTrace> > decode_sigs( | |
705 | session().get_decode_signals()); | |
448a72cf | 706 | for (auto s : decode_sigs) { |
68b21a71 | 707 | add_child_item(s); |
448a72cf | 708 | apply_offset(s, offset); |
ef8311a4 | 709 | } |
448a72cf | 710 | #endif |
ef8311a4 | 711 | |
a6c1726e | 712 | update_layout(); |
69dd2b03 JH |
713 | } |
714 | ||
cdf7bea7 | 715 | void View::data_updated() |
adb4b10c | 716 | { |
adb4b10c JH |
717 | // Update the scroll bars |
718 | update_scroll(); | |
719 | ||
720 | // Repaint the view | |
8dbbc7f0 | 721 | viewport_->update(); |
adb4b10c | 722 | } |
cdf7bea7 | 723 | |
ca4ec3ea JH |
724 | void View::marker_time_changed() |
725 | { | |
8dbbc7f0 JH |
726 | cursorheader_->update(); |
727 | viewport_->update(); | |
ca4ec3ea JH |
728 | } |
729 | ||
07204819 | 730 | void View::on_signals_moved() |
54401bbb | 731 | { |
07204819 JH |
732 | update_scroll(); |
733 | signals_moved(); | |
54401bbb JH |
734 | } |
735 | ||
32218d3e | 736 | void View::process_sticky_events() |
d7c0ca4a | 737 | { |
8dbbc7f0 | 738 | if (sticky_events_ & SelectableItemHExtentsChanged) |
32218d3e | 739 | update_layout(); |
8dbbc7f0 | 740 | if (sticky_events_ & SelectableItemVExtentsChanged) |
7ff0145f | 741 | restack_all_row_items(); |
32218d3e JH |
742 | |
743 | // Clear the sticky events | |
8dbbc7f0 | 744 | sticky_events_ = 0; |
d7c0ca4a JH |
745 | } |
746 | ||
33c62f44 JH |
747 | void View::on_hover_point_changed() |
748 | { | |
aa59d5c2 | 749 | for (shared_ptr<RowItem> r : *this) |
eae6e30a | 750 | r->hover_point_changed(); |
33c62f44 JH |
751 | } |
752 | ||
cdf7bea7 JH |
753 | } // namespace view |
754 | } // namespace pv |