Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for pinch events #515

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions wgpu/gui/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
try:
WA_PaintOnScreen = QtCore.Qt.WidgetAttribute.WA_PaintOnScreen
WA_DeleteOnClose = QtCore.Qt.WidgetAttribute.WA_DeleteOnClose
PinchGesture = QtCore.Qt.GestureType.PinchGesture
PreciseTimer = QtCore.Qt.TimerType.PreciseTimer
KeyboardModifiers = QtCore.Qt.KeyboardModifier
FocusPolicy = QtCore.Qt.FocusPolicy
Keys = QtCore.Qt.Key
except AttributeError:
WA_PaintOnScreen = QtCore.Qt.WA_PaintOnScreen
WA_DeleteOnClose = QtCore.Qt.WA_DeleteOnClose
PinchGesture = QtCore.Qt.PinchGesture
PreciseTimer = QtCore.Qt.PreciseTimer
KeyboardModifiers = QtCore.Qt
FocusPolicy = QtCore.Qt
Expand Down Expand Up @@ -146,6 +148,7 @@ def __init__(self, *args, **kwargs):
self.setAutoFillBackground(False)
self.setMouseTracking(True)
self.setFocusPolicy(FocusPolicy.StrongFocus)
self.grabGesture(PinchGesture)

# A timer for limiting fps
self._request_draw_timer = QtCore.QTimer()
Expand Down Expand Up @@ -244,6 +247,13 @@ def _key_event(self, event_type, event):
}
self._handle_event_and_flush(ev)

def event(self, event):
if isinstance(event, QtWidgets.QGestureEvent):
self._handle_gesture_event(event)
return True
else:
return super().event(event)

def keyPressEvent(self, event): # noqa: N802
self._key_event("key_down", event)

Expand Down Expand Up @@ -327,6 +337,20 @@ def wheelEvent(self, event): # noqa: N802
accum_keys = {"dx", "dy"}
self._handle_event_rate_limited(ev, call_later, match_keys, accum_keys)

def _handle_gesture_event(self, event):
for gesture in event.activeGestures():
if gesture.gestureType() == PinchGesture:
ev = {
"event_type": "pinch",
"scale": gesture.scaleFactor(),
"angle": gesture.lastRotationAngle() - gesture.rotationAngle(),
# "buttons": buttons, -> event does not have them. Drop or track these?
# "modifiers": modifiers,
}
match_keys = set()
accum_keys = {"scale", "angle"}
self._handle_event_rate_limited(ev, call_later, match_keys, accum_keys)

def resizeEvent(self, event): # noqa: N802
ev = {
"event_type": "resize",
Expand Down
Loading