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