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