Add AxesWidget base class to handle event activation and clean up. #724

Merged
merged 8 commits into from Mar 17, 2012

Projects

None yet

3 participants

Contributor
tonysyu commented Feb 28, 2012
  • Adds connect_event method to connect event and store a reference. Additional disconnect_events method allows user to clean up widget. Some widgets did their own cleanup, but this makes behavior more consistent.
  • Adds default ignore method and call it in event callbacks. Some widgets had their own ignore methods, but this makes behavior more consistent.

Todo:

  • I think SpanSelector.new_axes should be removed, but may be some user's code depends on it. Right now I duplicate most of the new_axes code in __init__: If new_axis isn't removed, then duplication should be removed.
  • update_background in SpanSelector and RectangleSelector don't ignore events when deactivated.
tonysyu added some commits Feb 27, 2012
@tonysyu tonysyu Change cursors and selectors to subclass Widget. 8af797d
@tonysyu tonysyu Remove call to `new_axes` in SpanSelector.
* This change is in preparation for generalizing the base Widget.
* It's not clear to me why `new_axes` is necessary, but I left it in (for now).
5b35427
@tonysyu tonysyu Add AxesWidget class and let it initialize `ax` and `canvas` attributes.
* Renamed `Lasso.axes` to `Lasso.ax` for consistency.
* Remove `Lasso.figure`, which was unused.
e37c135
@tonysyu tonysyu Add `connect_event` and `disconnect_events` to AxesWidget. 21bca27
@tonysyu tonysyu Add default ignore method and check it in callbacks.
* Attribute name "active" can be confused with parameter to `RadioButtons`, but `RadioButtons` doesn't save that parameter as an attribute, so there's no name clash. BUT this is still really confusing to users. Consider renaming `AxesWidget.active`.
*
* * This could have been implemented as a decorator, but some callbacks wouldn't be compatible. For example, `SpanSelector.release` should not be ignored if widget was deactivated during mouse press.
*
* * `SpanSelector` and `RectangleSelector` already had `ignore` methods, but they do not call it in `update_background`. I don't change the current behavior, although it seems desirable.
c8f152a
@WeatherGod WeatherGod and 1 other commented on an outdated diff Feb 28, 2012
lib/matplotlib/widgets.py
+ Attributes
+ ----------
+ *ax*
+ The parent :class:`matplotlib.axes.Axes` for the widget
+ *canvas*
+ The parent FigureCanvas for the widget
+ """
+ def __init__(self, ax):
+ self.ax = ax
+ self.canvas = ax.figure.canvas
+ self.cids = []
+ self.active = True
+
+ def connect_event(self, event, callback):
+ self.canvas.mpl_connect(event, callback)
+ self.cids.append(callback)
WeatherGod
WeatherGod Feb 28, 2012 Member

This is wrong. The cids are not the callback functions themselves, they are the integers returned by the call to mpl_connect().

tonysyu
tonysyu Feb 28, 2012 Contributor

Good catch! I had this correct at some point, but somehow, I broke it along the way.

@WeatherGod WeatherGod and 1 other commented on an outdated diff Feb 28, 2012
lib/matplotlib/widgets.py
@@ -958,21 +1001,36 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
self.buttonDown = False
self.prev = (0, 0)
- self.new_axes(ax)
WeatherGod
WeatherGod Feb 28, 2012 Member

Not sure why you did this here. Now we have duplicated logic in init and in new_axes().

WeatherGod
WeatherGod Feb 28, 2012 Member

Yeah, we won't be getting rid of new_axes() anytime soon because it is a public-facing API and would have to go through the deprecation process. I am not exactly sure of the use-case for it, but there really isn't much of a rational for removing it, either, I think.

tonysyu
tonysyu Feb 28, 2012 Contributor

I left in the duplicate code to remind myself that a decision about new_axes needs to made. The duplicate in __init__ is now removed in favor of a call to new_axes.

@WeatherGod WeatherGod and 1 other commented on an outdated diff Feb 28, 2012
lib/matplotlib/widgets.py
@@ -10,6 +10,7 @@
"""
from __future__ import print_function
+import functools
WeatherGod
WeatherGod Feb 28, 2012 Member

why is this imported?

tonysyu
tonysyu Feb 28, 2012 Contributor

Oops. I had the ignore calls implemented as a decorator, originally. Thanks!

Member

Overall, I like the idea, and I do see its value in bringing uniformity to the widgets. This does open the doors to subsequent updates.

@tonysyu tonysyu referenced this pull request Mar 1, 2012
Merged

Lasso selector #730

@tonysyu tonysyu Fix and improve docstring.
Class links were incorrect.
f169460
@efiring efiring merged commit f169460 into matplotlib:master Mar 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment