@@ -228,7 +228,7 @@ def remove_tool(self, name):
228
228
229
229
del self ._tools [name ]
230
230
231
- def add_tool (self , tool_name , * args , ** kwargs ):
231
+ def add_tool (self , tool_cls_or_name , * args , ** kwargs ):
232
232
"""
233
233
Add *tool* to `ToolManager`
234
234
@@ -238,8 +238,8 @@ def add_tool(self, tool_name, *args, **kwargs):
238
238
239
239
Parameters
240
240
----------
241
- name : str
242
- Name of the tool, treated as the ID, has to be unique
241
+ tool_cls_or_name : callable or name
242
+ Class or name of the tool, treated as the ID, has to be unique
243
243
tool : class_like, i.e. str or type
244
244
Reference to find the class of the Tool to added.
245
245
@@ -252,18 +252,25 @@ def add_tool(self, tool_name, *args, **kwargs):
252
252
matplotlib.backend_tools.ToolBase : The base class for tools.
253
253
"""
254
254
255
- tool_cls = tools .get_tool (tool_name , type (self .canvas ))
255
+ if callable (tool_cls_or_name ):
256
+ tool_cls = tool_cls_or_name
257
+ elif isinstance (tool_cls_or_name , six .string_types ):
258
+ tool_cls = tools .get_tool (tool_cls_or_name , type (self .canvas ))
259
+ else :
260
+ raise TypeError (
261
+ "'tool_cls_or_name' must be a callable or a tool name" )
262
+ name = tool_cls .name
256
263
257
- if tool_name in self ._tools :
264
+ if name in self ._tools :
258
265
warnings .warn ('A "Tool class" with the same name already exists, '
259
266
'not added' )
260
- return self ._tools [tool_name ]
267
+ return self ._tools [name ]
261
268
262
- tool_obj = tool_cls (self , tool_name , * args , ** kwargs )
263
- self ._tools [tool_name ] = tool_obj
269
+ tool_obj = tool_cls (self , * args , ** kwargs )
270
+ self ._tools [name ] = tool_obj
264
271
265
272
if tool_cls .default_keymap is not None :
266
- self .update_keymap (tool_name , tool_cls .default_keymap )
273
+ self .update_keymap (name , tool_cls .default_keymap )
267
274
268
275
# For toggle tools init the radio_group in self._toggled
269
276
if isinstance (tool_obj , tools .ToolToggleBase ):
0 commit comments