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