]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Moved forward declaration of SignalData to correct namespace
[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"
cdf7bea7
JH
33#include "view.h"
34#include "viewport.h"
adb4b10c 35
51e77110
JH
36#include "../logicdata.h"
37#include "../logicdatasnapshot.h"
38#include "../sigsession.h"
adb4b10c
JH
39
40using namespace boost;
41using namespace std;
42
cdf7bea7
JH
43namespace pv {
44namespace view {
adb4b10c 45
cdf7bea7
JH
46const double View::MaxScale = 1e9;
47const double View::MinScale = 1e-15;
adb4b10c 48
cdf7bea7
JH
49const int View::LabelMarginWidth = 70;
50const int View::RulerHeight = 30;
51
f25770e2
JH
52const int View::MaxScrollValue = INT_MAX / 2;
53
1d8dca91
JH
54const int View::SignalHeight = 50;
55
f76af637
JH
56const QColor View::CursorAreaColour(220, 231, 243);
57
2e04f9bd
JH
58const QSizeF View::LabelPadding(4, 0);
59
cdf7bea7 60View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
61 QAbstractScrollArea(parent),
62 _session(session),
cdf7bea7 63 _viewport(new Viewport(*this)),
ccdd3ef5 64 _ruler(new Ruler(*this)),
1d8dca91 65 _header(new Header(*this)),
adb4b10c
JH
66 _data_length(0),
67 _scale(1e-6),
68 _offset(0),
cbd80f64 69 _v_offset(0),
f76af637
JH
70 _show_cursors(false),
71 _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
72 Cursor(*this, 1.0))),
cbd80f64 73 _hover_point(-1, -1)
adb4b10c
JH
74{
75 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
76 this, SLOT(h_scroll_value_changed(int)));
77 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
78 this, SLOT(v_scroll_value_changed(int)));
79 connect(&_session, SIGNAL(data_updated()),
80 this, SLOT(data_updated()));
1d8dca91 81
ca4ec3ea
JH
82 connect(&_cursors.first, SIGNAL(time_changed()),
83 this, SLOT(marker_time_changed()));
84 connect(&_cursors.second, SIGNAL(time_changed()),
85 this, SLOT(marker_time_changed()));
86
ccdd3ef5 87 setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
adb4b10c 88 setViewport(_viewport);
cbd80f64
JH
89
90 _viewport->installEventFilter(this);
91 _ruler->installEventFilter(this);
92 _header->installEventFilter(this);
adb4b10c
JH
93}
94
1d19ef83
JH
95SigSession& View::session()
96{
97 return _session;
98}
99
cdf7bea7 100double View::scale() const
adb4b10c
JH
101{
102 return _scale;
103}
104
cdf7bea7 105double View::offset() const
adb4b10c
JH
106{
107 return _offset;
108}
109
cdf7bea7 110int View::v_offset() const
adb4b10c
JH
111{
112 return _v_offset;
113}
114
cdf7bea7 115void View::zoom(double steps)
adb4b10c
JH
116{
117 zoom(steps, (width() - LabelMarginWidth) / 2);
118}
119
17c0f398
JH
120void View::zoom(double steps, int offset)
121{
122 const double cursor_offset = _offset + _scale * offset;
123 _scale *= pow(3.0/2.0, -steps);
124 _scale = max(min(_scale, MaxScale), MinScale);
125 _offset = cursor_offset - _scale * offset;
126
127 _ruler->update();
128 _viewport->update();
129 update_scroll();
130}
131
132
cdf7bea7 133void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
134{
135 _scale = scale;
136 _offset = offset;
ccdd3ef5 137
adb4b10c 138 update_scroll();
ccdd3ef5 139 _ruler->update();
adb4b10c
JH
140 _viewport->update();
141}
142
f76af637
JH
143bool View::cursors_shown() const
144{
145 return _show_cursors;
146}
147
148void View::show_cursors(bool show)
149{
150 _show_cursors = show;
151 _ruler->update();
152 _viewport->update();
153}
154
155std::pair<Cursor, Cursor>& View::cursors()
156{
157 return _cursors;
158}
159
cbd80f64
JH
160const QPoint& View::hover_point() const
161{
162 return _hover_point;
163}
164
f25770e2
JH
165void View::get_scroll_layout(double &length, double &offset) const
166{
167 const shared_ptr<SignalData> sig_data = _session.get_data();
168 if(!sig_data)
169 return;
170
171 length = _data_length / (sig_data->get_samplerate() * _scale);
172 offset = _offset / _scale;
173}
174
cdf7bea7 175void View::update_scroll()
adb4b10c
JH
176{
177 assert(_viewport);
178
179 const QSize areaSize = _viewport->size();
180
181 // Set the horizontal scroll bar
182 double length = 0, offset = 0;
f25770e2
JH
183 get_scroll_layout(length, offset);
184 length = max(length - areaSize.width(), 0.0);
adb4b10c
JH
185
186 horizontalScrollBar()->setPageStep(areaSize.width());
f25770e2
JH
187
188 if(length < MaxScrollValue) {
189 horizontalScrollBar()->setRange(0, length);
190 horizontalScrollBar()->setSliderPosition(offset);
191 } else {
192 horizontalScrollBar()->setRange(0, MaxScrollValue);
193 horizontalScrollBar()->setSliderPosition(
194 _offset * MaxScrollValue / (_scale * length));
195 }
adb4b10c
JH
196
197 // Set the vertical scrollbar
198 verticalScrollBar()->setPageStep(areaSize.height());
199 verticalScrollBar()->setRange(0,
200 _viewport->get_total_height() - areaSize.height());
201}
202
cbd80f64
JH
203bool View::eventFilter(QObject *object, QEvent *event)
204{
205 const QEvent::Type type = event->type();
206 if(type == QEvent::MouseMove) {
207
208 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
209 if(object == _viewport)
210 _hover_point = mouse_event->pos();
211 else if(object == _ruler)
212 _hover_point = QPoint(mouse_event->x(), 0);
213 else if(object == _header)
214 _hover_point = QPoint(0, mouse_event->y());
215 else
216 _hover_point = QPoint(-1, -1);
217
218 hover_point_changed();
219
220 } else if(type == QEvent::Leave) {
221 _hover_point = QPoint(-1, -1);
222 hover_point_changed();
223 }
224
225 return QObject::eventFilter(object, event);
226}
227
cdf7bea7 228bool View::viewportEvent(QEvent *e)
adb4b10c
JH
229{
230 switch(e->type()) {
231 case QEvent::Paint:
232 case QEvent::MouseButtonPress:
233 case QEvent::MouseButtonRelease:
234 case QEvent::MouseButtonDblClick:
235 case QEvent::MouseMove:
236 case QEvent::Wheel:
237 return false;
238
239 default:
240 return QAbstractScrollArea::viewportEvent(e);
241 }
242}
243
cdf7bea7 244void View::resizeEvent(QResizeEvent *e)
adb4b10c 245{
ccdd3ef5
JH
246 _ruler->setGeometry(_viewport->x(), 0,
247 _viewport->width(), _viewport->y());
248 _header->setGeometry(0, _viewport->y(),
1d8dca91 249 _viewport->x(), _viewport->height());
adb4b10c
JH
250 update_scroll();
251}
252
cdf7bea7 253void View::h_scroll_value_changed(int value)
adb4b10c 254{
f25770e2
JH
255 const int range = horizontalScrollBar()->maximum();
256 if(range < MaxScrollValue)
257 _offset = _scale * value;
258 else {
259 double length = 0, offset;
260 get_scroll_layout(length, offset);
261 _offset = _scale * length * value / MaxScrollValue;
262 }
263
ccdd3ef5 264 _ruler->update();
adb4b10c
JH
265 _viewport->update();
266}
267
cdf7bea7 268void View::v_scroll_value_changed(int value)
adb4b10c
JH
269{
270 _v_offset = value;
1d8dca91 271 _header->update();
adb4b10c
JH
272 _viewport->update();
273}
274
cdf7bea7 275void View::data_updated()
adb4b10c
JH
276{
277 // Get the new data length
278 _data_length = 0;
279 shared_ptr<LogicData> sig_data = _session.get_data();
280 if(sig_data) {
281 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
282 sig_data->get_snapshots();
283 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
284 if(s)
285 _data_length = max(_data_length,
286 s->get_sample_count());
287 }
288
289 // Update the scroll bars
290 update_scroll();
291
292 // Repaint the view
293 _viewport->update();
294}
cdf7bea7 295
ca4ec3ea
JH
296void View::marker_time_changed()
297{
298 _ruler->update();
299 _viewport->update();
300}
301
cdf7bea7
JH
302} // namespace view
303} // namespace pv