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