@@ -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"""
0 commit comments