]> sigrok.org Git - pulseview.git/blame - pv/views/trace/viewport.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / viewport.cpp
CommitLineData
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>
633e8ade
RG
33#include <QScreen>
34#include <QWindow>
e9c41f8c 35
f4e57597
SA
36#include <QDebug>
37
c8870d9e 38using std::abs;
e9e4e5e7
JH
39using std::back_inserter;
40using std::copy;
e8b969a9 41using std::dynamic_pointer_cast;
20f59e95 42using std::none_of; // NOLINT. Used in assert()s.
f9abf97e 43using std::shared_ptr;
68b21a71 44using std::stable_sort;
819f4c25 45using std::vector;
e3f65ace 46
cdf7bea7 47namespace pv {
f4e57597 48namespace views {
1573bf16 49namespace trace {
e9c41f8c 50
cdf7bea7 51Viewport::Viewport(View &parent) :
40aca27e 52 ViewWidget(parent),
8dbbc7f0 53 pinch_zoom_active_(false)
6fa02541 54{
64b60583
JH
55 setAutoFillBackground(true);
56 setBackgroundRole(QPalette::Base);
e6c8d58b
SA
57
58 // Set up settings and event handlers
59 GlobalSettings settings;
60 allow_vertical_dragging_ = settings.value(GlobalSettings::Key_View_AllowVerticalDragging).toBool();
61
62 GlobalSettings::add_change_handler(this);
63}
64
65Viewport::~Viewport()
66{
67 GlobalSettings::remove_change_handler(this);
d7002724
JH
68}
69
e9e4e5e7
JH
70shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
71{
cbf0f87e 72 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
e9e4e5e7
JH
73 const vector< shared_ptr<ViewItem> > items(this->items());
74 for (auto i = items.rbegin(); i != items.rend(); i++)
c063290a 75 if ((*i)->enabled() && (*i)->hit_box_rect(pp).contains(pt))
e9e4e5e7
JH
76 return *i;
77 return nullptr;
78}
79
119c5c23 80void Viewport::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
e8b969a9 81{
119c5c23 82 if (item && item->is_draggable(pos))
af33d4cb 83 setCursor(dynamic_pointer_cast<ViewItem>(item) ?
8b4ce4ae 84 Qt::SizeHorCursor : Qt::SizeVerCursor);
e8b969a9
JH
85 else
86 unsetCursor();
87}
88
28290534
JH
89void Viewport::drag()
90{
91 drag_offset_ = view_.offset();
e6c8d58b
SA
92
93 if (allow_vertical_dragging_)
94 drag_v_offset_ = view_.owner_visual_v_offset();
28290534
JH
95}
96
97void Viewport::drag_by(const QPoint &delta)
98{
60d9b99a 99 if (drag_offset_ == boost::none)
28290534
JH
100 return;
101
60d9b99a
JS
102 view_.set_scale_offset(view_.scale(),
103 (*drag_offset_ - delta.x() * view_.scale()));
838d0522 104
e6c8d58b
SA
105 if (allow_vertical_dragging_)
106 view_.set_v_offset(-drag_v_offset_ - delta.y());
28290534
JH
107}
108
109void Viewport::drag_release()
110{
60d9b99a 111 drag_offset_ = boost::none;
28290534
JH
112}
113
e9e4e5e7
JH
114vector< shared_ptr<ViewItem> > Viewport::items()
115{
cc88566c 116 vector< shared_ptr<ViewItem> > items;
6f925ba9 117 const vector< shared_ptr<ViewItem> > view_items(
e94f43c6
JH
118 view_.list_by_type<ViewItem>());
119 copy(view_items.begin(), view_items.end(), back_inserter(items));
e9e4e5e7
JH
120 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
121 copy(time_items.begin(), time_items.end(), back_inserter(items));
122 return items;
123}
124
c9743553
JH
125bool Viewport::touch_event(QTouchEvent *event)
126{
1ed73ebd
VPP
127#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
128 QList<QEventPoint> touchPoints = event->points();
129#else
c9743553 130 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
1ed73ebd 131#endif
c9743553
JH
132
133 if (touchPoints.count() != 2) {
134 pinch_zoom_active_ = false;
135 return false;
136 }
1ed73ebd
VPP
137#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
138 if (event->device()->type() == QInputDevice::DeviceType::TouchPad) {
139 return false;
140 }
141
142 const QEventPoint &touchPoint0 = touchPoints.first();
143 const QEventPoint &touchPoint1 = touchPoints.last();
144
145 if (!pinch_zoom_active_ ||
146 (event->touchPointStates() & QEventPoint::Pressed)) {
147 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.position().x()).convert_to<double>();
148 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.position().x()).convert_to<double>();
149 pinch_zoom_active_ = true;
150 }
151
152 double w = touchPoint1.position().x() - touchPoint0.position().x();
153 if (abs(w) >= 1.0) {
154 const double scale =
155 fabs((pinch_offset1_ - pinch_offset0_) / w);
156 double offset = pinch_offset0_ - touchPoint0.position().x() * scale;
157 if (scale > 0)
158 view_.set_scale_offset(scale, offset);
159 }
160#else
1f5d683e
SS
161 if (event->device()->type() == QTouchDevice::TouchPad) {
162 return false;
163 }
c9743553
JH
164
165 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
166 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
167
168 if (!pinch_zoom_active_ ||
169 (event->touchPointStates() & Qt::TouchPointPressed)) {
60d9b99a
JS
170 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
171 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
c9743553
JH
172 pinch_zoom_active_ = true;
173 }
174
175 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
176 if (abs(w) >= 1.0) {
1c91f1a2
JH
177 const double scale =
178 fabs((pinch_offset1_ - pinch_offset0_) / w);
c9743553
JH
179 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
180 if (scale > 0)
181 view_.set_scale_offset(scale, offset);
182 }
1ed73ebd 183#endif
c9743553
JH
184
185 if (event->touchPointStates() & Qt::TouchPointReleased) {
186 pinch_zoom_active_ = false;
187
188 if (touchPoint0.state() & Qt::TouchPointReleased) {
189 // Primary touch released
28290534 190 drag_release();
c9743553
JH
191 } else {
192 // Update the mouse down fields so that continued
193 // dragging with the primary touch will work correctly
1ed73ebd
VPP
194#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
195 mouse_down_point_ = touchPoint0.position().toPoint();
196#else
c9743553 197 mouse_down_point_ = touchPoint0.pos().toPoint();
1ed73ebd 198#endif
28290534 199 drag();
c9743553
JH
200 }
201 }
202
203 return true;
204}
205
e314eca4 206void Viewport::paintEvent(QPaintEvent*)
d7002724 207{
60938e04
JH
208 typedef void (ViewItem::*LayerPaintFunc)(
209 QPainter &p, ViewItemPaintParams &pp);
210 LayerPaintFunc layer_paint_funcs[] = {
211 &ViewItem::paint_back, &ViewItem::paint_mid,
212 &ViewItem::paint_fore, nullptr};
213
af33d4cb 214 vector< shared_ptr<ViewItem> > row_items(view_.list_by_type<ViewItem>());
e94f43c6 215 assert(none_of(row_items.begin(), row_items.end(),
af33d4cb 216 [](const shared_ptr<ViewItem> &r) { return !r; }));
beb897c6 217
e94f43c6 218 stable_sort(row_items.begin(), row_items.end(),
af33d4cb 219 [](const shared_ptr<ViewItem> &a, const shared_ptr<ViewItem> &b) {
a3d5a7c7 220 return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });
3e46726a 221
beb897c6
JH
222 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
223 assert(none_of(time_items.begin(), time_items.end(),
224 [](const shared_ptr<TimeItem> &t) { return !t; }));
225
64b60583 226 QPainter p(this);
633e8ade 227
f0b99257
SA
228 // Disable antialiasing for high-DPI displays
229 bool use_antialiasing =
633e8ade 230 window()->windowHandle()->screen()->devicePixelRatio() < 2.0;
f0b99257 231 p.setRenderHint(QPainter::Antialiasing, use_antialiasing);
3e46726a 232
60938e04
JH
233 for (LayerPaintFunc *paint_func = layer_paint_funcs;
234 *paint_func; paint_func++) {
235 ViewItemPaintParams time_pp(rect(), view_.scale(), view_.offset());
f4ab4b5c 236 for (const shared_ptr<TimeItem>& t : time_items)
60938e04 237 (t.get()->*(*paint_func))(p, time_pp);
3e46726a 238
60938e04 239 ViewItemPaintParams row_pp(rect(), view_.scale(), view_.offset());
af33d4cb 240 for (const shared_ptr<ViewItem>& r : row_items)
60938e04
JH
241 (r.get()->*(*paint_func))(p, row_pp);
242 }
f76af637 243
64b60583 244 p.end();
6fa02541 245}
009e1503 246
903038a8
JH
247void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
248{
249 assert(event);
250
1ed73ebd
VPP
251#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
252 if (event->buttons() & Qt::LeftButton)
253 view_.zoom(2.0, event->position().x());
254 else if (event->buttons() & Qt::RightButton)
255 view_.zoom(-2.0, event->position().x());
256#else
903038a8 257 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 258 view_.zoom(2.0, event->x());
903038a8 259 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 260 view_.zoom(-2.0, event->x());
1ed73ebd 261#endif
903038a8
JH
262}
263
d9ea9628 264void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70 265{
d9ea9628 266 assert(event);
208c6fc3 267
ffad6cd6
SA
268#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
269 int delta = (event->angleDelta().x() != 0) ? event->angleDelta().x() : event->angleDelta().y();
270#else
271 int delta = event->delta();
272#endif
273
274#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
275 if (event->angleDelta().y() != 0) {
276#else
d9ea9628 277 if (event->orientation() == Qt::Vertical) {
ffad6cd6 278#endif
d9ea9628 279 if (event->modifiers() & Qt::ControlModifier) {
208c6fc3
JH
280 // Vertical scrolling with the control key pressed
281 // is intrepretted as vertical scrolling
282 view_.set_v_offset(-view_.owner_visual_v_offset() -
ffad6cd6 283 (delta * height()) / (8 * 120));
f375439f
ŁS
284 } else if (event->modifiers() & Qt::ShiftModifier) {
285 // Vertical scrolling with the shift key pressed
286 // acts as horizontal scrolling like in Gimp
287 // and Inkscape.
288 view_.set_scale_offset(view_.scale(),
289 - delta * view_.scale() + view_.offset());
208c6fc3
JH
290 } else {
291 // Vertical scrolling is interpreted as zooming in/out
a7988e20 292#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
ffad6cd6 293 view_.zoom(delta / 120.0, event->position().x());
9e5ccb72
SA
294#else
295 view_.zoom(delta / 120.0, event->x());
296#endif
208c6fc3 297 }
ffad6cd6
SA
298#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
299 } else if (event->angleDelta().x() != 0) {
300#else
d9ea9628 301 } else if (event->orientation() == Qt::Horizontal) {
ffad6cd6 302#endif
23840406 303 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0 304 view_.set_scale_offset(view_.scale(),
ffad6cd6 305 delta * view_.scale() + view_.offset());
ffd5cd20 306 }
7cd5faf8
JH
307}
308
e6c8d58b
SA
309void Viewport::on_setting_changed(const QString &key, const QVariant &value)
310{
66a43b4f
SA
311 if (key == GlobalSettings::Key_View_AllowVerticalDragging)
312 allow_vertical_dragging_ = value.toBool();
e6c8d58b
SA
313}
314
1573bf16 315} // namespace trace
f4e57597 316} // namespace views
cdf7bea7 317} // namespace pv