1
1
"""
2
- This provides several classes used for blocking interaction with figure windows:
2
+ This provides several classes used for blocking interaction with figure
3
+ windows:
3
4
4
5
:class:`BlockingInput`
5
- creates a callable object to retrieve events in a blocking way for interactive sessions
6
+ creates a callable object to retrieve events in a blocking way for
7
+ interactive sessions
6
8
7
9
:class:`BlockingKeyMouseInput`
8
- creates a callable object to retrieve key or mouse clicks in a blocking way for interactive sessions.
10
+ creates a callable object to retrieve key or mouse clicks in a blocking
11
+ way for interactive sessions.
9
12
Note: Subclass of BlockingInput. Used by waitforbuttonpress
10
13
11
14
:class:`BlockingMouseInput`
12
- creates a callable object to retrieve mouse clicks in a blocking way for interactive sessions.
15
+ creates a callable object to retrieve mouse clicks in a blocking way for
16
+ interactive sessions.
13
17
Note: Subclass of BlockingInput. Used by ginput
14
18
15
19
:class:`BlockingContourLabeler`
16
- creates a callable object to retrieve mouse clicks in a blocking way that will then be used to place labels on a ContourSet
20
+ creates a callable object to retrieve mouse clicks in a blocking way that
21
+ will then be used to place labels on a ContourSet
17
22
Note: Subclass of BlockingMouseInput. Used by clabel
18
23
"""
19
24
22
27
from matplotlib .cbook import is_sequence_of_strings
23
28
import matplotlib .lines as mlines
24
29
30
+
25
31
class BlockingInput (object ):
26
32
"""
27
33
Class that creates a callable object to retrieve events in a
28
34
blocking way.
29
35
"""
30
36
def __init__ (self , fig , eventslist = ()):
31
37
self .fig = fig
32
- assert is_sequence_of_strings (eventslist ), "Requires a sequence of event name strings"
38
+ assert is_sequence_of_strings (
39
+ eventslist ), "Requires a sequence of event name strings"
33
40
self .eventslist = eventslist
34
41
35
42
def on_event (self , event ):
@@ -60,13 +67,13 @@ def cleanup(self):
60
67
for cb in self .callbacks :
61
68
self .fig .canvas .mpl_disconnect (cb )
62
69
63
- self .callbacks = []
70
+ self .callbacks = []
64
71
65
- def add_event (self ,event ):
72
+ def add_event (self , event ):
66
73
"""For base class, this just appends an event to events."""
67
74
self .events .append (event )
68
75
69
- def pop_event (self ,index = - 1 ):
76
+ def pop_event (self , index = - 1 ):
70
77
"""
71
78
This removes an event from the event list. Defaults to
72
79
removing last event, but an index can be supplied. Note that
@@ -76,11 +83,11 @@ def pop_event(self,index=-1):
76
83
"""
77
84
self .events .pop (index )
78
85
79
- def pop (self ,index = - 1 ):
86
+ def pop (self , index = - 1 ):
80
87
self .pop_event (index )
81
- pop .__doc__ = pop_event .__doc__
88
+ pop .__doc__ = pop_event .__doc__
82
89
83
- def __call__ (self , n = 1 , timeout = 30 ):
90
+ def __call__ (self , n = 1 , timeout = 30 ):
84
91
"""
85
92
Blocking call to retrieve n events
86
93
"""
@@ -96,18 +103,20 @@ def __call__(self, n=1, timeout=30 ):
96
103
97
104
# connect the events to the on_event function call
98
105
for n in self .eventslist :
99
- self .callbacks .append ( self .fig .canvas .mpl_connect (n , self .on_event ) )
106
+ self .callbacks .append (
107
+ self .fig .canvas .mpl_connect (n , self .on_event ))
100
108
101
109
try :
102
110
# Start event loop
103
111
self .fig .canvas .start_event_loop (timeout = timeout )
104
- finally : # Run even on exception like ctrl-c
112
+ finally : # Run even on exception like ctrl-c
105
113
# Disconnect the callbacks
106
114
self .cleanup ()
107
115
108
116
# Return the events in this case
109
117
return self .events
110
118
119
+
111
120
class BlockingMouseInput (BlockingInput ):
112
121
"""
113
122
Class that creates a callable object to retrieve mouse clicks in a
@@ -118,25 +127,23 @@ class BlockingMouseInput(BlockingInput):
118
127
enter is like mouse button 2 and all others are like mouse button 1).
119
128
"""
120
129
121
- button_add = 1
122
- button_pop = 3
123
- button_stop = 2
130
+ button_add = 1
131
+ button_pop = 3
132
+ button_stop = 2
124
133
125
134
def __init__ (self , fig , mouse_add = 1 , mouse_pop = 3 , mouse_stop = 2 ):
126
135
BlockingInput .__init__ (self , fig = fig ,
127
136
eventslist = ('button_press_event' ,
128
- 'key_press_event' ) )
137
+ 'key_press_event' ))
129
138
self .button_add = mouse_add
130
139
self .button_pop = mouse_pop
131
- self .button_stop = mouse_stop
132
-
133
-
140
+ self .button_stop = mouse_stop
134
141
135
142
def post_event (self ):
136
143
"""
137
144
This will be called to process events
138
145
"""
139
- assert len (self .events )> 0 , "No events yet"
146
+ assert len (self .events ) > 0 , "No events yet"
140
147
141
148
if self .events [- 1 ].name == 'key_press_event' :
142
149
self .key_event ()
@@ -171,56 +178,57 @@ def key_event(self):
171
178
172
179
if key in ['backspace' , 'delete' ]:
173
180
self .mouse_event_pop (event )
174
- elif key in ['escape' , 'enter' ]: # on windows XP and wxAgg, the enter key doesn't seem to register
181
+ elif key in ['escape' , 'enter' ]:
182
+ # on windows XP and wxAgg, the enter key doesn't seem to register
175
183
self .mouse_event_stop (event )
176
184
else :
177
185
self .mouse_event_add (event )
178
186
179
- def mouse_event_add ( self , event ):
187
+ def mouse_event_add (self , event ):
180
188
"""
181
189
Will be called for any event involving a button other than
182
190
button 2 or 3. This will add a click if it is inside axes.
183
191
"""
184
192
if event .inaxes :
185
193
self .add_click (event )
186
- else : # If not a valid click, remove from event list
187
- BlockingInput .pop (self ,- 1 )
194
+ else : # If not a valid click, remove from event list
195
+ BlockingInput .pop (self , - 1 )
188
196
189
- def mouse_event_stop ( self , event ):
197
+ def mouse_event_stop (self , event ):
190
198
"""
191
199
Will be called for any event involving button 2.
192
200
Button 2 ends blocking input.
193
201
"""
194
202
195
203
# Remove last event just for cleanliness
196
- BlockingInput .pop (self ,- 1 )
204
+ BlockingInput .pop (self , - 1 )
197
205
198
206
# This will exit even if not in infinite mode. This is
199
207
# consistent with MATLAB and sometimes quite useful, but will
200
208
# require the user to test how many points were actually
201
209
# returned before using data.
202
210
self .fig .canvas .stop_event_loop ()
203
211
204
- def mouse_event_pop ( self , event ):
212
+ def mouse_event_pop (self , event ):
205
213
"""
206
214
Will be called for any event involving button 3.
207
215
Button 3 removes the last click.
208
216
"""
209
217
# Remove this last event
210
- BlockingInput .pop (self ,- 1 )
218
+ BlockingInput .pop (self , - 1 )
211
219
212
220
# Now remove any existing clicks if possible
213
- if len (self .events )> 0 :
214
- self .pop (event ,- 1 )
221
+ if len (self .events ) > 0 :
222
+ self .pop (event , - 1 )
215
223
216
- def add_click (self ,event ):
224
+ def add_click (self , event ):
217
225
"""
218
226
This add the coordinates of an event to the list of clicks
219
227
"""
220
- self .clicks .append ((event .xdata ,event .ydata ))
228
+ self .clicks .append ((event .xdata , event .ydata ))
221
229
222
230
verbose .report ("input %i: %f,%f" %
223
- (len (self .clicks ),event .xdata , event .ydata ))
231
+ (len (self .clicks ), event .xdata , event .ydata ))
224
232
225
233
# If desired plot up click
226
234
if self .show_clicks :
@@ -230,9 +238,7 @@ def add_click(self,event):
230
238
self .marks .append (line )
231
239
self .fig .canvas .draw ()
232
240
233
-
234
-
235
- def pop_click (self ,event ,index = - 1 ):
241
+ def pop_click (self , event , index = - 1 ):
236
242
"""
237
243
This removes a click from the list of clicks. Defaults to
238
244
removing the last click.
@@ -249,17 +255,16 @@ def pop_click(self,event,index=-1):
249
255
# for the keyboard backspace event on windows XP wxAgg.
250
256
# maybe event.inaxes here is a COPY of the actual axes?
251
257
252
-
253
- def pop (self ,event ,index = - 1 ):
258
+ def pop (self , event , index = - 1 ):
254
259
"""
255
260
This removes a click and the associated event from the object.
256
261
Defaults to removing the last click, but any index can be
257
262
supplied.
258
263
"""
259
- self .pop_click (event ,index )
260
- BlockingInput .pop (self ,index )
264
+ self .pop_click (event , index )
265
+ BlockingInput .pop (self , index )
261
266
262
- def cleanup (self ,event = None ):
267
+ def cleanup (self , event = None ):
263
268
# clean the figure
264
269
if self .show_clicks :
265
270
@@ -278,28 +283,29 @@ def __call__(self, n=1, timeout=30, show_clicks=True):
278
283
clicks.
279
284
"""
280
285
self .show_clicks = show_clicks
281
- self .clicks = []
282
- self .marks = []
283
- BlockingInput .__call__ (self ,n = n ,timeout = timeout )
286
+ self .clicks = []
287
+ self .marks = []
288
+ BlockingInput .__call__ (self , n = n , timeout = timeout )
284
289
285
290
return self .clicks
286
291
287
- class BlockingContourLabeler ( BlockingMouseInput ):
292
+
293
+ class BlockingContourLabeler (BlockingMouseInput ):
288
294
"""
289
295
Class that creates a callable object that uses mouse clicks or key
290
296
clicks on a figure window to place contour labels.
291
297
"""
292
- def __init__ (self ,cs ):
298
+ def __init__ (self , cs ):
293
299
self .cs = cs
294
- BlockingMouseInput .__init__ (self , fig = cs .ax .figure )
300
+ BlockingMouseInput .__init__ (self , fig = cs .ax .figure )
295
301
296
302
def add_click (self , event ):
297
303
self .button1 (event )
298
304
299
305
def pop_click (self , event , index = - 1 ):
300
306
self .button3 (event )
301
307
302
- def button1 (self ,event ):
308
+ def button1 (self , event ):
303
309
"""
304
310
This will be called if an event involving a button other than
305
311
2 or 3 occcurs. This will add a label to a contour.
@@ -311,10 +317,10 @@ def button1(self,event):
311
317
inline_spacing = self .inline_spacing ,
312
318
transform = False )
313
319
self .fig .canvas .draw ()
314
- else : # Remove event if not valid
320
+ else : # Remove event if not valid
315
321
BlockingInput .pop (self )
316
322
317
- def button3 (self ,event ):
323
+ def button3 (self , event ):
318
324
"""
319
325
This will be called if button 3 is clicked. This will remove
320
326
a label if not in inline mode. Unfortunately, if one is doing
@@ -329,26 +335,28 @@ def button3(self,event):
329
335
self .cs .pop_label ()
330
336
self .cs .ax .figure .canvas .draw ()
331
337
332
- def __call__ (self ,inline ,inline_spacing = 5 ,n = - 1 ,timeout = - 1 ):
333
- self .inline = inline
334
- self .inline_spacing = inline_spacing
338
+ def __call__ (self , inline , inline_spacing = 5 , n = - 1 , timeout = - 1 ):
339
+ self .inline = inline
340
+ self .inline_spacing = inline_spacing
335
341
336
- BlockingMouseInput .__call__ (self ,n = n ,timeout = timeout ,
342
+ BlockingMouseInput .__call__ (self , n = n , timeout = timeout ,
337
343
show_clicks = False )
338
344
345
+
339
346
class BlockingKeyMouseInput (BlockingInput ):
340
347
"""
341
348
Class that creates a callable object to retrieve a single mouse or
342
349
keyboard click
343
350
"""
344
351
def __init__ (self , fig ):
345
- BlockingInput .__init__ (self , fig = fig , eventslist = ('button_press_event' ,'key_press_event' ) )
352
+ BlockingInput .__init__ (self , fig = fig , eventslist = (
353
+ 'button_press_event' , 'key_press_event' ))
346
354
347
355
def post_event (self ):
348
356
"""
349
357
Determines if it is a key event
350
358
"""
351
- assert len (self .events )> 0 , "No events yet"
359
+ assert len (self .events ) > 0 , "No events yet"
352
360
353
361
self .keyormouse = self .events [- 1 ].name == 'key_press_event'
354
362
@@ -358,7 +366,6 @@ def __call__(self, timeout=30):
358
366
Returns True if key click, False if mouse, or None if timeout
359
367
"""
360
368
self .keyormouse = None
361
- BlockingInput .__call__ (self ,n = 1 ,timeout = timeout )
369
+ BlockingInput .__call__ (self , n = 1 , timeout = timeout )
362
370
363
371
return self .keyormouse
364
-
0 commit comments