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