]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/viewport.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / trace / viewport.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <algorithm>
21#include <cassert>
22#include <cmath>
23#include <limits>
24
25#include "signal.hpp"
26#include "view.hpp"
27#include "viewitempaintparams.hpp"
28#include "viewport.hpp"
29
30#include <pv/session.hpp>
31
32#include <QMouseEvent>
33#include <QScreen>
34#include <QWindow>
35
36#include <QDebug>
37
38using std::abs;
39using std::back_inserter;
40using std::copy;
41using std::dynamic_pointer_cast;
42using std::none_of; // NOLINT. Used in assert()s.
43using std::shared_ptr;
44using std::stable_sort;
45using std::vector;
46
47namespace pv {
48namespace views {
49namespace trace {
50
51Viewport::Viewport(View &parent) :
52 ViewWidget(parent),
53 pinch_zoom_active_(false)
54{
55 setAutoFillBackground(true);
56 setBackgroundRole(QPalette::Base);
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);
68}
69
70shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
71{
72 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
73 const vector< shared_ptr<ViewItem> > items(this->items());
74 for (auto i = items.rbegin(); i != items.rend(); i++)
75 if ((*i)->enabled() && (*i)->hit_box_rect(pp).contains(pt))
76 return *i;
77 return nullptr;
78}
79
80void Viewport::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
81{
82 if (item && item->is_draggable(pos))
83 setCursor(dynamic_pointer_cast<ViewItem>(item) ?
84 Qt::SizeHorCursor : Qt::SizeVerCursor);
85 else
86 unsetCursor();
87}
88
89void Viewport::drag()
90{
91 drag_offset_ = view_.offset();
92
93 if (allow_vertical_dragging_)
94 drag_v_offset_ = view_.owner_visual_v_offset();
95}
96
97void Viewport::drag_by(const QPoint &delta)
98{
99 if (drag_offset_ == boost::none)
100 return;
101
102 view_.set_scale_offset(view_.scale(),
103 (*drag_offset_ - delta.x() * view_.scale()));
104
105 if (allow_vertical_dragging_)
106 view_.set_v_offset(-drag_v_offset_ - delta.y());
107}
108
109void Viewport::drag_release()
110{
111 drag_offset_ = boost::none;
112}
113
114vector< shared_ptr<ViewItem> > Viewport::items()
115{
116 vector< shared_ptr<ViewItem> > items;
117 const vector< shared_ptr<ViewItem> > view_items(
118 view_.list_by_type<ViewItem>());
119 copy(view_items.begin(), view_items.end(), back_inserter(items));
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
125bool Viewport::touch_event(QTouchEvent *event)
126{
127 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
128
129 if (touchPoints.count() != 2) {
130 pinch_zoom_active_ = false;
131 return false;
132 }
133 if (event->device()->type() == QTouchDevice::TouchPad) {
134 return false;
135 }
136
137 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
138 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
139
140 if (!pinch_zoom_active_ ||
141 (event->touchPointStates() & Qt::TouchPointPressed)) {
142 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
143 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
144 pinch_zoom_active_ = true;
145 }
146
147 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
148 if (abs(w) >= 1.0) {
149 const double scale =
150 fabs((pinch_offset1_ - pinch_offset0_) / w);
151 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
152 if (scale > 0)
153 view_.set_scale_offset(scale, offset);
154 }
155
156 if (event->touchPointStates() & Qt::TouchPointReleased) {
157 pinch_zoom_active_ = false;
158
159 if (touchPoint0.state() & Qt::TouchPointReleased) {
160 // Primary touch released
161 drag_release();
162 } else {
163 // Update the mouse down fields so that continued
164 // dragging with the primary touch will work correctly
165 mouse_down_point_ = touchPoint0.pos().toPoint();
166 drag();
167 }
168 }
169
170 return true;
171}
172
173void Viewport::paintEvent(QPaintEvent*)
174{
175 typedef void (ViewItem::*LayerPaintFunc)(
176 QPainter &p, ViewItemPaintParams &pp);
177 LayerPaintFunc layer_paint_funcs[] = {
178 &ViewItem::paint_back, &ViewItem::paint_mid,
179 &ViewItem::paint_fore, nullptr};
180
181 vector< shared_ptr<ViewItem> > row_items(view_.list_by_type<ViewItem>());
182 assert(none_of(row_items.begin(), row_items.end(),
183 [](const shared_ptr<ViewItem> &r) { return !r; }));
184
185 stable_sort(row_items.begin(), row_items.end(),
186 [](const shared_ptr<ViewItem> &a, const shared_ptr<ViewItem> &b) {
187 return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });
188
189 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
190 assert(none_of(time_items.begin(), time_items.end(),
191 [](const shared_ptr<TimeItem> &t) { return !t; }));
192
193 QPainter p(this);
194
195 // Disable antialiasing for high-DPI displays
196 bool use_antialiasing =
197 window()->windowHandle()->screen()->devicePixelRatio() < 2.0;
198 p.setRenderHint(QPainter::Antialiasing, use_antialiasing);
199
200 for (LayerPaintFunc *paint_func = layer_paint_funcs;
201 *paint_func; paint_func++) {
202 ViewItemPaintParams time_pp(rect(), view_.scale(), view_.offset());
203 for (const shared_ptr<TimeItem>& t : time_items)
204 (t.get()->*(*paint_func))(p, time_pp);
205
206 ViewItemPaintParams row_pp(rect(), view_.scale(), view_.offset());
207 for (const shared_ptr<ViewItem>& r : row_items)
208 (r.get()->*(*paint_func))(p, row_pp);
209 }
210
211 p.end();
212}
213
214void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
215{
216 assert(event);
217
218 if (event->buttons() & Qt::LeftButton)
219 view_.zoom(2.0, event->x());
220 else if (event->buttons() & Qt::RightButton)
221 view_.zoom(-2.0, event->x());
222}
223
224void Viewport::wheelEvent(QWheelEvent *event)
225{
226 assert(event);
227
228 if (event->orientation() == Qt::Vertical) {
229 if (event->modifiers() & Qt::ControlModifier) {
230 // Vertical scrolling with the control key pressed
231 // is intrepretted as vertical scrolling
232 view_.set_v_offset(-view_.owner_visual_v_offset() -
233 (event->delta() * height()) / (8 * 120));
234 } else {
235 // Vertical scrolling is interpreted as zooming in/out
236 view_.zoom(event->delta() / 120.0, event->x());
237 }
238 } else if (event->orientation() == Qt::Horizontal) {
239 // Horizontal scrolling is interpreted as moving left/right
240 view_.set_scale_offset(view_.scale(),
241 event->delta() * view_.scale() + view_.offset());
242 }
243}
244
245void Viewport::on_setting_changed(const QString &key, const QVariant &value)
246{
247 if (key == GlobalSettings::Key_View_AllowVerticalDragging) {
248 GlobalSettings settings;
249 allow_vertical_dragging_ = settings.value(GlobalSettings::Key_View_AllowVerticalDragging).toBool();
250 }
251}
252
253} // namespace trace
254} // namespace views
255} // namespace pv