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

Skip to content

Commit 9f2ee2b

Browse files
OceanWolffariza
authored andcommitted
Lots of fixes
1 parent ba61dec commit 9f2ee2b

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

examples/user_interfaces/navigation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def trigger(self, *args, **kwargs):
5656
fig.canvas.manager.navigation.add_tool('List', ListTools)
5757
if matplotlib.rcParams['backend'] == 'GTK3Cairo':
5858
fig.canvas.manager.navigation.add_tool('copy', CopyToolGTK3)
59-
59+
fig.canvas.manager.toolbar.add_tool('zoom', 'foo')
6060
# Uncomment to remove the forward button
6161
# fig.canvas.manager.navigation.remove_tool('forward')
6262

lib/matplotlib/backend_bases.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,7 +3413,7 @@ def add_tools(self, tools):
34133413
for tool, name in tools:
34143414
self.add_tool(name, tool)
34153415

3416-
def add_tool(self, name, tool):
3416+
def add_tool(self, name, tool, *args, **kwargs):
34173417
"""Add tool to `NavigationBase`
34183418
34193419
Add a tool to the tools controlled by Navigation
@@ -3428,6 +3428,10 @@ def add_tool(self, name, tool):
34283428
Name of the tool, treated as the ID, has to be unique
34293429
tool : string or `matplotlib.backend_tools.ToolBase` derived class
34303430
Reference to find the class of the Tool to be added
3431+
3432+
Notes
3433+
-----
3434+
args and kwargs get passed directly to the tools constructor.
34313435
"""
34323436

34333437
tool_cls = self._get_cls_to_instantiate(tool)
@@ -3440,12 +3444,7 @@ def add_tool(self, name, tool):
34403444
'not added')
34413445
return
34423446

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
3447+
self._tools[name] = tool_cls(self, name, *args, **kwargs)
34493448

34503449
if tool_cls.keymap is not None:
34513450
self.set_tool_keymap(name, tool_cls.keymap)
@@ -3460,7 +3459,6 @@ def add_tool(self, name, tool):
34603459
self._toggled.setdefault(tool_cls.radio_group, None)
34613460

34623461
self._tool_added_event(self._tools[name])
3463-
34643462
return self._tools[name]
34653463

34663464
def _tool_added_event(self, tool):
@@ -3583,7 +3581,7 @@ def get_tool(self, name):
35833581
name : String, ToolBase
35843582
Name of the tool, or the tool itself
35853583
"""
3586-
if isinstance(name, tools.ToolBase):
3584+
if isinstance(name, tools.ToolBase) and tool.name in self._tools:
35873585
return name
35883586
if name not in self._tools:
35893587
warnings.warn("%s is not a tool controlled by Navigation" % name)
@@ -3612,15 +3610,12 @@ def _message_cbk(self, event):
36123610
"""Captures the 'tool_message_event' to set the message on the toolbar"""
36133611
self.set_message(event.message)
36143612

3615-
def _tool_triggered_cbk(self, event):
3613+
def _tool_toggled_cbk(self, event):
36163614
"""Captures the 'tool-trigger-toolname
36173615
36183616
This only gets used for toggled tools
36193617
"""
3620-
if event.sender is self:
3621-
return
3622-
3623-
self.toggle_toolitem(event.tool.name)
3618+
self.toggle_toolitem(event.tool.name, event.tool.toggled)
36243619

36253620
def add_tools(self, tools):
36263621
""" Add multiple tools to `Navigation`
@@ -3639,25 +3634,38 @@ def add_tools(self, tools):
36393634
for position, tool in enumerate(grouptools):
36403635
self.add_tool(tool, group, position)
36413636

3642-
def add_tool(self, tool, group, position):
3643-
"""Adds a tool to the toolbar"""
3637+
def add_tool(self, tool, group, position=-1, name=None, **kwargs):
3638+
"""Adds a tool to the toolbar
3639+
3640+
Parameters
3641+
----------
3642+
tool : string, tool
3643+
The name or the type of tool to add.
3644+
group : string
3645+
The name of the group to add this tool to.
3646+
position : int
3647+
the relative position within the group to place this tool.
3648+
name : string (optional)
3649+
If given, and the above fails, we use this to create a new tool of
3650+
type given by tool, and use this as the name of the tool.
3651+
"""
36443652
t = self.navigation.get_tool(tool)
36453653
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
3654+
if isinstance(tool, type):
3655+
tool = tool.__class__
3656+
if name is not None:
3657+
t = self.navigation.add_tool(name, tool, **kwargs)
3658+
if t is None:
3659+
warning.warn('Cannot add tool %s'%tool)
3660+
return
36533661
tool = t
36543662
image = self._get_image_filename(tool.image)
36553663
toggle = getattr(tool, 'toggled', None) is not None
36563664
self.add_toolitem(tool.name, group, position, image,
36573665
tool.description, toggle)
36583666
if toggle:
36593667
self.navigation.nav_connect('tool_trigger_%s' % tool.name,
3660-
self._tool_triggered_cbk)
3668+
self._tool_toggled_cbk)
36613669

36623670
def _remove_tool_cbk(self, event):
36633671
"""Captures the 'tool_removed_event' signal and removes the tool"""

lib/matplotlib/backend_tools.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ 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):
7168
self.navigation = navigation
7269
self.set_figure(navigation.canvas.figure)
7370

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,6 @@ def __init__(self, navigation):
767767
self.pack_start(self._toolbar, False, False, 0)
768768
self._toolbar.show_all()
769769
self._toolitems = {}
770-
self._signals = {}
771770
self._setup_message_area()
772771

773772
def _setup_message_area(self):
@@ -788,9 +787,6 @@ def _setup_message_area(self):
788787

789788
def add_toolitem(self, name, group, position, image_file, description,
790789
toggle):
791-
if group is None:
792-
return
793-
794790
if toggle:
795791
tbutton = Gtk.ToggleToolButton()
796792
else:
@@ -809,29 +805,29 @@ def add_toolitem(self, name, group, position, image_file, description,
809805
signal = tbutton.connect('clicked', self._call_tool, name)
810806
tbutton.set_tooltip_text(description)
811807
tbutton.show_all()
812-
self._toolitems[name] = tbutton
813-
self._signals[name] = signal
808+
self._toolitems.setdefault(name, [])
809+
self._toolitems[name].append((tbutton, signal))
814810

815811
def _call_tool(self, btn, name):
816812
self.trigger_tool(name)
817813

818814
def set_message(self, s):
819815
self.message.set_label(s)
820816

821-
def toggle_toolitem(self, name):
817+
def toggle_toolitem(self, name, toggled):
822818
if name not in self._toolitems:
823819
return
824-
825-
status = self._toolitems[name].get_active()
826-
self._toolitems[name].handler_block(self._signals[name])
827-
self._toolitems[name].set_active(not status)
828-
self._toolitems[name].handler_unblock(self._signals[name])
820+
for toolitem, signal in self._toolitems[name]:
821+
toolitem.handler_block(signal)
822+
toolitem.set_active(toggled)
823+
toolitem.handler_unblock(signal)
829824

830825
def remove_toolitem(self, name):
831826
if name not in self._toolitems:
832827
self.set_message('%s Not in toolbar' % name)
833828
return
834-
self._toolbar.remove(self._toolitems[name])
829+
for toolitem, signal in self._toolitems[name]:
830+
self._toolbar.remove(toolitem)
835831
del self._toolitems[name]
836832

837833
def add_separator(self, pos=-1):

0 commit comments

Comments
 (0)