]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
RowItemOwner: Added depth() method
[pulseview.git] / pv / view / view.cpp
CommitLineData
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
c3a740dd
JH
25#include <cassert>
26#include <climits>
27#include <cmath>
28#include <mutex>
adb4b10c 29
adb4b10c 30#include <QEvent>
cbd80f64 31#include <QMouseEvent>
adb4b10c
JH
32#include <QScrollBar>
33
84a0d458 34#include "cursorheader.h"
b9329558 35#include "decodetrace.h"
1d8dca91 36#include "header.h"
ccdd3ef5 37#include "ruler.h"
2e575351 38#include "signal.h"
cdf7bea7
JH
39#include "view.h"
40#include "viewport.h"
adb4b10c 41
1b1ec774
JH
42#include "pv/sigsession.h"
43#include "pv/data/logic.h"
44#include "pv/data/logicsnapshot.h"
adb4b10c 45
aca64cac
JH
46using boost::shared_lock;
47using boost::shared_mutex;
1bc6525b 48using pv::data::SignalData;
a2321722 49using std::back_inserter;
819f4c25
JH
50using std::deque;
51using std::list;
c3a740dd 52using std::lock_guard;
819f4c25 53using std::max;
1bc6525b 54using std::make_pair;
819f4c25 55using std::min;
1bc6525b 56using std::pair;
819f4c25 57using std::set;
f9abf97e 58using std::shared_ptr;
819f4c25 59using std::vector;
f9abf97e 60using std::weak_ptr;
adb4b10c 61
cdf7bea7
JH
62namespace pv {
63namespace view {
adb4b10c 64
cdf7bea7
JH
65const double View::MaxScale = 1e9;
66const double View::MinScale = 1e-15;
adb4b10c 67
f25770e2
JH
68const int View::MaxScrollValue = INT_MAX / 2;
69
f76af637
JH
70const QColor View::CursorAreaColour(220, 231, 243);
71
2e04f9bd
JH
72const QSizeF View::LabelPadding(4, 0);
73
cdf7bea7 74View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
75 QAbstractScrollArea(parent),
76 _session(session),
cdf7bea7 77 _viewport(new Viewport(*this)),
ccdd3ef5 78 _ruler(new Ruler(*this)),
84a0d458 79 _cursorheader(new CursorHeader(*this)),
1d8dca91 80 _header(new Header(*this)),
adb4b10c
JH
81 _scale(1e-6),
82 _offset(0),
cbd80f64 83 _v_offset(0),
528bd8a1 84 _updating_scroll(false),
f76af637 85 _show_cursors(false),
b42d25c4 86 _cursors(*this),
cbd80f64 87 _hover_point(-1, -1)
adb4b10c 88{
b16907d3
JH
89 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
90 this, SLOT(h_scroll_value_changed(int)));
adb4b10c
JH
91 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
92 this, SLOT(v_scroll_value_changed(int)));
69dd2b03
JH
93
94 connect(&_session, SIGNAL(signals_changed()),
95 this, SLOT(signals_changed()));
50f97924
PZ
96 connect(&_session, SIGNAL(capture_state_changed(int)),
97 this, SLOT(data_updated()));
1f374035
JH
98 connect(&_session, SIGNAL(data_received()),
99 this, SLOT(data_updated()));
100 connect(&_session, SIGNAL(frame_ended()),
adb4b10c 101 this, SLOT(data_updated()));
1d8dca91 102
58864c5c 103 connect(_cursors.first().get(), SIGNAL(time_changed()),
ca4ec3ea 104 this, SLOT(marker_time_changed()));
58864c5c 105 connect(_cursors.second().get(), SIGNAL(time_changed()),
ca4ec3ea
JH
106 this, SLOT(marker_time_changed()));
107
54401bbb 108 connect(_header, SIGNAL(signals_moved()),
07204819 109 this, SLOT(on_signals_moved()));
54401bbb 110
17348f85 111 connect(_header, SIGNAL(selection_changed()),
84a0d458
JS
112 _cursorheader, SLOT(clear_selection()));
113 connect(_cursorheader, SIGNAL(selection_changed()),
17348f85
JH
114 _header, SLOT(clear_selection()));
115
8b454527
JH
116 connect(_header, SIGNAL(selection_changed()),
117 this, SIGNAL(selection_changed()));
84a0d458 118 connect(_cursorheader, SIGNAL(selection_changed()),
8b454527
JH
119 this, SIGNAL(selection_changed()));
120
33c62f44
JH
121 connect(this, SIGNAL(hover_point_changed()),
122 this, SLOT(on_hover_point_changed()));
123
32218d3e
JH
124 connect(&_lazy_event_handler, SIGNAL(timeout()),
125 this, SLOT(process_sticky_events()));
126 _lazy_event_handler.setSingleShot(true);
127
adb4b10c 128 setViewport(_viewport);
cbd80f64
JH
129
130 _viewport->installEventFilter(this);
131 _ruler->installEventFilter(this);
84a0d458 132 _cursorheader->installEventFilter(this);
cbd80f64 133 _header->installEventFilter(this);
d873f4d6 134
9f46d905
JH
135 // Trigger the initial event manually. The default device has signals
136 // which were created before this object came into being
d873f4d6 137 signals_changed();
84a0d458 138
512bfc56 139 // make sure the transparent widgets are on the top
84a0d458 140 _cursorheader->raise();
512bfc56 141 _header->raise();
adb4b10c
JH
142}
143
1d19ef83
JH
144SigSession& View::session()
145{
146 return _session;
147}
148
38eeddea
JH
149const SigSession& View::session() const
150{
151 return _session;
152}
153
eae6e30a
JH
154View* View::view()
155{
156 return this;
157}
158
159const View* View::view() const
160{
161 return this;
162}
163
2ae445ba
SA
164Viewport* View::viewport()
165{
166 return _viewport;
167}
168
169const Viewport* View::viewport() const
170{
171 return _viewport;
172}
173
cdf7bea7 174double View::scale() const
adb4b10c
JH
175{
176 return _scale;
177}
178
cdf7bea7 179double View::offset() const
adb4b10c
JH
180{
181 return _offset;
182}
183
eae6e30a 184int View::owner_v_offset() const
adb4b10c 185{
23935421 186 return -_v_offset;
adb4b10c
JH
187}
188
3e769a37
JH
189unsigned int View::depth() const
190{
191 return 0;
192}
193
cdf7bea7 194void View::zoom(double steps)
adb4b10c 195{
f3f98f8f 196 zoom(steps, _viewport->width() / 2);
adb4b10c
JH
197}
198
17c0f398
JH
199void View::zoom(double steps, int offset)
200{
8b2e2a9a 201 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
17c0f398
JH
202}
203
ca46b534
JH
204void View::zoom_fit()
205{
1bc6525b
JH
206 const pair<double, double> extents = get_time_extents();
207 const double delta = extents.second - extents.first;
208 if (delta < 1e-12)
ca46b534
JH
209 return;
210
211 assert(_viewport);
212 const int w = _viewport->width();
213 if (w <= 0)
214 return;
215
78311127
JH
216 const double scale = max(min(delta / w, MaxScale), MinScale);
217 set_scale_offset(scale, extents.first);
ca46b534
JH
218}
219
d1e7d82c
JH
220void View::zoom_one_to_one()
221{
222 using pv::data::SignalData;
223
d1e7d82c 224 // Make a set of all the visible data objects
0fc664a9 225 set< shared_ptr<SignalData> > visible_data = get_visible_data();
d1e7d82c
JH
226 if (visible_data.empty())
227 return;
228
229 double samplerate = 0.0;
d9aecf1f 230 for (const shared_ptr<SignalData> d : visible_data) {
d1e7d82c
JH
231 assert(d);
232 samplerate = max(samplerate, d->samplerate());
233 }
234
235 if (samplerate == 0.0)
236 return;
237
238 assert(_viewport);
239 const int w = _viewport->width();
240 if (w <= 0)
241 return;
242
243 set_zoom(1.0 / samplerate, w / 2);
244}
245
cdf7bea7 246void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
247{
248 _scale = scale;
249 _offset = offset;
ccdd3ef5 250
adb4b10c 251 update_scroll();
ccdd3ef5 252 _ruler->update();
84a0d458 253 _cursorheader->update();
adb4b10c 254 _viewport->update();
e0fc5810 255 scale_offset_changed();
adb4b10c
JH
256}
257
1bc6525b
JH
258set< shared_ptr<SignalData> > View::get_visible_data() const
259{
aca64cac 260 shared_lock<shared_mutex> lock(session().signals_mutex());
c3a740dd 261 const vector< shared_ptr<Signal> > &sigs(session().signals());
1bc6525b
JH
262
263 // Make a set of all the visible data objects
264 set< shared_ptr<SignalData> > visible_data;
d9aecf1f 265 for (const shared_ptr<Signal> sig : sigs)
1bc6525b
JH
266 if (sig->enabled())
267 visible_data.insert(sig->data());
268
269 return visible_data;
270}
271
272pair<double, double> View::get_time_extents() const
273{
274 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
275 if (visible_data.empty())
276 return make_pair(0.0, 0.0);
277
278 double left_time = DBL_MAX, right_time = DBL_MIN;
d9aecf1f 279 for (const shared_ptr<SignalData> d : visible_data)
1bc6525b
JH
280 {
281 const double start_time = d->get_start_time();
a472a884
JH
282 double samplerate = d->samplerate();
283 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
284
1bc6525b
JH
285 left_time = min(left_time, start_time);
286 right_time = max(right_time, start_time +
a472a884 287 d->get_max_sample_count() / samplerate);
1bc6525b
JH
288 }
289
290 assert(left_time < right_time);
291 return make_pair(left_time, right_time);
292}
293
f76af637
JH
294bool View::cursors_shown() const
295{
296 return _show_cursors;
297}
298
299void View::show_cursors(bool show)
300{
301 _show_cursors = show;
84a0d458 302 _cursorheader->update();
f76af637
JH
303 _viewport->update();
304}
305
b4d91e56
JH
306void View::centre_cursors()
307{
308 const double time_width = _scale * _viewport->width();
58864c5c
JH
309 _cursors.first()->set_time(_offset + time_width * 0.4);
310 _cursors.second()->set_time(_offset + time_width * 0.6);
84a0d458 311 _cursorheader->update();
b4d91e56
JH
312 _viewport->update();
313}
314
b42d25c4 315CursorPair& View::cursors()
f76af637
JH
316{
317 return _cursors;
318}
319
58864c5c
JH
320const CursorPair& View::cursors() const
321{
322 return _cursors;
323}
324
cbd80f64
JH
325const QPoint& View::hover_point() const
326{
327 return _hover_point;
328}
329
9cef9567
JH
330void View::update_viewport()
331{
332 assert(_viewport);
333 _viewport->update();
334}
335
f25770e2
JH
336void View::get_scroll_layout(double &length, double &offset) const
337{
1bc6525b
JH
338 const pair<double, double> extents = get_time_extents();
339 length = (extents.second - extents.first) / _scale;
f25770e2
JH
340 offset = _offset / _scale;
341}
342
d1e7d82c
JH
343void View::set_zoom(double scale, int offset)
344{
345 const double cursor_offset = _offset + _scale * offset;
346 const double new_scale = max(min(scale, MaxScale), MinScale);
347 const double new_offset = cursor_offset - new_scale * offset;
348 set_scale_offset(new_scale, new_offset);
349}
350
cdf7bea7 351void View::update_scroll()
adb4b10c
JH
352{
353 assert(_viewport);
354
355 const QSize areaSize = _viewport->size();
356
357 // Set the horizontal scroll bar
358 double length = 0, offset = 0;
f25770e2
JH
359 get_scroll_layout(length, offset);
360 length = max(length - areaSize.width(), 0.0);
adb4b10c 361
b4ef7f2a 362 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 363
528bd8a1
JH
364 _updating_scroll = true;
365
333d5bbc 366 if (length < MaxScrollValue) {
f25770e2
JH
367 horizontalScrollBar()->setRange(0, length);
368 horizontalScrollBar()->setSliderPosition(offset);
369 } else {
370 horizontalScrollBar()->setRange(0, MaxScrollValue);
371 horizontalScrollBar()->setSliderPosition(
372 _offset * MaxScrollValue / (_scale * length));
373 }
adb4b10c 374
528bd8a1
JH
375 _updating_scroll = false;
376
adb4b10c
JH
377 // Set the vertical scrollbar
378 verticalScrollBar()->setPageStep(areaSize.height());
a5d93c27
JH
379
380 const pair<int, int> extents = v_extents();
381 const int extra_scroll_height = (extents.second - extents.first) / 4;
382 verticalScrollBar()->setRange(extents.first - extra_scroll_height,
383 extents.first + extra_scroll_height);
adb4b10c
JH
384}
385
d7c0ca4a
JH
386void View::update_layout()
387{
512bfc56
JS
388 setViewportMargins(
389 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
a6c1726e 390 _ruler->sizeHint().height(), 0, 0);
d7c0ca4a
JH
391 _ruler->setGeometry(_viewport->x(), 0,
392 _viewport->width(), _viewport->y());
84a0d458
JS
393 _cursorheader->setGeometry(
394 _viewport->x(),
395 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
396 _viewport->width(), _cursorheader->sizeHint().height());
d7c0ca4a 397 _header->setGeometry(0, _viewport->y(),
512bfc56 398 _header->sizeHint().width(), _viewport->height());
d7c0ca4a
JH
399 update_scroll();
400}
401
eae6e30a
JH
402void View::paint_label(QPainter &p, int right, bool hover)
403{
404 (void)p;
405 (void)right;
406 (void)hover;
407}
408
409QRectF View::label_rect(int right)
410{
411 (void)right;
412 return QRectF();
413}
414
cbd80f64
JH
415bool View::eventFilter(QObject *object, QEvent *event)
416{
417 const QEvent::Type type = event->type();
333d5bbc 418 if (type == QEvent::MouseMove) {
cbd80f64
JH
419
420 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 421 if (object == _viewport)
cbd80f64 422 _hover_point = mouse_event->pos();
84a0d458 423 else if (object == _ruler || object == _cursorheader)
cbd80f64 424 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 425 else if (object == _header)
cbd80f64
JH
426 _hover_point = QPoint(0, mouse_event->y());
427 else
428 _hover_point = QPoint(-1, -1);
429
430 hover_point_changed();
431
333d5bbc 432 } else if (type == QEvent::Leave) {
cbd80f64
JH
433 _hover_point = QPoint(-1, -1);
434 hover_point_changed();
435 }
436
437 return QObject::eventFilter(object, event);
438}
439
cdf7bea7 440bool View::viewportEvent(QEvent *e)
adb4b10c
JH
441{
442 switch(e->type()) {
443 case QEvent::Paint:
444 case QEvent::MouseButtonPress:
445 case QEvent::MouseButtonRelease:
446 case QEvent::MouseButtonDblClick:
447 case QEvent::MouseMove:
448 case QEvent::Wheel:
4b4f1c0d
MC
449 case QEvent::TouchBegin:
450 case QEvent::TouchUpdate:
451 case QEvent::TouchEnd:
adb4b10c
JH
452 return false;
453
454 default:
455 return QAbstractScrollArea::viewportEvent(e);
456 }
457}
458
e314eca4 459void View::resizeEvent(QResizeEvent*)
adb4b10c 460{
d7c0ca4a 461 update_layout();
adb4b10c
JH
462}
463
32218d3e
JH
464void View::appearance_changed(bool label, bool content)
465{
466 if (label)
467 _header->update();
468 if (content)
469 _viewport->update();
470}
471
472void View::extents_changed(bool horz, bool vert)
473{
474 _sticky_events |=
475 (horz ? SelectableItemHExtentsChanged : 0) |
476 (vert ? SelectableItemVExtentsChanged : 0);
477 _lazy_event_handler.start();
478}
479
b16907d3 480void View::h_scroll_value_changed(int value)
adb4b10c 481{
528bd8a1
JH
482 if (_updating_scroll)
483 return;
484
f25770e2 485 const int range = horizontalScrollBar()->maximum();
333d5bbc 486 if (range < MaxScrollValue)
f25770e2
JH
487 _offset = _scale * value;
488 else {
489 double length = 0, offset;
490 get_scroll_layout(length, offset);
491 _offset = _scale * length * value / MaxScrollValue;
492 }
493
ccdd3ef5 494 _ruler->update();
84a0d458 495 _cursorheader->update();
adb4b10c
JH
496 _viewport->update();
497}
498
cdf7bea7 499void View::v_scroll_value_changed(int value)
adb4b10c
JH
500{
501 _v_offset = value;
1d8dca91 502 _header->update();
adb4b10c
JH
503 _viewport->update();
504}
505
69dd2b03
JH
506void View::signals_changed()
507{
68b21a71
JH
508 // Populate the traces
509 clear_child_items();
510
aca64cac 511 shared_lock<shared_mutex> lock(session().signals_mutex());
c3a740dd 512 const vector< shared_ptr<Signal> > &sigs(session().signals());
68b21a71
JH
513 for (auto s : sigs)
514 add_child_item(s);
515
516#ifdef ENABLE_DECODE
517 const vector< shared_ptr<DecodeTrace> > decode_sigs(
518 session().get_decode_signals());
519 for (auto s : decode_sigs)
520 add_child_item(s);
521#endif
522
523 // Create the initial layout
a5d93c27 524 int offset = 0;
aa59d5c2 525 for (shared_ptr<RowItem> r : *this) {
a5d93c27
JH
526 const pair<int, int> extents = r->v_extents();
527 if (r->enabled())
528 offset += -extents.first;
be9e7b4b 529 r->force_to_v_offset(offset);
a5d93c27
JH
530 if (r->enabled())
531 offset += extents.second;
ef8311a4
JH
532 }
533
a6c1726e 534 update_layout();
69dd2b03
JH
535}
536
cdf7bea7 537void View::data_updated()
adb4b10c 538{
adb4b10c
JH
539 // Update the scroll bars
540 update_scroll();
541
542 // Repaint the view
543 _viewport->update();
544}
cdf7bea7 545
ca4ec3ea
JH
546void View::marker_time_changed()
547{
84a0d458 548 _cursorheader->update();
ca4ec3ea
JH
549 _viewport->update();
550}
551
07204819 552void View::on_signals_moved()
54401bbb 553{
07204819
JH
554 update_scroll();
555 signals_moved();
54401bbb
JH
556}
557
32218d3e 558void View::process_sticky_events()
d7c0ca4a 559{
32218d3e
JH
560 if (_sticky_events & SelectableItemHExtentsChanged)
561 update_layout();
562 if (_sticky_events & SelectableItemVExtentsChanged) {
563 _viewport->update();
564 _header->update();
565 }
566
567 // Clear the sticky events
568 _sticky_events = 0;
d7c0ca4a
JH
569}
570
33c62f44
JH
571void View::on_hover_point_changed()
572{
aa59d5c2 573 for (shared_ptr<RowItem> r : *this)
eae6e30a 574 r->hover_point_changed();
33c62f44
JH
575}
576
cdf7bea7
JH
577} // namespace view
578} // namespace pv