]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Replaced using namespace with using class directives
[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 47
819f4c25
JH
48using boost::shared_ptr;
49using boost::weak_ptr;
50using std::deque;
51using std::list;
52using std::max;
53using std::min;
54using std::set;
55using std::vector;
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)),
1d8dca91 78 _header(new Header(*this)),
adb4b10c
JH
79 _data_length(0),
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()));
adb4b10c
JH
95 connect(&_session, SIGNAL(data_updated()),
96 this, SLOT(data_updated()));
1d8dca91 97
58864c5c 98 connect(_cursors.first().get(), SIGNAL(time_changed()),
ca4ec3ea 99 this, SLOT(marker_time_changed()));
58864c5c 100 connect(_cursors.second().get(), SIGNAL(time_changed()),
ca4ec3ea
JH
101 this, SLOT(marker_time_changed()));
102
d7c0ca4a
JH
103 connect(_header, SIGNAL(geometry_updated()),
104 this, SLOT(on_geometry_updated()));
54401bbb 105 connect(_header, SIGNAL(signals_moved()),
07204819 106 this, SLOT(on_signals_moved()));
54401bbb 107
17348f85
JH
108 connect(_header, SIGNAL(selection_changed()),
109 _ruler, SLOT(clear_selection()));
110 connect(_ruler, SIGNAL(selection_changed()),
111 _header, SLOT(clear_selection()));
112
8b454527
JH
113 connect(_header, SIGNAL(selection_changed()),
114 this, SIGNAL(selection_changed()));
115 connect(_ruler, SIGNAL(selection_changed()),
116 this, SIGNAL(selection_changed()));
117
adb4b10c 118 setViewport(_viewport);
cbd80f64
JH
119
120 _viewport->installEventFilter(this);
121 _ruler->installEventFilter(this);
122 _header->installEventFilter(this);
adb4b10c
JH
123}
124
1d19ef83
JH
125SigSession& View::session()
126{
127 return _session;
128}
129
38eeddea
JH
130const SigSession& View::session() const
131{
132 return _session;
133}
134
cdf7bea7 135double View::scale() const
adb4b10c
JH
136{
137 return _scale;
138}
139
cdf7bea7 140double View::offset() const
adb4b10c
JH
141{
142 return _offset;
143}
144
cdf7bea7 145int View::v_offset() const
adb4b10c
JH
146{
147 return _v_offset;
148}
149
cdf7bea7 150void View::zoom(double steps)
adb4b10c 151{
f3f98f8f 152 zoom(steps, _viewport->width() / 2);
adb4b10c
JH
153}
154
17c0f398
JH
155void View::zoom(double steps, int offset)
156{
e31551da
JH
157 const double new_scale = max(min(_scale * pow(3.0/2.0, -steps),
158 MaxScale), MinScale);
d1e7d82c 159 set_zoom(new_scale, offset);
17c0f398
JH
160}
161
ca46b534
JH
162void View::zoom_fit()
163{
164 using pv::data::SignalData;
165
166 const vector< shared_ptr<Signal> > sigs(
167 session().get_signals());
168
169 // Make a set of all the visible data objects
170 set< shared_ptr<SignalData> > visible_data;
171 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
172 if (sig->enabled())
173 visible_data.insert(sig->data());
174
175 if (visible_data.empty())
176 return;
177
178 double left_time = DBL_MAX, right_time = DBL_MIN;
179 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
180 {
181 const double start_time = d->get_start_time();
182 left_time = min(left_time, start_time);
183 right_time = max(right_time, start_time +
9d28da5a 184 d->get_max_sample_count() / d->samplerate());
ca46b534
JH
185 }
186
187 assert(left_time < right_time);
188 if (right_time - left_time < 1e-12)
189 return;
190
191 assert(_viewport);
192 const int w = _viewport->width();
193 if (w <= 0)
194 return;
195
196 set_scale_offset((right_time - left_time) / w, left_time);
197}
198
d1e7d82c
JH
199void View::zoom_one_to_one()
200{
201 using pv::data::SignalData;
202
203 const vector< shared_ptr<Signal> > sigs(
204 session().get_signals());
205
206 // Make a set of all the visible data objects
207 set< shared_ptr<SignalData> > visible_data;
208 BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
209 if (sig->enabled())
210 visible_data.insert(sig->data());
211
212 if (visible_data.empty())
213 return;
214
215 double samplerate = 0.0;
216 BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data) {
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();
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
JH
255
256 vector< shared_ptr<Trace> >::iterator i = traces.begin();
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
JH
271 const vector< shared_ptr<Trace> > traces(get_traces());
272 BOOST_FOREACH (shared_ptr<Trace> t, traces) {
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
f76af637
JH
287bool View::cursors_shown() const
288{
289 return _show_cursors;
290}
291
292void View::show_cursors(bool show)
293{
294 _show_cursors = show;
295 _ruler->update();
296 _viewport->update();
297}
298
b4d91e56
JH
299void View::centre_cursors()
300{
301 const double time_width = _scale * _viewport->width();
58864c5c
JH
302 _cursors.first()->set_time(_offset + time_width * 0.4);
303 _cursors.second()->set_time(_offset + time_width * 0.6);
b4d91e56
JH
304 _ruler->update();
305 _viewport->update();
306}
307
b42d25c4 308CursorPair& View::cursors()
f76af637
JH
309{
310 return _cursors;
311}
312
58864c5c
JH
313const CursorPair& View::cursors() const
314{
315 return _cursors;
316}
317
cbd80f64
JH
318const QPoint& View::hover_point() const
319{
320 return _hover_point;
321}
322
4b192962
JH
323void View::normalize_layout()
324{
38eeddea 325 const vector< shared_ptr<Trace> > traces(get_traces());
4b192962
JH
326
327 int v_min = INT_MAX;
38eeddea
JH
328 BOOST_FOREACH(const shared_ptr<Trace> t, traces)
329 v_min = min(t->get_v_offset(), v_min);
4b192962
JH
330
331 const int delta = -min(v_min, 0);
38eeddea
JH
332 BOOST_FOREACH(shared_ptr<Trace> t, traces)
333 t->set_v_offset(t->get_v_offset() + delta);
4b192962
JH
334
335 verticalScrollBar()->setSliderPosition(_v_offset + delta);
336 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
337}
338
9cef9567
JH
339void View::update_viewport()
340{
341 assert(_viewport);
342 _viewport->update();
343}
344
f25770e2
JH
345void View::get_scroll_layout(double &length, double &offset) const
346{
1b1ec774 347 const shared_ptr<data::SignalData> sig_data = _session.get_data();
333d5bbc 348 if (!sig_data)
f25770e2
JH
349 return;
350
9d28da5a 351 length = _data_length / (sig_data->samplerate() * _scale);
f25770e2
JH
352 offset = _offset / _scale;
353}
354
d1e7d82c
JH
355void View::set_zoom(double scale, int offset)
356{
357 const double cursor_offset = _offset + _scale * offset;
358 const double new_scale = max(min(scale, MaxScale), MinScale);
359 const double new_offset = cursor_offset - new_scale * offset;
360 set_scale_offset(new_scale, new_offset);
361}
362
cdf7bea7 363void View::update_scroll()
adb4b10c
JH
364{
365 assert(_viewport);
366
367 const QSize areaSize = _viewport->size();
368
369 // Set the horizontal scroll bar
370 double length = 0, offset = 0;
f25770e2
JH
371 get_scroll_layout(length, offset);
372 length = max(length - areaSize.width(), 0.0);
adb4b10c 373
b4ef7f2a 374 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 375
528bd8a1
JH
376 _updating_scroll = true;
377
333d5bbc 378 if (length < MaxScrollValue) {
f25770e2
JH
379 horizontalScrollBar()->setRange(0, length);
380 horizontalScrollBar()->setSliderPosition(offset);
381 } else {
382 horizontalScrollBar()->setRange(0, MaxScrollValue);
383 horizontalScrollBar()->setSliderPosition(
384 _offset * MaxScrollValue / (_scale * length));
385 }
adb4b10c 386
528bd8a1
JH
387 _updating_scroll = false;
388
adb4b10c
JH
389 // Set the vertical scrollbar
390 verticalScrollBar()->setPageStep(areaSize.height());
391 verticalScrollBar()->setRange(0,
4b192962
JH
392 _viewport->get_total_height() + SignalMargin -
393 areaSize.height());
adb4b10c
JH
394}
395
d7c0ca4a
JH
396void View::update_layout()
397{
a6c1726e
JH
398 setViewportMargins(_header->sizeHint().width(),
399 _ruler->sizeHint().height(), 0, 0);
d7c0ca4a
JH
400 _ruler->setGeometry(_viewport->x(), 0,
401 _viewport->width(), _viewport->y());
402 _header->setGeometry(0, _viewport->y(),
403 _viewport->x(), _viewport->height());
404 update_scroll();
405}
406
38eeddea
JH
407bool View::compare_trace_v_offsets(const shared_ptr<Trace> &a,
408 const shared_ptr<Trace> &b)
409{
410 assert(a);
411 assert(b);
412 return a->get_v_offset() < b->get_v_offset();
413}
414
cbd80f64
JH
415bool View::eventFilter(QObject *object, QEvent *event)
416{
417 const QEvent::Type type = event->type();
333d5bbc 418 if (type == QEvent::MouseMove) {
cbd80f64
JH
419
420 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 421 if (object == _viewport)
cbd80f64 422 _hover_point = mouse_event->pos();
333d5bbc 423 else if (object == _ruler)
cbd80f64 424 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 425 else if (object == _header)
cbd80f64
JH
426 _hover_point = QPoint(0, mouse_event->y());
427 else
428 _hover_point = QPoint(-1, -1);
429
430 hover_point_changed();
431
333d5bbc 432 } else if (type == QEvent::Leave) {
cbd80f64
JH
433 _hover_point = QPoint(-1, -1);
434 hover_point_changed();
435 }
436
437 return QObject::eventFilter(object, event);
438}
439
cdf7bea7 440bool View::viewportEvent(QEvent *e)
adb4b10c
JH
441{
442 switch(e->type()) {
443 case QEvent::Paint:
444 case QEvent::MouseButtonPress:
445 case QEvent::MouseButtonRelease:
446 case QEvent::MouseButtonDblClick:
447 case QEvent::MouseMove:
448 case QEvent::Wheel:
449 return false;
450
451 default:
452 return QAbstractScrollArea::viewportEvent(e);
453 }
454}
455
e314eca4 456void View::resizeEvent(QResizeEvent*)
adb4b10c 457{
d7c0ca4a 458 update_layout();
adb4b10c
JH
459}
460
b16907d3 461void View::h_scroll_value_changed(int value)
adb4b10c 462{
528bd8a1
JH
463 if (_updating_scroll)
464 return;
465
f25770e2 466 const int range = horizontalScrollBar()->maximum();
333d5bbc 467 if (range < MaxScrollValue)
f25770e2
JH
468 _offset = _scale * value;
469 else {
470 double length = 0, offset;
471 get_scroll_layout(length, offset);
472 _offset = _scale * length * value / MaxScrollValue;
473 }
474
ccdd3ef5 475 _ruler->update();
adb4b10c
JH
476 _viewport->update();
477}
478
cdf7bea7 479void View::v_scroll_value_changed(int value)
adb4b10c
JH
480{
481 _v_offset = value;
1d8dca91 482 _header->update();
adb4b10c
JH
483 _viewport->update();
484}
485
69dd2b03
JH
486void View::signals_changed()
487{
ef8311a4 488 int offset = SignalMargin + SignalHeight;
01fd3263
JH
489 const vector< shared_ptr<Trace> > traces(get_traces());
490 BOOST_FOREACH(shared_ptr<Trace> t, traces) {
491 t->set_view(this);
01fd3263 492 t->set_v_offset(offset);
ef8311a4
JH
493 offset += SignalHeight + 2 * SignalMargin;
494 }
495
a6c1726e 496 update_layout();
ef8311a4 497 normalize_layout();
69dd2b03
JH
498}
499
cdf7bea7 500void View::data_updated()
adb4b10c
JH
501{
502 // Get the new data length
503 _data_length = 0;
1b1ec774 504 shared_ptr<data::Logic> sig_data = _session.get_data();
333d5bbc 505 if (sig_data) {
1b1ec774 506 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
adb4b10c 507 sig_data->get_snapshots();
1b1ec774 508 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
333d5bbc 509 if (s)
adb4b10c
JH
510 _data_length = max(_data_length,
511 s->get_sample_count());
512 }
513
514 // Update the scroll bars
515 update_scroll();
516
517 // Repaint the view
518 _viewport->update();
519}
cdf7bea7 520
ca4ec3ea
JH
521void View::marker_time_changed()
522{
523 _ruler->update();
524 _viewport->update();
525}
526
07204819 527void View::on_signals_moved()
54401bbb 528{
07204819
JH
529 update_scroll();
530 signals_moved();
54401bbb
JH
531}
532
d7c0ca4a
JH
533void View::on_geometry_updated()
534{
535 update_layout();
536}
537
cdf7bea7
JH
538} // namespace view
539} // namespace pv