Skip to content

Commit

Permalink
Merge pull request #3376 from anntzer/widget-active-setter-getter
Browse files Browse the repository at this point in the history
ENH : Move widget.{get,set}_active to AxisWidget.
  • Loading branch information
tacaswell committed Sep 11, 2014
2 parents 184285d + 1b39b24 commit c74dfa5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
15 changes: 15 additions & 0 deletions doc/users/whats_new.rst
Expand Up @@ -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
Expand Down
66 changes: 32 additions & 34 deletions lib/matplotlib/widgets.py
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1302,7 +1323,7 @@ def onmove(self, event):
return False


class RectangleSelector(AxesWidget):
class RectangleSelector(_SelectorWidget):
"""
Select a rectangular region of an axes.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c74dfa5

Please sign in to comment.