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

Skip to content

Add AxesWidget base class to handle event activation and clean up. #724

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 8 commits into from
Mar 17, 2012
Prev Previous commit
Next Next commit
Add connect_event and disconnect_events to AxesWidget.
  • Loading branch information
tonysyu committed Feb 28, 2012
commit 21bca27fc3df736561c47fe74c01845f39757ed3
67 changes: 36 additions & 31 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def locked(self):
return self._owner is not None



class Widget(object):
"""
Abstract base class for GUI neutral widgets
"""
drawon = True
eventson = True


class AxesWidget(Widget):
"""
Widget that is connected to a single :class:`Axes`.
Expand All @@ -77,6 +77,15 @@ class AxesWidget(Widget):
def __init__(self, ax):
self.ax = ax
self.canvas = ax.figure.canvas
self.cids = []

def connect_event(self, event, callback):
self.canvas.mpl_connect(event, callback)
self.cids.append(callback)
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong. The cids are not the callback functions themselves, they are the integers returned by the call to mpl_connect().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I had this correct at some point, but somehow, I broke it along the way.


def disconnect_events(self):
for c in self.cids:
self.canvas.mpl_disconnect(c)


class Button(AxesWidget):
Expand Down Expand Up @@ -133,9 +142,9 @@ def __init__(self, ax, label, image=None,
self.cnt = 0
self.observers = {}

ax.figure.canvas.mpl_connect('button_press_event', self._click)
ax.figure.canvas.mpl_connect('button_release_event', self._release)
ax.figure.canvas.mpl_connect('motion_notify_event', self._motion)
self.connect_event('button_press_event', self._click)
self.connect_event('button_release_event', self._release)
self.connect_event('motion_notify_event', self._motion)
ax.set_navigate(False)
ax.set_axis_bgcolor(color)
ax.set_xticks([])
Expand Down Expand Up @@ -270,10 +279,10 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
ax.set_xticks([])
ax.set_navigate(False)

ax.figure.canvas.mpl_connect('button_press_event', self._update)
ax.figure.canvas.mpl_connect('button_release_event', self._update)
self.connect_event('button_press_event', self._update)
self.connect_event('button_release_event', self._update)
if dragging:
ax.figure.canvas.mpl_connect('motion_notify_event', self._update)
self.connect_event('motion_notify_event', self._update)
self.label = ax.text(-0.02, 0.5, label, transform=ax.transAxes,
verticalalignment='center',
horizontalalignment='right')
Expand Down Expand Up @@ -446,7 +455,7 @@ def __init__(self, ax, labels, actives):
ax.add_line(l2)
cnt += 1

ax.figure.canvas.mpl_connect('button_press_event', self._clicked)
self.connect_event('button_press_event', self._clicked)

self.cnt = 0
self.observers = {}
Expand Down Expand Up @@ -557,7 +566,7 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
ax.add_patch(p)
cnt += 1

ax.figure.canvas.mpl_connect('button_press_event', self._clicked)
self.connect_event('button_press_event', self._clicked)

self.cnt = 0
self.observers = {}
Expand Down Expand Up @@ -770,8 +779,8 @@ def __init__(self, ax, useblit=False, **lineprops):
# TODO: Is the GTKAgg limitation still true?
AxesWidget.__init__(self, ax)

self.canvas.mpl_connect('motion_notify_event', self.onmove)
self.canvas.mpl_connect('draw_event', self.clear)
self.connect_event('motion_notify_event', self.onmove)
self.connect_event('draw_event', self.clear)

self.visible = True
self.horizOn = True
Expand Down Expand Up @@ -951,7 +960,6 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
self.direction = direction

self.visible = True
self.cids=[]

self.rect = None
self.background = None
Expand All @@ -967,10 +975,10 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
self.buttonDown = False
self.prev = (0, 0)

self.cids.append(self.canvas.mpl_connect('motion_notify_event', self.onmove))
self.cids.append(self.canvas.mpl_connect('button_press_event', self.press))
self.cids.append(self.canvas.mpl_connect('button_release_event', self.release))
self.cids.append(self.canvas.mpl_connect('draw_event', self.update_background))
self.connect_event('motion_notify_event', self.onmove)
self.connect_event('button_press_event', self.press)
self.connect_event('button_release_event', self.release)
self.connect_event('draw_event', self.update_background)

if self.direction == 'horizontal':
trans = blended_transform_factory(self.ax.transData, self.ax.transAxes)
Expand All @@ -989,15 +997,14 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
def new_axes(self,ax):
self.ax = ax
if self.canvas is not ax.figure.canvas:
for cid in self.cids:
self.canvas.mpl_disconnect(cid)
self.disconnect_events()

self.canvas = ax.figure.canvas
self.connect_event('motion_notify_event', self.onmove)
self.connect_event('button_press_event', self.press)
self.connect_event('button_release_event', self.release)
self.connect_event('draw_event', self.update_background)

self.cids.append(self.canvas.mpl_connect('motion_notify_event', self.onmove))
self.cids.append(self.canvas.mpl_connect('button_press_event', self.press))
self.cids.append(self.canvas.mpl_connect('button_release_event', self.release))
self.cids.append(self.canvas.mpl_connect('draw_event', self.update_background))
if self.direction == 'horizontal':
trans = blended_transform_factory(self.ax.transData, self.ax.transAxes)
w,h = 0,1
Expand Down Expand Up @@ -1193,10 +1200,10 @@ def __init__(self, ax, onselect, drawtype='box',
AxesWidget.__init__(self, ax)

self.visible = True
self.canvas.mpl_connect('motion_notify_event', self.onmove)
self.canvas.mpl_connect('button_press_event', self.press)
self.canvas.mpl_connect('button_release_event', self.release)
self.canvas.mpl_connect('draw_event', self.update_background)
self.connect_event('motion_notify_event', self.onmove)
self.connect_event('button_press_event', self.press)
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
Expand Down Expand Up @@ -1400,9 +1407,8 @@ def __init__(self, ax, xy, callback=None, useblit=True):
self.line = Line2D([x], [y], linestyle='-', color='black', lw=2)
self.ax.add_line(self.line)
self.callback = callback
self.cids = []
self.cids.append(self.canvas.mpl_connect('button_release_event', self.onrelease))
self.cids.append(self.canvas.mpl_connect('motion_notify_event', self.onmove))
self.connect_event('button_release_event', self.onrelease)
self.connect_event('motion_notify_event', self.onmove)

def onrelease(self, event):
if self.verts is not None:
Expand All @@ -1411,8 +1417,7 @@ def onrelease(self, event):
self.callback(self.verts)
self.ax.lines.remove(self.line)
self.verts = None
for cid in self.cids:
self.canvas.mpl_disconnect(cid)
self.disconnect_events()

def onmove(self, event):
if self.verts is None: return
Expand Down