Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 888bf17

Browse filesBrowse files
authored
Merge pull request #6407 from fariza/toggletool-initial-state
adding default toggled state for toggle tools
2 parents 2d7c5ad + b440068 commit 888bf17
Copy full SHA for 888bf17

File tree

Expand file treeCollapse file tree

4 files changed

+40
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+40
-11
lines changed

‎examples/user_interfaces/toolmanager.py

Copy file name to clipboardExpand all lines: examples/user_interfaces/toolmanager.py
+7-6Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ def trigger(self, *args, **kwargs):
4545

4646

4747
class GroupHideTool(ToolToggleBase):
48-
'''Hide lines with a given gid'''
48+
'''Show lines with a given gid'''
4949
default_keymap = 'G'
50-
description = 'Hide by gid'
50+
description = 'Show by gid'
51+
default_toggled = True
5152

5253
def __init__(self, *args, **kwargs):
5354
self.gid = kwargs.pop('gid')
5455
ToolToggleBase.__init__(self, *args, **kwargs)
5556

5657
def enable(self, *args):
57-
self.set_lines_visibility(False)
58+
self.set_lines_visibility(True)
5859

5960
def disable(self, *args):
60-
self.set_lines_visibility(True)
61+
self.set_lines_visibility(False)
6162

6263
def set_lines_visibility(self, state):
6364
gr_lines = []
@@ -75,7 +76,7 @@ def set_lines_visibility(self, state):
7576

7677
# Add the custom tools that we created
7778
fig.canvas.manager.toolmanager.add_tool('List', ListTools)
78-
fig.canvas.manager.toolmanager.add_tool('Hide', GroupHideTool, gid='mygroup')
79+
fig.canvas.manager.toolmanager.add_tool('Show', GroupHideTool, gid='mygroup')
7980

8081

8182
# Add an existing tool to new group `foo`.
@@ -87,6 +88,6 @@ def set_lines_visibility(self, state):
8788

8889
# To add a custom tool to the toolbar at specific location inside
8990
# the navigation group
90-
fig.canvas.manager.toolbar.add_tool('Hide', 'navigation', 1)
91+
fig.canvas.manager.toolbar.add_tool('Show', 'navigation', 1)
9192

9293
plt.show()

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,9 @@ def add_tool(self, tool, group, position=-1):
32173217
if toggle:
32183218
self.toolmanager.toolmanager_connect('tool_trigger_%s' % tool.name,
32193219
self._tool_toggled_cbk)
3220+
# If initially toggled
3221+
if tool.toggled:
3222+
self.toggle_toolitem(tool.name, True)
32203223

32213224
def _remove_tool_cbk(self, event):
32223225
"""Captures the 'tool_removed_event' signal and removes the tool"""

‎lib/matplotlib/backend_managers.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_managers.py
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ def add_tool(self, name, tool, *args, **kwargs):
282282
else:
283283
self._toggled.setdefault(tool_obj.radio_group, None)
284284

285+
# If initially toggled
286+
if tool_obj.toggled:
287+
self._handle_toggle(tool_obj, None, None, None)
285288
tool_obj.set_figure(self.figure)
289+
286290
self._tool_added_event(tool_obj)
287291
return tool_obj
288292

@@ -311,7 +315,7 @@ def _handle_toggle(self, tool, sender, canvasevent, data):
311315
# radio_group None is not mutually exclusive
312316
# just keep track of toggled tools in this group
313317
if radio_group is None:
314-
if tool.toggled:
318+
if tool.name in self._toggled[None]:
315319
self._toggled[None].remove(tool.name)
316320
else:
317321
self._toggled[None].add(tool.name)

‎lib/matplotlib/backend_tools.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_tools.py
+25-4Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ class ToolToggleBase(ToolBase):
147147
Toggleable tool
148148
149149
Every time it is triggered, it switches between enable and disable
150+
151+
Parameters
152+
----------
153+
``*args``
154+
Variable length argument to be used by the Tool
155+
``**kwargs``
156+
`toggled` if present and True, sets the initial state ot the Tool
157+
Arbitrary keyword arguments to be consumed by the Tool
150158
"""
151159

152160
radio_group = None
@@ -159,9 +167,12 @@ class ToolToggleBase(ToolBase):
159167
cursor = None
160168
"""Cursor to use when the tool is active"""
161169

170+
default_toggled = False
171+
"""Default of toggled state"""
172+
162173
def __init__(self, *args, **kwargs):
174+
self._toggled = kwargs.pop('toggled', self.default_toggled)
163175
ToolBase.__init__(self, *args, **kwargs)
164-
self._toggled = False
165176

166177
def trigger(self, sender, event, data=None):
167178
"""Calls `enable` or `disable` based on `toggled` value"""
@@ -205,10 +216,20 @@ def toggled(self):
205216
def set_figure(self, figure):
206217
toggled = self.toggled
207218
if toggled:
208-
self.trigger(self, None)
219+
if self.figure:
220+
self.trigger(self, None)
221+
else:
222+
# if no figure the internal state is not changed
223+
# we change it here so next call to trigger will change it back
224+
self._toggled = False
209225
ToolBase.set_figure(self, figure)
210-
if figure and toggled:
211-
self.trigger(self, None)
226+
if toggled:
227+
if figure:
228+
self.trigger(self, None)
229+
else:
230+
# if there is no figure, triggen wont change the internal state
231+
# we change it back
232+
self._toggled = True
212233

213234

214235
class SetCursorBase(ToolBase):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.