@@ -141,37 +141,13 @@ def unregister(self, *args):
141
141
class ToolToggleBase (ToolPersistentBase ):
142
142
"""Toggleable tool
143
143
144
- This tool is a Persistent Tool, that has the ability to capture
145
- the keypress, press and release events, preventing other tools
146
- to use the same events at the same time
144
+ This tool is a Persistent Tool that has a toggled state.
145
+ Every time it is triggered, it switches between enable and disable
146
+
147
147
"""
148
148
toggle = True
149
149
_toggled = False
150
150
151
- def mouse_move (self , event ):
152
- """Mouse move event
153
-
154
- Called when a motion_notify_event is emited by the `FigureCanvas` if
155
- `navigation.movelock(self)` was setted
156
- """
157
- pass
158
-
159
- def press (self , event ):
160
- """Mouse press event
161
-
162
- Called when a button_press_event is emited by the `FigureCanvas` if
163
- `navigation.presslock(self)` was setted
164
- """
165
- pass
166
-
167
- def release (self , event ):
168
- """Mouse release event
169
-
170
- Called when a button_release_event is emited by the `FigureCanvas` if
171
- `navigation.releaselock(self)` was setted
172
- """
173
- pass
174
-
175
151
def trigger (self , event ):
176
152
if self ._toggled :
177
153
self .disable (event )
@@ -182,26 +158,23 @@ def trigger(self, event):
182
158
def enable (self , event = None ):
183
159
"""Enable the toggle tool
184
160
185
- This method is called when the tool is triggered and not active
161
+ This method is called when the tool is triggered and not toggled
186
162
"""
187
163
pass
188
164
189
165
def disable (self , event = None ):
190
166
"""Disable the toggle tool
191
167
192
- This method is called when the tool is triggered and active .
193
- * Second click on the toolbar button
168
+ This method is called when the tool is triggered and toggled .
169
+ * Second click on the toolbar tool button
194
170
* Another toogle tool is triggered (from the same `navigation`)
195
171
"""
196
172
pass
197
173
198
- def key_press (self , event ):
199
- """Key press event
200
-
201
- Called when a key_press_event is emited by the `FigureCanvas` if
202
- `navigation.keypresslock(self)` was setted
203
- """
204
- pass
174
+ @property
175
+ def toggled (self ):
176
+ """State of the toggled tool"""
177
+ return self ._toggled
205
178
206
179
207
180
class ToolQuit (ToolBase ):
@@ -391,26 +364,29 @@ def __init__(self, *args):
391
364
self ._ids_zoom = []
392
365
self ._button_pressed = None
393
366
self ._xypress = None
367
+ self ._idPress = None
368
+ self ._idRelease = None
394
369
395
370
def enable (self , event ):
396
- self .navigation .canvaslock (self )
397
- self .navigation .presslock (self )
398
- self .navigation .releaselock (self )
371
+ self .figure .canvas .widgetlock (self )
372
+ self ._idPress = self .figure .canvas .mpl_connect (
373
+ 'button_press_event' , self ._press )
374
+ self ._idRelease = self .figure .canvas .mpl_connect (
375
+ 'button_release_event' , self ._release )
399
376
400
377
def disable (self , event ):
401
- self .navigation . canvaslock .release (self )
402
- self .navigation . presslock . release (self )
403
- self .navigation . releaselock . release (self )
378
+ self .figure . canvas . widgetlock .release (self )
379
+ self .figure . canvas . mpl_disconnect (self . _idPress )
380
+ self .figure . canvas . mpl_disconnect (self . _idRelease )
404
381
405
- def press (self , event ):
406
- """the press mouse button in zoom to rect mode callback"""
382
+ def _press (self , event ):
383
+ """the _press mouse button in zoom to rect mode callback"""
407
384
# If we're already in the middle of a zoom, pressing another
408
385
# button works to "cancel"
409
386
if self ._ids_zoom != []:
410
- self .navigation .movelock .release (self )
411
387
for zoom_id in self ._ids_zoom :
412
388
self .figure .canvas .mpl_disconnect (zoom_id )
413
- self .navigation .release (event )
389
+ self .navigation .remove_rubberband (event , self )
414
390
self .navigation .draw ()
415
391
self ._xypress = None
416
392
self ._button_pressed = None
@@ -439,26 +415,25 @@ def press(self, event):
439
415
self ._xypress .append ((x , y , a , i , a .viewLim .frozen (),
440
416
a .transData .frozen ()))
441
417
442
- self .navigation .movelock (self )
418
+ id1 = self .figure .canvas .mpl_connect (
419
+ 'motion_notify_event' , self ._mouse_move )
443
420
id2 = self .figure .canvas .mpl_connect ('key_press_event' ,
444
421
self ._switch_on_zoom_mode )
445
422
id3 = self .figure .canvas .mpl_connect ('key_release_event' ,
446
423
self ._switch_off_zoom_mode )
447
424
448
- self ._ids_zoom = id2 , id3
425
+ self ._ids_zoom = id1 , id2 , id3
449
426
self ._zoom_mode = event .key
450
427
451
- self .navigation .press (event )
452
-
453
428
def _switch_on_zoom_mode (self , event ):
454
429
self ._zoom_mode = event .key
455
- self .mouse_move (event )
430
+ self ._mouse_move (event )
456
431
457
432
def _switch_off_zoom_mode (self , event ):
458
433
self ._zoom_mode = None
459
- self .mouse_move (event )
434
+ self ._mouse_move (event )
460
435
461
- def mouse_move (self , event ):
436
+ def _mouse_move (self , event ):
462
437
"""the drag callback in zoom mode"""
463
438
if self ._xypress :
464
439
x , y = event .x , event .y
@@ -476,11 +451,10 @@ def mouse_move(self, event):
476
451
x1 , y1 , x2 , y2 = a .bbox .extents
477
452
x , lastx = x1 , x2
478
453
479
- self .navigation .draw_rubberband (event , x , y , lastx , lasty )
454
+ self .navigation .draw_rubberband (event , self , x , y , lastx , lasty )
480
455
481
- def release (self , event ):
456
+ def _release (self , event ):
482
457
"""the release mouse button callback in zoom to rect mode"""
483
- self .navigation .movelock .release (self )
484
458
for zoom_id in self ._ids_zoom :
485
459
self .figure .canvas .mpl_disconnect (zoom_id )
486
460
self ._ids_zoom = []
@@ -496,7 +470,7 @@ def release(self, event):
496
470
# ignore singular clicks - 5 pixels is a threshold
497
471
if abs (x - lastx ) < 5 or abs (y - lasty ) < 5 :
498
472
self ._xypress = None
499
- self .navigation .release (event )
473
+ self .navigation .remove_rubberband (event , self )
500
474
self .navigation .draw ()
501
475
return
502
476
@@ -604,7 +578,7 @@ def release(self, event):
604
578
self ._zoom_mode = None
605
579
606
580
self .navigation .push_current ()
607
- self .navigation .release (event )
581
+ self .navigation .remove_rubberband (event , self )
608
582
609
583
610
584
class ToolPan (ToolToggleBase ):
@@ -620,18 +594,23 @@ def __init__(self, *args):
620
594
ToolToggleBase .__init__ (self , * args )
621
595
self ._button_pressed = None
622
596
self ._xypress = None
597
+ self ._idPress = None
598
+ self ._idRelease = None
599
+ self ._idDrag = None
623
600
624
601
def enable (self , event ):
625
- self .navigation .canvaslock (self )
626
- self .navigation .presslock (self )
627
- self .navigation .releaselock (self )
602
+ self .figure .canvas .widgetlock (self )
603
+ self ._idPress = self .figure .canvas .mpl_connect (
604
+ 'button_press_event' , self ._press )
605
+ self ._idRelease = self .figure .canvas .mpl_connect (
606
+ 'button_release_event' , self ._release )
628
607
629
608
def disable (self , event ):
630
- self .navigation . canvaslock .release (self )
631
- self .navigation . presslock . release (self )
632
- self .navigation . releaselock . release (self )
609
+ self .figure . canvas . widgetlock .release (self )
610
+ self .figure . canvas . mpl_disconnect (self . _idPress )
611
+ self .figure . canvas . mpl_disconnect (self . _idRelease )
633
612
634
- def press (self , event ):
613
+ def _press (self , event ):
635
614
if event .button == 1 :
636
615
self ._button_pressed = 1
637
616
elif event .button == 3 :
@@ -653,14 +632,16 @@ def press(self, event):
653
632
a .get_navigate () and a .can_pan ()):
654
633
a .start_pan (x , y , event .button )
655
634
self ._xypress .append ((a , i ))
656
- self .navigation .movelock (self )
657
- self .navigation .press (event )
635
+ self .navigation .messagelock (self )
636
+ self ._idDrag = self .figure .canvas .mpl_connect (
637
+ 'motion_notify_event' , self ._mouse_move )
658
638
659
- def release (self , event ):
639
+ def _release (self , event ):
660
640
if self ._button_pressed is None :
661
641
return
662
642
663
- self .navigation .movelock .release (self )
643
+ self .figure .canvas .mpl_disconnect (self ._idDrag )
644
+ self .navigation .messagelock .release (self )
664
645
665
646
for a , _ind in self ._xypress :
666
647
a .end_pan ()
@@ -669,12 +650,11 @@ def release(self, event):
669
650
self ._xypress = []
670
651
self ._button_pressed = None
671
652
self .navigation .push_current ()
672
- self .navigation .release (event )
673
653
self .navigation .draw ()
674
654
675
- def mouse_move (self , event ):
655
+ def _mouse_move (self , event ):
676
656
for a , _ind in self ._xypress :
677
- #safer to use the recorded button at the press than current button:
678
- #multiple button can get pressed during motion...
657
+ #safer to use the recorded button at the _press than current
658
+ #button: # multiple button can get pressed during motion...
679
659
a .drag_pan (self ._button_pressed , event .key , event .x , event .y )
680
660
self .navigation .dynamic_update ()
0 commit comments