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