@@ -3413,7 +3413,7 @@ def add_tools(self, tools):
3413
3413
for tool , name in tools :
3414
3414
self .add_tool (name , tool )
3415
3415
3416
- def add_tool (self , name , tool ):
3416
+ def add_tool (self , name , tool , * args , ** kwargs ):
3417
3417
"""Add tool to `NavigationBase`
3418
3418
3419
3419
Add a tool to the tools controlled by Navigation
@@ -3428,6 +3428,10 @@ def add_tool(self, name, tool):
3428
3428
Name of the tool, treated as the ID, has to be unique
3429
3429
tool : string or `matplotlib.backend_tools.ToolBase` derived class
3430
3430
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.
3431
3435
"""
3432
3436
3433
3437
tool_cls = self ._get_cls_to_instantiate (tool )
@@ -3440,12 +3444,7 @@ def add_tool(self, name, tool):
3440
3444
'not added' )
3441
3445
return
3442
3446
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 )
3449
3448
3450
3449
if tool_cls .keymap is not None :
3451
3450
self .set_tool_keymap (name , tool_cls .keymap )
@@ -3460,7 +3459,6 @@ def add_tool(self, name, tool):
3460
3459
self ._toggled .setdefault (tool_cls .radio_group , None )
3461
3460
3462
3461
self ._tool_added_event (self ._tools [name ])
3463
-
3464
3462
return self ._tools [name ]
3465
3463
3466
3464
def _tool_added_event (self , tool ):
@@ -3583,7 +3581,7 @@ def get_tool(self, name):
3583
3581
name : String, ToolBase
3584
3582
Name of the tool, or the tool itself
3585
3583
"""
3586
- if isinstance (name , tools .ToolBase ):
3584
+ if isinstance (name , tools .ToolBase ) and tool . name in self . _tools :
3587
3585
return name
3588
3586
if name not in self ._tools :
3589
3587
warnings .warn ("%s is not a tool controlled by Navigation" % name )
@@ -3612,15 +3610,12 @@ def _message_cbk(self, event):
3612
3610
"""Captures the 'tool_message_event' to set the message on the toolbar"""
3613
3611
self .set_message (event .message )
3614
3612
3615
- def _tool_triggered_cbk (self , event ):
3613
+ def _tool_toggled_cbk (self , event ):
3616
3614
"""Captures the 'tool-trigger-toolname
3617
3615
3618
3616
This only gets used for toggled tools
3619
3617
"""
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 )
3624
3619
3625
3620
def add_tools (self , tools ):
3626
3621
""" Add multiple tools to `Navigation`
@@ -3639,25 +3634,38 @@ def add_tools(self, tools):
3639
3634
for position , tool in enumerate (grouptools ):
3640
3635
self .add_tool (tool , group , position )
3641
3636
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
+ """
3644
3652
t = self .navigation .get_tool (tool )
3645
3653
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
3653
3661
tool = t
3654
3662
image = self ._get_image_filename (tool .image )
3655
3663
toggle = getattr (tool , 'toggled' , None ) is not None
3656
3664
self .add_toolitem (tool .name , group , position , image ,
3657
3665
tool .description , toggle )
3658
3666
if toggle :
3659
3667
self .navigation .nav_connect ('tool_trigger_%s' % tool .name ,
3660
- self ._tool_triggered_cbk )
3668
+ self ._tool_toggled_cbk )
3661
3669
3662
3670
def _remove_tool_cbk (self , event ):
3663
3671
"""Captures the 'tool_removed_event' signal and removes the tool"""
0 commit comments