1- from matplotlib import _api , cbook , widgets
2- import matplotlib .backend_tools as tools
1+ from matplotlib import _api , backend_tools , cbook , widgets
32
43
54class ToolEvent :
@@ -233,8 +232,9 @@ def add_tool(self, name, tool, *args, **kwargs):
233232 ----------
234233 name : str
235234 Name of the tool, treated as the ID, has to be unique.
236- tool : class_like, i.e. str or type
237- Reference to find the class of the Tool to added.
235+ tool : type
236+ Class of the tool to be added. A subclass will be used
237+ instead if one was registered for the current canvas class.
238238
239239 Notes
240240 -----
@@ -245,7 +245,7 @@ def add_tool(self, name, tool, *args, **kwargs):
245245 matplotlib.backend_tools.ToolBase : The base class for tools.
246246 """
247247
248- tool_cls = self . _get_cls_to_instantiate ( tool )
248+ tool_cls = backend_tools . _find_tool_class ( type ( self . canvas ), tool )
249249 if not tool_cls :
250250 raise ValueError ('Impossible to find class for %s' % str (tool ))
251251
@@ -254,7 +254,7 @@ def add_tool(self, name, tool, *args, **kwargs):
254254 'exists, not added' )
255255 return self ._tools [name ]
256256
257- if name == 'cursor' and tool_cls != tools .SetCursorBase :
257+ if name == 'cursor' and tool_cls != backend_tools .SetCursorBase :
258258 _api .warn_deprecated ("3.5" ,
259259 message = "Overriding ToolSetCursor with "
260260 f"{ tool_cls .__qualname__ } was only "
@@ -271,7 +271,7 @@ def add_tool(self, name, tool, *args, **kwargs):
271271 self .update_keymap (name , tool_cls .default_keymap )
272272
273273 # For toggle tools init the radio_group in self._toggled
274- if isinstance (tool_obj , tools .ToolToggleBase ):
274+ if isinstance (tool_obj , backend_tools .ToolToggleBase ):
275275 # None group is not mutually exclusive, a set is used to keep track
276276 # of all toggled tools in this group
277277 if tool_obj .radio_group is None :
@@ -337,23 +337,6 @@ def _handle_toggle(self, tool, sender, canvasevent, data):
337337 # Keep track of the toggled tool in the radio_group
338338 self ._toggled [radio_group ] = toggled
339339
340- def _get_cls_to_instantiate (self , callback_class ):
341- # Find the class that corresponds to the tool
342- if isinstance (callback_class , str ):
343- # FIXME: make more complete searching structure
344- if callback_class in globals ():
345- callback_class = globals ()[callback_class ]
346- else :
347- mod = 'backend_tools'
348- current_module = __import__ (mod ,
349- globals (), locals (), [mod ], 1 )
350-
351- callback_class = getattr (current_module , callback_class , False )
352- if callable (callback_class ):
353- return callback_class
354- else :
355- return None
356-
357340 def trigger_tool (self , name , sender = None , canvasevent = None , data = None ):
358341 """
359342 Trigger a tool and emit the ``tool_trigger_{name}`` event.
@@ -376,7 +359,7 @@ def trigger_tool(self, name, sender=None, canvasevent=None, data=None):
376359 if sender is None :
377360 sender = self
378361
379- if isinstance (tool , tools .ToolToggleBase ):
362+ if isinstance (tool , backend_tools .ToolToggleBase ):
380363 self ._handle_toggle (tool , sender , canvasevent , data )
381364
382365 tool .trigger (sender , canvasevent , data ) # Actually trigger Tool.
@@ -418,7 +401,8 @@ def get_tool(self, name, warn=True):
418401 `.ToolBase` or None
419402 The tool or None if no tool with the given name exists.
420403 """
421- if isinstance (name , tools .ToolBase ) and name .name in self ._tools :
404+ if (isinstance (name , backend_tools .ToolBase )
405+ and name .name in self ._tools ):
422406 return name
423407 if name not in self ._tools :
424408 if warn :
0 commit comments