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