KWidgetsAddons

ktwofingertap.cpp
1/*
2 This file is part of the KDE libraries
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 "ktwofingertap.h"
10
11// Qt
12#include <QApplication>
13#include <QGraphicsWidget>
14#include <QTouchEvent>
15
16class KTwoFingerTapRecognizerPrivate
17{
18public:
19 KTwoFingerTapRecognizerPrivate(KTwoFingerTapRecognizer *parent)
20 : q(parent)
21 {
22 }
24 bool mGestureTriggered = false;
25 int mTapRadius = 40; // same as Qt::TapGesture
27};
28
31 , d(new KTwoFingerTapRecognizerPrivate(this))
32{
33}
34
38
40{
41 Q_UNUSED(target)
42 return static_cast<QGesture *>(new KTwoFingerTap());
43}
44
46{
47 Q_UNUSED(watched)
48
49 KTwoFingerTap *ktwoFingerTap = static_cast<KTwoFingerTap *>(gesture);
50 const QTouchEvent *touchEvent = static_cast<const QTouchEvent *>(event);
51
52 switch (event->type()) {
53 case QEvent::TouchBegin: {
54 const QTouchEvent::TouchPoint tp = touchEvent->points().first();
55 ktwoFingerTap->setHotSpot(tp.globalPressPosition());
56 ktwoFingerTap->setPos(tp.pressPosition());
57 ktwoFingerTap->setScreenPos(tp.globalPressPosition());
58 ktwoFingerTap->setScenePos(tp.scenePressPosition());
59 d->mLastState = Qt::NoGesture;
60 return MayBeGesture;
61 }
62
64 // The gesture was already canceled or finished, so we will ignore this event.
65 if (d->mLastState == Qt::GestureCanceled || d->mLastState == Qt::GestureFinished) {
66 return Ignore;
67 }
68
69 const int touchPointSize = touchEvent->points().size();
70
71 if (touchPointSize > 2) {
72 d->mLastState = Qt::GestureCanceled;
73 return CancelGesture;
74 }
75
76 if (touchPointSize == 2) {
77 if ((touchEvent->points().first().pressPosition() - touchEvent->points().first().position()).manhattanLength() > d->mTapRadius) {
78 d->mLastState = Qt::GestureCanceled;
79 return CancelGesture;
80 }
81 if ((touchEvent->points().at(1).pressPosition() - touchEvent->points().at(1).position()).manhattanLength() > d->mTapRadius) {
82 d->mLastState = Qt::GestureCanceled;
83 return CancelGesture;
84 }
85 if (touchEvent->touchPointStates() & Qt::TouchPointPressed) {
86 d->mLastState = gesture->state() == Qt::NoGesture ? Qt::GestureStarted : Qt::GestureUpdated;
87 return TriggerGesture;
88 }
89 if (touchEvent->touchPointStates() & Qt::TouchPointMoved) {
90 d->mLastState = gesture->state() == Qt::NoGesture ? Qt::GestureStarted : Qt::GestureUpdated;
91 return TriggerGesture;
92 }
93 if (touchEvent->touchPointStates() & Qt::TouchPointReleased) {
94 d->mLastState = Qt::GestureFinished;
95 return FinishGesture;
96 }
97 }
98 break;
99 }
100
101 case QEvent::TouchEnd: {
102 if (d->mLastState == Qt::GestureStarted || d->mLastState == Qt::GestureUpdated) {
103 return FinishGesture;
104 }
105 break;
106 }
107
108 default:
109 return Ignore;
110 }
111 return Ignore;
112}
113
115{
116 if (i < 0) {
117 i = 0;
118 }
119
120 d->mTapRadius = i;
121}
122
124{
125 return d->mTapRadius;
126}
127
128class KTwoFingerTapPrivate
129{
130public:
131 KTwoFingerTapPrivate(KTwoFingerTap *parent)
132 : q(parent)
133 {
134 }
135 KTwoFingerTap *const q;
136 QPointF mPos = QPointF(-1, -1);
137 QPointF mScreenPos = QPointF(-1, -1);
138 QPointF mScenePos = QPointF(-1, -1);
139};
140
142 : QGesture(parent)
143 , d(new KTwoFingerTapPrivate(this))
144{
145}
146
150
151QPointF KTwoFingerTap::pos() const
152{
153 return d->mPos;
154}
155
157{
158 d->mPos = _pos;
159}
160
161QPointF KTwoFingerTap::screenPos() const
162{
163 return d->mScreenPos;
164}
165
167{
168 d->mScreenPos = _screenPos;
169}
170
171QPointF KTwoFingerTap::scenePos() const
172{
173 return d->mScenePos;
174}
175
177{
178 d->mScenePos = _scenePos;
179}
180
181#include "moc_ktwofingertap.cpp"
The recognizer for a two finger tap gesture.
void setTapRadius(int i)
Set the maximum wiggle room for a touch point.
~KTwoFingerTapRecognizer() override
Destructor.
Result recognize(QGesture *gesture, QObject *watched, QEvent *event) override
Handles the given event for the watched object and update the gesture object.
QGesture * create(QObject *target) override
Qt called this member to create a new QGesture object.
KTwoFingerTapRecognizer()
The constructor.
A two finger tap gesture.
void setPos(QPointF pos)
Sets the position, relative to the widget.
KTwoFingerTap(QObject *parent=nullptr)
The constructor.
void setScenePos(QPointF scenePos)
Sets the scene position.
void setScreenPos(QPointF screenPos)
~KTwoFingerTap() override
Destructor.
void setHotSpot(const QPointF &value)
GestureState
TouchPointPressed
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.