Properly capture graphics scene mouse events

fsp_feeds
Tyler Goodlet 2021-08-13 12:01:36 -04:00
parent 3ddd4bc2c2
commit 5ce6dcf3cb
1 changed files with 19 additions and 9 deletions

View File

@ -21,11 +21,21 @@ Qt event proxying and processing using ``trio`` mem chans.
from contextlib import asynccontextmanager, AsyncExitStack from contextlib import asynccontextmanager, AsyncExitStack
from typing import Callable 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.QtCore import QEvent, pyqtBoundSignal
from PyQt5.QtWidgets import QWidget from PyQt5.QtWidgets import QWidget
import trio from PyQt5.QtWidgets import QGraphicsSceneMouseEvent as gs_mouse
from pydantic import BaseModel
MOUSE_EVENTS = {
gs_mouse.GraphicsSceneMousePress,
gs_mouse.GraphicsSceneMouseRelease,
QEvent.MouseButtonPress,
QEvent.MouseButtonRelease,
# QtGui.QMouseEvent,
}
# TODO: maybe consider some constrained ints down the road? # TODO: maybe consider some constrained ints down the road?
@ -59,6 +69,7 @@ class MouseMsg(BaseModel):
etype: int etype: int
button: int button: int
# TODO: maybe add some methods to detect key combos? Or is that gonna be # TODO: maybe add some methods to detect key combos? Or is that gonna be
# better with pattern matching? # better with pattern matching?
# # ctl + alt as combo # # ctl + alt as combo
@ -78,8 +89,10 @@ class EventRelay(QtCore.QObject):
def eventFilter( def eventFilter(
self, self,
source: QWidget, source: QWidget,
ev: QEvent, ev: QEvent,
) -> None: ) -> None:
''' '''
Qt global event filter: return `False` to pass through and `True` Qt global event filter: return `False` to pass through and `True`
@ -90,6 +103,8 @@ class EventRelay(QtCore.QObject):
''' '''
etype = ev.type() etype = ev.type()
# TODO: turn this on and see what we can filter by default (such
# as mouseWheelEvent).
# print(f'ev: {ev}') # print(f'ev: {ev}')
if etype in self._event_types: if etype in self._event_types:
@ -125,12 +140,7 @@ class EventRelay(QtCore.QObject):
# processing **before** running a ``trio`` guest mode # processing **before** running a ``trio`` guest mode
# tick, thus special handling or copying must be done. # tick, thus special handling or copying must be done.
elif etype in { elif etype in MOUSE_EVENTS:
QEvent.MouseButtonPress,
QEvent.MouseButtonRelease,
QtGui.QMouseEvent,
}:
print('mouse')
msg = MouseMsg( msg = MouseMsg(
event=ev, event=ev,
etype=etype, etype=etype,