Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c74dfa5

Browse files
committed
Merge pull request #3376 from anntzer/widget-active-setter-getter
ENH : Move widget.{get,set}_active to AxisWidget.
2 parents 184285d + 1b39b24 commit c74dfa5

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

doc/users/whats_new.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ revision, see the :ref:`github-stats`.
2424
.. contents:: Table of Contents
2525
:depth: 3
2626

27+
.. _whats-new-1-5:
28+
29+
new in matplotlib-1.5
30+
=====================
31+
32+
Widgets
33+
-------
34+
35+
Active state of Selectors
36+
`````````````````````````
37+
38+
All selectors now implement ``set_active`` and ``get_active`` methods (also
39+
called when accessing the ``active`` property) to properly update and query
40+
whether they are active.
41+
2742
.. _whats-new-1-4:
2843

2944
new in matplotlib-1.4

lib/matplotlib/widgets.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self, ax):
9595
self.ax = ax
9696
self.canvas = ax.figure.canvas
9797
self.cids = []
98-
self.active = True
98+
self._active = True
9999

100100
def connect_event(self, event, callback):
101101
"""Connect callback with an event.
@@ -111,6 +111,20 @@ def disconnect_events(self):
111111
for c in self.cids:
112112
self.canvas.mpl_disconnect(c)
113113

114+
def set_active(self, active):
115+
"""Set whether the widget is active.
116+
"""
117+
self._active = active
118+
119+
def get_active(self):
120+
"""Get whether the widget is active.
121+
"""
122+
return self._active
123+
124+
# set_active is overriden by SelectorWidgets.
125+
active = property(get_active, lambda self, active: self.set_active(active),
126+
doc="Is the widget active?")
127+
114128
def ignore(self, event):
115129
"""Return True if event should be ignored.
116130
@@ -1081,7 +1095,21 @@ def _update(self):
10811095
self.canvas.draw_idle()
10821096

10831097

1084-
class SpanSelector(AxesWidget):
1098+
class _SelectorWidget(AxesWidget):
1099+
def set_active(self, active):
1100+
AxesWidget.set_active(self, active)
1101+
if active:
1102+
self.update_background(None)
1103+
1104+
def update_background(self, event):
1105+
"""force an update of the background"""
1106+
# If you add a call to `ignore` here, you'll want to check edge case:
1107+
# `release` can call a draw event even when `ignore` is True.
1108+
if self.useblit:
1109+
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
1110+
1111+
1112+
class SpanSelector(_SelectorWidget):
10851113
"""
10861114
Select a min/max range of the x or y axes for a matplotlib Axes.
10871115
@@ -1189,13 +1217,6 @@ def new_axes(self, ax):
11891217
if not self.useblit:
11901218
self.ax.add_patch(self.rect)
11911219

1192-
def update_background(self, event):
1193-
"""force an update of the background"""
1194-
# If you add a call to `ignore` here, you'll want to check edge case:
1195-
# `release` can call a draw event even when `ignore` is True.
1196-
if self.useblit:
1197-
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
1198-
11991220
def ignore(self, event):
12001221
"""return *True* if *event* should be ignored"""
12011222
widget_off = not self.visible or not self.active
@@ -1302,7 +1323,7 @@ def onmove(self, event):
13021323
return False
13031324

13041325

1305-
class RectangleSelector(AxesWidget):
1326+
class RectangleSelector(_SelectorWidget):
13061327
"""
13071328
Select a rectangular region of an axes.
13081329
@@ -1393,7 +1414,6 @@ def __init__(self, ax, onselect, drawtype='box',
13931414
self.connect_event('button_release_event', self.release)
13941415
self.connect_event('draw_event', self.update_background)
13951416

1396-
self.active = True # for activation / deactivation
13971417
self.to_draw = None
13981418
self.background = None
13991419

@@ -1437,11 +1457,6 @@ def __init__(self, ax, onselect, drawtype='box',
14371457
# will save the data (pos. at mouserelease)
14381458
self.eventrelease = None
14391459

1440-
def update_background(self, event):
1441-
"""force an update of the background"""
1442-
if self.useblit:
1443-
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
1444-
14451460
def ignore(self, event):
14461461
"""return *True* if *event* should be ignored"""
14471462
if not self.active:
@@ -1575,19 +1590,8 @@ def onmove(self, event):
15751590
self.update()
15761591
return False
15771592

1578-
def set_active(self, active):
1579-
"""
1580-
Use this to activate / deactivate the RectangleSelector
1581-
from your program with an boolean parameter *active*.
1582-
"""
1583-
self.active = active
1584-
1585-
def get_active(self):
1586-
""" Get status of active mode (boolean variable)"""
1587-
return self.active
15881593

1589-
1590-
class LassoSelector(AxesWidget):
1594+
class LassoSelector(_SelectorWidget):
15911595
"""Selection curve of an arbitrary shape.
15921596
15931597
For the selector to remain responsive you much keep a reference to
@@ -1679,12 +1683,6 @@ def onmove(self, event):
16791683
else:
16801684
self.canvas.draw_idle()
16811685

1682-
def update_background(self, event):
1683-
if self.ignore(event):
1684-
return
1685-
if self.useblit:
1686-
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
1687-
16881686

16891687
class Lasso(AxesWidget):
16901688
"""Selection curve of an arbitrary shape.

0 commit comments

Comments
 (0)