]>
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 | ||
cf124e47 | 27 | #include <algorithm> |
c3a740dd JH |
28 | #include <cassert> |
29 | #include <climits> | |
30 | #include <cmath> | |
cf124e47 | 31 | #include <iterator> |
c3a740dd | 32 | #include <mutex> |
7ff0145f | 33 | #include <unordered_set> |
adb4b10c | 34 | |
361c560e | 35 | #include <QApplication> |
adb4b10c | 36 | #include <QEvent> |
361c560e | 37 | #include <QFontMetrics> |
cbd80f64 | 38 | #include <QMouseEvent> |
adb4b10c JH |
39 | #include <QScrollBar> |
40 | ||
fe3a1c21 | 41 | #include <libsigrokcxx/libsigrokcxx.hpp> |
448a72cf | 42 | |
2acdb232 JH |
43 | #include "decodetrace.hpp" |
44 | #include "header.hpp" | |
45 | #include "logicsignal.hpp" | |
46 | #include "ruler.hpp" | |
47 | #include "signal.hpp" | |
48 | #include "tracegroup.hpp" | |
49 | #include "view.hpp" | |
50 | #include "viewport.hpp" | |
adb4b10c | 51 | |
f65cd27b | 52 | #include "pv/session.hpp" |
da30ecb7 | 53 | #include "pv/devices/device.hpp" |
2acdb232 | 54 | #include "pv/data/logic.hpp" |
f3d66e52 | 55 | #include "pv/data/logicsegment.hpp" |
361c560e | 56 | #include "pv/util.hpp" |
adb4b10c | 57 | |
aca64cac JH |
58 | using boost::shared_lock; |
59 | using boost::shared_mutex; | |
361c560e | 60 | |
1bc6525b | 61 | using pv::data::SignalData; |
f3d66e52 | 62 | using pv::data::Segment; |
361c560e | 63 | using pv::util::format_time; |
ef454ad5 | 64 | using pv::util::TimeUnit; |
60d9b99a | 65 | using pv::util::Timestamp; |
361c560e | 66 | |
819f4c25 | 67 | using std::deque; |
448a72cf | 68 | using std::dynamic_pointer_cast; |
cf124e47 | 69 | using std::inserter; |
819f4c25 | 70 | using std::list; |
c3a740dd | 71 | using std::lock_guard; |
819f4c25 | 72 | using std::max; |
1bc6525b | 73 | using std::make_pair; |
819f4c25 | 74 | using std::min; |
1bc6525b | 75 | using std::pair; |
819f4c25 | 76 | using std::set; |
cf124e47 | 77 | using std::set_difference; |
f9abf97e | 78 | using std::shared_ptr; |
448a72cf | 79 | using std::unordered_map; |
7ff0145f | 80 | using std::unordered_set; |
819f4c25 | 81 | using std::vector; |
f9abf97e | 82 | using std::weak_ptr; |
adb4b10c | 83 | |
cdf7bea7 JH |
84 | namespace pv { |
85 | namespace view { | |
adb4b10c | 86 | |
60d9b99a JS |
87 | const Timestamp View::MaxScale("1e9"); |
88 | const Timestamp View::MinScale("1e-12"); | |
adb4b10c | 89 | |
f25770e2 | 90 | const int View::MaxScrollValue = INT_MAX / 2; |
ce11b2ea | 91 | const int View::MaxViewAutoUpdateRate = 25; // No more than 25 Hz with sticky scrolling |
f25770e2 | 92 | |
361c560e JH |
93 | const int View::ScaleUnits[3] = {1, 2, 5}; |
94 | ||
2b81ae46 | 95 | View::View(Session &session, QWidget *parent) : |
adb4b10c | 96 | QAbstractScrollArea(parent), |
8dbbc7f0 JH |
97 | session_(session), |
98 | viewport_(new Viewport(*this)), | |
99 | ruler_(new Ruler(*this)), | |
8dbbc7f0 | 100 | header_(new Header(*this)), |
c0100b55 | 101 | scale_(1e-3), |
8dbbc7f0 | 102 | offset_(0), |
8dbbc7f0 | 103 | updating_scroll_(false), |
c7b03d9d | 104 | sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui() |
ce11b2ea | 105 | always_zoom_to_fit_(false), |
361c560e JH |
106 | tick_period_(0.0), |
107 | tick_prefix_(0), | |
d40f4db7 | 108 | tick_precision_(0), |
ef454ad5 | 109 | time_unit_(util::Time), |
8dbbc7f0 | 110 | show_cursors_(false), |
5c5ce757 | 111 | cursors_(new CursorPair(*this)), |
8914fe79 | 112 | next_flag_text_('A'), |
8dbbc7f0 | 113 | hover_point_(-1, -1) |
adb4b10c | 114 | { |
b16907d3 JH |
115 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), |
116 | this, SLOT(h_scroll_value_changed(int))); | |
adb4b10c | 117 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), |
f8400017 | 118 | this, SLOT(v_scroll_value_changed())); |
69dd2b03 | 119 | |
8dbbc7f0 | 120 | connect(&session_, SIGNAL(signals_changed()), |
69dd2b03 | 121 | this, SLOT(signals_changed())); |
8dbbc7f0 | 122 | connect(&session_, SIGNAL(capture_state_changed(int)), |
7ee199a7 | 123 | this, SLOT(capture_state_updated(int))); |
8dbbc7f0 | 124 | connect(&session_, SIGNAL(data_received()), |
1f374035 | 125 | this, SLOT(data_updated())); |
8dbbc7f0 | 126 | connect(&session_, SIGNAL(frame_ended()), |
adb4b10c | 127 | this, SLOT(data_updated())); |
1d8dca91 | 128 | |
8dbbc7f0 | 129 | connect(header_, SIGNAL(selection_changed()), |
819e2e95 JH |
130 | ruler_, SLOT(clear_selection())); |
131 | connect(ruler_, SIGNAL(selection_changed()), | |
8dbbc7f0 | 132 | header_, SLOT(clear_selection())); |
17348f85 | 133 | |
8dbbc7f0 | 134 | connect(header_, SIGNAL(selection_changed()), |
8b454527 | 135 | this, SIGNAL(selection_changed())); |
819e2e95 | 136 | connect(ruler_, SIGNAL(selection_changed()), |
8b454527 JH |
137 | this, SIGNAL(selection_changed())); |
138 | ||
33c62f44 JH |
139 | connect(this, SIGNAL(hover_point_changed()), |
140 | this, SLOT(on_hover_point_changed())); | |
141 | ||
8dbbc7f0 | 142 | connect(&lazy_event_handler_, SIGNAL(timeout()), |
32218d3e | 143 | this, SLOT(process_sticky_events())); |
8dbbc7f0 | 144 | lazy_event_handler_.setSingleShot(true); |
32218d3e | 145 | |
c7b03d9d SA |
146 | connect(&delayed_view_updater_, SIGNAL(timeout()), |
147 | this, SLOT(perform_delayed_view_update())); | |
148 | delayed_view_updater_.setSingleShot(true); | |
149 | delayed_view_updater_.setInterval(1000 / MaxViewAutoUpdateRate); | |
150 | ||
8dbbc7f0 | 151 | setViewport(viewport_); |
cbd80f64 | 152 | |
8dbbc7f0 JH |
153 | viewport_->installEventFilter(this); |
154 | ruler_->installEventFilter(this); | |
8dbbc7f0 | 155 | header_->installEventFilter(this); |
d873f4d6 | 156 | |
9f46d905 JH |
157 | // Trigger the initial event manually. The default device has signals |
158 | // which were created before this object came into being | |
d873f4d6 | 159 | signals_changed(); |
84a0d458 | 160 | |
512bfc56 | 161 | // make sure the transparent widgets are on the top |
819e2e95 | 162 | ruler_->raise(); |
8dbbc7f0 | 163 | header_->raise(); |
361c560e JH |
164 | |
165 | // Update the zoom state | |
166 | calculate_tick_spacing(); | |
adb4b10c JH |
167 | } |
168 | ||
2b81ae46 | 169 | Session& View::session() |
1d19ef83 | 170 | { |
8dbbc7f0 | 171 | return session_; |
1d19ef83 JH |
172 | } |
173 | ||
2b81ae46 | 174 | const Session& View::session() const |
38eeddea | 175 | { |
8dbbc7f0 | 176 | return session_; |
38eeddea JH |
177 | } |
178 | ||
eae6e30a JH |
179 | View* View::view() |
180 | { | |
181 | return this; | |
182 | } | |
183 | ||
184 | const View* View::view() const | |
185 | { | |
186 | return this; | |
187 | } | |
188 | ||
2ae445ba SA |
189 | Viewport* View::viewport() |
190 | { | |
8dbbc7f0 | 191 | return viewport_; |
2ae445ba SA |
192 | } |
193 | ||
194 | const Viewport* View::viewport() const | |
195 | { | |
8dbbc7f0 | 196 | return viewport_; |
2ae445ba SA |
197 | } |
198 | ||
2496bf45 JH |
199 | vector< shared_ptr<TimeItem> > View::time_items() const |
200 | { | |
8914fe79 JH |
201 | const vector<shared_ptr<Flag>> f(flags()); |
202 | vector<shared_ptr<TimeItem>> items(f.begin(), f.end()); | |
5a3e53f6 | 203 | items.push_back(cursors_); |
5c5ce757 JH |
204 | items.push_back(cursors_->first()); |
205 | items.push_back(cursors_->second()); | |
2496bf45 JH |
206 | return items; |
207 | } | |
208 | ||
cdf7bea7 | 209 | double View::scale() const |
adb4b10c | 210 | { |
8dbbc7f0 | 211 | return scale_; |
adb4b10c JH |
212 | } |
213 | ||
60d9b99a | 214 | const Timestamp& View::offset() const |
adb4b10c | 215 | { |
8dbbc7f0 | 216 | return offset_; |
adb4b10c JH |
217 | } |
218 | ||
7ff0145f | 219 | int View::owner_visual_v_offset() const |
adb4b10c | 220 | { |
f8400017 | 221 | return -verticalScrollBar()->sliderPosition(); |
adb4b10c JH |
222 | } |
223 | ||
4d476647 JH |
224 | void View::set_v_offset(int offset) |
225 | { | |
226 | verticalScrollBar()->setSliderPosition(offset); | |
227 | header_->update(); | |
228 | viewport_->update(); | |
229 | } | |
230 | ||
3e769a37 JH |
231 | unsigned int View::depth() const |
232 | { | |
233 | return 0; | |
234 | } | |
235 | ||
361c560e JH |
236 | unsigned int View::tick_prefix() const |
237 | { | |
238 | return tick_prefix_; | |
239 | } | |
240 | ||
d40f4db7 SA |
241 | unsigned int View::tick_precision() const |
242 | { | |
243 | return tick_precision_; | |
244 | } | |
245 | ||
361c560e JH |
246 | double View::tick_period() const |
247 | { | |
248 | return tick_period_; | |
249 | } | |
250 | ||
ef454ad5 SA |
251 | TimeUnit View::time_unit() const |
252 | { | |
253 | return time_unit_; | |
254 | } | |
255 | ||
cdf7bea7 | 256 | void View::zoom(double steps) |
adb4b10c | 257 | { |
8dbbc7f0 | 258 | zoom(steps, viewport_->width() / 2); |
adb4b10c JH |
259 | } |
260 | ||
17c0f398 JH |
261 | void View::zoom(double steps, int offset) |
262 | { | |
8dbbc7f0 | 263 | set_zoom(scale_ * pow(3.0/2.0, -steps), offset); |
17c0f398 JH |
264 | } |
265 | ||
ce11b2ea | 266 | void View::zoom_fit(bool gui_state) |
ca46b534 | 267 | { |
ce11b2ea SA |
268 | // Act as one-shot when stopped, toggle along with the GUI otherwise |
269 | if (session_.get_capture_state() == Session::Stopped) { | |
270 | always_zoom_to_fit_ = false; | |
271 | always_zoom_to_fit_changed(false); | |
272 | } else { | |
273 | always_zoom_to_fit_ = gui_state; | |
274 | always_zoom_to_fit_changed(gui_state); | |
275 | } | |
276 | ||
60d9b99a JS |
277 | const pair<Timestamp, Timestamp> extents = get_time_extents(); |
278 | const Timestamp delta = extents.second - extents.first; | |
279 | if (delta < Timestamp("1e-12")) | |
ca46b534 JH |
280 | return; |
281 | ||
8dbbc7f0 JH |
282 | assert(viewport_); |
283 | const int w = viewport_->width(); | |
ca46b534 JH |
284 | if (w <= 0) |
285 | return; | |
286 | ||
60d9b99a JS |
287 | const Timestamp scale = max(min(delta / w, MaxScale), MinScale); |
288 | set_scale_offset(scale.convert_to<double>(), extents.first); | |
ca46b534 JH |
289 | } |
290 | ||
d1e7d82c JH |
291 | void View::zoom_one_to_one() |
292 | { | |
293 | using pv::data::SignalData; | |
294 | ||
d1e7d82c | 295 | // Make a set of all the visible data objects |
0fc664a9 | 296 | set< shared_ptr<SignalData> > visible_data = get_visible_data(); |
d1e7d82c JH |
297 | if (visible_data.empty()) |
298 | return; | |
299 | ||
300 | double samplerate = 0.0; | |
d9aecf1f | 301 | for (const shared_ptr<SignalData> d : visible_data) { |
d1e7d82c | 302 | assert(d); |
f3d66e52 JH |
303 | const vector< shared_ptr<Segment> > segments = |
304 | d->segments(); | |
305 | for (const shared_ptr<Segment> &s : segments) | |
ff008de6 | 306 | samplerate = max(samplerate, s->samplerate()); |
d1e7d82c JH |
307 | } |
308 | ||
309 | if (samplerate == 0.0) | |
310 | return; | |
311 | ||
8dbbc7f0 JH |
312 | assert(viewport_); |
313 | const int w = viewport_->width(); | |
d1e7d82c JH |
314 | if (w <= 0) |
315 | return; | |
316 | ||
317 | set_zoom(1.0 / samplerate, w / 2); | |
318 | } | |
319 | ||
60d9b99a | 320 | void View::set_scale_offset(double scale, const Timestamp& offset) |
adb4b10c | 321 | { |
ce11b2ea SA |
322 | // Disable sticky scrolling / always zoom to fit when acquisition runs |
323 | // and user drags the viewport | |
c7b03d9d | 324 | if ((scale_ == scale) && (offset_ != offset) && |
ce11b2ea SA |
325 | (session_.get_capture_state() == Session::Running)) { |
326 | ||
327 | if (sticky_scrolling_) { | |
328 | sticky_scrolling_ = false; | |
329 | sticky_scrolling_changed(false); | |
330 | } | |
331 | ||
332 | if (always_zoom_to_fit_) { | |
333 | always_zoom_to_fit_ = false; | |
334 | always_zoom_to_fit_changed(false); | |
335 | } | |
c7b03d9d SA |
336 | } |
337 | ||
8dbbc7f0 JH |
338 | scale_ = scale; |
339 | offset_ = offset; | |
ccdd3ef5 | 340 | |
361c560e JH |
341 | calculate_tick_spacing(); |
342 | ||
adb4b10c | 343 | update_scroll(); |
8dbbc7f0 | 344 | ruler_->update(); |
8dbbc7f0 | 345 | viewport_->update(); |
e0fc5810 | 346 | scale_offset_changed(); |
adb4b10c JH |
347 | } |
348 | ||
1bc6525b JH |
349 | set< shared_ptr<SignalData> > View::get_visible_data() const |
350 | { | |
aca64cac | 351 | shared_lock<shared_mutex> lock(session().signals_mutex()); |
78b0af3e | 352 | const unordered_set< shared_ptr<Signal> > &sigs(session().signals()); |
1bc6525b JH |
353 | |
354 | // Make a set of all the visible data objects | |
355 | set< shared_ptr<SignalData> > visible_data; | |
d9aecf1f | 356 | for (const shared_ptr<Signal> sig : sigs) |
1bc6525b JH |
357 | if (sig->enabled()) |
358 | visible_data.insert(sig->data()); | |
359 | ||
360 | return visible_data; | |
361 | } | |
362 | ||
60d9b99a | 363 | pair<Timestamp, Timestamp> View::get_time_extents() const |
1bc6525b | 364 | { |
60d9b99a | 365 | boost::optional<Timestamp> left_time, right_time; |
7f4038d6 | 366 | const set< shared_ptr<SignalData> > visible_data = get_visible_data(); |
d9aecf1f | 367 | for (const shared_ptr<SignalData> d : visible_data) |
1bc6525b | 368 | { |
f3d66e52 JH |
369 | const vector< shared_ptr<Segment> > segments = |
370 | d->segments(); | |
371 | for (const shared_ptr<Segment> &s : segments) { | |
ff008de6 JH |
372 | double samplerate = s->samplerate(); |
373 | samplerate = (samplerate <= 0.0) ? 1.0 : samplerate; | |
374 | ||
60d9b99a JS |
375 | const Timestamp start_time = s->start_time(); |
376 | left_time = left_time ? | |
377 | min(*left_time, start_time) : | |
378 | start_time; | |
379 | right_time = right_time ? | |
380 | max(*right_time, start_time + d->max_sample_count() / samplerate) : | |
381 | start_time + d->max_sample_count() / samplerate; | |
7f4038d6 | 382 | } |
1bc6525b JH |
383 | } |
384 | ||
60d9b99a JS |
385 | if (!left_time || !right_time) |
386 | return make_pair(0, 0); | |
7f4038d6 | 387 | |
60d9b99a JS |
388 | assert(*left_time < *right_time); |
389 | return make_pair(*left_time, *right_time); | |
1bc6525b JH |
390 | } |
391 | ||
c7b03d9d SA |
392 | void View::enable_sticky_scrolling(bool state) |
393 | { | |
394 | sticky_scrolling_ = state; | |
395 | } | |
396 | ||
f76af637 JH |
397 | bool View::cursors_shown() const |
398 | { | |
8dbbc7f0 | 399 | return show_cursors_; |
f76af637 JH |
400 | } |
401 | ||
402 | void View::show_cursors(bool show) | |
403 | { | |
8dbbc7f0 | 404 | show_cursors_ = show; |
819e2e95 | 405 | ruler_->update(); |
8dbbc7f0 | 406 | viewport_->update(); |
f76af637 JH |
407 | } |
408 | ||
b4d91e56 JH |
409 | void View::centre_cursors() |
410 | { | |
8dbbc7f0 | 411 | const double time_width = scale_ * viewport_->width(); |
5c5ce757 JH |
412 | cursors_->first()->set_time(offset_ + time_width * 0.4); |
413 | cursors_->second()->set_time(offset_ + time_width * 0.6); | |
819e2e95 | 414 | ruler_->update(); |
8dbbc7f0 | 415 | viewport_->update(); |
b4d91e56 JH |
416 | } |
417 | ||
5c5ce757 | 418 | std::shared_ptr<CursorPair> View::cursors() const |
58864c5c | 419 | { |
8dbbc7f0 | 420 | return cursors_; |
58864c5c JH |
421 | } |
422 | ||
60d9b99a | 423 | void View::add_flag(const Timestamp& time) |
8914fe79 JH |
424 | { |
425 | flags_.push_back(shared_ptr<Flag>(new Flag(*this, time, | |
426 | QString("%1").arg(next_flag_text_)))); | |
427 | next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' : | |
428 | (next_flag_text_ + 1); | |
429 | time_item_appearance_changed(true, true); | |
430 | } | |
431 | ||
432 | void View::remove_flag(std::shared_ptr<Flag> flag) | |
433 | { | |
434 | flags_.remove(flag); | |
435 | time_item_appearance_changed(true, true); | |
436 | } | |
437 | ||
438 | vector< std::shared_ptr<Flag> > View::flags() const | |
439 | { | |
440 | vector< std::shared_ptr<Flag> > flags(flags_.begin(), flags_.end()); | |
441 | stable_sort(flags.begin(), flags.end(), | |
442 | [](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) { | |
443 | return a->time() < b->time(); | |
444 | }); | |
445 | ||
446 | return flags; | |
447 | } | |
448 | ||
cbd80f64 JH |
449 | const QPoint& View::hover_point() const |
450 | { | |
8dbbc7f0 | 451 | return hover_point_; |
cbd80f64 JH |
452 | } |
453 | ||
9cef9567 JH |
454 | void View::update_viewport() |
455 | { | |
8dbbc7f0 JH |
456 | assert(viewport_); |
457 | viewport_->update(); | |
458 | header_->update(); | |
7ff0145f JH |
459 | } |
460 | ||
461 | void View::restack_all_row_items() | |
462 | { | |
873cbed0 JH |
463 | // Make a list of owners that is sorted from deepest first |
464 | const auto owners = list_row_item_owners(); | |
7ff0145f JH |
465 | vector< RowItemOwner* > sorted_owners(owners.begin(), owners.end()); |
466 | sort(sorted_owners.begin(), sorted_owners.end(), | |
467 | [](const RowItemOwner* a, const RowItemOwner *b) { | |
468 | return a->depth() > b->depth(); }); | |
469 | ||
470 | // Restack the items recursively | |
471 | for (auto &o : sorted_owners) | |
472 | o->restack_items(); | |
473 | ||
474 | // Animate the items to their destination | |
475 | for (const auto &r : *this) | |
476 | r->animate_to_layout_v_offset(); | |
9cef9567 JH |
477 | } |
478 | ||
60d9b99a | 479 | void View::get_scroll_layout(double &length, Timestamp &offset) const |
f25770e2 | 480 | { |
60d9b99a JS |
481 | const pair<Timestamp, Timestamp> extents = get_time_extents(); |
482 | length = ((extents.second - extents.first) / scale_).convert_to<double>(); | |
8dbbc7f0 | 483 | offset = offset_ / scale_; |
f25770e2 JH |
484 | } |
485 | ||
d1e7d82c JH |
486 | void View::set_zoom(double scale, int offset) |
487 | { | |
ce11b2ea SA |
488 | // Reset the "always zoom to fit" feature as the user changed the zoom |
489 | always_zoom_to_fit_ = false; | |
490 | always_zoom_to_fit_changed(false); | |
491 | ||
60d9b99a JS |
492 | const Timestamp cursor_offset = offset_ + scale_ * offset; |
493 | const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale); | |
494 | const Timestamp new_offset = cursor_offset - new_scale * offset; | |
495 | set_scale_offset(new_scale.convert_to<double>(), new_offset); | |
d1e7d82c JH |
496 | } |
497 | ||
361c560e JH |
498 | void View::calculate_tick_spacing() |
499 | { | |
8a1bc087 | 500 | const double SpacingIncrement = 10.0f; |
8a463dbc | 501 | const double MinValueSpacing = 40.0f; |
361c560e | 502 | |
1e124d4b SA |
503 | // Figure out the highest numeric value visible on a label |
504 | const QSize areaSize = viewport_->size(); | |
60d9b99a | 505 | const Timestamp max_time = max(fabs(offset_), |
1e124d4b SA |
506 | fabs(offset_ + scale_ * areaSize.width())); |
507 | ||
d40f4db7 SA |
508 | double min_width = SpacingIncrement; |
509 | double label_width, tick_period_width; | |
361c560e JH |
510 | |
511 | QFontMetrics m(QApplication::font()); | |
512 | ||
513 | do { | |
514 | const double min_period = scale_ * min_width; | |
515 | ||
516 | const int order = (int)floorf(log10f(min_period)); | |
517 | const double order_decimal = pow(10.0, order); | |
518 | ||
8a1bc087 SA |
519 | // Allow for a margin of error so that a scale unit of 1 can be used. |
520 | // Otherwise, for a SU of 1 the tick period will almost always be below | |
521 | // the min_period by a small amount - and thus skipped in favor of 2. | |
522 | // Note: margin assumes that SU[0] and SU[1] contain the smallest values | |
523 | double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0; | |
524 | double tp_with_margin; | |
361c560e JH |
525 | unsigned int unit = 0; |
526 | ||
527 | do { | |
8a1bc087 SA |
528 | tp_with_margin = order_decimal * (ScaleUnits[unit++] + tp_margin); |
529 | } while (tp_with_margin < min_period && unit < countof(ScaleUnits)); | |
361c560e | 530 | |
8a1bc087 | 531 | tick_period_ = order_decimal * ScaleUnits[unit - 1]; |
361c560e JH |
532 | tick_prefix_ = (order - pv::util::FirstSIPrefixPower) / 3; |
533 | ||
d40f4db7 SA |
534 | // Precision is the number of fractional digits required, not |
535 | // taking the prefix into account (and it must never be negative) | |
536 | tick_precision_ = std::max((int)ceil(log10f(1 / tick_period_)), 0); | |
537 | ||
538 | tick_period_width = tick_period_ / scale_; | |
539 | ||
540 | const QString label_text = | |
1e124d4b | 541 | format_time(max_time, tick_prefix_, time_unit_, tick_precision_); |
d40f4db7 SA |
542 | |
543 | label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX, | |
544 | Qt::AlignLeft | Qt::AlignTop, label_text).width() + | |
361c560e JH |
545 | MinValueSpacing; |
546 | ||
547 | min_width += SpacingIncrement; | |
548 | ||
d40f4db7 | 549 | } while (tick_period_width < label_width); |
361c560e JH |
550 | } |
551 | ||
cdf7bea7 | 552 | void View::update_scroll() |
adb4b10c | 553 | { |
8dbbc7f0 | 554 | assert(viewport_); |
adb4b10c | 555 | |
8dbbc7f0 | 556 | const QSize areaSize = viewport_->size(); |
adb4b10c JH |
557 | |
558 | // Set the horizontal scroll bar | |
60d9b99a JS |
559 | double length = 0; |
560 | Timestamp offset; | |
f25770e2 JH |
561 | get_scroll_layout(length, offset); |
562 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c | 563 | |
d7aae647 SA |
564 | int major_tick_distance = tick_period_ / scale_; |
565 | ||
b4ef7f2a | 566 | horizontalScrollBar()->setPageStep(areaSize.width() / 2); |
d7aae647 | 567 | horizontalScrollBar()->setSingleStep(major_tick_distance); |
f25770e2 | 568 | |
8dbbc7f0 | 569 | updating_scroll_ = true; |
528bd8a1 | 570 | |
333d5bbc | 571 | if (length < MaxScrollValue) { |
f25770e2 | 572 | horizontalScrollBar()->setRange(0, length); |
60d9b99a | 573 | horizontalScrollBar()->setSliderPosition(offset.convert_to<double>()); |
f25770e2 JH |
574 | } else { |
575 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
576 | horizontalScrollBar()->setSliderPosition( | |
60d9b99a | 577 | (offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>()); |
f25770e2 | 578 | } |
adb4b10c | 579 | |
8dbbc7f0 | 580 | updating_scroll_ = false; |
528bd8a1 | 581 | |
adb4b10c JH |
582 | // Set the vertical scrollbar |
583 | verticalScrollBar()->setPageStep(areaSize.height()); | |
65c34596 | 584 | verticalScrollBar()->setSingleStep(areaSize.height() / 8); |
a5d93c27 JH |
585 | |
586 | const pair<int, int> extents = v_extents(); | |
f1d9ac67 SA |
587 | verticalScrollBar()->setRange(extents.first - (areaSize.height() / 2), |
588 | extents.second - (areaSize.height() / 2)); | |
adb4b10c JH |
589 | } |
590 | ||
d7c0ca4a JH |
591 | void View::update_layout() |
592 | { | |
512bfc56 | 593 | setViewportMargins( |
8dbbc7f0 JH |
594 | header_->sizeHint().width() - pv::view::Header::BaselineOffset, |
595 | ruler_->sizeHint().height(), 0, 0); | |
596 | ruler_->setGeometry(viewport_->x(), 0, | |
819e2e95 | 597 | viewport_->width(), ruler_->extended_size_hint().height()); |
8dbbc7f0 | 598 | header_->setGeometry(0, viewport_->y(), |
819e2e95 | 599 | header_->extended_size_hint().width(), viewport_->height()); |
d7c0ca4a JH |
600 | update_scroll(); |
601 | } | |
602 | ||
b3f44329 | 603 | void View::paint_label(QPainter &p, const QRect &rect, bool hover) |
eae6e30a JH |
604 | { |
605 | (void)p; | |
b3f44329 | 606 | (void)rect; |
eae6e30a JH |
607 | (void)hover; |
608 | } | |
609 | ||
b3f44329 | 610 | QRectF View::label_rect(const QRectF &rect) |
eae6e30a | 611 | { |
b3f44329 | 612 | (void)rect; |
eae6e30a JH |
613 | return QRectF(); |
614 | } | |
615 | ||
cf124e47 JH |
616 | RowItemOwner* View::find_prevalent_trace_group( |
617 | const shared_ptr<sigrok::ChannelGroup> &group, | |
618 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
619 | &signal_map) | |
448a72cf | 620 | { |
cf124e47 | 621 | assert(group); |
448a72cf | 622 | |
cf124e47 JH |
623 | unordered_set<RowItemOwner*> owners; |
624 | vector<RowItemOwner*> owner_list; | |
448a72cf | 625 | |
cf124e47 JH |
626 | // Make a set and a list of all the owners |
627 | for (const auto &channel : group->channels()) { | |
448a72cf | 628 | const auto iter = signal_map.find(channel); |
cf124e47 | 629 | if (iter == signal_map.end()) |
448a72cf JH |
630 | continue; |
631 | ||
cf124e47 JH |
632 | RowItemOwner *const o = (*iter).second->owner(); |
633 | owner_list.push_back(o); | |
634 | owners.insert(o); | |
635 | } | |
448a72cf | 636 | |
cf124e47 JH |
637 | // Iterate through the list of owners, and find the most prevalent |
638 | size_t max_prevalence = 0; | |
639 | RowItemOwner *prevalent_owner = nullptr; | |
640 | for (RowItemOwner *owner : owners) { | |
641 | const size_t prevalence = std::count_if( | |
642 | owner_list.begin(), owner_list.end(), | |
643 | [&](RowItemOwner *o) { return o == owner; }); | |
644 | if (prevalence > max_prevalence) { | |
645 | max_prevalence = prevalence; | |
646 | prevalent_owner = owner; | |
647 | } | |
448a72cf JH |
648 | } |
649 | ||
cf124e47 | 650 | return prevalent_owner; |
448a72cf JH |
651 | } |
652 | ||
cf124e47 JH |
653 | vector< shared_ptr<Trace> > View::extract_new_traces_for_channels( |
654 | const vector< shared_ptr<sigrok::Channel> > &channels, | |
655 | const unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
656 | &signal_map, | |
657 | set< shared_ptr<Trace> > &add_list) | |
658 | { | |
659 | vector< shared_ptr<Trace> > filtered_traces; | |
660 | ||
661 | for (const auto &channel : channels) | |
662 | { | |
663 | const auto map_iter = signal_map.find(channel); | |
664 | if (map_iter == signal_map.end()) | |
665 | continue; | |
666 | ||
667 | shared_ptr<Trace> trace = (*map_iter).second; | |
668 | const auto list_iter = add_list.find(trace); | |
669 | if (list_iter == add_list.end()) | |
670 | continue; | |
671 | ||
672 | filtered_traces.push_back(trace); | |
673 | add_list.erase(list_iter); | |
674 | } | |
675 | ||
676 | return filtered_traces; | |
448a72cf JH |
677 | } |
678 | ||
ef454ad5 SA |
679 | void View::determine_time_unit() |
680 | { | |
7ee199a7 SA |
681 | // Check whether we know the sample rate and hence can use time as the unit |
682 | if (time_unit_ == util::Samples) { | |
683 | shared_lock<shared_mutex> lock(session().signals_mutex()); | |
684 | const unordered_set< shared_ptr<Signal> > &sigs(session().signals()); | |
685 | ||
686 | // Check all signals but... | |
687 | for (const shared_ptr<Signal> signal : sigs) { | |
688 | const shared_ptr<SignalData> data = signal->data(); | |
689 | ||
690 | // ...only check first segment of each | |
691 | const vector< shared_ptr<Segment> > segments = data->segments(); | |
692 | if (!segments.empty()) | |
693 | if (segments[0]->samplerate()) { | |
694 | time_unit_ = util::Time; | |
695 | break; | |
696 | } | |
697 | } | |
ef454ad5 SA |
698 | } |
699 | } | |
700 | ||
cbd80f64 JH |
701 | bool View::eventFilter(QObject *object, QEvent *event) |
702 | { | |
703 | const QEvent::Type type = event->type(); | |
333d5bbc | 704 | if (type == QEvent::MouseMove) { |
cbd80f64 JH |
705 | |
706 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
8dbbc7f0 JH |
707 | if (object == viewport_) |
708 | hover_point_ = mouse_event->pos(); | |
819e2e95 | 709 | else if (object == ruler_) |
8dbbc7f0 JH |
710 | hover_point_ = QPoint(mouse_event->x(), 0); |
711 | else if (object == header_) | |
712 | hover_point_ = QPoint(0, mouse_event->y()); | |
cbd80f64 | 713 | else |
8dbbc7f0 | 714 | hover_point_ = QPoint(-1, -1); |
cbd80f64 JH |
715 | |
716 | hover_point_changed(); | |
717 | ||
333d5bbc | 718 | } else if (type == QEvent::Leave) { |
8dbbc7f0 | 719 | hover_point_ = QPoint(-1, -1); |
cbd80f64 JH |
720 | hover_point_changed(); |
721 | } | |
722 | ||
723 | return QObject::eventFilter(object, event); | |
724 | } | |
725 | ||
cdf7bea7 | 726 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
727 | { |
728 | switch(e->type()) { | |
729 | case QEvent::Paint: | |
730 | case QEvent::MouseButtonPress: | |
731 | case QEvent::MouseButtonRelease: | |
732 | case QEvent::MouseButtonDblClick: | |
733 | case QEvent::MouseMove: | |
734 | case QEvent::Wheel: | |
4b4f1c0d MC |
735 | case QEvent::TouchBegin: |
736 | case QEvent::TouchUpdate: | |
737 | case QEvent::TouchEnd: | |
adb4b10c JH |
738 | return false; |
739 | ||
740 | default: | |
741 | return QAbstractScrollArea::viewportEvent(e); | |
742 | } | |
743 | } | |
744 | ||
e314eca4 | 745 | void View::resizeEvent(QResizeEvent*) |
adb4b10c | 746 | { |
d7c0ca4a | 747 | update_layout(); |
adb4b10c JH |
748 | } |
749 | ||
6e2c3c85 | 750 | void View::row_item_appearance_changed(bool label, bool content) |
32218d3e JH |
751 | { |
752 | if (label) | |
8dbbc7f0 | 753 | header_->update(); |
32218d3e | 754 | if (content) |
8dbbc7f0 | 755 | viewport_->update(); |
32218d3e JH |
756 | } |
757 | ||
98cfe4e8 JH |
758 | void View::time_item_appearance_changed(bool label, bool content) |
759 | { | |
760 | if (label) | |
819e2e95 | 761 | ruler_->update(); |
98cfe4e8 JH |
762 | if (content) |
763 | viewport_->update(); | |
764 | } | |
765 | ||
32218d3e JH |
766 | void View::extents_changed(bool horz, bool vert) |
767 | { | |
8dbbc7f0 | 768 | sticky_events_ |= |
76fea660 JH |
769 | (horz ? RowItemHExtentsChanged : 0) | |
770 | (vert ? RowItemVExtentsChanged : 0); | |
8dbbc7f0 | 771 | lazy_event_handler_.start(); |
32218d3e JH |
772 | } |
773 | ||
b16907d3 | 774 | void View::h_scroll_value_changed(int value) |
adb4b10c | 775 | { |
8dbbc7f0 | 776 | if (updating_scroll_) |
528bd8a1 JH |
777 | return; |
778 | ||
c7b03d9d SA |
779 | // Disable sticky scrolling when user moves the horizontal scroll bar |
780 | // during a running acquisition | |
781 | if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) { | |
782 | sticky_scrolling_ = false; | |
783 | sticky_scrolling_changed(false); | |
784 | } | |
785 | ||
f25770e2 | 786 | const int range = horizontalScrollBar()->maximum(); |
333d5bbc | 787 | if (range < MaxScrollValue) |
8dbbc7f0 | 788 | offset_ = scale_ * value; |
f25770e2 | 789 | else { |
60d9b99a JS |
790 | double length = 0; |
791 | Timestamp offset; | |
f25770e2 | 792 | get_scroll_layout(length, offset); |
8dbbc7f0 | 793 | offset_ = scale_ * length * value / MaxScrollValue; |
f25770e2 JH |
794 | } |
795 | ||
8dbbc7f0 | 796 | ruler_->update(); |
8dbbc7f0 | 797 | viewport_->update(); |
adb4b10c JH |
798 | } |
799 | ||
f8400017 | 800 | void View::v_scroll_value_changed() |
adb4b10c | 801 | { |
8dbbc7f0 JH |
802 | header_->update(); |
803 | viewport_->update(); | |
adb4b10c JH |
804 | } |
805 | ||
69dd2b03 JH |
806 | void View::signals_changed() |
807 | { | |
cf124e47 | 808 | vector< shared_ptr<RowItem> > new_top_level_items; |
68b21a71 | 809 | |
e37e05f7 JH |
810 | const auto device = session_.device(); |
811 | if (!device) | |
812 | return; | |
813 | ||
814 | shared_ptr<sigrok::Device> sr_dev = device->device(); | |
815 | assert(sr_dev); | |
448a72cf | 816 | |
cf124e47 JH |
817 | // Make a list of traces that are being added, and a list of traces |
818 | // that are being removed | |
819 | const set<shared_ptr<Trace>> prev_traces = list_by_type<Trace>(); | |
448a72cf | 820 | |
8dbbc7f0 | 821 | shared_lock<shared_mutex> lock(session_.signals_mutex()); |
78b0af3e | 822 | const unordered_set< shared_ptr<Signal> > &sigs(session_.signals()); |
448a72cf | 823 | |
cf124e47 JH |
824 | set< shared_ptr<Trace> > traces(sigs.begin(), sigs.end()); |
825 | ||
826 | #ifdef ENABLE_DECODE | |
827 | const vector< shared_ptr<DecodeTrace> > decode_traces( | |
828 | session().get_decode_signals()); | |
829 | traces.insert(decode_traces.begin(), decode_traces.end()); | |
830 | #endif | |
831 | ||
832 | set< shared_ptr<Trace> > add_traces; | |
833 | set_difference(traces.begin(), traces.end(), | |
834 | prev_traces.begin(), prev_traces.end(), | |
835 | inserter(add_traces, add_traces.begin())); | |
836 | ||
837 | set< shared_ptr<Trace> > remove_traces; | |
838 | set_difference(prev_traces.begin(), prev_traces.end(), | |
839 | traces.begin(), traces.end(), | |
840 | inserter(remove_traces, remove_traces.begin())); | |
841 | ||
842 | // Make a look-up table of sigrok Channels to pulseview Signals | |
843 | unordered_map<shared_ptr<sigrok::Channel>, shared_ptr<Signal> > | |
844 | signal_map; | |
448a72cf JH |
845 | for (const shared_ptr<Signal> &sig : sigs) |
846 | signal_map[sig->channel()] = sig; | |
847 | ||
848 | // Populate channel groups | |
e37e05f7 | 849 | for (auto entry : sr_dev->channel_groups()) |
448a72cf JH |
850 | { |
851 | const shared_ptr<sigrok::ChannelGroup> &group = entry.second; | |
852 | ||
853 | if (group->channels().size() <= 1) | |
854 | continue; | |
855 | ||
cf124e47 JH |
856 | // Find best trace group to add to |
857 | RowItemOwner *owner = find_prevalent_trace_group( | |
858 | group, signal_map); | |
448a72cf | 859 | |
cf124e47 JH |
860 | // If there is no trace group, create one |
861 | shared_ptr<TraceGroup> new_trace_group; | |
862 | if (!owner) { | |
863 | new_trace_group.reset(new TraceGroup()); | |
864 | owner = new_trace_group.get(); | |
865 | } | |
448a72cf | 866 | |
cf124e47 JH |
867 | // Extract traces for the trace group, removing them from |
868 | // the add list | |
869 | const vector< shared_ptr<Trace> > new_traces_in_group = | |
870 | extract_new_traces_for_channels(group->channels(), | |
871 | signal_map, add_traces); | |
872 | ||
873 | // Add the traces to the group | |
874 | const pair<int, int> prev_v_extents = owner->v_extents(); | |
875 | int offset = prev_v_extents.second - prev_v_extents.first; | |
876 | for (shared_ptr<Trace> trace : new_traces_in_group) { | |
877 | assert(trace); | |
878 | owner->add_child_item(trace); | |
879 | ||
880 | const pair<int, int> extents = trace->v_extents(); | |
881 | if (trace->enabled()) | |
882 | offset += -extents.first; | |
883 | trace->force_to_v_offset(offset); | |
884 | if (trace->enabled()) | |
885 | offset += extents.second; | |
886 | } | |
68b21a71 | 887 | |
cf124e47 JH |
888 | // If this is a new group, enqueue it in the new top level |
889 | // items list | |
890 | if (!new_traces_in_group.empty() && new_trace_group) | |
891 | new_top_level_items.push_back(new_trace_group); | |
448a72cf JH |
892 | } |
893 | ||
cf124e47 JH |
894 | // Enqueue the remaining channels as free ungrouped traces |
895 | const vector< shared_ptr<Trace> > new_top_level_signals = | |
896 | extract_new_traces_for_channels(sr_dev->channels(), | |
897 | signal_map, add_traces); | |
898 | new_top_level_items.insert(new_top_level_items.end(), | |
899 | new_top_level_signals.begin(), new_top_level_signals.end()); | |
900 | ||
901 | // Enqueue any remaining traces i.e. decode traces | |
902 | new_top_level_items.insert(new_top_level_items.end(), | |
903 | add_traces.begin(), add_traces.end()); | |
904 | ||
905 | // Remove any removed traces | |
906 | for (shared_ptr<Trace> trace : remove_traces) { | |
907 | RowItemOwner *const owner = trace->owner(); | |
908 | assert(owner); | |
909 | owner->remove_child_item(trace); | |
910 | } | |
448a72cf | 911 | |
cf124e47 JH |
912 | // Add and position the pending top levels items |
913 | for (auto item : new_top_level_items) { | |
914 | add_child_item(item); | |
915 | ||
916 | // Position the item after the last present item | |
917 | int offset = v_extents().second; | |
918 | const pair<int, int> extents = item->v_extents(); | |
919 | if (item->enabled()) | |
920 | offset += -extents.first; | |
921 | item->force_to_v_offset(offset); | |
922 | if (item->enabled()) | |
923 | offset += extents.second; | |
ef8311a4 JH |
924 | } |
925 | ||
a6c1726e | 926 | update_layout(); |
cf124e47 JH |
927 | |
928 | header_->update(); | |
929 | viewport_->update(); | |
69dd2b03 JH |
930 | } |
931 | ||
7ee199a7 | 932 | void View::capture_state_updated(int state) |
adb4b10c | 933 | { |
7ee199a7 SA |
934 | if (state == Session::Running) |
935 | time_unit_ = util::Samples; | |
fd03a279 SA |
936 | |
937 | if (state == Session::Stopped) { | |
938 | // After acquisition has stopped we need to re-calculate the ticks once | |
939 | // as it's otherwise done when the user pans or zooms, which is too late | |
940 | calculate_tick_spacing(); | |
941 | ||
942 | // Reset "always zoom to fit", the acquisition has stopped | |
943 | if (always_zoom_to_fit_) { | |
944 | always_zoom_to_fit_ = false; | |
945 | always_zoom_to_fit_changed(false); | |
946 | } | |
947 | } | |
7ee199a7 SA |
948 | } |
949 | ||
950 | void View::data_updated() | |
951 | { | |
ce11b2ea | 952 | if (always_zoom_to_fit_ || sticky_scrolling_) { |
c7b03d9d SA |
953 | if (!delayed_view_updater_.isActive()) |
954 | delayed_view_updater_.start(); | |
955 | } else { | |
ef454ad5 | 956 | determine_time_unit(); |
c7b03d9d SA |
957 | update_scroll(); |
958 | ruler_->update(); | |
959 | viewport_->update(); | |
960 | } | |
961 | } | |
962 | ||
963 | void View::perform_delayed_view_update() | |
964 | { | |
ce11b2ea SA |
965 | if (always_zoom_to_fit_) |
966 | zoom_fit(true); | |
967 | ||
c7b03d9d SA |
968 | if (sticky_scrolling_) { |
969 | // Make right side of the view sticky | |
60d9b99a JS |
970 | double length = 0; |
971 | Timestamp offset; | |
c7b03d9d SA |
972 | get_scroll_layout(length, offset); |
973 | ||
974 | const QSize areaSize = viewport_->size(); | |
975 | length = max(length - areaSize.width(), 0.0); | |
976 | ||
977 | offset_ = scale_ * length; | |
978 | } | |
adb4b10c | 979 | |
ef454ad5 | 980 | determine_time_unit(); |
c7b03d9d SA |
981 | update_scroll(); |
982 | ruler_->update(); | |
8dbbc7f0 | 983 | viewport_->update(); |
adb4b10c | 984 | } |
cdf7bea7 | 985 | |
32218d3e | 986 | void View::process_sticky_events() |
d7c0ca4a | 987 | { |
76fea660 | 988 | if (sticky_events_ & RowItemHExtentsChanged) |
32218d3e | 989 | update_layout(); |
f15bb3bc | 990 | if (sticky_events_ & RowItemVExtentsChanged) { |
7ff0145f | 991 | restack_all_row_items(); |
f15bb3bc JH |
992 | update_scroll(); |
993 | } | |
32218d3e JH |
994 | |
995 | // Clear the sticky events | |
8dbbc7f0 | 996 | sticky_events_ = 0; |
d7c0ca4a JH |
997 | } |
998 | ||
33c62f44 JH |
999 | void View::on_hover_point_changed() |
1000 | { | |
aa59d5c2 | 1001 | for (shared_ptr<RowItem> r : *this) |
eae6e30a | 1002 | r->hover_point_changed(); |
33c62f44 JH |
1003 | } |
1004 | ||
cdf7bea7 JH |
1005 | } // namespace view |
1006 | } // namespace pv |