STY: Refactor parts of LineTool to CanvasToolBase

This commit is contained in:
Tony S Yu
2012-11-15 22:04:02 -05:00
parent d219a76f58
commit 0d411e21dc
2 changed files with 20 additions and 19 deletions
+18 -1
View File
@@ -12,7 +12,7 @@ class CanvasToolBase(object):
Parameters
----------
"""
def __init__(self, ax, useblit=None):
def __init__(self, ax, useblit=None, on_update=None, on_enter=None):
self.ax = ax
self.canvas = ax.figure.canvas
self.cids = []
@@ -26,6 +26,14 @@ class CanvasToolBase(object):
self.canvas.draw()
self.img_background = self.canvas.copy_from_bbox(self.ax.bbox)
if on_update is None:
on_update = lambda *args: None
self.on_update = on_update
if on_enter is None:
on_enter = lambda *args: None
self.on_enter = on_enter
def connect_event(self, event, callback):
"""Connect callback with an event.
@@ -52,6 +60,15 @@ class CanvasToolBase(object):
for a in self._artists:
a.set_visible(val)
def redraw(self):
if self.useblit:
self.canvas.restore_region(self.img_background)
for artist in self._artists:
self.ax.draw_artist(artist)
self.canvas.blit(self.ax.bbox)
else:
self.canvas.draw_idle()
class ToolHandles(object):
"""Control handles for canvas tools.
+2 -18
View File
@@ -21,7 +21,8 @@ class LineTool(CanvasToolBase):
"""
def __init__(self, ax, x, y, on_update=None, on_enter=None, maxdist=10,
lineprops=None):
super(LineTool, self).__init__(ax)
super(LineTool, self).__init__(ax, on_update=on_update,
on_enter=on_enter)
props = dict(color='r', linewidth=1, alpha=0.4, solid_capstyle='butt')
props.update(lineprops if lineprops is not None else {})
@@ -29,14 +30,6 @@ class LineTool(CanvasToolBase):
self.maxdist = maxdist
self._active_pt = None
if on_update is None:
on_update = lambda *args: None
self.on_update = on_update
if on_enter is None:
on_enter = lambda *args: None
self.on_enter = on_enter
self._init_end_pts = np.transpose([x, y])
self.end_pts = self._init_end_pts.copy()
@@ -100,15 +93,6 @@ class LineTool(CanvasToolBase):
self.on_update(self.end_pts)
def redraw(self):
if self.useblit:
self.canvas.restore_region(self.img_background)
for artist in self._artists:
self.ax.draw_artist(artist)
self.canvas.blit(self.ax.bbox)
else:
self.canvas.draw_idle()
class ThickLineTool(LineTool):