10
10
11
11
12
12
@pytest .fixture (autouse = True )
13
- def mpl_test_settings (qt_module , mpl_test_settings ):
13
+ def mpl_test_settings (qt_core , mpl_test_settings ):
14
14
"""
15
- Ensure qt_module fixture is *first* fixture.
15
+ Ensure qt_core fixture is *first* fixture.
16
16
17
- We override the `mpl_test_settings` fixture and depend on the `qt_module `
17
+ We override the `mpl_test_settings` fixture and depend on the `qt_core `
18
18
fixture first. It is very important that it is first, because it skips
19
19
tests when Qt is not available, and if not, then the main
20
20
`mpl_test_settings` fixture will try to switch backends before the skip can
21
21
be triggered.
22
22
"""
23
- pass
24
23
25
24
26
25
@pytest .fixture
27
- def qt_module (request ):
26
+ def qt_core (request ):
28
27
backend , = request .node .get_closest_marker ('backend' ).args
29
28
if backend == 'Qt4Agg' :
30
29
if any (k in sys .modules for k in ('PyQt5' , 'PySide2' )):
@@ -59,43 +58,10 @@ def qt_module(request):
59
58
py_qt_ver = int (QtCore .PYQT_VERSION_STR .split ('.' )[0 ])
60
59
except AttributeError :
61
60
py_qt_ver = QtCore .__version_info__ [0 ]
62
-
63
61
if py_qt_ver != 4 :
64
62
pytest .skip ('Qt4 is not available' )
65
63
66
- from matplotlib .backends .backend_qt4 import (
67
- MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT )
68
- elif backend == 'Qt5Agg' :
69
- from matplotlib .backends .backend_qt5 import (
70
- MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT )
71
-
72
- mods = {}
73
- keys = {}
74
- for name , index in zip (['Alt' , 'Control' , 'Shift' , 'Super' ],
75
- [ALT , CTRL , SHIFT , SUPER ]):
76
- _ , mod , key = MODIFIER_KEYS [index ]
77
- mods [name + 'Modifier' ] = mod
78
- keys [name + 'Key' ] = key
79
-
80
- return QtCore , mods , keys
81
-
82
-
83
- @pytest .fixture
84
- def qt_key (request ):
85
- QtCore , _ , keys = request .getfixturevalue ('qt_module' )
86
- if request .param .startswith ('Key' ):
87
- return getattr (QtCore .Qt , request .param )
88
- else :
89
- return keys [request .param ]
90
-
91
-
92
- @pytest .fixture
93
- def qt_mods (request ):
94
- QtCore , mods , _ = request .getfixturevalue ('qt_module' )
95
- result = QtCore .Qt .NoModifier
96
- for mod in request .param :
97
- result |= mods [mod ]
98
- return result
64
+ return QtCore
99
65
100
66
101
67
@pytest .mark .parametrize ('backend' , [
@@ -120,13 +86,10 @@ def test_fig_close(backend):
120
86
121
87
122
88
@pytest .mark .backend ('Qt5Agg' )
123
- def test_fig_signals (qt_module ):
89
+ def test_fig_signals (qt_core ):
124
90
# Create a figure
125
91
plt .figure ()
126
92
127
- # Access QtCore
128
- QtCore = qt_module [0 ]
129
-
130
93
# Access signals
131
94
import signal
132
95
event_loop_signal = None
@@ -138,10 +101,10 @@ def fire_signal_and_quit():
138
101
event_loop_signal = signal .getsignal (signal .SIGINT )
139
102
140
103
# Request event loop exit
141
- QtCore .QCoreApplication .exit ()
104
+ qt_core .QCoreApplication .exit ()
142
105
143
106
# Timer to exit event loop
144
- QtCore .QTimer .singleShot (0 , fire_signal_and_quit )
107
+ qt_core .QTimer .singleShot (0 , fire_signal_and_quit )
145
108
146
109
# Save original SIGINT handler
147
110
original_signal = signal .getsignal (signal .SIGINT )
@@ -176,15 +139,14 @@ def CustomHandler(signum, frame):
176
139
'\N{LATIN CAPITAL LETTER A WITH ACUTE} ' ),
177
140
('Key_Aacute' , [],
178
141
'\N{LATIN SMALL LETTER A WITH ACUTE} ' ),
179
- ('ControlKey ' , ['AltModifier' ], 'alt+control' ),
180
- ('AltKey ' , ['ControlModifier' ], 'ctrl+alt' ),
181
- ('Key_Aacute' , ['ControlModifier' , 'AltModifier' , 'SuperModifier ' ],
142
+ ('Key_Control ' , ['AltModifier' ], 'alt+control' ),
143
+ ('Key_Alt ' , ['ControlModifier' ], 'ctrl+alt' ),
144
+ ('Key_Aacute' , ['ControlModifier' , 'AltModifier' , 'MetaModifier ' ],
182
145
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE} ' ),
183
146
('Key_Backspace' , [], 'backspace' ),
184
147
('Key_Backspace' , ['ControlModifier' ], 'ctrl+backspace' ),
185
148
('Key_Play' , [], None ),
186
149
],
187
- indirect = ['qt_key' , 'qt_mods' ],
188
150
ids = [
189
151
'shift' ,
190
152
'lower' ,
@@ -204,23 +166,26 @@ def CustomHandler(signum, frame):
204
166
pytest .param ('Qt4Agg' , marks = pytest .mark .backend ('Qt4Agg' )),
205
167
pytest .param ('Qt5Agg' , marks = pytest .mark .backend ('Qt5Agg' )),
206
168
])
207
- def test_correct_key (backend , qt_key , qt_mods , answer ):
169
+ def test_correct_key (backend , qt_core , qt_key , qt_mods , answer ):
208
170
"""
209
171
Make a figure.
210
172
Send a key_press_event event (using non-public, qtX backend specific api).
211
173
Catch the event.
212
174
Assert sent and caught keys are the same.
213
175
"""
214
- qt_canvas = plt .figure ().canvas
176
+ qt_mod = qt_core .Qt .NoModifier
177
+ for mod in qt_mods :
178
+ qt_mod |= getattr (qt_core .Qt , mod )
215
179
216
180
class _Event :
217
181
def isAutoRepeat (self ): return False
218
- def key (self ): return qt_key
219
- def modifiers (self ): return qt_mods
182
+ def key (self ): return getattr ( qt_core . Qt , qt_key )
183
+ def modifiers (self ): return qt_mod
220
184
221
185
def receive (event ):
222
186
assert event .key == answer
223
187
188
+ qt_canvas = plt .figure ().canvas
224
189
qt_canvas .mpl_connect ('key_press_event' , receive )
225
190
qt_canvas .keyPressEvent (_Event ())
226
191
0 commit comments