]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
android/: Add some missing license headers.
[pulseview.git] / pv / view / viewport.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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 <cassert>
22
23#include "view.h"
24#include "viewport.h"
25
26#include "signal.h"
27#include "../sigsession.h"
28
29#include <QMouseEvent>
30
31using std::max;
32using std::min;
33using std::shared_ptr;
34using std::vector;
35
36namespace pv {
37namespace view {
38
39Viewport::Viewport(View &parent) :
40 QWidget(&parent),
41 _view(parent),
42 _mouse_down_valid(false),
43 _pinch_zoom_active(false)
44{
45 setAttribute(Qt::WA_AcceptTouchEvents, true);
46
47 setMouseTracking(true);
48 setAutoFillBackground(true);
49 setBackgroundRole(QPalette::Base);
50
51 connect(&_view.session(), SIGNAL(signals_changed()),
52 this, SLOT(on_signals_changed()));
53
54 connect(&_view, SIGNAL(signals_moved()),
55 this, SLOT(on_signals_moved()));
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();
60}
61
62int Viewport::get_total_height() const
63{
64 int h = 0;
65 const vector< shared_ptr<Trace> > traces(_view.get_traces());
66 for (const shared_ptr<Trace> t : traces) {
67 assert(t);
68 h = max(t->get_v_offset() + View::SignalHeight, h);
69 }
70
71 return h;
72}
73
74void Viewport::paintEvent(QPaintEvent*)
75{
76 const vector< shared_ptr<Trace> > traces(_view.get_traces());
77
78 QPainter p(this);
79 p.setRenderHint(QPainter::Antialiasing);
80
81 if (_view.cursors_shown())
82 _view.cursors().draw_viewport_background(p, rect());
83
84 // Plot the signal
85 for (const shared_ptr<Trace> t : traces)
86 {
87 assert(t);
88 t->paint_back(p, 0, width());
89 }
90
91 for (const shared_ptr<Trace> t : traces)
92 t->paint_mid(p, 0, width());
93
94 for (const shared_ptr<Trace> t : traces)
95 t->paint_fore(p, 0, width());
96
97 if (_view.cursors_shown())
98 _view.cursors().draw_viewport_foreground(p, rect());
99
100 p.end();
101}
102
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
120void Viewport::mousePressEvent(QMouseEvent *event)
121{
122 assert(event);
123
124 if (event->button() == Qt::LeftButton) {
125 _mouse_down_point = event->pos();
126 _mouse_down_offset = _view.offset();
127 _mouse_down_valid = true;
128 }
129}
130
131void Viewport::mouseReleaseEvent(QMouseEvent *event)
132{
133 assert(event);
134
135 if (event->button() == Qt::LeftButton)
136 _mouse_down_valid = false;
137}
138
139void Viewport::mouseMoveEvent(QMouseEvent *event)
140{
141 assert(event);
142
143 if (event->buttons() & Qt::LeftButton) {
144 if (!_mouse_down_valid) {
145 _mouse_down_point = event->pos();
146 _mouse_down_offset = _view.offset();
147 _mouse_down_valid = true;
148 }
149
150 _view.set_scale_offset(_view.scale(),
151 _mouse_down_offset +
152 (_mouse_down_point - event->pos()).x() *
153 _view.scale());
154 }
155}
156
157void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
158{
159 assert(event);
160
161 if (event->buttons() & Qt::LeftButton)
162 _view.zoom(2.0, event->x());
163 else if (event->buttons() & Qt::RightButton)
164 _view.zoom(-2.0, event->x());
165}
166
167void Viewport::wheelEvent(QWheelEvent *event)
168{
169 assert(event);
170
171 if (event->orientation() == Qt::Vertical) {
172 // Vertical scrolling is interpreted as zooming in/out
173 _view.zoom(event->delta() / 120, event->x());
174 } else if (event->orientation() == Qt::Horizontal) {
175 // Horizontal scrolling is interpreted as moving left/right
176 _view.set_scale_offset(_view.scale(),
177 event->delta() * _view.scale()
178 + _view.offset());
179 }
180}
181
182bool Viewport::touchEvent(QTouchEvent *event)
183{
184 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
185
186 if (touchPoints.count() != 2) {
187 _pinch_zoom_active = false;
188 return false;
189 }
190
191 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
192 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
193
194 if (!_pinch_zoom_active ||
195 (event->touchPointStates() & Qt::TouchPointPressed)) {
196 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
197 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
198 _pinch_zoom_active = true;
199 }
200
201 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
202 if (abs(w) >= 1.0) {
203 double scale = (_pinch_offset1 - _pinch_offset0) / w;
204 if (scale < 0)
205 scale = -scale;
206 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
207 if (scale > 0)
208 _view.set_scale_offset(scale, offset);
209 }
210
211 if (event->touchPointStates() & Qt::TouchPointReleased) {
212 _pinch_zoom_active = false;
213
214 if (touchPoint0.state() & Qt::TouchPointReleased) {
215 // Primary touch released
216 _mouse_down_valid = false;
217 } else {
218 // Update the mouse down fields so that continued
219 // dragging with the primary touch will work correctly
220 _mouse_down_point = touchPoint0.pos().toPoint();
221 _mouse_down_offset = _view.offset();
222 _mouse_down_valid = true;
223 }
224 }
225
226 return true;
227}
228
229void Viewport::on_signals_changed()
230{
231 const vector< shared_ptr<Trace> > traces(_view.get_traces());
232 for (shared_ptr<Trace> t : traces) {
233 assert(t);
234 connect(t.get(), SIGNAL(visibility_changed()),
235 this, SLOT(update()));
236 }
237}
238
239void Viewport::on_signals_moved()
240{
241 update();
242}
243
244} // namespace view
245} // namespace pv