-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_finger_pan_gesture.py
More file actions
85 lines (67 loc) · 2.98 KB
/
three_finger_pan_gesture.py
File metadata and controls
85 lines (67 loc) · 2.98 KB
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
from PySide6.QtCore import QPointF, QEvent, Qt
from PySide6.QtWidgets import QGesture, QGestureRecognizer
class ThreeFingerPanGesture(QGesture):
def __init__(self, parent=None):
super().__init__(parent)
self._delta = QPointF()
self._last_offset = QPointF()
self._offset = QPointF()
def delta(self):
return self._delta
def lastOffset(self):
return self._last_offset
def offset(self):
return self._offset
def setDelta(self, value):
self._delta = value
def setLastOffset(self, value):
self._last_offset = value
def setOffset(self, value):
self._offset = value
from PySide6.QtCore import QEvent, Qt, QPointF
from PySide6.QtWidgets import QGestureRecognizer
class ThreeFingerPanGestureRecognizer(QGestureRecognizer):
def __init__(self):
super().__init__()
self.amplification_factor = 2.0 # Adjust this factor to increase sensitivity
def create(self, target):
return ThreeFingerPanGesture(target)
def recognize(self, gesture, watched, event):
if not isinstance(gesture, ThreeFingerPanGesture):
return QGestureRecognizer.ResultFlag.CancelGesture
if event.type() == QEvent.Type.TouchBegin:
if len(event.touchPoints()) == 3:
gesture.setLastOffset(QPointF())
gesture.setOffset(QPointF())
gesture.setDelta(QPointF())
gesture.state = Qt.GestureState.GestureStarted
return QGestureRecognizer.ResultFlag.MayBeGesture
else:
return QGestureRecognizer.ResultFlag.CancelGesture
elif event.type() == QEvent.Type.TouchUpdate:
if len(event.touchPoints()) == 3:
current_pos = event.touchPoints()[0].pos()
start_pos = event.touchPoints()[0].startPos()
last_offset = gesture.offset()
new_offset = current_pos - start_pos
delta = (new_offset - last_offset) * self.amplification_factor
gesture.setLastOffset(last_offset)
gesture.setOffset(new_offset)
gesture.setDelta(delta)
gesture.state = Qt.GestureState.GestureUpdated
return QGestureRecognizer.ResultFlag.TriggerGesture
else:
return QGestureRecognizer.ResultFlag.CancelGesture
elif event.type() == QEvent.Type.TouchEnd:
if gesture.state == Qt.GestureState.GestureUpdated:
gesture.state = Qt.GestureState.GestureFinished
return QGestureRecognizer.ResultFlag.FinishGesture
else:
return QGestureRecognizer.ResultFlag.CancelGesture
return QGestureRecognizer.ResultFlag.Ignore
def reset(self, gesture):
if isinstance(gesture, ThreeFingerPanGesture):
gesture.setDelta(QPointF())
gesture.setLastOffset(QPointF())
gesture.setOffset(QPointF())
super().reset(gesture)