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