]> sigrok.org Git - pulseview.git/blame_incremental - pv/views/trace/viewport.cpp
Disable antialiasing on high-DPI displays
[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
59shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
60{
61 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
62 const vector< shared_ptr<ViewItem> > items(this->items());
63 for (auto i = items.rbegin(); i != items.rend(); i++)
64 if ((*i)->enabled() && (*i)->hit_box_rect(pp).contains(pt))
65 return *i;
66 return nullptr;
67}
68
69void Viewport::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
70{
71 if (item && item->is_draggable(pos))
72 setCursor(dynamic_pointer_cast<RowItem>(item) ?
73 Qt::SizeVerCursor : Qt::SizeHorCursor);
74 else
75 unsetCursor();
76}
77
78void Viewport::drag()
79{
80 drag_offset_ = view_.offset();
81 drag_v_offset_ = view_.owner_visual_v_offset();
82}
83
84void Viewport::drag_by(const QPoint &delta)
85{
86 if (drag_offset_ == boost::none)
87 return;
88
89 view_.set_scale_offset(view_.scale(),
90 (*drag_offset_ - delta.x() * view_.scale()));
91
92 view_.set_v_offset(-drag_v_offset_ - delta.y());
93}
94
95void Viewport::drag_release()
96{
97 drag_offset_ = boost::none;
98}
99
100vector< shared_ptr<ViewItem> > Viewport::items()
101{
102 vector< shared_ptr<ViewItem> > items;
103 const vector< shared_ptr<ViewItem> > view_items(
104 view_.list_by_type<ViewItem>());
105 copy(view_items.begin(), view_items.end(), back_inserter(items));
106 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
107 copy(time_items.begin(), time_items.end(), back_inserter(items));
108 return items;
109}
110
111bool Viewport::touch_event(QTouchEvent *event)
112{
113 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
114
115 if (touchPoints.count() != 2) {
116 pinch_zoom_active_ = false;
117 return false;
118 }
119
120 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
121 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
122
123 if (!pinch_zoom_active_ ||
124 (event->touchPointStates() & Qt::TouchPointPressed)) {
125 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
126 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
127 pinch_zoom_active_ = true;
128 }
129
130 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
131 if (abs(w) >= 1.0) {
132 const double scale =
133 fabs((pinch_offset1_ - pinch_offset0_) / w);
134 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
135 if (scale > 0)
136 view_.set_scale_offset(scale, offset);
137 }
138
139 if (event->touchPointStates() & Qt::TouchPointReleased) {
140 pinch_zoom_active_ = false;
141
142 if (touchPoint0.state() & Qt::TouchPointReleased) {
143 // Primary touch released
144 drag_release();
145 } else {
146 // Update the mouse down fields so that continued
147 // dragging with the primary touch will work correctly
148 mouse_down_point_ = touchPoint0.pos().toPoint();
149 drag();
150 }
151 }
152
153 return true;
154}
155
156void Viewport::paintEvent(QPaintEvent*)
157{
158 typedef void (ViewItem::*LayerPaintFunc)(
159 QPainter &p, ViewItemPaintParams &pp);
160 LayerPaintFunc layer_paint_funcs[] = {
161 &ViewItem::paint_back, &ViewItem::paint_mid,
162 &ViewItem::paint_fore, nullptr};
163
164 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
165 assert(none_of(row_items.begin(), row_items.end(),
166 [](const shared_ptr<RowItem> &r) { return !r; }));
167
168 stable_sort(row_items.begin(), row_items.end(),
169 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
170 return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });
171
172 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
173 assert(none_of(time_items.begin(), time_items.end(),
174 [](const shared_ptr<TimeItem> &t) { return !t; }));
175
176 QPainter p(this);
177
178 bool useAntialiasing =
179 window()->windowHandle()->screen()->devicePixelRatio() < 2.0;
180 p.setRenderHint(QPainter::Antialiasing, useAntialiasing);
181
182 for (LayerPaintFunc *paint_func = layer_paint_funcs;
183 *paint_func; paint_func++) {
184 ViewItemPaintParams time_pp(rect(), view_.scale(), view_.offset());
185 for (const shared_ptr<TimeItem>& t : time_items)
186 (t.get()->*(*paint_func))(p, time_pp);
187
188 ViewItemPaintParams row_pp(rect(), view_.scale(), view_.offset());
189 for (const shared_ptr<RowItem>& r : row_items)
190 (r.get()->*(*paint_func))(p, row_pp);
191 }
192
193 p.end();
194}
195
196void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
197{
198 assert(event);
199
200 if (event->buttons() & Qt::LeftButton)
201 view_.zoom(2.0, event->x());
202 else if (event->buttons() & Qt::RightButton)
203 view_.zoom(-2.0, event->x());
204}
205
206void Viewport::wheelEvent(QWheelEvent *event)
207{
208 assert(event);
209
210 if (event->orientation() == Qt::Vertical) {
211 if (event->modifiers() & Qt::ControlModifier) {
212 // Vertical scrolling with the control key pressed
213 // is intrepretted as vertical scrolling
214 view_.set_v_offset(-view_.owner_visual_v_offset() -
215 (event->delta() * height()) / (8 * 120));
216 } else {
217 // Vertical scrolling is interpreted as zooming in/out
218 view_.zoom(event->delta() / 120.0, event->x());
219 }
220 } else if (event->orientation() == Qt::Horizontal) {
221 // Horizontal scrolling is interpreted as moving left/right
222 view_.set_scale_offset(view_.scale(),
223 event->delta() * view_.scale() + view_.offset());
224 }
225}
226
227} // namespace trace
228} // namespace views
229} // namespace pv