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