Skip to content

Commit d49c431

Browse files
committed
changing toggle and persistent attributes for issubclass
1 parent fb46fc1 commit d49c431

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

lib/matplotlib/backend_bases.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -3231,8 +3231,9 @@ def __init__(self, canvas, toolbar=None):
32313231
self._instances = {}
32323232
self._toggled = None
32333233

3234-
#to communicate with tools and redirect events
3234+
#to process keypress event
32353235
self.keypresslock = widgets.LockDraw()
3236+
#to write into toolbar message
32363237
self.messagelock = widgets.LockDraw()
32373238

32383239
for tool in self._default_tools:
@@ -3359,6 +3360,7 @@ def add_tool(self, tool):
33593360
"""
33603361
tool_cls = self._get_cls_to_instantiate(tool)
33613362
name = tool_cls.name
3363+
33623364
if name is None:
33633365
warnings.warn('tool_clss need a name to be added, it is used '
33643366
'as ID')
@@ -3383,10 +3385,11 @@ def add_tool(self, tool):
33833385
fname = os.path.join(basedir, tool_cls.image + '.png')
33843386
else:
33853387
fname = None
3388+
toggle = issubclass(tool_cls, tools.ToolToggleBase)
33863389
self.toolbar._add_toolitem(name, tool_cls.description,
33873390
fname,
33883391
tool_cls.position,
3389-
tool_cls.toggle)
3392+
toggle)
33903393

33913394
def _get_cls_to_instantiate(self, callback_class):
33923395
if isinstance(callback_class, basestring):
@@ -3415,9 +3418,9 @@ def _trigger_tool(self, name, event, from_toolbar):
34153418
raise AttributeError('%s not in Tools' % name)
34163419

34173420
tool = self._tools[name]
3418-
if tool.toggle:
3421+
if issubclass(tool, tools.ToolToggleBase):
34193422
self._handle_toggle(name, event=event, from_toolbar=from_toolbar)
3420-
elif tool.persistent:
3423+
elif issubclass(tool, tools.ToolPersistentBase):
34213424
instance = self._get_instance(name)
34223425
instance.trigger(event)
34233426
else:

lib/matplotlib/backend_tools.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,14 @@ class ToolBase(object):
7272
`name` is used as label in the toolbar button
7373
"""
7474

75-
toggle = False # Change the status (take control of the events)
76-
"""Is toggleable tool
77-
78-
**bool**:
79-
80-
* **True**: The tool is a toogleable tool
81-
* **False**: The tool is not toggleable
82-
83-
"""
84-
85-
persistent = False
86-
"""Is persistent tool
87-
88-
**bool**:
89-
* `True`: The tool is persistent
90-
* `False`: The tool is not persistent
91-
"""
92-
9375
cursor = None
9476
"""Cursor to use when the tool is active
9577
"""
9678

9779
def __init__(self, figure, event=None):
98-
self.figure = figure
99-
self.navigation = figure.canvas.manager.navigation
80+
self.figure = None
81+
self.navigation = None
82+
self.set_figure(figure)
10083
self.trigger(event)
10184

10285
def trigger(self, event):
@@ -109,6 +92,18 @@ def trigger(self, event):
10992
"""
11093
pass
11194

95+
def set_figure(self, figure):
96+
"""Set the figure and navigation
97+
98+
Set the figure to be affected by this tool
99+
100+
Parameters
101+
----------
102+
figure : `Figure`
103+
"""
104+
self.figure = figure
105+
self.navigation = figure.canvas.manager.navigation
106+
112107

113108
class ToolPersistentBase(ToolBase):
114109
"""Persisten tool
@@ -121,12 +116,13 @@ class ToolPersistentBase(ToolBase):
121116
The difference with `ToolBase` is that `trigger` method
122117
is not called automatically at initialization
123118
"""
124-
persistent = True
125119

126120
def __init__(self, figure, event=None):
127-
self.figure = figure
128-
self.navigation = figure.canvas.manager.navigation
121+
self.figure = None
122+
self.navigation = None
123+
self.set_figure(figure)
129124
#persistent tools don't call trigger a at instantiation
125+
#it will be called by Navigation
130126

131127
def unregister(self, *args):
132128
"""Unregister the tool from the instances of Navigation
@@ -145,7 +141,6 @@ class ToolToggleBase(ToolPersistentBase):
145141
Every time it is triggered, it switches between enable and disable
146142
147143
"""
148-
toggle = True
149144
_toggled = False
150145

151146
def trigger(self, event):

0 commit comments

Comments
 (0)