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