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