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