-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ToolManager/Tools adding methods to set figure after initialization #6405
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,14 +56,12 @@ class ToolManager(object): | |
`LockDraw` object to know if the message is available to write | ||
""" | ||
|
||
def __init__(self, canvas): | ||
def __init__(self, figure=None): | ||
warnings.warn('Treat the new Tool classes introduced in v1.5 as ' + | ||
'experimental for now, the API will likely change in ' + | ||
'version 2.1 and perhaps the rcParam as well') | ||
self.canvas = canvas | ||
|
||
self._key_press_handler_id = self.canvas.mpl_connect( | ||
'key_press_event', self._key_press) | ||
self._key_press_handler_id = None | ||
|
||
self._tools = {} | ||
self._keys = {} | ||
|
@@ -74,6 +72,45 @@ def __init__(self, canvas): | |
self.keypresslock = widgets.LockDraw() | ||
self.messagelock = widgets.LockDraw() | ||
|
||
self._figure = None | ||
self.set_figure(figure) | ||
|
||
@property | ||
def canvas(self): | ||
"""Canvas managed by FigureManager""" | ||
if not self._figure: | ||
return None | ||
return self._figure.canvas | ||
|
||
@property | ||
def figure(self): | ||
"""Figure that holds the canvas""" | ||
return self._figure | ||
|
||
@figure.setter | ||
def figure(self, figure): | ||
self.set_figure(figure) | ||
|
||
def set_figure(self, figure, update_tools=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Internally None is a valid value, so it is possible to set the figure to None, but it has to be done explicitly |
||
""" | ||
Sets the figure to interact with the tools | ||
|
||
Parameters | ||
========== | ||
figure: `Figure` | ||
update_tools: bool | ||
Force tools to update figure | ||
""" | ||
if self._key_press_handler_id: | ||
self.canvas.mpl_disconnect(self._key_press_handler_id) | ||
self._figure = figure | ||
if figure: | ||
self._key_press_handler_id = self.canvas.mpl_connect( | ||
'key_press_event', self._key_press) | ||
if update_tools: | ||
for tool in self._tools.values(): | ||
tool.figure = figure | ||
|
||
def toolmanager_connect(self, s, func): | ||
""" | ||
Connect event with string *s* to *func*. | ||
|
@@ -245,6 +282,7 @@ def add_tool(self, name, tool, *args, **kwargs): | |
else: | ||
self._toggled.setdefault(tool_obj.radio_group, None) | ||
|
||
tool_obj.set_figure(self.figure) | ||
self._tool_added_event(tool_obj) | ||
return tool_obj | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks odd to me: if a default
ToolManager
is created and its first property,canvas
, is accessed, it will raise AttributeError becauseself._figure
will be None. Presumably this won't happen ordinarily, but it just doesn't look right. Maybecanvas
should trap it and raise a more informative exception.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, but perhaps we could just return
None
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of returning
None