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