diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index bbc9c0c041ee..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 diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index bef623bb64e0..5cc66dbb7303 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. @@ -111,6 +111,20 @@ 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 + + # 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. @@ -1081,7 +1095,21 @@ def _update(self): self.canvas.draw_idle() -class SpanSelector(AxesWidget): +class _SelectorWidget(AxesWidget): + def set_active(self, active): + AxesWidget.set_active(self, 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. @@ -1189,13 +1217,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 @@ -1302,7 +1323,7 @@ def onmove(self, event): return False -class RectangleSelector(AxesWidget): +class RectangleSelector(_SelectorWidget): """ Select a rectangular region of an axes. @@ -1393,7 +1414,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 @@ -1437,11 +1457,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: @@ -1575,19 +1590,8 @@ 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): +class LassoSelector(_SelectorWidget): """Selection curve of an arbitrary shape. For the selector to remain responsive you much keep a reference to @@ -1679,12 +1683,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.