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