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