]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
Viewport: Use fabs instead of branch
[pulseview.git] / pv / view / 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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
d2344534 21#include <cassert>
61d64fe9 22#include <cmath>
68b21a71 23#include <algorithm>
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
c8870d9e 34using std::abs;
e9e4e5e7
JH
35using std::back_inserter;
36using std::copy;
819f4c25
JH
37using std::max;
38using std::min;
beb897c6 39using std::none_of;
f9abf97e 40using std::shared_ptr;
68b21a71 41using std::stable_sort;
819f4c25 42using std::vector;
e3f65ace 43
cdf7bea7
JH
44namespace pv {
45namespace view {
e9c41f8c 46
cdf7bea7 47Viewport::Viewport(View &parent) :
40aca27e 48 ViewWidget(parent),
8dbbc7f0
JH
49 mouse_down_valid_(false),
50 pinch_zoom_active_(false)
6fa02541 51{
64b60583
JH
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
d7002724
JH
54}
55
e9e4e5e7
JH
56shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
57{
58 const vector< shared_ptr<ViewItem> > items(this->items());
59 for (auto i = items.rbegin(); i != items.rend(); i++)
60 if ((*i)->enabled() &&
61 (*i)->hit_box_rect(rect()).contains(pt))
62 return *i;
63 return nullptr;
64}
65
66vector< shared_ptr<ViewItem> > Viewport::items()
67{
68 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
69 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
70 copy(time_items.begin(), time_items.end(), back_inserter(items));
71 return items;
72}
73
c9743553
JH
74bool Viewport::touch_event(QTouchEvent *event)
75{
76 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
77
78 if (touchPoints.count() != 2) {
79 pinch_zoom_active_ = false;
80 return false;
81 }
82
83 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
84 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
85
86 if (!pinch_zoom_active_ ||
87 (event->touchPointStates() & Qt::TouchPointPressed)) {
88 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
89 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
90 pinch_zoom_active_ = true;
91 }
92
93 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
94 if (abs(w) >= 1.0) {
1c91f1a2
JH
95 const double scale =
96 fabs((pinch_offset1_ - pinch_offset0_) / w);
c9743553
JH
97 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
98 if (scale > 0)
99 view_.set_scale_offset(scale, offset);
100 }
101
102 if (event->touchPointStates() & Qt::TouchPointReleased) {
103 pinch_zoom_active_ = false;
104
105 if (touchPoint0.state() & Qt::TouchPointReleased) {
106 // Primary touch released
107 mouse_down_valid_ = false;
108 } else {
109 // Update the mouse down fields so that continued
110 // dragging with the primary touch will work correctly
111 mouse_down_point_ = touchPoint0.pos().toPoint();
112 mouse_down_offset_ = view_.offset();
113 mouse_down_valid_ = true;
114 }
115 }
116
117 return true;
118}
119
e314eca4 120void Viewport::paintEvent(QPaintEvent*)
d7002724 121{
8dbbc7f0 122 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
beb897c6
JH
123 assert(none_of(row_items.begin(), row_items.end(),
124 [](const shared_ptr<RowItem> &r) { return !r; }));
125
68b21a71
JH
126 stable_sort(row_items.begin(), row_items.end(),
127 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 128 return a->visual_v_offset() < b->visual_v_offset(); });
3e46726a 129
beb897c6
JH
130 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
131 assert(none_of(time_items.begin(), time_items.end(),
132 [](const shared_ptr<TimeItem> &t) { return !t; }));
133
64b60583 134 QPainter p(this);
ce6e73a8 135 p.setRenderHint(QPainter::Antialiasing);
3e46726a 136
36e7001d 137 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
3eb29afd 138
beb897c6
JH
139 for (const shared_ptr<TimeItem> t : time_items)
140 t->paint_back(p, pp);
eae6e30a 141 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 142 r->paint_back(p, pp);
3e46726a 143
beb897c6
JH
144 for (const shared_ptr<TimeItem> t : time_items)
145 t->paint_mid(p, pp);
eae6e30a 146 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 147 r->paint_mid(p, pp);
fe08b6e8 148
eae6e30a 149 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 150 r->paint_fore(p, pp);
beb897c6
JH
151 for (const shared_ptr<TimeItem> t : time_items)
152 t->paint_fore(p, pp);
f76af637 153
64b60583 154 p.end();
6fa02541 155}
009e1503 156
cdf7bea7 157void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
158{
159 assert(event);
1dd95c70 160
300fc11e 161 if (event->button() == Qt::LeftButton) {
8dbbc7f0
JH
162 mouse_down_point_ = event->pos();
163 mouse_down_offset_ = view_.offset();
164 mouse_down_valid_ = true;
4b4f1c0d
MC
165 }
166}
167
168void Viewport::mouseReleaseEvent(QMouseEvent *event)
169{
170 assert(event);
171
172 if (event->button() == Qt::LeftButton)
8dbbc7f0 173 mouse_down_valid_ = false;
7cd5faf8
JH
174}
175
cdf7bea7 176void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
177{
178 assert(event);
1dd95c70 179
300fc11e 180 if (event->buttons() & Qt::LeftButton) {
8dbbc7f0
JH
181 if (!mouse_down_valid_) {
182 mouse_down_point_ = event->pos();
183 mouse_down_offset_ = view_.offset();
184 mouse_down_valid_ = true;
4b4f1c0d
MC
185 }
186
8dbbc7f0
JH
187 view_.set_scale_offset(view_.scale(),
188 mouse_down_offset_ +
189 (mouse_down_point_ - event->pos()).x() *
190 view_.scale());
1dd95c70 191 }
7cd5faf8
JH
192}
193
903038a8
JH
194void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
195{
196 assert(event);
197
198 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 199 view_.zoom(2.0, event->x());
903038a8 200 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 201 view_.zoom(-2.0, event->x());
903038a8
JH
202}
203
cdf7bea7 204void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
205{
206 assert(event);
ffd5cd20
AG
207
208 if (event->orientation() == Qt::Vertical) {
209 // Vertical scrolling is interpreted as zooming in/out
8dbbc7f0 210 view_.zoom(event->delta() / 120, event->x());
23840406
AG
211 } else if (event->orientation() == Qt::Horizontal) {
212 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0
JH
213 view_.set_scale_offset(view_.scale(),
214 event->delta() * view_.scale()
215 + view_.offset());
ffd5cd20 216 }
7cd5faf8
JH
217}
218
cdf7bea7
JH
219} // namespace view
220} // namespace pv