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