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