Revision: 8105 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8105&view=rev Author: leejjoon Date: 2010-01-29 18:03:13 +0000 (Fri, 29 Jan 2010)
Log Message: ----------- draggable legend now optionally blitted Modified Paths: -------------- trunk/matplotlib/examples/animation/draggable_legend.py trunk/matplotlib/lib/matplotlib/legend.py trunk/matplotlib/lib/matplotlib/offsetbox.py trunk/matplotlib/lib/matplotlib/text.py Modified: trunk/matplotlib/examples/animation/draggable_legend.py =================================================================== --- trunk/matplotlib/examples/animation/draggable_legend.py 2010-01-29 17:33:34 UTC (rev 8104) +++ trunk/matplotlib/examples/animation/draggable_legend.py 2010-01-29 18:03:13 UTC (rev 8105) @@ -37,7 +37,7 @@ ax.add_artist(ab) -d3 = ab.draggable() +d3 = ab.draggable(use_blit=True) plt.show() Modified: trunk/matplotlib/lib/matplotlib/legend.py =================================================================== --- trunk/matplotlib/lib/matplotlib/legend.py 2010-01-29 17:33:34 UTC (rev 8104) +++ trunk/matplotlib/lib/matplotlib/legend.py 2010-01-29 18:03:13 UTC (rev 8105) @@ -39,9 +39,10 @@ class DraggableLegend(DraggableOffsetBox): - def __init__(self, legend): + def __init__(self, legend, use_blit=False): self.legend=legend - DraggableOffsetBox.__init__(self, legend, legend._legend_box) + DraggableOffsetBox.__init__(self, legend, legend._legend_box, + use_blit=use_blit) def artist_picker(self, legend, evt): return self.legend.legendPatch.contains(evt) @@ -917,7 +918,7 @@ return ox, oy - def draggable(self, state=None): + def draggable(self, state=None, use_blit=False): """ Set the draggable state -- if state is @@ -939,7 +940,7 @@ if state: if self._draggable is None: - self._draggable = DraggableLegend(self) + self._draggable = DraggableLegend(self, use_blit) else: if self._draggable is not None: self._draggable.disconnect() Modified: trunk/matplotlib/lib/matplotlib/offsetbox.py =================================================================== --- trunk/matplotlib/lib/matplotlib/offsetbox.py 2010-01-29 17:33:34 UTC (rev 8104) +++ trunk/matplotlib/lib/matplotlib/offsetbox.py 2010-01-29 18:03:13 UTC (rev 8105) @@ -1416,10 +1416,11 @@ the normalized axes coordinate and set a relavant attribute. """ - def __init__(self, ref_artist): + def __init__(self, ref_artist, use_blit=False): self.ref_artist = ref_artist self.got_artist = False - + self._use_blit = use_blit + self.canvas = self.ref_artist.figure.canvas c2 = self.canvas.mpl_connect('pick_event', self.on_pick) c3 = self.canvas.mpl_connect('button_release_event', self.on_release) @@ -1432,16 +1433,34 @@ dx = evt.x - self.mouse_x dy = evt.y - self.mouse_y self.update_offset(dx, dy) + self.canvas.draw() + def on_motion_blit(self, evt): + if self.got_artist: + dx = evt.x - self.mouse_x + dy = evt.y - self.mouse_y + self.update_offset(dx, dy) + self.canvas.restore_region(self.background) + self.ref_artist.draw(self.ref_artist.figure._cachedRenderer) + self.canvas.blit(self.ref_artist.figure.bbox) + def on_pick(self, evt): if evt.artist == self.ref_artist: - self.save_offset() self.mouse_x = evt.mouseevent.x self.mouse_y = evt.mouseevent.y self.got_artist = True - self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion) + if self._use_blit: + self.ref_artist.set_animated(True) + self.canvas.draw() + self.background = self.canvas.copy_from_bbox(self.ref_artist.figure.bbox) + self.ref_artist.draw(self.ref_artist.figure._cachedRenderer) + self.canvas.blit(self.ref_artist.figure.bbox) + self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion_blit) + else: + self._c1 = self.canvas.mpl_connect('motion_notify_event', self.on_motion) + self.save_offset() def on_release(self, event): if self.got_artist: @@ -1449,6 +1468,9 @@ self.got_artist = False self.canvas.mpl_disconnect(self._c1) + if self._use_blit: + self.ref_artist.set_animated(False) + def disconnect(self): 'disconnect the callbacks' for cid in self.cids: @@ -1468,8 +1490,8 @@ class DraggableOffsetBox(DraggableBase): - def __init__(self, ref_artist, offsetbox): - DraggableBase.__init__(self, ref_artist) + def __init__(self, ref_artist, offsetbox, use_blit=False): + DraggableBase.__init__(self, ref_artist, use_blit=use_blit) self.offsetbox = offsetbox def save_offset(self): @@ -1482,7 +1504,6 @@ def update_offset(self, dx, dy): loc_in_canvas = self.offsetbox_x + dx, self.offsetbox_y + dy self.offsetbox.set_offset(loc_in_canvas) - self.offsetbox.figure.canvas.draw() def get_loc_in_canvas(self): @@ -1496,8 +1517,8 @@ class DraggableAnnotation(DraggableBase): - def __init__(self, annotation): - DraggableBase.__init__(self, annotation) + def __init__(self, annotation, use_blit=False): + DraggableBase.__init__(self, annotation, use_blit=use_blit) self.annotation = annotation def save_offset(self): @@ -1519,7 +1540,6 @@ ann.xytext = self.ox + dx, self.oy + dy x, y = ann.xytext xy = ann._get_xy(x, y, ann.textcoords) - self.canvas.draw() def finalize_offset(self): loc_in_canvas = self.annotation.xytext Modified: trunk/matplotlib/lib/matplotlib/text.py =================================================================== --- trunk/matplotlib/lib/matplotlib/text.py 2010-01-29 17:33:34 UTC (rev 8104) +++ trunk/matplotlib/lib/matplotlib/text.py 2010-01-29 18:03:13 UTC (rev 8105) @@ -1537,7 +1537,7 @@ return True - def draggable(self, state=None): + def draggable(self, state=None, use_blit=False): """ Set the draggable state -- if state is @@ -1560,7 +1560,7 @@ if state: if self._draggable is None: - self._draggable = DraggableAnnotation(self) + self._draggable = DraggableAnnotation(self, use_blit) else: if self._draggable is not None: self._draggable.disconnect() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ The Planet: dedicated and managed hosting, cloud storage, colocation Stay online with enterprise data centers and the best network in the business Choose flexible plans and management services without long-term contracts Personal 24x7 support from experience hosting pros just a phone call away. http://p.sf.net/sfu/theplanet-com _______________________________________________ Matplotlib-checkins mailing list Matplotlib-checkins@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins