]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
CMakeLists.txt: For Android, make a library out of the native code
[pulseview.git] / pv / view / viewport.cpp
CommitLineData
6fa02541 1/*
b3f22de0 2 * This file is part of the PulseView project.
6fa02541
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
d2344534
JH
21#include <cassert>
22
cdf7bea7
JH
23#include "view.h"
24#include "viewport.h"
6fa02541 25
8d634081 26#include "signal.h"
51e77110 27#include "../sigsession.h"
e3f65ace 28
7cd5faf8 29#include <QMouseEvent>
e9c41f8c 30
819f4c25
JH
31using std::max;
32using std::min;
f9abf97e 33using std::shared_ptr;
819f4c25 34using std::vector;
e3f65ace 35
cdf7bea7
JH
36namespace pv {
37namespace view {
e9c41f8c 38
cdf7bea7 39Viewport::Viewport(View &parent) :
64b60583 40 QWidget(&parent),
4b4f1c0d
MC
41 _view(parent),
42 _mouse_down_valid(false),
43 _pinch_zoom_active(false)
6fa02541 44{
4b4f1c0d
MC
45 setAttribute(Qt::WA_AcceptTouchEvents, true);
46
d7002724 47 setMouseTracking(true);
64b60583
JH
48 setAutoFillBackground(true);
49 setBackgroundRole(QPalette::Base);
07204819 50
aca00b1e
JH
51 connect(&_view.session(), SIGNAL(signals_changed()),
52 this, SLOT(on_signals_changed()));
53
07204819
JH
54 connect(&_view, SIGNAL(signals_moved()),
55 this, SLOT(on_signals_moved()));
9f46d905
JH
56
57 // Trigger the initial event manually. The default device has signals
58 // which were created before this object came into being
59 on_signals_changed();
d7002724
JH
60}
61
cdf7bea7 62int Viewport::get_total_height() const
a429590b 63{
07204819 64 int h = 0;
38eeddea 65 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 66 for (const shared_ptr<Trace> t : traces) {
38eeddea
JH
67 assert(t);
68 h = max(t->get_v_offset() + View::SignalHeight, h);
adb4b10c
JH
69 }
70
07204819 71 return h;
a429590b
JH
72}
73
e314eca4 74void Viewport::paintEvent(QPaintEvent*)
d7002724 75{
38eeddea 76 const vector< shared_ptr<Trace> > traces(_view.get_traces());
3e46726a 77
64b60583 78 QPainter p(this);
ce6e73a8 79 p.setRenderHint(QPainter::Antialiasing);
3e46726a 80
0ba172cf
JH
81 if (_view.cursors_shown())
82 _view.cursors().draw_viewport_background(p, rect());
f76af637 83
3e46726a 84 // Plot the signal
d9aecf1f 85 for (const shared_ptr<Trace> t : traces)
3e46726a 86 {
38eeddea 87 assert(t);
fe08b6e8 88 t->paint_back(p, 0, width());
3e46726a
JH
89 }
90
d9aecf1f 91 for (const shared_ptr<Trace> t : traces)
fe08b6e8
JH
92 t->paint_mid(p, 0, width());
93
d9aecf1f 94 for (const shared_ptr<Trace> t : traces)
fe08b6e8
JH
95 t->paint_fore(p, 0, width());
96
0ba172cf
JH
97 if (_view.cursors_shown())
98 _view.cursors().draw_viewport_foreground(p, rect());
f76af637 99
64b60583 100 p.end();
6fa02541 101}
009e1503 102
4b4f1c0d
MC
103bool Viewport::event(QEvent *event)
104{
105 switch(event->type()) {
106 case QEvent::TouchBegin:
107 case QEvent::TouchUpdate:
108 case QEvent::TouchEnd:
109 if (touchEvent(static_cast<QTouchEvent *>(event)))
110 return true;
111 break;
112
113 default:
114 break;
115 }
116
117 return QWidget::event(event);
118}
119
cdf7bea7 120void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
121{
122 assert(event);
1dd95c70 123
4b4f1c0d
MC
124 if (event->button() == Qt::LeftButton)
125 {
126 _mouse_down_point = event->pos();
127 _mouse_down_offset = _view.offset();
128 _mouse_down_valid = true;
129 }
130}
131
132void Viewport::mouseReleaseEvent(QMouseEvent *event)
133{
134 assert(event);
135
136 if (event->button() == Qt::LeftButton)
137 {
138 _mouse_down_valid = false;
139 }
7cd5faf8
JH
140}
141
cdf7bea7 142void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
143{
144 assert(event);
1dd95c70 145
333d5bbc 146 if (event->buttons() & Qt::LeftButton)
1dd95c70 147 {
4b4f1c0d
MC
148 if (! _mouse_down_valid) {
149 _mouse_down_point = event->pos();
150 _mouse_down_offset = _view.offset();
151 _mouse_down_valid = true;
152 }
153
adb4b10c
JH
154 _view.set_scale_offset(_view.scale(),
155 _mouse_down_offset +
156 (_mouse_down_point - event->pos()).x() *
157 _view.scale());
1dd95c70 158 }
7cd5faf8
JH
159}
160
903038a8
JH
161void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
162{
163 assert(event);
164
165 if (event->buttons() & Qt::LeftButton)
166 _view.zoom(2.0, event->x());
167 else if (event->buttons() & Qt::RightButton)
168 _view.zoom(-2.0, event->x());
169}
170
cdf7bea7 171void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
172{
173 assert(event);
ffd5cd20
AG
174
175 if (event->orientation() == Qt::Vertical) {
176 // Vertical scrolling is interpreted as zooming in/out
177 _view.zoom(event->delta() / 120, event->x());
23840406
AG
178 } else if (event->orientation() == Qt::Horizontal) {
179 // Horizontal scrolling is interpreted as moving left/right
180 _view.set_scale_offset(_view.scale(),
181 event->delta() * _view.scale()
182 + _view.offset());
ffd5cd20 183 }
7cd5faf8
JH
184}
185
4b4f1c0d
MC
186bool Viewport::touchEvent(QTouchEvent *event)
187{
188 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
189
190 if (touchPoints.count() != 2) {
191 _pinch_zoom_active = false;
192 return false;
193 }
194
195 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
196 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
197
198 if (!_pinch_zoom_active ||
199 (event->touchPointStates() & Qt::TouchPointPressed)) {
200 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
201 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
202 _pinch_zoom_active = true;
203 }
204
205 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
206 if (abs(w) >= 1.0) {
207 double scale = (_pinch_offset1 - _pinch_offset0) / w;
208 if (scale < 0)
209 scale = -scale;
210 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
211 if (scale > 0)
212 _view.set_scale_offset(scale, offset);
213 }
214
215 if (event->touchPointStates() & Qt::TouchPointReleased) {
216 _pinch_zoom_active = false;
217
218 if (touchPoint0.state() & Qt::TouchPointReleased) {
219 // Primary touch released
220 _mouse_down_valid = false;
221 } else {
222 // Update the mouse down fields so that continued
223 // dragging with the primary touch will work correctly
224 _mouse_down_point = touchPoint0.pos().toPoint();
225 _mouse_down_offset = _view.offset();
226 _mouse_down_valid = true;
227 }
228 }
229
230 return true;
231}
232
aca00b1e
JH
233void Viewport::on_signals_changed()
234{
235 const vector< shared_ptr<Trace> > traces(_view.get_traces());
d9aecf1f 236 for (shared_ptr<Trace> t : traces) {
aca00b1e
JH
237 assert(t);
238 connect(t.get(), SIGNAL(visibility_changed()),
239 this, SLOT(update()));
240 }
241}
242
07204819
JH
243void Viewport::on_signals_moved()
244{
245 update();
246}
247
cdf7bea7
JH
248} // namespace view
249} // namespace pv