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