]> sigrok.org Git - pulseview.git/blame - pv/views/trace/viewport.cpp
DecodeTrace: Allow row hiding
[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);
d7002724
JH
57}
58
e9e4e5e7
JH
59shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
60{
cbf0f87e 61 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
e9e4e5e7
JH
62 const vector< shared_ptr<ViewItem> > items(this->items());
63 for (auto i = items.rbegin(); i != items.rend(); i++)
c063290a 64 if ((*i)->enabled() && (*i)->hit_box_rect(pp).contains(pt))
e9e4e5e7
JH
65 return *i;
66 return nullptr;
67}
68
119c5c23 69void Viewport::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
e8b969a9 70{
119c5c23 71 if (item && item->is_draggable(pos))
af33d4cb 72 setCursor(dynamic_pointer_cast<ViewItem>(item) ?
8b4ce4ae 73 Qt::SizeHorCursor : Qt::SizeVerCursor);
e8b969a9
JH
74 else
75 unsetCursor();
76}
77
28290534
JH
78void Viewport::drag()
79{
80 drag_offset_ = view_.offset();
838d0522 81 drag_v_offset_ = view_.owner_visual_v_offset();
28290534
JH
82}
83
84void Viewport::drag_by(const QPoint &delta)
85{
60d9b99a 86 if (drag_offset_ == boost::none)
28290534
JH
87 return;
88
60d9b99a
JS
89 view_.set_scale_offset(view_.scale(),
90 (*drag_offset_ - delta.x() * view_.scale()));
838d0522
SA
91
92 view_.set_v_offset(-drag_v_offset_ - delta.y());
28290534
JH
93}
94
95void Viewport::drag_release()
96{
60d9b99a 97 drag_offset_ = boost::none;
28290534
JH
98}
99
e9e4e5e7
JH
100vector< shared_ptr<ViewItem> > Viewport::items()
101{
cc88566c 102 vector< shared_ptr<ViewItem> > items;
6f925ba9 103 const vector< shared_ptr<ViewItem> > view_items(
e94f43c6
JH
104 view_.list_by_type<ViewItem>());
105 copy(view_items.begin(), view_items.end(), back_inserter(items));
e9e4e5e7
JH
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
c9743553
JH
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 }
1f5d683e
SS
119 if (event->device()->type() == QTouchDevice::TouchPad) {
120 return false;
121 }
c9743553
JH
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 159void Viewport::paintEvent(QPaintEvent*)
d7002724 160{
60938e04
JH
161 typedef void (ViewItem::*LayerPaintFunc)(
162 QPainter &p, ViewItemPaintParams &pp);
163 LayerPaintFunc layer_paint_funcs[] = {
164 &ViewItem::paint_back, &ViewItem::paint_mid,
165 &ViewItem::paint_fore, nullptr};
166
af33d4cb 167 vector< shared_ptr<ViewItem> > row_items(view_.list_by_type<ViewItem>());
e94f43c6 168 assert(none_of(row_items.begin(), row_items.end(),
af33d4cb 169 [](const shared_ptr<ViewItem> &r) { return !r; }));
beb897c6 170
e94f43c6 171 stable_sort(row_items.begin(), row_items.end(),
af33d4cb 172 [](const shared_ptr<ViewItem> &a, const shared_ptr<ViewItem> &b) {
a3d5a7c7 173 return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });
3e46726a 174
beb897c6
JH
175 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
176 assert(none_of(time_items.begin(), time_items.end(),
177 [](const shared_ptr<TimeItem> &t) { return !t; }));
178
64b60583 179 QPainter p(this);
633e8ade 180
f0b99257
SA
181 // Disable antialiasing for high-DPI displays
182 bool use_antialiasing =
633e8ade 183 window()->windowHandle()->screen()->devicePixelRatio() < 2.0;
f0b99257 184 p.setRenderHint(QPainter::Antialiasing, use_antialiasing);
3e46726a 185
60938e04
JH
186 for (LayerPaintFunc *paint_func = layer_paint_funcs;
187 *paint_func; paint_func++) {
188 ViewItemPaintParams time_pp(rect(), view_.scale(), view_.offset());
f4ab4b5c 189 for (const shared_ptr<TimeItem>& t : time_items)
60938e04 190 (t.get()->*(*paint_func))(p, time_pp);
3e46726a 191
60938e04 192 ViewItemPaintParams row_pp(rect(), view_.scale(), view_.offset());
af33d4cb 193 for (const shared_ptr<ViewItem>& r : row_items)
60938e04
JH
194 (r.get()->*(*paint_func))(p, row_pp);
195 }
f76af637 196
64b60583 197 p.end();
6fa02541 198}
009e1503 199
903038a8
JH
200void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
201{
202 assert(event);
203
204 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 205 view_.zoom(2.0, event->x());
903038a8 206 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 207 view_.zoom(-2.0, event->x());
903038a8
JH
208}
209
d9ea9628 210void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70 211{
d9ea9628 212 assert(event);
208c6fc3 213
d9ea9628
UH
214 if (event->orientation() == Qt::Vertical) {
215 if (event->modifiers() & Qt::ControlModifier) {
208c6fc3
JH
216 // Vertical scrolling with the control key pressed
217 // is intrepretted as vertical scrolling
218 view_.set_v_offset(-view_.owner_visual_v_offset() -
d9ea9628 219 (event->delta() * height()) / (8 * 120));
208c6fc3
JH
220 } else {
221 // Vertical scrolling is interpreted as zooming in/out
3592f405 222 view_.zoom(event->delta() / 120.0, event->x());
208c6fc3 223 }
d9ea9628 224 } else if (event->orientation() == Qt::Horizontal) {
23840406 225 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0 226 view_.set_scale_offset(view_.scale(),
d9ea9628 227 event->delta() * view_.scale() + view_.offset());
ffd5cd20 228 }
7cd5faf8
JH
229}
230
1573bf16 231} // namespace trace
f4e57597 232} // namespace views
cdf7bea7 233} // namespace pv