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