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