]>
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 JH |
21 | #include <cassert> |
22 | ||
cdf7bea7 JH |
23 | #include "view.h" |
24 | #include "viewport.h" | |
6fa02541 | 25 | |
8d634081 | 26 | #include "signal.h" |
51e77110 | 27 | #include "../sigsession.h" |
e3f65ace | 28 | |
7cd5faf8 | 29 | #include <QMouseEvent> |
e9c41f8c | 30 | |
819f4c25 JH |
31 | using std::max; |
32 | using std::min; | |
f9abf97e | 33 | using std::shared_ptr; |
819f4c25 | 34 | using std::vector; |
e3f65ace | 35 | |
cdf7bea7 JH |
36 | namespace pv { |
37 | namespace view { | |
e9c41f8c | 38 | |
cdf7bea7 | 39 | Viewport::Viewport(View &parent) : |
64b60583 | 40 | QWidget(&parent), |
300fc11e | 41 | _view(parent), |
4b4f1c0d MC |
42 | _mouse_down_valid(false), |
43 | _pinch_zoom_active(false) | |
6fa02541 | 44 | { |
4b4f1c0d MC |
45 | setAttribute(Qt::WA_AcceptTouchEvents, true); |
46 | ||
d7002724 | 47 | setMouseTracking(true); |
64b60583 JH |
48 | setAutoFillBackground(true); |
49 | setBackgroundRole(QPalette::Base); | |
07204819 | 50 | |
aca00b1e JH |
51 | connect(&_view.session(), SIGNAL(signals_changed()), |
52 | this, SLOT(on_signals_changed())); | |
53 | ||
07204819 JH |
54 | connect(&_view, SIGNAL(signals_moved()), |
55 | this, SLOT(on_signals_moved())); | |
9f46d905 JH |
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(); | |
d7002724 JH |
60 | } |
61 | ||
cdf7bea7 | 62 | int Viewport::get_total_height() const |
a429590b | 63 | { |
07204819 | 64 | int h = 0; |
38eeddea | 65 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
d9aecf1f | 66 | for (const shared_ptr<Trace> t : traces) { |
38eeddea JH |
67 | assert(t); |
68 | h = max(t->get_v_offset() + View::SignalHeight, h); | |
adb4b10c JH |
69 | } |
70 | ||
07204819 | 71 | return h; |
a429590b JH |
72 | } |
73 | ||
e314eca4 | 74 | void Viewport::paintEvent(QPaintEvent*) |
d7002724 | 75 | { |
38eeddea | 76 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); |
3e46726a | 77 | |
64b60583 | 78 | QPainter p(this); |
ce6e73a8 | 79 | p.setRenderHint(QPainter::Antialiasing); |
3e46726a | 80 | |
0ba172cf JH |
81 | if (_view.cursors_shown()) |
82 | _view.cursors().draw_viewport_background(p, rect()); | |
f76af637 | 83 | |
3e46726a | 84 | // Plot the signal |
d9aecf1f | 85 | for (const shared_ptr<Trace> t : traces) |
3e46726a | 86 | { |
38eeddea | 87 | assert(t); |
fe08b6e8 | 88 | t->paint_back(p, 0, width()); |
3e46726a JH |
89 | } |
90 | ||
d9aecf1f | 91 | for (const shared_ptr<Trace> t : traces) |
fe08b6e8 JH |
92 | t->paint_mid(p, 0, width()); |
93 | ||
d9aecf1f | 94 | for (const shared_ptr<Trace> t : traces) |
fe08b6e8 JH |
95 | t->paint_fore(p, 0, width()); |
96 | ||
0ba172cf JH |
97 | if (_view.cursors_shown()) |
98 | _view.cursors().draw_viewport_foreground(p, rect()); | |
f76af637 | 99 | |
64b60583 | 100 | p.end(); |
6fa02541 | 101 | } |
009e1503 | 102 | |
4b4f1c0d MC |
103 | bool Viewport::event(QEvent *event) |
104 | { | |
300fc11e | 105 | switch (event->type()) { |
4b4f1c0d MC |
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 | ||
cdf7bea7 | 120 | void Viewport::mousePressEvent(QMouseEvent *event) |
7cd5faf8 JH |
121 | { |
122 | assert(event); | |
1dd95c70 | 123 | |
300fc11e | 124 | if (event->button() == Qt::LeftButton) { |
4b4f1c0d MC |
125 | _mouse_down_point = event->pos(); |
126 | _mouse_down_offset = _view.offset(); | |
127 | _mouse_down_valid = true; | |
128 | } | |
129 | } | |
130 | ||
131 | void Viewport::mouseReleaseEvent(QMouseEvent *event) | |
132 | { | |
133 | assert(event); | |
134 | ||
135 | if (event->button() == Qt::LeftButton) | |
4b4f1c0d | 136 | _mouse_down_valid = false; |
7cd5faf8 JH |
137 | } |
138 | ||
cdf7bea7 | 139 | void Viewport::mouseMoveEvent(QMouseEvent *event) |
7cd5faf8 JH |
140 | { |
141 | assert(event); | |
1dd95c70 | 142 | |
300fc11e UH |
143 | if (event->buttons() & Qt::LeftButton) { |
144 | if (!_mouse_down_valid) { | |
4b4f1c0d MC |
145 | _mouse_down_point = event->pos(); |
146 | _mouse_down_offset = _view.offset(); | |
147 | _mouse_down_valid = true; | |
148 | } | |
149 | ||
adb4b10c JH |
150 | _view.set_scale_offset(_view.scale(), |
151 | _mouse_down_offset + | |
152 | (_mouse_down_point - event->pos()).x() * | |
153 | _view.scale()); | |
1dd95c70 | 154 | } |
7cd5faf8 JH |
155 | } |
156 | ||
903038a8 JH |
157 | void 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 | ||
cdf7bea7 | 167 | void Viewport::wheelEvent(QWheelEvent *event) |
1dd95c70 JH |
168 | { |
169 | assert(event); | |
ffd5cd20 AG |
170 | |
171 | if (event->orientation() == Qt::Vertical) { | |
172 | // Vertical scrolling is interpreted as zooming in/out | |
173 | _view.zoom(event->delta() / 120, event->x()); | |
23840406 AG |
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()); | |
ffd5cd20 | 179 | } |
7cd5faf8 JH |
180 | } |
181 | ||
4b4f1c0d MC |
182 | bool 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 | ||
aca00b1e JH |
229 | void Viewport::on_signals_changed() |
230 | { | |
231 | const vector< shared_ptr<Trace> > traces(_view.get_traces()); | |
d9aecf1f | 232 | for (shared_ptr<Trace> t : traces) { |
aca00b1e JH |
233 | assert(t); |
234 | connect(t.get(), SIGNAL(visibility_changed()), | |
235 | this, SLOT(update())); | |
236 | } | |
237 | } | |
238 | ||
07204819 JH |
239 | void Viewport::on_signals_moved() |
240 | { | |
241 | update(); | |
242 | } | |
243 | ||
cdf7bea7 JH |
244 | } // namespace view |
245 | } // namespace pv |