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