5
5
6
6
from qtpy .QtCore import QEvent , QObject
7
7
from qtpy .QtGui import QMouseEvent
8
- from qtpy .QtWidgets import QApplication
9
-
10
- from ndv ._types import MouseButton , MouseMoveEvent , MousePressEvent , MouseReleaseEvent
8
+ from qtpy .QtWidgets import QApplication , QWidget
9
+
10
+ from ndv ._types import (
11
+ CursorType ,
12
+ MouseButton ,
13
+ MouseMoveEvent ,
14
+ MousePressEvent ,
15
+ MouseReleaseEvent ,
16
+ )
11
17
from ndv .views .bases ._app import NDVApp
12
18
13
19
if TYPE_CHECKING :
@@ -67,22 +73,22 @@ def array_view_class(self) -> type[ArrayView]:
67
73
def filter_mouse_events (
68
74
self , canvas : Any , receiver : Mouseable
69
75
) -> Callable [[], None ]:
70
- if not isinstance (canvas , QObject ):
71
- raise TypeError (f"Expected canvas to be QObject , got { type (canvas )} " )
76
+ if not isinstance (canvas , QWidget ):
77
+ raise TypeError (f"Expected canvas to be QWidget , got { type (canvas )} " )
72
78
73
79
f = MouseEventFilter (canvas , receiver )
74
80
canvas .installEventFilter (f )
75
81
return lambda : canvas .removeEventFilter (f )
76
82
77
83
78
84
class MouseEventFilter (QObject ):
79
- def __init__ (self , canvas : QObject , receiver : Mouseable ):
85
+ def __init__ (self , canvas : QWidget , receiver : Mouseable ):
80
86
super ().__init__ ()
81
87
self .canvas = canvas
82
88
self .receiver = receiver
83
89
self .active_button = MouseButton .NONE
84
90
85
- def mouse_btn (self , btn : Any ) -> MouseButton :
91
+ def mouse_btn (self , btn : Any ) -> MouseButton :
86
92
from qtpy .QtCore import Qt
87
93
88
94
if btn == Qt .MouseButton .LeftButton :
@@ -94,6 +100,9 @@ def mouse_btn(self, btn : Any) -> MouseButton:
94
100
95
101
raise Exception (f"Qt mouse button { btn } is unknown" )
96
102
103
+ def set_cursor (self , type : CursorType ) -> None :
104
+ self .canvas .setCursor (type .to_qt ())
105
+
97
106
def eventFilter (self , obj : QObject | None , qevent : QEvent | None ) -> bool :
98
107
"""Event filter installed on the canvas to handle mouse events.
99
108
@@ -121,14 +130,18 @@ def eventFilter(self, obj: QObject | None, qevent: QEvent | None) -> bool:
121
130
if etype == QEvent .Type .MouseMove :
122
131
mme = MouseMoveEvent (x = pos .x (), y = pos .y (), btn = self .active_button )
123
132
intercept |= receiver .on_mouse_move (mme )
133
+ if cursor := receiver .get_cursor (mme ):
134
+ self .set_cursor (cursor )
124
135
receiver .mouseMoved .emit (mme )
125
136
elif etype == QEvent .Type .MouseButtonPress :
126
137
self .active_button = btn
127
138
mpe = MousePressEvent (x = pos .x (), y = pos .y (), btn = self .active_button )
128
139
intercept |= receiver .on_mouse_press (mpe )
129
140
receiver .mousePressed .emit (mpe )
130
141
elif etype == QEvent .Type .MouseButtonRelease :
131
- mre = MouseReleaseEvent (x = pos .x (), y = pos .y (), btn = self .active_button )
142
+ mre = MouseReleaseEvent (
143
+ x = pos .x (), y = pos .y (), btn = self .active_button
144
+ )
132
145
self .active_button = MouseButton .NONE
133
146
intercept |= receiver .on_mouse_release (mre )
134
147
receiver .mouseReleased .emit (mre )
0 commit comments