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