From 5ce6dcf3cb7c2d33ea7289c353d3c19c11d2a38c Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 13 Aug 2021 12:01:36 -0400 Subject: [PATCH] Properly capture graphics scene mouse events --- piker/ui/_event.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/piker/ui/_event.py b/piker/ui/_event.py index ee5fb05b..c944301d 100644 --- a/piker/ui/_event.py +++ b/piker/ui/_event.py @@ -21,11 +21,21 @@ Qt event proxying and processing using ``trio`` mem chans. from contextlib import asynccontextmanager, AsyncExitStack from typing import Callable -from PyQt5 import QtCore, QtGui +from pydantic import BaseModel +import trio +from PyQt5 import QtCore from PyQt5.QtCore import QEvent, pyqtBoundSignal from PyQt5.QtWidgets import QWidget -import trio -from pydantic import BaseModel +from PyQt5.QtWidgets import QGraphicsSceneMouseEvent as gs_mouse + + +MOUSE_EVENTS = { + gs_mouse.GraphicsSceneMousePress, + gs_mouse.GraphicsSceneMouseRelease, + QEvent.MouseButtonPress, + QEvent.MouseButtonRelease, + # QtGui.QMouseEvent, +} # TODO: maybe consider some constrained ints down the road? @@ -59,6 +69,7 @@ class MouseMsg(BaseModel): etype: int button: int + # TODO: maybe add some methods to detect key combos? Or is that gonna be # better with pattern matching? # # ctl + alt as combo @@ -78,8 +89,10 @@ class EventRelay(QtCore.QObject): def eventFilter( self, + source: QWidget, ev: QEvent, + ) -> None: ''' Qt global event filter: return `False` to pass through and `True` @@ -90,6 +103,8 @@ class EventRelay(QtCore.QObject): ''' etype = ev.type() + # TODO: turn this on and see what we can filter by default (such + # as mouseWheelEvent). # print(f'ev: {ev}') if etype in self._event_types: @@ -125,12 +140,7 @@ class EventRelay(QtCore.QObject): # processing **before** running a ``trio`` guest mode # tick, thus special handling or copying must be done. - elif etype in { - QEvent.MouseButtonPress, - QEvent.MouseButtonRelease, - QtGui.QMouseEvent, - }: - print('mouse') + elif etype in MOUSE_EVENTS: msg = MouseMsg( event=ev, etype=etype,