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