Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba61dec

Browse files
OceanWolffariza
authored andcommitted
Some short cuts for adding tools
1 parent 1213086 commit ba61dec

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,13 @@ def add_tool(self, name, tool):
34403440
'not added')
34413441
return
34423442

3443-
self._tools[name] = tool_cls(self, name)
3443+
if isinstance(tool_cls, type):
3444+
self._tools[name] = tool_cls(self, name)
3445+
else:
3446+
tool_cls.set_navigation(self)
3447+
tool.name = name
3448+
self._tools[name] = tool_cls
3449+
34443450
if tool_cls.keymap is not None:
34453451
self.set_tool_keymap(name, tool_cls.keymap)
34463452

@@ -3455,6 +3461,8 @@ def add_tool(self, name, tool):
34553461

34563462
self._tool_added_event(self._tools[name])
34573463

3464+
return self._tools[name]
3465+
34583466
def _tool_added_event(self, tool):
34593467
s = 'tool_added_event'
34603468
event = ToolEvent(s, self, tool)
@@ -3629,10 +3637,20 @@ def add_tools(self, tools):
36293637

36303638
for group, grouptools in tools:
36313639
for position, tool in enumerate(grouptools):
3632-
self.add_tool(self.navigation.get_tool(tool), group, position)
3640+
self.add_tool(tool, group, position)
36333641

36343642
def add_tool(self, tool, group, position):
36353643
"""Adds a tool to the toolbar"""
3644+
t = self.navigation.get_tool(tool)
3645+
if t is None:
3646+
if isinstance(tool, (list, tuple)):
3647+
t = self.navigation.add_tool(tool[0], tool[1])
3648+
elif isinstance(tool, ToolBase):
3649+
t = self.navigation.add_tool(tool.name, tool)
3650+
else:
3651+
warning.warn('Cannot add tool %s'%tool)
3652+
return
3653+
tool = t
36363654
image = self._get_image_filename(tool.image)
36373655
toggle = getattr(tool, 'toggled', None) is not None
36383656
self.add_toolitem(tool.name, group, position, image,

lib/matplotlib/backend_tools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class ToolBase(object):
6565
def __init__(self, navigation, name):
6666
self._name = name
6767
self.figure = None
68+
self.set_navigation(navigation)
69+
70+
def set_navigation(self, navigation):
6871
self.navigation = navigation
6972
self.set_figure(navigation.canvas.figure)
7073

0 commit comments

Comments
 (0)