Skip to content

Commit

Permalink
Support removing absent tools from ToolContainerBase.
Browse files Browse the repository at this point in the history
ToolManager.remove_tool calls ToolContainerBase.remove_toolitem
(via a tool_removed_event) and cannot know (due to the lack of API)
whether the tool is actually on the container (it can also be a
keybind-only tool not associated with any entry on the toolbar), so
ToolContainerBase.remove_toolitem should work (as a no-op) even for
tools not present on the container.
  • Loading branch information
anntzer committed May 6, 2024
1 parent 9387431 commit 3d0f153
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 20 deletions.
4 changes: 4 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,10 @@ def remove_toolitem(self, name):
backend-specific code to remove an element from the toolbar; it is
called when `.ToolManager` emits a `tool_removed_event`.
Because some tools are present only on the `.ToolManager` but not on
the `ToolContainer`, this method must be a no-op when called on a tool
absent from the container.
.. warning::
This is part of the backend implementation and should
not be called by end-users. They should instead call
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,9 +1011,8 @@ def toggle_toolitem(self, name, toggled):
toolitem.deselect()

def remove_toolitem(self, name):
for toolitem in self._toolitems[name]:
for toolitem in self._toolitems.pop(name, []):
toolitem.pack_forget()
del self._toolitems[name]

def set_message(self, s):
self._message.set(s)
Expand Down
9 changes: 2 additions & 7 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,10 @@ def toggle_toolitem(self, name, toggled):
toolitem.handler_unblock(signal)

def remove_toolitem(self, name):
if name not in self._toolitems:
self.toolmanager.message_event(f'{name} not in toolbar', self)
return

for group in self._groups:
for toolitem, _signal in self._toolitems[name]:
for toolitem, _signal in self._toolitems.pop(name, []):
for group in self._groups:
if toolitem in self._groups[group]:
self._groups[group].remove(toolitem)
del self._toolitems[name]

def _add_separator(self):
sep = Gtk.Separator()
Expand Down
9 changes: 2 additions & 7 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,10 @@ def toggle_toolitem(self, name, toggled):
toolitem.handler_unblock(signal)

def remove_toolitem(self, name):
if name not in self._toolitems:
self.toolmanager.message_event(f'{name} not in toolbar', self)
return

for group in self._groups:
for toolitem, _signal in self._toolitems[name]:
for toolitem, _signal in self._toolitems.pop(name, []):
for group in self._groups:
if toolitem in self._groups[group]:
self._groups[group].remove(toolitem)
del self._toolitems[name]

def _add_separator(self):
sep = Gtk.Separator()
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,9 +1007,8 @@ def toggle_toolitem(self, name, toggled):
button.toggled.connect(handler)

def remove_toolitem(self, name):
for button, handler in self._toolitems[name]:
for button, handler in self._toolitems.pop(name, []):
button.setParent(None)
del self._toolitems[name]

def set_message(self, s):
self.widgetForAction(self._message_action).setText(s)
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,9 +1257,8 @@ def toggle_toolitem(self, name, toggled):
self.Refresh()

def remove_toolitem(self, name):
for tool, handler in self._toolitems[name]:
for tool, handler in self._toolitems.pop(name, []):
self.DeleteTool(tool.Id)
del self._toolitems[name]

def set_message(self, s):
self._label_text.SetLabel(s)
Expand Down

0 comments on commit 3d0f153

Please sign in to comment.