]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
RowItem: Replaced fixed signal heights with extents
[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
JH
54
55 connect(&_view, SIGNAL(signals_moved()),
56 this, SLOT(on_signals_moved()));
d7002724
JH
57}
58
14009012
JH
59void Viewport::signals_updated()
60{
61 for (shared_ptr<RowItem> r : _view) {
62 assert(r);
63 connect(r.get(), SIGNAL(appearance_changed()),
64 this, SLOT(update()));
65 }
66}
67
e314eca4 68void Viewport::paintEvent(QPaintEvent*)
d7002724 69{
aa59d5c2 70 vector< shared_ptr<RowItem> > row_items(_view.begin(), _view.end());
68b21a71
JH
71 stable_sort(row_items.begin(), row_items.end(),
72 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
73 return a->v_offset() < b->v_offset(); });
3e46726a 74
64b60583 75 QPainter p(this);
ce6e73a8 76 p.setRenderHint(QPainter::Antialiasing);
3e46726a 77
0ba172cf
JH
78 if (_view.cursors_shown())
79 _view.cursors().draw_viewport_background(p, rect());
f76af637 80
3e46726a 81 // Plot the signal
eae6e30a 82 for (const shared_ptr<RowItem> r : row_items)
3e46726a 83 {
eae6e30a
JH
84 assert(r);
85 r->paint_back(p, 0, width());
3e46726a
JH
86 }
87
eae6e30a
JH
88 for (const shared_ptr<RowItem> r : row_items)
89 r->paint_mid(p, 0, width());
fe08b6e8 90
eae6e30a
JH
91 for (const shared_ptr<RowItem> r : row_items)
92 r->paint_fore(p, 0, width());
fe08b6e8 93
0ba172cf
JH
94 if (_view.cursors_shown())
95 _view.cursors().draw_viewport_foreground(p, rect());
f76af637 96
64b60583 97 p.end();
6fa02541 98}
009e1503 99
4b4f1c0d
MC
100bool Viewport::event(QEvent *event)
101{
300fc11e 102 switch (event->type()) {
4b4f1c0d
MC
103 case QEvent::TouchBegin:
104 case QEvent::TouchUpdate:
105 case QEvent::TouchEnd:
106 if (touchEvent(static_cast<QTouchEvent *>(event)))
107 return true;
108 break;
109
110 default:
111 break;
112 }
113
114 return QWidget::event(event);
115}
116
cdf7bea7 117void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
118{
119 assert(event);
1dd95c70 120
300fc11e 121 if (event->button() == Qt::LeftButton) {
4b4f1c0d
MC
122 _mouse_down_point = event->pos();
123 _mouse_down_offset = _view.offset();
124 _mouse_down_valid = true;
125 }
126}
127
128void Viewport::mouseReleaseEvent(QMouseEvent *event)
129{
130 assert(event);
131
132 if (event->button() == Qt::LeftButton)
4b4f1c0d 133 _mouse_down_valid = false;
7cd5faf8
JH
134}
135
cdf7bea7 136void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
137{
138 assert(event);
1dd95c70 139
300fc11e
UH
140 if (event->buttons() & Qt::LeftButton) {
141 if (!_mouse_down_valid) {
4b4f1c0d
MC
142 _mouse_down_point = event->pos();
143 _mouse_down_offset = _view.offset();
144 _mouse_down_valid = true;
145 }
146
adb4b10c
JH
147 _view.set_scale_offset(_view.scale(),
148 _mouse_down_offset +
149 (_mouse_down_point - event->pos()).x() *
150 _view.scale());
1dd95c70 151 }
7cd5faf8
JH
152}
153
903038a8
JH
154void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
155{
156 assert(event);
157
158 if (event->buttons() & Qt::LeftButton)
159 _view.zoom(2.0, event->x());
160 else if (event->buttons() & Qt::RightButton)
161 _view.zoom(-2.0, event->x());
162}
163
cdf7bea7 164void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
165{
166 assert(event);
ffd5cd20
AG
167
168 if (event->orientation() == Qt::Vertical) {
169 // Vertical scrolling is interpreted as zooming in/out
170 _view.zoom(event->delta() / 120, event->x());
23840406
AG
171 } else if (event->orientation() == Qt::Horizontal) {
172 // Horizontal scrolling is interpreted as moving left/right
173 _view.set_scale_offset(_view.scale(),
174 event->delta() * _view.scale()
175 + _view.offset());
ffd5cd20 176 }
7cd5faf8
JH
177}
178
4b4f1c0d
MC
179bool Viewport::touchEvent(QTouchEvent *event)
180{
181 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
182
183 if (touchPoints.count() != 2) {
184 _pinch_zoom_active = false;
185 return false;
186 }
187
188 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
189 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
190
191 if (!_pinch_zoom_active ||
192 (event->touchPointStates() & Qt::TouchPointPressed)) {
193 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
194 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
195 _pinch_zoom_active = true;
196 }
197
198 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
199 if (abs(w) >= 1.0) {
200 double scale = (_pinch_offset1 - _pinch_offset0) / w;
201 if (scale < 0)
202 scale = -scale;
203 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
204 if (scale > 0)
205 _view.set_scale_offset(scale, offset);
206 }
207
208 if (event->touchPointStates() & Qt::TouchPointReleased) {
209 _pinch_zoom_active = false;
210
211 if (touchPoint0.state() & Qt::TouchPointReleased) {
212 // Primary touch released
213 _mouse_down_valid = false;
214 } else {
215 // Update the mouse down fields so that continued
216 // dragging with the primary touch will work correctly
217 _mouse_down_point = touchPoint0.pos().toPoint();
218 _mouse_down_offset = _view.offset();
219 _mouse_down_valid = true;
220 }
221 }
222
223 return true;
224}
225
07204819
JH
226void Viewport::on_signals_moved()
227{
228 update();
229}
230
cdf7bea7
JH
231} // namespace view
232} // namespace pv