]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Added Bool property and bound to SR_CONF_RLE
[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
21#include <assert.h>
f25770e2 22#include <limits.h>
adb4b10c
JH
23#include <math.h>
24
25#include <boost/foreach.hpp>
26
27#include <QEvent>
cbd80f64 28#include <QMouseEvent>
adb4b10c
JH
29#include <QScrollBar>
30
1d8dca91 31#include "header.h"
ccdd3ef5 32#include "ruler.h"
2e575351 33#include "signal.h"
cdf7bea7
JH
34#include "view.h"
35#include "viewport.h"
adb4b10c 36
1b1ec774
JH
37#include "pv/sigsession.h"
38#include "pv/data/logic.h"
39#include "pv/data/logicsnapshot.h"
adb4b10c
JH
40
41using namespace boost;
42using namespace std;
43
cdf7bea7
JH
44namespace pv {
45namespace view {
adb4b10c 46
cdf7bea7
JH
47const double View::MaxScale = 1e9;
48const double View::MinScale = 1e-15;
adb4b10c 49
cdf7bea7
JH
50const int View::LabelMarginWidth = 70;
51const int View::RulerHeight = 30;
52
f25770e2
JH
53const int View::MaxScrollValue = INT_MAX / 2;
54
8d44d030
JH
55const int View::SignalHeight = 30;
56const int View::SignalMargin = 10;
49153683 57const int View::SignalSnapGridSize = 10;
1d8dca91 58
f76af637
JH
59const QColor View::CursorAreaColour(220, 231, 243);
60
2e04f9bd
JH
61const QSizeF View::LabelPadding(4, 0);
62
cdf7bea7 63View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
64 QAbstractScrollArea(parent),
65 _session(session),
cdf7bea7 66 _viewport(new Viewport(*this)),
ccdd3ef5 67 _ruler(new Ruler(*this)),
1d8dca91 68 _header(new Header(*this)),
adb4b10c
JH
69 _data_length(0),
70 _scale(1e-6),
71 _offset(0),
cbd80f64 72 _v_offset(0),
528bd8a1 73 _updating_scroll(false),
f76af637
JH
74 _show_cursors(false),
75 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
76 Cursor(*this, 1.0))),
cbd80f64 77 _hover_point(-1, -1)
adb4b10c 78{
b16907d3
JH
79 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
80 this, SLOT(h_scroll_value_changed(int)));
adb4b10c
JH
81 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
82 this, SLOT(v_scroll_value_changed(int)));
69dd2b03
JH
83
84 connect(&_session, SIGNAL(signals_changed()),
85 this, SLOT(signals_changed()));
adb4b10c
JH
86 connect(&_session, SIGNAL(data_updated()),
87 this, SLOT(data_updated()));
1d8dca91 88
ca4ec3ea
JH
89 connect(&_cursors.first, SIGNAL(time_changed()),
90 this, SLOT(marker_time_changed()));
91 connect(&_cursors.second, SIGNAL(time_changed()),
92 this, SLOT(marker_time_changed()));
93
54401bbb 94 connect(_header, SIGNAL(signals_moved()),
07204819 95 this, SLOT(on_signals_moved()));
54401bbb 96
ccdd3ef5 97 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
adb4b10c 98 setViewport(_viewport);
cbd80f64
JH
99
100 _viewport->installEventFilter(this);
101 _ruler->installEventFilter(this);
102 _header->installEventFilter(this);
adb4b10c
JH
103}
104
1d19ef83
JH
105SigSession& View::session()
106{
107 return _session;
108}
109
cdf7bea7 110double View::scale() const
adb4b10c
JH
111{
112 return _scale;
113}
114
cdf7bea7 115double View::offset() const
adb4b10c
JH
116{
117 return _offset;
118}
119
cdf7bea7 120int View::v_offset() const
adb4b10c
JH
121{
122 return _v_offset;
123}
124
cdf7bea7 125void View::zoom(double steps)
adb4b10c
JH
126{
127 zoom(steps, (width() - LabelMarginWidth) / 2);
128}
129
17c0f398
JH
130void View::zoom(double steps, int offset)
131{
132 const double cursor_offset = _offset + _scale * offset;
133 _scale *= pow(3.0/2.0, -steps);
134 _scale = max(min(_scale, MaxScale), MinScale);
135 _offset = cursor_offset - _scale * offset;
136
137 _ruler->update();
138 _viewport->update();
139 update_scroll();
140}
141
142
cdf7bea7 143void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
144{
145 _scale = scale;
146 _offset = offset;
ccdd3ef5 147
adb4b10c 148 update_scroll();
ccdd3ef5 149 _ruler->update();
adb4b10c
JH
150 _viewport->update();
151}
152
f76af637
JH
153bool View::cursors_shown() const
154{
155 return _show_cursors;
156}
157
158void View::show_cursors(bool show)
159{
160 _show_cursors = show;
161 _ruler->update();
162 _viewport->update();
163}
164
165std::pair<Cursor, Cursor>& View::cursors()
166{
167 return _cursors;
168}
169
cbd80f64
JH
170const QPoint& View::hover_point() const
171{
172 return _hover_point;
173}
174
4b192962
JH
175void View::normalize_layout()
176{
3868e5fa 177 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
4b192962
JH
178
179 int v_min = INT_MAX;
180 BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
181 v_min = min(s->get_v_offset(), v_min);
182
183 const int delta = -min(v_min, 0);
184 BOOST_FOREACH(shared_ptr<Signal> s, sigs)
185 s->set_v_offset(s->get_v_offset() + delta);
186
187 verticalScrollBar()->setSliderPosition(_v_offset + delta);
188 v_scroll_value_changed(verticalScrollBar()->sliderPosition());
189}
190
f25770e2
JH
191void View::get_scroll_layout(double &length, double &offset) const
192{
1b1ec774 193 const shared_ptr<data::SignalData> sig_data = _session.get_data();
333d5bbc 194 if (!sig_data)
f25770e2
JH
195 return;
196
197 length = _data_length / (sig_data->get_samplerate() * _scale);
198 offset = _offset / _scale;
199}
200
cdf7bea7 201void View::update_scroll()
adb4b10c
JH
202{
203 assert(_viewport);
204
205 const QSize areaSize = _viewport->size();
206
207 // Set the horizontal scroll bar
208 double length = 0, offset = 0;
f25770e2
JH
209 get_scroll_layout(length, offset);
210 length = max(length - areaSize.width(), 0.0);
adb4b10c 211
b4ef7f2a 212 horizontalScrollBar()->setPageStep(areaSize.width() / 2);
f25770e2 213
528bd8a1
JH
214 _updating_scroll = true;
215
333d5bbc 216 if (length < MaxScrollValue) {
f25770e2
JH
217 horizontalScrollBar()->setRange(0, length);
218 horizontalScrollBar()->setSliderPosition(offset);
219 } else {
220 horizontalScrollBar()->setRange(0, MaxScrollValue);
221 horizontalScrollBar()->setSliderPosition(
222 _offset * MaxScrollValue / (_scale * length));
223 }
adb4b10c 224
528bd8a1
JH
225 _updating_scroll = false;
226
adb4b10c
JH
227 // Set the vertical scrollbar
228 verticalScrollBar()->setPageStep(areaSize.height());
229 verticalScrollBar()->setRange(0,
4b192962
JH
230 _viewport->get_total_height() + SignalMargin -
231 areaSize.height());
adb4b10c
JH
232}
233
2e575351
JH
234void View::reset_signal_layout()
235{
2658961b 236 int offset = SignalMargin + SignalHeight;
3868e5fa 237 const vector< shared_ptr<Signal> > sigs(_session.get_signals());
2e575351
JH
238 BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
239 s->set_v_offset(offset);
8d44d030 240 offset += SignalHeight + 2 * SignalMargin;
2e575351 241 }
4b192962
JH
242
243 normalize_layout();
2e575351
JH
244}
245
cbd80f64
JH
246bool View::eventFilter(QObject *object, QEvent *event)
247{
248 const QEvent::Type type = event->type();
333d5bbc 249 if (type == QEvent::MouseMove) {
cbd80f64
JH
250
251 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
333d5bbc 252 if (object == _viewport)
cbd80f64 253 _hover_point = mouse_event->pos();
333d5bbc 254 else if (object == _ruler)
cbd80f64 255 _hover_point = QPoint(mouse_event->x(), 0);
333d5bbc 256 else if (object == _header)
cbd80f64
JH
257 _hover_point = QPoint(0, mouse_event->y());
258 else
259 _hover_point = QPoint(-1, -1);
260
261 hover_point_changed();
262
333d5bbc 263 } else if (type == QEvent::Leave) {
cbd80f64
JH
264 _hover_point = QPoint(-1, -1);
265 hover_point_changed();
266 }
267
268 return QObject::eventFilter(object, event);
269}
270
cdf7bea7 271bool View::viewportEvent(QEvent *e)
adb4b10c
JH
272{
273 switch(e->type()) {
274 case QEvent::Paint:
275 case QEvent::MouseButtonPress:
276 case QEvent::MouseButtonRelease:
277 case QEvent::MouseButtonDblClick:
278 case QEvent::MouseMove:
279 case QEvent::Wheel:
280 return false;
281
282 default:
283 return QAbstractScrollArea::viewportEvent(e);
284 }
285}
286
e314eca4 287void View::resizeEvent(QResizeEvent*)
adb4b10c 288{
ccdd3ef5
JH
289 _ruler->setGeometry(_viewport->x(), 0,
290 _viewport->width(), _viewport->y());
291 _header->setGeometry(0, _viewport->y(),
1d8dca91 292 _viewport->x(), _viewport->height());
adb4b10c
JH
293 update_scroll();
294}
295
b16907d3 296void View::h_scroll_value_changed(int value)
adb4b10c 297{
528bd8a1
JH
298 if (_updating_scroll)
299 return;
300
f25770e2 301 const int range = horizontalScrollBar()->maximum();
333d5bbc 302 if (range < MaxScrollValue)
f25770e2
JH
303 _offset = _scale * value;
304 else {
305 double length = 0, offset;
306 get_scroll_layout(length, offset);
307 _offset = _scale * length * value / MaxScrollValue;
308 }
309
ccdd3ef5 310 _ruler->update();
adb4b10c
JH
311 _viewport->update();
312}
313
cdf7bea7 314void View::v_scroll_value_changed(int value)
adb4b10c
JH
315{
316 _v_offset = value;
1d8dca91 317 _header->update();
adb4b10c
JH
318 _viewport->update();
319}
320
69dd2b03
JH
321void View::signals_changed()
322{
323 reset_signal_layout();
324}
325
cdf7bea7 326void View::data_updated()
adb4b10c
JH
327{
328 // Get the new data length
329 _data_length = 0;
1b1ec774 330 shared_ptr<data::Logic> sig_data = _session.get_data();
333d5bbc 331 if (sig_data) {
1b1ec774 332 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
adb4b10c 333 sig_data->get_snapshots();
1b1ec774 334 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
333d5bbc 335 if (s)
adb4b10c
JH
336 _data_length = max(_data_length,
337 s->get_sample_count());
338 }
339
340 // Update the scroll bars
341 update_scroll();
342
343 // Repaint the view
344 _viewport->update();
345}
cdf7bea7 346
ca4ec3ea
JH
347void View::marker_time_changed()
348{
349 _ruler->update();
350 _viewport->update();
351}
352
07204819 353void View::on_signals_moved()
54401bbb 354{
07204819
JH
355 update_scroll();
356 signals_moved();
54401bbb
JH
357}
358
cdf7bea7
JH
359} // namespace view
360} // namespace pv