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