Add *tool* to `ToolManager`. If successful, adds a new event ``tool_trigger_{name}`` where ``{name}`` is the *name* of the tool; the event is fired every time the tool is triggered. Parameters ---------- name : str Name of the to
(self, name, tool, *args, **kwargs)
| 213 | del self._tools[name] |
| 214 | |
| 215 | def add_tool(self, name, tool, *args, **kwargs): |
| 216 | """ |
| 217 | Add *tool* to `ToolManager`. |
| 218 | |
| 219 | If successful, adds a new event ``tool_trigger_{name}`` where |
| 220 | ``{name}`` is the *name* of the tool; the event is fired every time the |
| 221 | tool is triggered. |
| 222 | |
| 223 | Parameters |
| 224 | ---------- |
| 225 | name : str |
| 226 | Name of the tool, treated as the ID, has to be unique. |
| 227 | tool : type |
| 228 | Class of the tool to be added. A subclass will be used |
| 229 | instead if one was registered for the current canvas class. |
| 230 | *args, **kwargs |
| 231 | Passed to the *tool*'s constructor. |
| 232 | |
| 233 | See Also |
| 234 | -------- |
| 235 | matplotlib.backend_tools.ToolBase : The base class for tools. |
| 236 | """ |
| 237 | |
| 238 | tool_cls = backend_tools._find_tool_class(type(self.canvas), tool) |
| 239 | if not tool_cls: |
| 240 | raise ValueError('Impossible to find class for %s' % str(tool)) |
| 241 | |
| 242 | if name in self._tools: |
| 243 | _api.warn_external('A "Tool class" with the same name already ' |
| 244 | 'exists, not added') |
| 245 | return self._tools[name] |
| 246 | |
| 247 | tool_obj = tool_cls(self, name, *args, **kwargs) |
| 248 | self._tools[name] = tool_obj |
| 249 | |
| 250 | if tool_obj.default_keymap is not None: |
| 251 | self.update_keymap(name, tool_obj.default_keymap) |
| 252 | |
| 253 | # For toggle tools init the radio_group in self._toggled |
| 254 | if isinstance(tool_obj, backend_tools.ToolToggleBase): |
| 255 | # None group is not mutually exclusive, a set is used to keep track |
| 256 | # of all toggled tools in this group |
| 257 | if tool_obj.radio_group is None: |
| 258 | self._toggled.setdefault(None, set()) |
| 259 | else: |
| 260 | self._toggled.setdefault(tool_obj.radio_group, None) |
| 261 | |
| 262 | # If initially toggled |
| 263 | if tool_obj.toggled: |
| 264 | self._handle_toggle(tool_obj, None, None) |
| 265 | tool_obj.set_figure(self.figure) |
| 266 | |
| 267 | event = ToolEvent('tool_added_event', self, tool_obj) |
| 268 | self._callbacks.process(event.name, event) |
| 269 | |
| 270 | return tool_obj |
| 271 | |
| 272 | def _handle_toggle(self, tool, canvasevent, data): |