Skip to content

Commit 5eae7bf

Browse files
committed
Name as tool attribute.
1 parent 969f311 commit 5eae7bf

File tree

5 files changed

+83
-57
lines changed

5 files changed

+83
-57
lines changed

examples/user_interfaces/toolmanager_sgskip.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
from matplotlib.backend_tools import ToolBase, ToolToggleBase, register_tool
2323

2424

25-
@register_tool("List")
26-
class ListTools(ToolBase):
25+
class ListTool(ToolBase):
2726
'''List all the tools controlled by the `ToolManager`'''
2827
# keyboard shortcut
28+
name = "List"
2929
default_keymap = 'm'
3030
description = 'List Tools'
3131

@@ -49,9 +49,9 @@ def trigger(self, event):
4949
print("{0:12} {1:45}".format(str(group), str(active)))
5050

5151

52-
@register_tool("Show")
5352
class GroupHideTool(ToolToggleBase):
5453
'''Show lines with a given gid'''
54+
name = "Show"
5555
default_keymap = 'G'
5656
description = 'Show by gid'
5757
default_toggled = True
@@ -81,8 +81,8 @@ def set_lines_visibility(self, state):
8181
plt.plot([3, 2, 1], gid='mygroup')
8282

8383
# Add the custom tools that we created
84-
fig.canvas.manager.toolmanager.add_tool('List')
85-
fig.canvas.manager.toolmanager.add_tool('Show', gid='mygroup')
84+
fig.canvas.manager.toolmanager.add_tool(ListTool)
85+
fig.canvas.manager.toolmanager.add_tool(GroupHideTool, gid='mygroup')
8686

8787
# Add an existing tool to new group `foo`.
8888
# It can be added as many times as we want

lib/matplotlib/backend_managers.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def remove_tool(self, name):
228228

229229
del self._tools[name]
230230

231-
def add_tool(self, tool_name, *args, **kwargs):
231+
def add_tool(self, tool_cls_or_name, *args, **kwargs):
232232
"""
233233
Add *tool* to `ToolManager`
234234
@@ -238,8 +238,8 @@ def add_tool(self, tool_name, *args, **kwargs):
238238
239239
Parameters
240240
----------
241-
name : str
242-
Name of the tool, treated as the ID, has to be unique
241+
tool_cls_or_name : callable or name
242+
Class or name of the tool, treated as the ID, has to be unique
243243
tool : class_like, i.e. str or type
244244
Reference to find the class of the Tool to added.
245245
@@ -252,18 +252,25 @@ def add_tool(self, tool_name, *args, **kwargs):
252252
matplotlib.backend_tools.ToolBase : The base class for tools.
253253
"""
254254

255-
tool_cls = tools.get_tool(tool_name, type(self.canvas))
255+
if callable(tool_cls_or_name):
256+
tool_cls = tool_cls_or_name
257+
elif isinstance(tool_cls_or_name, six.string_types):
258+
tool_cls = tools.get_tool(tool_cls_or_name, type(self.canvas))
259+
else:
260+
raise TypeError(
261+
"'tool_cls_or_name' must be a callable or a tool name")
262+
name = tool_cls.name
256263

257-
if tool_name in self._tools:
264+
if name in self._tools:
258265
warnings.warn('A "Tool class" with the same name already exists, '
259266
'not added')
260-
return self._tools[tool_name]
267+
return self._tools[name]
261268

262-
tool_obj = tool_cls(self, tool_name, *args, **kwargs)
263-
self._tools[tool_name] = tool_obj
269+
tool_obj = tool_cls(self, *args, **kwargs)
270+
self._tools[name] = tool_obj
264271

265272
if tool_cls.default_keymap is not None:
266-
self.update_keymap(tool_name, tool_cls.default_keymap)
273+
self.update_keymap(name, tool_cls.default_keymap)
267274

268275
# For toggle tools init the radio_group in self._toggled
269276
if isinstance(tool_obj, tools.ToolToggleBase):

0 commit comments

Comments
 (0)