10000 adding locks for events, instead of modifying attributes · OceanWolf/matplotlib@aae701b · GitHub
[go: up one dir, main page]

Skip to content

Commit aae701b

Browse files
committed
adding locks for events, instead of modifying attributes
1 parent 72ce737 commit aae701b

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,11 +2609,6 @@ class SaveFigureBase(ToolBase):
26092609
class ToolToggleBase(ToolPersistentBase):
26102610
toggle = True
26112611
cursor = None
2612-
capture_keypress = False
2613-
capture_move = False
2614-
capture_press = False
2615-
capture_release = False
2616-
lock_drawing = False
26172612

26182613
def mouse_move(self, event):
26192614
pass
@@ -2637,23 +2632,30 @@ class ToolZoom(ToolToggleBase):
26372632
image = 'zoom_to_rect'
26382633
position = -1
26392634
keymap = rcParams['keymap.zoom']
2640-
26412635
cursor = cursors.SELECT_REGION
2642-
capture_press = True
2643-
capture_release = True
26442636

26452637
def __init__(self, *args):
26462638
ToolToggleBase.__init__(self, *args)
26472639
self._ids_zoom = []
26482640
self._button_pressed = None
26492641
self._xypress = None
26502642

2643+
def activate(self, event):
2644+
self.navigation.canvaslock(self)
2645+
self.navigation.presslock(self)
2646+
self.navigation.releaselock(self)
2647+
2648+
def deactivate(self, event):
2649+
self.navigation.canvaslock.release(self)
2650+
self.navigation.presslock.release(self)
2651+
self.navigation.releaselock.release(self)
2652+
26512653
def press(self, event):
26522654
"""the press mouse button in zoom to rect mode callback"""
26532655
# If we're already in the middle of a zoom, pressing another
26542656
# button works to "cancel"
26552657
if self._ids_zoom != []:
2656-
self.capture_move = False
2658+
self.navigation.movelock.release(self)
26572659
for zoom_id in self._ids_zoom:
26582660
self.figure.canvas.mpl_disconnect(zoom_id)
26592661
self.navigation.release(event)
@@ -2685,7 +2687,7 @@ def press(self, event):
26852687
self._xypress.append((x, y, a, i, a.viewLim.frozen(),
26862688
a.transData.frozen()))
26872689

2688-
self.capture_move = True
2690+
self.navigation.movelock(self)
26892691
id2 = self.figure.canvas.mpl_connect('key_press_event',
26902692
self._switch_on_zoom_mode)
26912693
id3 = self.figure.canvas.mpl_connect('key_release_event',
@@ -2726,7 +2728,7 @@ def mouse_move(self, event):
27262728

27272729
def release(self, event):
27282730
"""the release mouse button callback in zoom to rect mode"""
2729-
self.capture_move = False
2731+
self.navigation.movelock.release(self)
27302732
for zoom_id in self._ids_zoom:
27312733
self.figure.canvas.mpl_disconnect(zoom_id)
27322734
self._ids_zoom = []
@@ -2859,17 +2861,23 @@ class ToolPan(ToolToggleBase):
28592861
description = 'Pan axes with left mouse, zoom with right'
28602862
image = 'move'
28612863
position = -1
2862-
28632864
cursor = cursors.MOVE
2864-
capture_press = True
2865-
capture_release = True
2866-
lock_drawing = True
28672865

28682866
def __init__(self, *args):
28692867
ToolToggleBase.__init__(self, *args)
28702868
self._button_pressed = None
28712869
self._xypress = None
28722870

2871+
def activate(self, event):
2872+
self.navigation.canvaslock(self)
2873+
self.navigation.presslock(self)
2874+
self.navigation.releaselock(self)
2875+
2876+
def deactivate(self, event):
2877+
self.navigation.canvaslock.release(self)
2878+
self.navigation.presslock.release(self)
2879+
self.navigation.releaselock.release(self)
2880+
28732881
def press(self, event):
28742882
"""the press mouse button in pan/zoom mode callback"""
28752883

@@ -2894,14 +2902,14 @@ def press(self, event):
28942902
a.get_navigate() and a.can_pan()):
28952903
a.start_pan(x, y, event.button)
28962904
self._xypress.append((a, i))
2897-
self.capture_move = True
2905+
self.navigation.movelock(self)
28982906
self.navigation.press(event)
28992907

29002908
def release(self, event):
29012909
if self._button_pressed is None:
29022910
return
29032911

2904-
self.capture_move = False
2912+
self.navigation.movelock.release(self)
29052913

29062914
for a, _ind in self._xypress:
29072915
a.end_pan()
@@ -2957,6 +2965,14 @@ def __init__(self, canvas, toolbar=None):
29572965
self._instances = {}
29582966
self._toggled = None
29592967

2968+
#to communicate with tools and redirect events
2969+
self.keypresslock = widgets.LockDraw()
2970+
self.movelock = widgets.LockDraw()
2971+
self.presslock = widgets.LockDraw()
2972+
self.releaselock = widgets.LockDraw()
2973+
#just to group all the locks in one place
2974+
self.canvaslock = self.canvas.widgetlock
2975+
29602976
for tool in self.tools:
29612977
self.add_tool(tool)
29622978

@@ -2991,13 +3007,12 @@ def remove_tool(self, name):
29913007
def add_tool(self, callback_class):
29923008
tool = self._get_cls_to_instantiate(callback_class)
29933009
name = tool.name
3010+
29943011
if name is None:
2995-
#TODO: add a warning
2996-
print ('impossible to add without name')
3012+
warnings.warn('Tools need a name to be added, it is used as ID')
29973013
return
29983014
if name in self._tools:
2999-
#TODO: add a warning
3000-
print ('impossible to add two times with the same name')
3015+
warnings.warn('A tool with the same name already exist, not added')
30013016
return
30023017

30033018
self._tools[name] = tool
@@ -3040,8 +3055,10 @@ def key_press(self, event):
30403055
return
30413056

30423057
#some tools may need to capture keypress, but they need to be toggle
3043-
if self._toggled and self._tools[self._toggled].capture_keypress:
3044-
self._instances[self._toggled].key_press(event)
3058+
if self._toggled:
3059+
instance = self._get_instance(self._toggled)
3060+
if self.keypresslock.isowner(instance):
3061+
instance.key_press(event)
30453062
return
30463063

30473064
name = self._keys.get(event.key, None)
@@ -3062,6 +3079,7 @@ def key_press(self, event):
30623079
def _get_instance(self, name):
30633080
if name not in self._instances:
30643081
instance = self._tools[name](self.canvas.figure)
3082+
#register instance
30653083
self._instances[name] = instance
30663084

30673085
return self._instances[name]
@@ -3077,20 +3095,18 @@ def toolbar_callback(self, name):
30773095
tool(self.canvas.figure, None)
30783096

30793097
def _handle_toggle(self, name, event=None, from_toolbar=False):
3080-
#when from keypress toggle toolbar without callback
3098+
#toggle toolbar without callback
30813099
if not from_toolbar and self.toolbar:
30823100
self.toolbar.toggle(name, False)
30833101

30843102
instance = self._get_instance(name)
30853103
if self._toggled is None:
30863104
instance.activate(None)
30873105
self._toggled = name
3088-
self.canvas.widgetlock(self)
30893106

30903107
elif self._toggled == name:
30913108
instance.deactivate(None)
30923109
self._toggled = None
3093-
self.canvas.widgetlock.release(self)
30943110

30953111
else:
30963112
if self.toolbar:
@@ -3124,7 +3140,7 @@ def update(self):
31243140
def mouse_move(self, event):
31253141
if self._toggled:
31263142
instance = self._instances[self._toggled]
3127-
if instance.capture_move:
3143+
if self.movelock.isowner(instance):
31283144
instance.mouse_move(event)
31293145
return
31303146

@@ -3159,7 +3175,7 @@ def mouse_move(self, event):
31593175
def _release(self, event):
31603176
if self._toggled:
31613177
instance = self._instances[self._toggled]
3162-
if instance.capture_release:
3178+
if self.releaselock.isowner(instance):
31633179
instance.release(event)
31643180
return
31653181
self.release(event)
@@ -3171,7 +3187,7 @@ def _press(self, event):
31713187
"""Called whenver a mouse button is pressed."""
31723188
if self._toggled:
31733189
instance = self._instances[self._toggled]
3174-
if instance.capture_press:
3190+
if self.presslock.isowner(instance):
31753191
instance.press(event)
31763192
return
31773193
self.press(event)
@@ -3333,7 +3349,7 @@ def set_message(self, s):
33333349
pass
33343350

33353351
def toggle(self, name, callback=False):
3336-
#carefull, callback means to perform or not the callback while toggling
3352+
#callback = perform or not the callback while toggling
33373353
raise NotImplementedError
33383354

33393355
def remove_toolitem(self, name):

0 commit comments

Comments
 (0)
0