Skip to content

Move widget.{get,set}_active to AxisWidget. #3376

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

Merged
merged 5 commits into from
Sep 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should over-ride the property as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell, is this the only thing holding this PR up?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at this again, it does update, the property method is tricksy.

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