0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtGui module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "qstandardgestures_p.h"
|
|
43 |
#include "qgesture.h"
|
|
44 |
#include "qgesture_p.h"
|
|
45 |
#include "qevent.h"
|
|
46 |
#include "qwidget.h"
|
|
47 |
|
|
48 |
QT_BEGIN_NAMESPACE
|
|
49 |
|
|
50 |
QPanGestureRecognizer::QPanGestureRecognizer()
|
|
51 |
{
|
|
52 |
}
|
|
53 |
|
|
54 |
QGesture *QPanGestureRecognizer::createGesture(QObject *target)
|
|
55 |
{
|
|
56 |
if (target && target->isWidgetType()) {
|
|
57 |
static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents);
|
|
58 |
}
|
|
59 |
return new QPanGesture;
|
|
60 |
}
|
|
61 |
|
|
62 |
QGestureRecognizer::Result QPanGestureRecognizer::filterEvent(QGesture *state, QObject *, QEvent *event)
|
|
63 |
{
|
|
64 |
QPanGesture *q = static_cast<QPanGesture*>(state);
|
|
65 |
QPanGesturePrivate *d = q->d_func();
|
|
66 |
|
|
67 |
const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
|
|
68 |
|
|
69 |
QGestureRecognizer::Result result;
|
|
70 |
|
|
71 |
switch (event->type()) {
|
|
72 |
case QEvent::TouchBegin: {
|
|
73 |
result = QGestureRecognizer::MaybeGesture;
|
|
74 |
QTouchEvent::TouchPoint p = ev->touchPoints().at(0);
|
|
75 |
d->lastPosition = p.pos().toPoint();
|
|
76 |
d->lastOffset = d->totalOffset = d->offset = QPointF();
|
|
77 |
break;
|
|
78 |
}
|
|
79 |
case QEvent::TouchEnd: {
|
|
80 |
if (q->state() != Qt::NoGesture) {
|
|
81 |
if (ev->touchPoints().size() == 2) {
|
|
82 |
QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
|
|
83 |
QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1);
|
|
84 |
d->lastOffset = d->offset;
|
|
85 |
d->offset =
|
|
86 |
QPointF(p1.pos().x() - p1.lastPos().x() + p2.pos().x() - p2.lastPos().x(),
|
|
87 |
p1.pos().y() - p1.lastPos().y() + p2.pos().y() - p2.lastPos().y()) / 2;
|
|
88 |
d->totalOffset += d->offset;
|
|
89 |
}
|
|
90 |
result = QGestureRecognizer::GestureFinished;
|
|
91 |
} else {
|
|
92 |
result = QGestureRecognizer::NotGesture;
|
|
93 |
}
|
|
94 |
break;
|
|
95 |
}
|
|
96 |
case QEvent::TouchUpdate: {
|
|
97 |
if (ev->touchPoints().size() >= 2) {
|
|
98 |
QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
|
|
99 |
QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1);
|
|
100 |
d->lastOffset = d->offset;
|
|
101 |
d->offset =
|
|
102 |
QPointF(p1.pos().x() - p1.lastPos().x() + p2.pos().x() - p2.lastPos().x(),
|
|
103 |
p1.pos().y() - p1.lastPos().y() + p2.pos().y() - p2.lastPos().y()) / 2;
|
|
104 |
d->totalOffset += d->offset;
|
|
105 |
if (d->totalOffset.x() > 10 || d->totalOffset.y() > 10 ||
|
|
106 |
d->totalOffset.x() < -10 || d->totalOffset.y() < -10) {
|
|
107 |
result = QGestureRecognizer::GestureTriggered;
|
|
108 |
} else {
|
|
109 |
result = QGestureRecognizer::MaybeGesture;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
break;
|
|
113 |
}
|
|
114 |
case QEvent::MouseButtonPress:
|
|
115 |
case QEvent::MouseMove:
|
|
116 |
case QEvent::MouseButtonRelease:
|
|
117 |
result = QGestureRecognizer::Ignore;
|
|
118 |
break;
|
|
119 |
default:
|
|
120 |
result = QGestureRecognizer::Ignore;
|
|
121 |
break;
|
|
122 |
}
|
|
123 |
return result;
|
|
124 |
}
|
|
125 |
|
|
126 |
void QPanGestureRecognizer::reset(QGesture *state)
|
|
127 |
{
|
|
128 |
QPanGesture *pan = static_cast<QPanGesture*>(state);
|
|
129 |
QPanGesturePrivate *d = pan->d_func();
|
|
130 |
|
|
131 |
d->totalOffset = d->lastOffset = d->offset = QPointF();
|
|
132 |
d->lastPosition = QPoint();
|
|
133 |
d->acceleration = 0;
|
|
134 |
|
|
135 |
//#if defined(QT_MAC_USE_COCOA)
|
|
136 |
// d->singleTouchPanTimer.stop();
|
|
137 |
// d->prevMousePos = QPointF(0, 0);
|
|
138 |
//#endif
|
|
139 |
|
|
140 |
QGestureRecognizer::reset(state);
|
|
141 |
}
|
|
142 |
|
|
143 |
/*! \internal */
|
|
144 |
/*
|
|
145 |
bool QPanGestureRecognizer::event(QEvent *event)
|
|
146 |
{
|
|
147 |
#if defined(QT_MAC_USE_COCOA)
|
|
148 |
Q_D(QPanGesture);
|
|
149 |
if (event->type() == QEvent::Timer) {
|
|
150 |
const QTimerEvent *te = static_cast<QTimerEvent *>(event);
|
|
151 |
if (te->timerId() == d->singleTouchPanTimer.timerId()) {
|
|
152 |
d->singleTouchPanTimer.stop();
|
|
153 |
updateState(Qt::GestureStarted);
|
|
154 |
}
|
|
155 |
}
|
|
156 |
#endif
|
|
157 |
|
|
158 |
bool consume = false;
|
|
159 |
|
|
160 |
#if defined(Q_WS_WIN)
|
|
161 |
#elif defined(QT_MAC_USE_COCOA)
|
|
162 |
// The following implements single touch
|
|
163 |
// panning on Mac:
|
|
164 |
const int panBeginDelay = 300;
|
|
165 |
const int panBeginRadius = 3;
|
|
166 |
const QTouchEvent *ev = static_cast<const QTouchEvent*>(event);
|
|
167 |
|
|
168 |
switch (event->type()) {
|
|
169 |
case QEvent::TouchBegin: {
|
|
170 |
if (ev->touchPoints().size() == 1) {
|
|
171 |
d->delayManager->setEnabled(true);
|
|
172 |
consume = d->delayManager->append(d->gestureTarget, *event);
|
|
173 |
d->lastPosition = QCursor::pos();
|
|
174 |
d->singleTouchPanTimer.start(panBeginDelay, this);
|
|
175 |
}
|
|
176 |
break;}
|
|
177 |
case QEvent::TouchEnd: {
|
|
178 |
d->delayManager->setEnabled(false);
|
|
179 |
if (state() != Qt::NoGesture) {
|
|
180 |
updateState(Qt::GestureFinished);
|
|
181 |
consume = true;
|
|
182 |
d->delayManager->clear();
|
|
183 |
} else {
|
|
184 |
d->delayManager->replay();
|
|
185 |
}
|
|
186 |
reset();
|
|
187 |
break;}
|
|
188 |
case QEvent::TouchUpdate: {
|
|
189 |
consume = d->delayManager->append(d->gestureTarget, *event);
|
|
190 |
if (ev->touchPoints().size() == 1) {
|
|
191 |
if (state() == Qt::NoGesture) {
|
|
192 |
// INVARIANT: The singleTouchTimer has still not fired.
|
|
193 |
// Lets check if the user moved his finger so much from
|
|
194 |
// the starting point that it makes sense to cancel:
|
|
195 |
const QPointF startPos = ev->touchPoints().at(0).startPos().toPoint();
|
|
196 |
const QPointF p = ev->touchPoints().at(0).pos().toPoint();
|
|
197 |
if ((startPos - p).manhattanLength() > panBeginRadius) {
|
|
198 |
d->delayManager->replay();
|
|
199 |
consume = false;
|
|
200 |
reset();
|
|
201 |
} else {
|
|
202 |
d->lastPosition = QCursor::pos();
|
|
203 |
}
|
|
204 |
} else {
|
|
205 |
d->delayManager->clear();
|
|
206 |
QPointF mousePos = QCursor::pos();
|
|
207 |
QPointF dist = mousePos - d->lastPosition;
|
|
208 |
d->lastPosition = mousePos;
|
|
209 |
d->lastOffset = d->offset;
|
|
210 |
d->offset = QSizeF(dist.x(), dist.y());
|
|
211 |
d->totalOffset += d->offset;
|
|
212 |
updateState(Qt::GestureUpdated);
|
|
213 |
}
|
|
214 |
} else if (state() == Qt::NoGesture) {
|
|
215 |
d->delayManager->replay();
|
|
216 |
consume = false;
|
|
217 |
reset();
|
|
218 |
}
|
|
219 |
break;}
|
|
220 |
case QEvent::MouseButtonPress:
|
|
221 |
case QEvent::MouseMove:
|
|
222 |
case QEvent::MouseButtonRelease:
|
|
223 |
if (d->delayManager->isEnabled())
|
|
224 |
consume = d->delayManager->append(d->gestureTarget, *event);
|
|
225 |
break;
|
|
226 |
default:
|
|
227 |
return false;
|
|
228 |
}
|
|
229 |
#else
|
|
230 |
Q_UNUSED(event);
|
|
231 |
#endif
|
|
232 |
return QGestureRecognizer::Ignore;
|
|
233 |
}
|
|
234 |
*/
|
|
235 |
|
|
236 |
QT_END_NAMESPACE
|