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