From 13ce60928bccbac64a4e42a405a0cc8643c03556 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Aug 2014 13:02:53 -0700 Subject: [PATCH 1/5] Move widget.{get,set}_active to AxisWidget. The method was previously defined only for RectangleSelectors (#3375). Also, an redundant line was removed (RectangleSelectors are already set active on __init__ by the superclass). --- lib/matplotlib/widgets.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index bef623bb64e0..2f7c819116d0 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -111,6 +111,16 @@ def disconnect_events(self): for c in self.cids: self.canvas.mpl_disconnect(c) + def set_active(self, active): + """Set whether the widget is active. + """ + self.active = active + + def get_active(self): + """Get whether the widget is active. + """ + return self.active + def ignore(self, event): """Return True if event should be ignored. @@ -1393,7 +1403,6 @@ def __init__(self, ax, onselect, drawtype='box', self.connect_event('button_release_event', self.release) self.connect_event('draw_event', self.update_background) - self.active = True # for activation / deactivation self.to_draw = None self.background = None @@ -1575,17 +1584,6 @@ def onmove(self, event): self.update() return False - def set_active(self, active): - """ - Use this to activate / deactivate the RectangleSelector - from your program with an boolean parameter *active*. - """ - self.active = active - - def get_active(self): - """ Get status of active mode (boolean variable)""" - return self.active - class LassoSelector(AxesWidget): """Selection curve of an arbitrary shape. From 8a6845c6353a3df7c4efcc035a1fdc28035d8f33 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Aug 2014 13:24:33 -0700 Subject: [PATCH 2/5] Update background on widget.set_active(True). Because selectors must update their background on set_active(True), widget.active was made a property for backwards compatibility. --- lib/matplotlib/widgets.py | 46 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 2f7c819116d0..397190c0baf7 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -95,7 +95,7 @@ def __init__(self, ax): self.ax = ax self.canvas = ax.figure.canvas self.cids = [] - self.active = True + self._active = True def connect_event(self, event, callback): """Connect callback with an event. @@ -114,12 +114,14 @@ def disconnect_events(self): def set_active(self, active): """Set whether the widget is active. """ - self.active = active + self._active = active def get_active(self): """Get whether the widget is active. """ - return self.active + return self._active + + active = property(set_active, get_active, doc="Is the widget active?") def ignore(self, event): """Return True if event should be ignored. @@ -1091,7 +1093,21 @@ def _update(self): self.canvas.draw_idle() -class SpanSelector(AxesWidget): +class _SelectorWidget(AxesWidget): + def set_active(self, active): + super(_SelectorWidget, self).set_active(active) + if active: + self.update_background(None) + + def update_background(self, event): + """force an update of the background""" + # If you add a call to `ignore` here, you'll want to check edge case: + # `release` can call a draw event even when `ignore` is True. + if self.useblit: + self.background = self.canvas.copy_from_bbox(self.ax.bbox) + + +class SpanSelector(_SelectorWidget): """ Select a min/max range of the x or y axes for a matplotlib Axes. @@ -1199,13 +1215,6 @@ def new_axes(self, ax): if not self.useblit: self.ax.add_patch(self.rect) - def update_background(self, event): - """force an update of the background""" - # If you add a call to `ignore` here, you'll want to check edge case: - # `release` can call a draw event even when `ignore` is True. - if self.useblit: - self.background = self.canvas.copy_from_bbox(self.ax.bbox) - def ignore(self, event): """return *True* if *event* should be ignored""" widget_off = not self.visible or not self.active @@ -1312,7 +1321,7 @@ def onmove(self, event): return False -class RectangleSelector(AxesWidget): +class RectangleSelector(_SelectorWidget): """ Select a rectangular region of an axes. @@ -1446,11 +1455,6 @@ def __init__(self, ax, onselect, drawtype='box', # will save the data (pos. at mouserelease) self.eventrelease = None - def update_background(self, event): - """force an update of the background""" - if self.useblit: - self.background = self.canvas.copy_from_bbox(self.ax.bbox) - def ignore(self, event): """return *True* if *event* should be ignored""" if not self.active: @@ -1585,7 +1589,7 @@ def onmove(self, event): return False -class LassoSelector(AxesWidget): +class LassoSelector(_SelectorWidget): """Selection curve of an arbitrary shape. For the selector to remain responsive you much keep a reference to @@ -1677,12 +1681,6 @@ def onmove(self, event): else: self.canvas.draw_idle() - def update_background(self, event): - if self.ignore(event): - return - if self.useblit: - self.background = self.canvas.copy_from_bbox(self.ax.bbox) - class Lasso(AxesWidget): """Selection curve of an arbitrary shape. From 51f6a6e99ad2ddcd8b1be6d1938e0458dc2e090d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Aug 2014 13:31:36 -0700 Subject: [PATCH 3/5] Updated whats_new.rst. Because _Selector is a private class there are no references to the actual getter and setter in the rst file. --- doc/users/whats_new.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index bbc9c0c041ee..21a5dba22915 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -382,6 +382,13 @@ instead of ``:context:`` any time you want to reset the context. Widgets ------- +Active state of Selectors +````````````````````````` + +All selectors now implement ``set_active`` and ``get_active`` methods (also +called when accessing the ``active`` property) to properly update and query +whether they are active. + Span Selector ````````````` From f1161df23b6ca8810b986f8519e2f1a295e0f0ff Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Aug 2014 13:41:19 -0700 Subject: [PATCH 4/5] Minor fixes according to Github discussion. --- doc/users/whats_new.rst | 22 +++++++++++++++------- lib/matplotlib/widgets.py | 4 ++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index 21a5dba22915..64ec7fe5892f 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -24,6 +24,21 @@ revision, see the :ref:`github-stats`. .. contents:: Table of Contents :depth: 3 +.. _whats-new-1-5: + +new in matplotlib-1.5 +===================== + +Widgets +------- + +Active state of Selectors +````````````````````````` + +All selectors now implement ``set_active`` and ``get_active`` methods (also +called when accessing the ``active`` property) to properly update and query +whether they are active. + .. _whats-new-1-4: new in matplotlib-1.4 @@ -382,13 +397,6 @@ instead of ``:context:`` any time you want to reset the context. Widgets ------- -Active state of Selectors -````````````````````````` - -All selectors now implement ``set_active`` and ``get_active`` methods (also -called when accessing the ``active`` property) to properly update and query -whether they are active. - Span Selector ````````````` diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 397190c0baf7..c233863d0baa 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -121,7 +121,7 @@ def get_active(self): """ return self._active - active = property(set_active, get_active, doc="Is the widget active?") + active = property(get_active, set_active, doc="Is the widget active?") def ignore(self, event): """Return True if event should be ignored. @@ -1095,7 +1095,7 @@ def _update(self): class _SelectorWidget(AxesWidget): def set_active(self, active): - super(_SelectorWidget, self).set_active(active) + AxesWidget.set_active(self, active) if active: self.update_background(None) From 1b39b247b443433ce6a270428768d6fae371c5f4 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 18 Aug 2014 09:59:11 -0700 Subject: [PATCH 5/5] _SelectorWidget overrides the active property. ... so it should dynamically retrieve the set_active method. --- lib/matplotlib/widgets.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index c233863d0baa..5cc66dbb7303 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -121,7 +121,9 @@ def get_active(self): """ return self._active - active = property(get_active, set_active, doc="Is the widget active?") + # set_active is overriden by SelectorWidgets. + active = property(get_active, lambda self, active: self.set_active(active), + doc="Is the widget active?") def ignore(self, event): """Return True if event should be ignored.