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