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