Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Name as tool attribute.
  • Loading branch information
anntzer committed Jan 4, 2018
commit 5eae7bf9aebdae01b0861ac218fa2273e1f0cc1f
10 changes: 5 additions & 5 deletions examples/user_interfaces/toolmanager_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
from matplotlib.backend_tools import ToolBase, ToolToggleBase, register_tool


@register_tool("List")
class ListTools(ToolBase):
class ListTool(ToolBase):
'''List all the tools controlled by the `ToolManager`'''
# keyboard shortcut
name = "List"
default_keymap = 'm'
description = 'List Tools'

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


@register_tool("Show")
class GroupHideTool(ToolToggleBase):
'''Show lines with a given gid'''
name = "Show"
default_keymap = 'G'
description = 'Show by gid'
default_toggled = True
Expand Down Expand Up @@ -81,8 +81,8 @@ def set_lines_visibility(self, state):
plt.plot([3, 2, 1], gid='mygroup')

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

# Add an existing tool to new group `foo`.
# It can be added as many times as we want
Expand Down
25 changes: 16 additions & 9 deletions lib/matplotlib/backend_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def remove_tool(self, name):

del self._tools[name]

def add_tool(self, tool_name, *args, **kwargs):
def add_tool(self, tool_cls_or_name, *args, **kwargs):
"""
Add *tool* to `ToolManager`

Expand All @@ -238,8 +238,8 @@ def add_tool(self, tool_name, *args, **kwargs):

Parameters
----------
name : str
Name of the tool, treated as the ID, has to be unique
tool_cls_or_name : callable or name
Class or name of the tool, treated as the ID, has to be unique
tool : class_like, i.e. str or type
Reference to find the class of the Tool to added.

Expand All @@ -252,18 +252,25 @@ def add_tool(self, tool_name, *args, **kwargs):
matplotlib.backend_tools.ToolBase : The base class for tools.
"""

tool_cls = tools.get_tool(tool_name, type(self.canvas))
if callable(tool_cls_or_name):
tool_cls = tool_cls_or_name
elif isinstance(tool_cls_or_name, six.string_types):
tool_cls = tools.get_tool(tool_cls_or_name, type(self.canvas))
else:
raise TypeError(
"'tool_cls_or_name' must be a callable or a tool name")
name = tool_cls.name

if tool_name in self._tools:
if name in self._tools:
warnings.warn('A "Tool class" with the same name already exists, '
'not added')
return self._tools[tool_name]
return self._tools[name]

tool_obj = tool_cls(self, tool_name, *args, **kwargs)
self._tools[tool_name] = tool_obj
tool_obj = tool_cls(self, *args, **kwargs)
self._tools[name] = tool_obj

if tool_cls.default_keymap is not None:
self.update_keymap(tool_name, tool_cls.default_keymap)
self.update_keymap(name, tool_cls.default_keymap)

# For toggle tools init the radio_group in self._toggled
if isinstance(tool_obj, tools.ToolToggleBase):
Expand Down
Loading