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