-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgestures.cpp
434 lines (395 loc) · 13.7 KB
/
gestures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2017 Martin Gräßlin <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "gestures.h"
#include <QRect>
#include <functional>
#include <cmath>
#include <QDebug>
namespace KWin
{
// jing_kwin gesture
const quint32 GESTURE_INTERVAL = 30;
Gesture::Gesture(QObject *parent)
: QObject(parent)
{
}
Gesture::~Gesture() = default;
SwipeGesture::SwipeGesture(QObject *parent)
: Gesture(parent)
{
}
SwipeGesture::~SwipeGesture() = default;
PinchGesture::PinchGesture(QObject *parent)
: Gesture(parent)
{
}
PinchGesture::~PinchGesture() = default;
qreal PinchGesture::calFingerDistance(const QMap<qint32, QPointF> &points)
{
qreal distance = 0.f;
QPointF lastpoint;
for (auto it = points.begin(); it != points.end(); it++) {
if (it != points.begin()) {
double width = it.value().x() - (it-1).value().x();
double height = it.value().y() - (it-1).value().y();
distance += sqrt((height * height) + (width * width));
}
}
return distance;
}
void SwipeGesture::setStartGeometry(const QRect &geometry)
{
setMinimumX(geometry.x());
setMinimumY(geometry.y());
setMaximumX(geometry.x() + geometry.width());
setMaximumY(geometry.y() + geometry.height());
Q_ASSERT(m_maximumX >= m_minimumX);
Q_ASSERT(m_maximumY >= m_minimumY);
}
qreal SwipeGesture::minimumDeltaReachedProgress(const QSizeF &delta) const
{
if (!m_minimumDeltaRelevant || m_minimumDelta.isNull()) {
return 1.0;
}
if (m_direction == Direction::All) {
qreal ret = 1.0;
if (m_minimumSwipeAngle) {
qreal angle = std::atan(std::abs(delta.width())/std::abs(delta.height())) * 180/3.14;
ret = std::min(angle/m_minimumSwipeAngle, ret);
}
ret = std::min((std::abs(delta.width()) + std::abs(delta.height())) / (std::abs(m_minimumDelta.width()) + std::abs(m_minimumDelta.height())), ret);
return ret;
}
switch (m_direction) {
case Direction::Up:
if(delta.height()>0)
return 0;
return std::min(std::abs(delta.height()) / std::abs(m_minimumDelta.height()), 1.0);
case Direction::Down:
if(delta.height()<0)
return 0;
return std::min(std::abs(delta.height()) / std::abs(m_minimumDelta.height()), 1.0);
case Direction::Left:
case Direction::Right:
return std::min(std::abs(delta.width()) / std::abs(m_minimumDelta.width()), 1.0);
default:
Q_UNREACHABLE();
}
}
bool SwipeGesture::minimumDeltaReached(const QSizeF &delta) const
{
return minimumDeltaReachedProgress(delta) >= 1.0;
}
GestureRecognizer::GestureRecognizer(QObject *parent)
: QObject(parent)
{
}
GestureRecognizer::~GestureRecognizer() = default;
void GestureRecognizer::registerGesture(KWin::Gesture* gesture)
{
Q_ASSERT(!m_gestures.contains(gesture));
auto connection = connect(gesture, &QObject::destroyed, this, std::bind(&GestureRecognizer::unregisterGesture, this, gesture));
m_destroyConnections.insert(gesture, connection);
m_gestures << gesture;
}
void GestureRecognizer::unregisterGesture(KWin::Gesture* gesture)
{
auto it = m_destroyConnections.find(gesture);
if (it != m_destroyConnections.end()) {
disconnect(it.value());
m_destroyConnections.erase(it);
}
m_gestures.removeAll(gesture);
if (m_activeSwipeGestures.removeOne(gesture)) {
// jing_kwin gesture
emit gesture->cancelled(0, 0);
}
}
void GestureRecognizer::registerPinchGesture(Gesture *gesture)
{
Q_ASSERT(!m_pinchGestures.contains(gesture));
auto connection = connect(gesture, &QObject::destroyed, this, std::bind(&GestureRecognizer::unregisterPinchGesture, this, gesture));
m_destroyPinchConnections.insert(gesture, connection);
m_pinchGestures << gesture;
}
void GestureRecognizer::unregisterPinchGesture(Gesture *gesture)
{
auto it = m_destroyPinchConnections.find(gesture);
if (it != m_destroyPinchConnections.end()) {
disconnect(it.value());
m_destroyPinchConnections.erase(it);
}
m_pinchGestures.removeAll(gesture);
emit gesture->cancelled(0, 0);
}
// jing_kwin gesture
int GestureRecognizer::startSwipeGesture(uint fingerCount, const QPointF &startPos, StartPositionBehavior startPosBehavior, quint32 time)
{
int count = 0;
// TODO: verify that no gesture is running
for (Gesture *gesture : qAsConst(m_gestures)) {
SwipeGesture *swipeGesture = qobject_cast<SwipeGesture*>(gesture);
if (!gesture) {
continue;
}
if (swipeGesture->minimumFingerCountIsRelevant()) {
if (swipeGesture->minimumFingerCount() > fingerCount) {
continue;
}
}
if (swipeGesture->maximumFingerCountIsRelevant()) {
if (swipeGesture->maximumFingerCount() < fingerCount) {
continue;
}
}
if (startPosBehavior == StartPositionBehavior::Relevant) {
if (swipeGesture->minimumXIsRelevant()) {
if (swipeGesture->minimumX() > std::ceil(startPos.x())) {
continue;
}
}
if (swipeGesture->maximumXIsRelevant()) {
if (swipeGesture->maximumX() < std::floor(startPos.x())) {
continue;
}
}
if (swipeGesture->minimumYIsRelevant()) {
if (swipeGesture->minimumY() > std::ceil(startPos.y())) {
continue;
}
}
if (swipeGesture->maximumYIsRelevant()) {
if (swipeGesture->maximumY() < std::floor(startPos.y())) {
continue;
}
}
}
// direction doesn't matter yet
m_activeSwipeGestures << swipeGesture;
count++;
// jing_kwin gesture
m_lastUpdateTime = m_lastEndUpdateTime = time;
emit swipeGesture->started(time);
}
return count;
}
// jing_kwin gesture
void GestureRecognizer::updateSwipeGesture(const QSizeF &delta, quint32 time)
{
m_swipeUpdates << delta;
if (std::abs(delta.width()) < 1 && std::abs(delta.height()) < 1) {
// some (touch) devices report sub-pixel movement on screen edges
// this often cancels gestures -> ignore these movements
return;
}
// determine the direction of the swipe
if (delta.width() == delta.height()) {
// special case of diagonal, this is not yet supported, thus cancel all gestures
// jing_kwin gesture
cancelActiveSwipeGestures(time);
return;
}
// jing_kwin gesture
if ((time - m_lastUpdateTime) > GESTURE_INTERVAL) {
m_startIndex = m_endIndex;
m_endIndex = m_swipeUpdates.size();
m_lastUpdateTime = m_lastEndUpdateTime;
m_lastEndUpdateTime = time;
}
// jing_kwin gesture end
// SwipeGesture::Direction direction;
// if (std::abs(delta.width()) > std::abs(delta.height())) {
// // horizontal
// direction = delta.width() < 0 ? SwipeGesture::Direction::Left : SwipeGesture::Direction::Right;
// } else {
// // vertical
// direction = delta.height() < 0 ? SwipeGesture::Direction::Up : SwipeGesture::Direction::Down;
// }
bool isTriggerFlag = false;
const QSizeF combinedDelta = std::accumulate(m_swipeUpdates.constBegin(), m_swipeUpdates.constEnd(), QSizeF(0, 0));
for (auto it = m_activeSwipeGestures.begin(); it != m_activeSwipeGestures.end();) {
auto g = qobject_cast<SwipeGesture*>(*it);
//if (g->direction() == direction || g->direction() == SwipeGesture::Direction::All) {
if (g->minimumDeltaReached(combinedDelta)) {
qreal spead = 0.0;
const QSizeF lastDelta = std::accumulate(m_swipeUpdates.constBegin() + m_startIndex, m_swipeUpdates.constEnd(), QSizeF(0, 0));
if (std::abs(lastDelta.width()) > std::abs(lastDelta.height())) {
spead = lastDelta.width() / (time - m_lastUpdateTime);
} else {
spead = lastDelta.height() / (time - m_lastUpdateTime);
}
emit g->triggered(time, spead);
isTriggerFlag = true;
it = m_activeSwipeGestures.erase(it);
} else {
if (g->isMinimumDeltaRelevant()) {
// jing_kwin gesture
qreal curProgress = g->minimumDeltaReachedProgress(combinedDelta);
emit g->progress(curProgress, time);
emit g->update(delta, curProgress, time);
}
it++;
}
// } else {
// // jing_kwin gesture
// emit g->cancelled(time);
// it = m_activeSwipeGestures.erase(it);
// }
}
if (isTriggerFlag) {
isTriggerFlag = false;
for (auto it = m_activeSwipeGestures.begin(); it != m_activeSwipeGestures.end();){
auto g = qobject_cast<SwipeGesture*>(*it);
emit g->cancelled(time, 0);
it = m_activeSwipeGestures.erase(it);
}
}
}
// jing_kwin gesture
bool GestureRecognizer::cancelActiveSwipeGestures(quint32 time, int fingerCount)
{
if (fingerCount > 0) {
for (auto g : qAsConst(m_activeSwipeGestures)) {
// jing_kwin gesture
SwipeGesture *sg = dynamic_cast<SwipeGesture*>(g);
if (sg && sg->maximumFingerCount() >= fingerCount && sg->minimumFingerCount() <= fingerCount) {
return false;
}
}
}
for (auto g : qAsConst(m_activeSwipeGestures)) {
// jing_kwin gesture
emit g->cancelled(time, 0);
}
m_activeSwipeGestures.clear();
// jing_kwin gesture
m_lastUpdateTime = 0;
m_startIndex = 0;
m_endIndex = 0;
m_lastEndUpdateTime = 0;
// jing_kwin gesture end
return true;
}
// jing_kwin gesture
bool GestureRecognizer::cancelSwipeGesture(quint32 time, int fingerCount)
{
// jing_kwin gesture
if (!cancelActiveSwipeGestures(time, fingerCount)) {
return false;
}
m_swipeUpdates.clear();
// jing_kwin gesture
m_lastUpdateTime = 0;
m_startIndex = 0;
m_endIndex = 0;
m_lastEndUpdateTime = 0;
// jing_kwin gesture end
return true;
}
// jing_kwin gesture
void GestureRecognizer::endSwipeGesture(quint32 time)
{
const QSizeF delta = std::accumulate(m_swipeUpdates.constBegin(), m_swipeUpdates.constEnd(), QSizeF(0, 0));
for (auto g : qAsConst(m_activeSwipeGestures)) {
// jing_kwin gesture
SwipeGesture *swipeGesture = static_cast<SwipeGesture*>(g);
qreal spead = 0.0;
const QSizeF lastDelta = std::accumulate(m_swipeUpdates.constBegin() + m_startIndex, m_swipeUpdates.constEnd(), QSizeF(0, 0));
if (std::abs(lastDelta.width()) > std::abs(lastDelta.height())) {
spead = lastDelta.width() / (time - m_lastUpdateTime);
} else {
spead = lastDelta.height() / (time - m_lastUpdateTime);
}
if (swipeGesture->minimumDeltaReached(delta)) {
emit g->triggered(time, spead);
} else {
emit g->cancelled(time, spead);
}
}
m_activeSwipeGestures.clear();
m_swipeUpdates.clear();
m_lastUpdateTime = 0;
m_startIndex = 0;
m_endIndex = 0;
m_lastEndUpdateTime = 0;
// jing_kwin gesture
}
// jing_kwin gesture
int GestureRecognizer::startPinchGesture(qint32 id, const QPointF &pos, quint32 time)
{
auto it = m_fingerPoints.find(id);
if (it == m_fingerPoints.end()) {
m_fingerPoints.insert(id, pos);
} else {
it.value() = pos;
}
return m_fingerPoints.size();
}
// jing_kwin gesture
void GestureRecognizer::updatePinchGesture(qint32 id, const QPointF &pos, quint32 time)
{
auto it = m_fingerPoints.find(id);
if (it != m_fingerPoints.end()) {
it.value() = pos;
} else {
m_fingerPoints.insert(id, pos);
}
for (Gesture *gesture : qAsConst(m_pinchGestures)) {
PinchGesture *pinchGesture = qobject_cast<PinchGesture*>(gesture);
if (!gesture) {
continue;
}
if (pinchGesture->maximumFingerCountIsRelevant()) {
if (pinchGesture->maximumFingerCount() < m_fingerPoints.size()) {
continue;
}
}
if (pinchGesture->minimumFingerCountIsRelevant()) {
if (pinchGesture->minimumFingerCount() > m_fingerPoints.size()) {
continue;
}
}
m_activePinchGestures << pinchGesture;
}
if (m_isPinchTrigger == false) {
for (auto it = m_activePinchGestures.begin(); it != m_activePinchGestures.end(); it++) {
auto g = qobject_cast<PinchGesture*>(*it);
if (g->isReachIncrement(PinchGesture::calFingerDistance(m_fingerPoints))) {
emit g->triggered(0, 0);
m_isPinchTrigger = true;
// it = m_activePinchGestures.erase(it);
// cancelPinchGesture(0);
// cancelSwipeGesture(0, 0);
break;
}
}
}
}
// jing_kwin gesture
bool GestureRecognizer::cancelPinchGesture(qint32 touches)
{
Q_UNUSED(touches);
m_isPinchTrigger = false;
m_fingerPoints.clear();
for (auto it = m_activePinchGestures.begin(); it != m_activePinchGestures.end(); it++) {
auto g = qobject_cast<PinchGesture*>(*it);
emit g->cancelled(0, 0);
}
m_activePinchGestures.clear();
return false;
}
// jing_kwin gesture
void GestureRecognizer::endPinchGesture(qint32 id, quint32 time)
{
cancelPinchGesture(0);
// Q_UNUSED(time);
// auto it = m_fingerPoints.find(id);
// if (it != m_fingerPoints.end()) {
// m_fingerPoints.erase(it);
// }
}
}