]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Added RowItemOwner
[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
adb4b10c 25#include <assert.h>
f25770e2 26#include <limits.h>
adb4b10c
JH
27#include <math.h>
28
adb4b10c 29#include <QEvent>
cbd80f64 30#include <QMouseEvent>
adb4b10c
JH
31#include <QScrollBar>
32
84a0d458 33#include "cursorheader.h"
b9329558 34#include "decodetrace.h"
1d8dca91 35#include "header.h"
ccdd3ef5 36#include "ruler.h"
2e575351 37#include "signal.h"
cdf7bea7
JH
38#include "view.h"
39#include "viewport.h"
adb4b10c 40
1b1ec774
JH
41#include "pv/sigsession.h"
42#include "pv/data/logic.h"
43#include "pv/data/logicsnapshot.h"
adb4b10c 44
1bc6525b 45using pv::data::SignalData;
a2321722 46using std::back_inserter;
819f4c25
JH
47using std::deque;
48using std::list;
49using std::max;
1bc6525b 50using std::make_pair;
819f4c25 51using std::min;
1bc6525b 52using std::pair;
819f4c25 53using std::set;
f9abf97e 54using std::shared_ptr;
819f4c25 55using std::vector;
f9abf97e 56using std::weak_ptr;
adb4b10c 57
cdf7bea7
JH
58namespace pv {
59namespace view {
adb4b10c 60
cdf7bea7
JH
61const double View::MaxScale = 1e9;
62const double View::MinScale = 1e-15;
adb4b10c 63
f25770e2
JH
64const int View::MaxScrollValue = INT_MAX / 2;
65
8d44d030
JH
66const int View::SignalHeight = 30;
67const int View::SignalMargin = 10;
49153683 68const int View::SignalSnapGridSize = 10;
1d8dca91 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
2ae445ba
SA
152Viewport* View::viewport()
153{
154 return _viewport;
155}
156
157const Viewport* View::viewport() const
158{
159 return _viewport;
160}
161
cdf7bea7 162double View::scale() const
adb4b10c
JH
163{
164 return _scale;
165}
166
cdf7bea7 167double View::offset() const
adb4b10c
JH
168{
169 return _offset;
170}
171
cdf7bea7 172int View::v_offset() const
adb4b10c 173{
23935421 174 return -_v_offset;
adb4b10c
JH
175}
176
cdf7bea7 177void View::zoom(double steps)
adb4b10c 178{
f3f98f8f 179 zoom(steps, _viewport->width() / 2);
adb4b10c
JH
180}
181
17c0f398
JH
182void View::zoom(double steps, int offset)
183{
8b2e2a9a 184 set_zoom(_scale * pow(3.0/2.0, -steps), offset);
17c0f398
JH
185}
186
ca46b534
JH
187void View::zoom_fit()
188{
1bc6525b
JH
189 const pair<double, double> extents = get_time_extents();
190 const double delta = extents.second - extents.first;
191 if (delta < 1e-12)
ca46b534
JH
192 return;
193
194 assert(_viewport);
195 const int w = _viewport->width();
196 if (w <= 0)
197 return;
198
78311127
JH
199 const double scale = max(min(delta / w, MaxScale), MinScale);
200 set_scale_offset(scale, extents.first);
ca46b534
JH
201}
202
d1e7d82c
JH
203void View::zoom_one_to_one()
204{
205 using pv::data::SignalData;
206
207 const vector< shared_ptr<Signal> > sigs(
208 session().get_signals());
209
210 // Make a set of all the visible data objects
0fc664a9 211 set< shared_ptr<SignalData> > visible_data = get_visible_data();
d1e7d82c
JH
212 if (visible_data.empty())
213 return;
214
215 double samplerate = 0.0;
d9aecf1f 216 for (const shared_ptr<SignalData> d : visible_data) {
d1e7d82c
JH
217 assert(d);
218 samplerate = max(samplerate, d->samplerate());
219 }
220
221 if (samplerate == 0.0)
222 return;
223
224 assert(_viewport);
225 const int w = _viewport->width();
226 if (w <= 0)
227 return;
228
229 set_zoom(1.0 / samplerate, w / 2);
230}
231
cdf7bea7 232void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
233{
234 _scale = scale;
235 _offset = offset;
ccdd3ef5 236
adb4b10c 237 update_scroll();
ccdd3ef5 238 _ruler->update();
84a0d458 239 _cursorheader->update();
adb4b10c 240 _viewport->update();
e0fc5810 241 scale_offset_changed();
adb4b10c
JH
242}
243
38eeddea
JH
244vector< shared_ptr<Trace> > View::get_traces() const
245{
a2321722
JH
246 vector< shared_ptr<Trace> > traces;
247
38eeddea
JH
248 const vector< shared_ptr<Signal> > sigs(
249 session().get_signals());
a2321722
JH
250 copy(sigs.begin(), sigs.end(), back_inserter(traces));
251
269528f5 252#ifdef ENABLE_DECODE
b9329558 253 const vector< shared_ptr<DecodeTrace> > decode_sigs(
38eeddea 254 session().get_decode_signals());
a2321722 255 copy(decode_sigs.begin(), decode_sigs.end(), back_inserter(traces));
269528f5 256#endif
38eeddea 257
2c25d912
JH
258 stable_sort(traces.begin(), traces.end(),
259 [](const shared_ptr<Trace> &a, const shared_ptr<Trace> &b) {
1ef49ddd 260 return a->v_offset() < b->v_offset(); });
2c25d912 261
38eeddea
JH
262 return traces;
263}
264
94936133
JH
265list<weak_ptr<SelectableItem> > View::selected_items() const
266{
267 list<weak_ptr<SelectableItem> > items;
268
269 // List the selected signals
38eeddea 270 const vector< shared_ptr<Trace> > traces(get_traces());
d9aecf1f 271 for (shared_ptr<Trace> t : traces) {
38eeddea
JH
272 assert(t);
273 if (t->selected())
274 items.push_back(t);
94936133
JH
275 }
276
277 // List the selected cursors
278 if (_cursors.first()->selected())
279 items.push_back(_cursors.first());
280 if (_cursors.second()->selected())
281 items.push_back(_cursors.second());
282
283 return items;
284}
285
1bc6525b
JH
286set< shared_ptr<SignalData> > View::get_visible_data() const
287{
288 const vector< shared_ptr<Signal> > sigs(
289 session().get_signals());
290
291 // Make a set of all the visible data objects
292 set< shared_ptr<SignalData> > visible_data;
d9aecf1f 293 for (const shared_ptr<Signal> sig : sigs)
1bc6525b
JH
294 if (sig->enabled())
295 visible_data.insert(sig->data());
296
297 return visible_data;
298}
299
300pair<double, double> View::get_time_extents() const
301{
302 const set< shared_ptr<SignalData> > visible_data = get_visible_data();
303 if (visible_data.empty())
304 return make_pair(0.0, 0.0);
305
306 double left_time = DBL_MAX, right_time = DBL_MIN;
d9aecf1f 307 for (const shared_ptr<SignalData> d : visible_data)
1bc6525b
JH
308 {
309 const double start_time = d->get_start_time();
a472a884
JH
310 double samplerate = d->samplerate();
311 samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
312
1bc6525b
JH
313 left_time = min(left_time, start_time);
314 right_time = max(right_time, start_time +
a472a884 315 d->get_max_sample_count() / samplerate);
1bc6525b
JH
316 }
317
318 assert(left_time < right_time);
319 return make_pair(left_time, right_time);
320}
321
f76af637
JH
322bool View::cursors_shown() const
323{
324 return _show_cursors;
325}
326
327void View::show_cursors(bool show)
328{
329 _show_cursors = show;
84a0d458 330 _cursorheader->update();
f76af637
JH
331 _viewport->update();
332}
333
b4d91e56
JH
334void View::centre_cursors()
335{
336 const double time_width = _scale * _viewport->width();
58864c5c
JH
337 _cursors.first()->set_time(_offset + time_width * 0.4);
338 _cursors.second()->set_time(_offset + time_width * 0.6);
84a0d458 339 _cursorheader->update();
b4d91e56
JH
340 _viewport->update();
341}
342
b42d25c4 343CursorPair& View::cursors()
f76af637
JH
344{
345 return _cursors;
346}
347
58864c5c
JH
348const CursorPair& View::cursors() const
349{
350 return _cursors;
351}
352
cbd80f64
JH
353const QPoint& View::hover_point() const
354{
355 return _hover_point;
356}
357
4b192962
JH
358void View::normalize_layout()
359{
38eeddea 360 const vector< shared_ptr<Trace> > traces(get_traces());
4b192962
JH
361
362 int v_min = INT_MAX;
d9aecf1f 363 for (const shared_ptr<Trace> t : traces)
1ef49ddd 364 v_min = min(t->v_offset(), v_min);
4b192962
JH
365
366 const int delta = -min(v_min, 0);
d9aecf1f 367 for (shared_ptr<Trace> t : traces)
1ef49ddd 368 t->set_v_offset(t->v_offset() + delta);
4b192962
JH
369
370 verticalScrollBar()->setSliderPosition(_v_offset + delta);
371 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
372}
373
9cef9567
JH
374void View::update_viewport()
375{
376 assert(_viewport);
377 _viewport->update();
378}
379
f25770e2
JH
380void View::get_scroll_layout(double &length, double &offset) const
381{
1bc6525b
JH
382 const pair<double, double> extents = get_time_extents();
383 length = (extents.second - extents.first) / _scale;
f25770e2
JH
384 offset = _offset / _scale;
385}
386
d1e7d82c
JH
387void View::set_zoom(double scale, int offset)
388{
389 const double cursor_offset = _offset + _scale * offset;
390 const double new_scale = max(min(scale, MaxScale), MinScale);
391 const double new_offset = cursor_offset - new_scale * offset;
392 set_scale_offset(new_scale, new_offset);
393}
394
cdf7bea7 395void View::update_scroll()
adb4b10c
JH
396{
397 assert(_viewport);
398
399 const QSize areaSize = _viewport->size();
400
401 // Set the horizontal scroll bar
402 double length = 0, offset = 0;
f25770e2
JH
403 get_scroll_layout(length, offset);
404 length = max(length - areaSize.width(), 0.0);
adb4b10c 405
b4ef7f2a 406 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 407
528bd8a1
JH
408 _updating_scroll = true;
409
333d5bbc 410 if (length < MaxScrollValue) {
f25770e2
JH
411 horizontalScrollBar()->setRange(0, length);
412 horizontalScrollBar()->setSliderPosition(offset);
413 } else {
414 horizontalScrollBar()->setRange(0, MaxScrollValue);
415 horizontalScrollBar()->setSliderPosition(
416 _offset * MaxScrollValue / (_scale * length));
417 }
adb4b10c 418
528bd8a1
JH
419 _updating_scroll = false;
420
adb4b10c
JH
421 // Set the vertical scrollbar
422 verticalScrollBar()->setPageStep(areaSize.height());
423 verticalScrollBar()->setRange(0,
4b192962
JH
424 _viewport->get_total_height() + SignalMargin -
425 areaSize.height());
adb4b10c
JH
426}
427
d7c0ca4a
JH
428void View::update_layout()
429{
512bfc56
JS
430 setViewportMargins(
431 _header->sizeHint().width() - pv::view::Header::BaselineOffset,
a6c1726e 432 _ruler->sizeHint().height(), 0, 0);
d7c0ca4a
JH
433 _ruler->setGeometry(_viewport->x(), 0,
434 _viewport->width(), _viewport->y());
84a0d458
JS
435 _cursorheader->setGeometry(
436 _viewport->x(),
437 _ruler->sizeHint().height() - _cursorheader->sizeHint().height() / 2,
438 _viewport->width(), _cursorheader->sizeHint().height());
d7c0ca4a 439 _header->setGeometry(0, _viewport->y(),
512bfc56 440 _header->sizeHint().width(), _viewport->height());
d7c0ca4a
JH
441 update_scroll();
442}
443
cbd80f64
JH
444bool View::eventFilter(QObject *object, QEvent *event)
445{
446 const QEvent::Type type = event->type();
333d5bbc 447 if (type == QEvent::MouseMove) {
cbd80f64
JH
448
449 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 450 if (object == _viewport)
cbd80f64 451 _hover_point = mouse_event->pos();
84a0d458 452 else if (object == _ruler || object == _cursorheader)
cbd80f64 453 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 454 else if (object == _header)
cbd80f64
JH
455 _hover_point = QPoint(0, mouse_event->y());
456 else
457 _hover_point = QPoint(-1, -1);
458
459 hover_point_changed();
460
333d5bbc 461 } else if (type == QEvent::Leave) {
cbd80f64
JH
462 _hover_point = QPoint(-1, -1);
463 hover_point_changed();
464 }
465
466 return QObject::eventFilter(object, event);
467}
468
cdf7bea7 469bool View::viewportEvent(QEvent *e)
adb4b10c
JH
470{
471 switch(e->type()) {
472 case QEvent::Paint:
473 case QEvent::MouseButtonPress:
474 case QEvent::MouseButtonRelease:
475 case QEvent::MouseButtonDblClick:
476 case QEvent::MouseMove:
477 case QEvent::Wheel:
4b4f1c0d
MC
478 case QEvent::TouchBegin:
479 case QEvent::TouchUpdate:
480 case QEvent::TouchEnd:
adb4b10c
JH
481 return false;
482
483 default:
484 return QAbstractScrollArea::viewportEvent(e);
485 }
486}
487
e314eca4 488void View::resizeEvent(QResizeEvent*)
adb4b10c 489{
d7c0ca4a 490 update_layout();
adb4b10c
JH
491}
492
b16907d3 493void View::h_scroll_value_changed(int value)
adb4b10c 494{
528bd8a1
JH
495 if (_updating_scroll)
496 return;
497
f25770e2 498 const int range = horizontalScrollBar()->maximum();
333d5bbc 499 if (range < MaxScrollValue)
f25770e2
JH
500 _offset = _scale * value;
501 else {
502 double length = 0, offset;
503 get_scroll_layout(length, offset);
504 _offset = _scale * length * value / MaxScrollValue;
505 }
506
ccdd3ef5 507 _ruler->update();
84a0d458 508 _cursorheader->update();
adb4b10c
JH
509 _viewport->update();
510}
511
cdf7bea7 512void View::v_scroll_value_changed(int value)
adb4b10c
JH
513{
514 _v_offset = value;
1d8dca91 515 _header->update();
adb4b10c
JH
516 _viewport->update();
517}
518
69dd2b03
JH
519void View::signals_changed()
520{
ef8311a4 521 int offset = SignalMargin + SignalHeight;
01fd3263 522 const vector< shared_ptr<Trace> > traces(get_traces());
d9aecf1f 523 for (shared_ptr<Trace> t : traces) {
01fd3263 524 t->set_view(this);
01fd3263 525 t->set_v_offset(offset);
ef8311a4
JH
526 offset += SignalHeight + 2 * SignalMargin;
527 }
528
a6c1726e 529 update_layout();
ef8311a4 530 normalize_layout();
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{
561 const vector< shared_ptr<Trace> > traces(get_traces());
562 for (shared_ptr<Trace> t : traces)
563 t->hover_point_changed();
564}
565
cdf7bea7
JH
566} // namespace view
567} // namespace pv