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