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