KWidgetsAddons

ktwofingerswipe.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2021 Steffen Hartleib <steffenhartleib@t-online.de>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8// Self
9#include "ktwofingerswipe.h"
10
11// Qt
12#include <QDebug>
13#include <QGraphicsWidget>
14#include <QLineF>
15#include <QTouchEvent>
16class KTwoFingerSwipeRecognizerPrivate
17{
18public:
19 KTwoFingerSwipeRecognizerPrivate(KTwoFingerSwipeRecognizer *parent)
20 : q(parent)
21 {
22 }
23 KTwoFingerSwipeRecognizer *const q;
24 qint64 mTouchBeginnTimestamp = 0;
25 bool mGestureAlreadyFinished = false;
26 int mMaxSwipeTime = 90;
27 int mMinSwipeDistance = 30;
28};
29
32 , d(new KTwoFingerSwipeRecognizerPrivate(this))
33{
34}
35
39
41{
42 Q_UNUSED(target);
43 return static_cast<QGesture *>(new KTwoFingerSwipe());
44}
45
47{
48 Q_UNUSED(watched)
49
50 KTwoFingerSwipe *kTwoFingerSwipe = static_cast<KTwoFingerSwipe *>(gesture);
51 const QTouchEvent *touchEvent = static_cast<const QTouchEvent *>(event);
52
53 switch (event->type()) {
54 case QEvent::TouchBegin: {
55 d->mTouchBeginnTimestamp = touchEvent->timestamp();
56 d->mGestureAlreadyFinished = false;
57 const QTouchEvent::TouchPoint tp = touchEvent->points().first();
58 kTwoFingerSwipe->setHotSpot(tp.globalPressPosition());
59 kTwoFingerSwipe->setPos(tp.pressPosition());
60 kTwoFingerSwipe->setScreenPos(tp.globalPressPosition());
61 kTwoFingerSwipe->setScenePos(tp.scenePressPosition());
62 kTwoFingerSwipe->setSwipeAngle(0.0);
63 return MayBeGesture;
64 }
65
67 // The gesture was already canceled or finished, so we will ignore this event.
68 if (d->mGestureAlreadyFinished) {
69 return Ignore;
70 }
71
72 const qint64 now = touchEvent->timestamp();
73 const qint64 elapsedTime = now - d->mTouchBeginnTimestamp;
74 const QLineF ql = QLineF(touchEvent->points().first().pressPosition(), touchEvent->points().first().position());
75 const qreal length = ql.length();
76 const int touchPointSize = touchEvent->points().size();
77
78 kTwoFingerSwipe->setSwipeAngle(ql.angle());
79
80 if (touchPointSize > 2) {
81 d->mGestureAlreadyFinished = true;
82 return CancelGesture;
83 }
84
85 if (touchPointSize == 2) {
86 if (elapsedTime > d->mMaxSwipeTime) {
87 d->mGestureAlreadyFinished = true;
88 return CancelGesture;
89 }
90 if (length >= d->mMinSwipeDistance && elapsedTime <= d->mMaxSwipeTime && !d->mGestureAlreadyFinished) {
91 d->mGestureAlreadyFinished = true;
92 return FinishGesture;
93 } else if (elapsedTime <= d->mMaxSwipeTime && !d->mGestureAlreadyFinished) {
94 return TriggerGesture;
95 }
96 }
97 break;
98 }
99
100 case QEvent::TouchEnd: {
101 if (gesture->state() == Qt::GestureUpdated || gesture->state() == Qt::GestureStarted) {
102 if (!d->mGestureAlreadyFinished) {
103 return CancelGesture;
104 }
105 }
106 break;
107 }
108
109 default:
110 return Ignore;
111 }
112 return Ignore;
113}
114
116{
117 return d->mMaxSwipeTime;
118}
119
121{
122 if (i < 0) {
123 i = 0;
124 }
125
126 d->mMaxSwipeTime = i;
127}
128
130{
131 return d->mMinSwipeDistance;
132}
133
135{
136 if (i < 0) {
137 i = 0;
138 }
139
140 d->mMinSwipeDistance = i;
141}
142
143class KTwoFingerSwipePrivate
144{
145public:
146 KTwoFingerSwipePrivate(KTwoFingerSwipe *parent)
147 : q(parent)
148 {
149 }
150 KTwoFingerSwipe *const q;
151 QPointF mPos = QPointF(-1, -1);
152 QPointF mScreenPos = QPointF(-1, -1);
153 QPointF mScenePos = QPointF(-1, -1);
154 qreal mSwipeAngle = 0.0;
155};
156
159 , d(new KTwoFingerSwipePrivate(this))
160{
161}
162
166
167QPointF KTwoFingerSwipe::pos() const
168{
169 return d->mPos;
170}
171
173{
174 d->mPos = _pos;
175}
176
177QPointF KTwoFingerSwipe::screenPos() const
178{
179 return d->mScreenPos;
180}
181
183{
184 d->mScreenPos = _screenPos;
185}
186
187QPointF KTwoFingerSwipe::scenePos() const
188{
189 return d->mScenePos;
190}
191
193{
194 d->mScenePos = _scenePos;
195}
196
197qreal KTwoFingerSwipe::swipeAngle() const
198{
199 return d->mSwipeAngle;
200}
201void KTwoFingerSwipe::setSwipeAngle(qreal _swipeAngle)
202{
203 d->mSwipeAngle = _swipeAngle;
204}
205
206#include "moc_ktwofingerswipe.cpp"
QGesture * create(QObject *target) override
Qt called this member to create a new QGesture object.
KTwoFingerSwipeRecognizer()
The constructor.
void setMaxSwipeTime(int i)
Set the maximum duration of the swipe gesture.
Result recognize(QGesture *gesture, QObject *watched, QEvent *event) override
Handles the given event for the watched object and update the gesture object.
void setSwipeDistance(int i)
Set the minimum distance of the swipe gesture.
~KTwoFingerSwipeRecognizer() override
Destructor.
A two finger swipe gesture.
KTwoFingerSwipe(QObject *parent=nullptr)
The constructor.
void setScenePos(QPointF scenePos)
Sets the scene position.
void setPos(QPointF pos)
Sets the position, relative to the widget.
void setSwipeAngle(qreal swipeAngle)
Sets the angle of the swipe gesture.
void setScreenPos(QPointF screenPos)
Sets the screen position.
~KTwoFingerSwipe() override
Destructor.
QGesture(QObject *parent)
void setHotSpot(const QPointF &value)
qreal angle() const const
qreal length() const const
QObject(QObject *parent)
QObject * parent() const const
GestureUpdated
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:02:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.