]>
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> |
28290534 | 24 | #include <limits> |
d2344534 | 25 | |
2acdb232 JH |
26 | #include "signal.hpp" |
27 | #include "view.hpp" | |
5b5fa4da | 28 | #include "viewitempaintparams.hpp" |
2acdb232 | 29 | #include "viewport.hpp" |
6fa02541 | 30 | |
f65cd27b | 31 | #include <pv/session.hpp> |
e3f65ace | 32 | |
7cd5faf8 | 33 | #include <QMouseEvent> |
e9c41f8c | 34 | |
f4e57597 SA |
35 | #include <QDebug> |
36 | ||
c8870d9e | 37 | using std::abs; |
e9e4e5e7 JH |
38 | using std::back_inserter; |
39 | using std::copy; | |
e8b969a9 | 40 | using std::dynamic_pointer_cast; |
819f4c25 JH |
41 | using std::max; |
42 | using std::min; | |
beb897c6 | 43 | using std::none_of; |
28290534 | 44 | using std::numeric_limits; |
f9abf97e | 45 | using std::shared_ptr; |
68b21a71 | 46 | using std::stable_sort; |
819f4c25 | 47 | using std::vector; |
e3f65ace | 48 | |
cdf7bea7 | 49 | namespace pv { |
f4e57597 SA |
50 | namespace views { |
51 | namespace TraceView { | |
e9c41f8c | 52 | |
cdf7bea7 | 53 | Viewport::Viewport(View &parent) : |
40aca27e | 54 | ViewWidget(parent), |
8dbbc7f0 | 55 | pinch_zoom_active_(false) |
6fa02541 | 56 | { |
64b60583 JH |
57 | setAutoFillBackground(true); |
58 | setBackgroundRole(QPalette::Base); | |
d7002724 JH |
59 | } |
60 | ||
e9e4e5e7 JH |
61 | shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt) |
62 | { | |
cbf0f87e | 63 | const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset()); |
e9e4e5e7 JH |
64 | const vector< shared_ptr<ViewItem> > items(this->items()); |
65 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
66 | if ((*i)->enabled() && | |
cbf0f87e | 67 | (*i)->hit_box_rect(pp).contains(pt)) |
e9e4e5e7 JH |
68 | return *i; |
69 | return nullptr; | |
70 | } | |
71 | ||
e8b969a9 JH |
72 | void Viewport::item_hover(const shared_ptr<ViewItem> &item) |
73 | { | |
d75c5d12 | 74 | if (item && item->is_draggable()) |
e8b969a9 JH |
75 | setCursor(dynamic_pointer_cast<RowItem>(item) ? |
76 | Qt::SizeVerCursor : Qt::SizeHorCursor); | |
77 | else | |
78 | unsetCursor(); | |
79 | } | |
80 | ||
28290534 JH |
81 | void Viewport::drag() |
82 | { | |
83 | drag_offset_ = view_.offset(); | |
838d0522 | 84 | drag_v_offset_ = view_.owner_visual_v_offset(); |
28290534 JH |
85 | } |
86 | ||
87 | void Viewport::drag_by(const QPoint &delta) | |
88 | { | |
60d9b99a | 89 | if (drag_offset_ == boost::none) |
28290534 JH |
90 | return; |
91 | ||
60d9b99a JS |
92 | view_.set_scale_offset(view_.scale(), |
93 | (*drag_offset_ - delta.x() * view_.scale())); | |
838d0522 SA |
94 | |
95 | view_.set_v_offset(-drag_v_offset_ - delta.y()); | |
28290534 JH |
96 | } |
97 | ||
98 | void Viewport::drag_release() | |
99 | { | |
60d9b99a | 100 | drag_offset_ = boost::none; |
28290534 JH |
101 | } |
102 | ||
e9e4e5e7 JH |
103 | vector< shared_ptr<ViewItem> > Viewport::items() |
104 | { | |
cc88566c | 105 | vector< shared_ptr<ViewItem> > items; |
e94f43c6 JH |
106 | const std::vector< shared_ptr<ViewItem> > view_items( |
107 | view_.list_by_type<ViewItem>()); | |
108 | copy(view_items.begin(), view_items.end(), back_inserter(items)); | |
e9e4e5e7 JH |
109 | const vector< shared_ptr<TimeItem> > time_items(view_.time_items()); |
110 | copy(time_items.begin(), time_items.end(), back_inserter(items)); | |
111 | return items; | |
112 | } | |
113 | ||
c9743553 JH |
114 | bool Viewport::touch_event(QTouchEvent *event) |
115 | { | |
116 | QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints(); | |
117 | ||
118 | if (touchPoints.count() != 2) { | |
119 | pinch_zoom_active_ = false; | |
120 | return false; | |
121 | } | |
122 | ||
123 | const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first(); | |
124 | const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last(); | |
125 | ||
126 | if (!pinch_zoom_active_ || | |
127 | (event->touchPointStates() & Qt::TouchPointPressed)) { | |
60d9b99a JS |
128 | pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>(); |
129 | pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>(); | |
c9743553 JH |
130 | pinch_zoom_active_ = true; |
131 | } | |
132 | ||
133 | double w = touchPoint1.pos().x() - touchPoint0.pos().x(); | |
134 | if (abs(w) >= 1.0) { | |
1c91f1a2 JH |
135 | const double scale = |
136 | fabs((pinch_offset1_ - pinch_offset0_) / w); | |
c9743553 JH |
137 | double offset = pinch_offset0_ - touchPoint0.pos().x() * scale; |
138 | if (scale > 0) | |
139 | view_.set_scale_offset(scale, offset); | |
140 | } | |
141 | ||
142 | if (event->touchPointStates() & Qt::TouchPointReleased) { | |
143 | pinch_zoom_active_ = false; | |
144 | ||
145 | if (touchPoint0.state() & Qt::TouchPointReleased) { | |
146 | // Primary touch released | |
28290534 | 147 | drag_release(); |
c9743553 JH |
148 | } else { |
149 | // Update the mouse down fields so that continued | |
150 | // dragging with the primary touch will work correctly | |
151 | mouse_down_point_ = touchPoint0.pos().toPoint(); | |
28290534 | 152 | drag(); |
c9743553 JH |
153 | } |
154 | } | |
155 | ||
156 | return true; | |
157 | } | |
158 | ||
e314eca4 | 159 | void Viewport::paintEvent(QPaintEvent*) |
d7002724 | 160 | { |
e94f43c6 JH |
161 | vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>()); |
162 | assert(none_of(row_items.begin(), row_items.end(), | |
163 | [](const shared_ptr<RowItem> &r) { return !r; })); | |
beb897c6 | 164 | |
e94f43c6 JH |
165 | stable_sort(row_items.begin(), row_items.end(), |
166 | [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) { | |
167 | return a->point(QRect()).y() < b->point(QRect()).y(); }); | |
3e46726a | 168 | |
beb897c6 JH |
169 | const vector< shared_ptr<TimeItem> > time_items(view_.time_items()); |
170 | assert(none_of(time_items.begin(), time_items.end(), | |
171 | [](const shared_ptr<TimeItem> &t) { return !t; })); | |
172 | ||
64b60583 | 173 | QPainter p(this); |
ce6e73a8 | 174 | p.setRenderHint(QPainter::Antialiasing); |
3e46726a | 175 | |
36e7001d | 176 | const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset()); |
3eb29afd | 177 | |
beb897c6 JH |
178 | for (const shared_ptr<TimeItem> t : time_items) |
179 | t->paint_back(p, pp); | |
e94f43c6 | 180 | for (const shared_ptr<RowItem> r : row_items) |
3eb29afd | 181 | r->paint_back(p, pp); |
3e46726a | 182 | |
beb897c6 JH |
183 | for (const shared_ptr<TimeItem> t : time_items) |
184 | t->paint_mid(p, pp); | |
e94f43c6 | 185 | for (const shared_ptr<RowItem> r : row_items) |
3eb29afd | 186 | r->paint_mid(p, pp); |
fe08b6e8 | 187 | |
e94f43c6 | 188 | for (const shared_ptr<RowItem> r : row_items) |
3eb29afd | 189 | r->paint_fore(p, pp); |
34ec0968 JH |
190 | |
191 | p.setRenderHint(QPainter::Antialiasing, false); | |
beb897c6 JH |
192 | for (const shared_ptr<TimeItem> t : time_items) |
193 | t->paint_fore(p, pp); | |
f76af637 | 194 | |
64b60583 | 195 | p.end(); |
6fa02541 | 196 | } |
009e1503 | 197 | |
903038a8 JH |
198 | void Viewport::mouseDoubleClickEvent(QMouseEvent *event) |
199 | { | |
200 | assert(event); | |
201 | ||
202 | if (event->buttons() & Qt::LeftButton) | |
8dbbc7f0 | 203 | view_.zoom(2.0, event->x()); |
903038a8 | 204 | else if (event->buttons() & Qt::RightButton) |
8dbbc7f0 | 205 | view_.zoom(-2.0, event->x()); |
903038a8 JH |
206 | } |
207 | ||
d9ea9628 | 208 | void Viewport::wheelEvent(QWheelEvent *event) |
1dd95c70 | 209 | { |
d9ea9628 | 210 | assert(event); |
208c6fc3 | 211 | |
d9ea9628 UH |
212 | if (event->orientation() == Qt::Vertical) { |
213 | if (event->modifiers() & Qt::ControlModifier) { | |
208c6fc3 JH |
214 | // Vertical scrolling with the control key pressed |
215 | // is intrepretted as vertical scrolling | |
216 | view_.set_v_offset(-view_.owner_visual_v_offset() - | |
d9ea9628 | 217 | (event->delta() * height()) / (8 * 120)); |
208c6fc3 JH |
218 | } else { |
219 | // Vertical scrolling is interpreted as zooming in/out | |
d9ea9628 | 220 | view_.zoom(event->delta() / 120, event->x()); |
208c6fc3 | 221 | } |
d9ea9628 | 222 | } else if (event->orientation() == Qt::Horizontal) { |
23840406 | 223 | // Horizontal scrolling is interpreted as moving left/right |
8dbbc7f0 | 224 | view_.set_scale_offset(view_.scale(), |
d9ea9628 | 225 | event->delta() * view_.scale() + view_.offset()); |
ffd5cd20 | 226 | } |
7cd5faf8 JH |
227 | } |
228 | ||
f4e57597 SA |
229 | } // namespace TraceView |
230 | } // namespace views | |
cdf7bea7 | 231 | } // namespace pv |