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