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