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