Thanks to visit codestin.com
Credit goes to github.com

Skip to content

adding default toggled state for toggle tools #6407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions examples/user_interfaces/toolmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ def trigger(self, *args, **kwargs):


class GroupHideTool(ToolToggleBase):
'''Hide lines with a given gid'''
'''Show lines with a given gid'''
default_keymap = 'G'
description = 'Hide by gid'
description = 'Show by gid'
default_toggled = True

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

def enable(self, *args):
self.set_lines_visibility(False)
self.set_lines_visibility(True)

def disable(self, *args):
self.set_lines_visibility(True)
self.set_lines_visibility(False)

def set_lines_visibility(self, state):
gr_lines = []
Expand All @@ -75,7 +76,7 @@ def set_lines_visibility(self, state):

# Add the custom tools that we created
fig.canvas.manager.toolmanager.add_tool('List', ListTools)
fig.canvas.manager.toolmanager.add_tool('Hide', GroupHideTool, gid='mygroup')
fig.canvas.manager.toolmanager.add_tool('Show', GroupHideTool, gid='mygroup')


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

# To add a custom tool to the toolbar at specific location inside
# the navigation group
fig.canvas.manager.toolbar.add_tool('Hide', 'navigation', 1)
fig.canvas.manager.toolbar.add_tool('Show', 'navigation', 1)

plt.show()
3 changes: 3 additions & 0 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3229,6 +3229,9 @@ def add_tool(self, tool, group, position=-1):
if toggle:
self.toolmanager.toolmanager_connect('tool_trigger_%s' % tool.name,
self._tool_toggled_cbk)
# If initially toggled
if tool.toggled:
self.toggle_toolitem(tool.name, True)

def _remove_tool_cbk(self, event):
"""Captures the 'tool_removed_event' signal and removes the tool"""
Expand Down
6 changes: 5 additions & 1 deletion lib/matplotlib/backend_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ def add_tool(self, name, tool, *args, **kwargs):
else:
self._toggled.setdefault(tool_obj.radio_group, None)

# If initially toggled
if tool_obj.toggled:
self._handle_toggle(tool_obj, None, None, None)
tool_obj.set_figure(self.figure)

self._tool_added_event(tool_obj)
return tool_obj

Expand Down Expand Up @@ -311,7 +315,7 @@ def _handle_toggle(self, tool, sender, canvasevent, data):
# radio_group None is not mutually exclusive
# just keep track of toggled tools in this group
if radio_group is None:
if tool.toggled:
if tool.name in self._toggled[None]:
self._toggled[None].remove(tool.name)
else:
self._toggled[None].add(tool.name)
Expand Down
29 changes: 25 additions & 4 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ class ToolToggleBase(ToolBase):
Toggleable tool

Every time it is triggered, it switches between enable and disable

Parameters
----------
``*args``
Variable length argument to be used by the Tool
``**kwargs``
`toggled` if present and True, sets the initial state ot the Tool
Arbitrary keyword arguments to be consumed by the Tool
"""

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

default_toggled = False
"""Default of toggled state"""

def __init__(self, *args, **kwargs):
self._toggled = kwargs.pop('toggled', self.default_toggled)
ToolBase.__init__(self, *args, **kwargs)
self._toggled = False

def trigger(self, sender, event, data=None):
"""Calls `enable` or `disable` based on `toggled` value"""
Expand Down Expand Up @@ -205,10 +216,20 @@ def toggled(self):
def set_figure(self, figure):
toggled = self.toggled
if toggled:
self.trigger(self, None)
if self.figure:
self.trigger(self, None)
else:
# if no figure the internal state is not changed
# we change it here so next call to trigger will change it back
self._toggled = False
ToolBase.set_figure(self, figure)
if figure and toggled:
self.trigger(self, None)
if toggled:
if figure:
self.trigger(self, None)
else:
# if there is no figure, triggen wont change the internal state
# we change it back
self._toggled = True


class SetCursorBase(ToolBase):
Expand Down