From: <jd...@us...> - 2008-06-23 21:39:23
|
Revision: 5652 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5652&view=rev Author: jdh2358 Date: 2008-06-23 14:39:11 -0700 (Mon, 23 Jun 2008) Log Message: ----------- draft idle/timeout api Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/afm.py trunk/matplotlib/lib/matplotlib/backend_bases.py trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py Added Paths: ----------- trunk/matplotlib/examples/event_handling/idle_and_timeout.py Added: trunk/matplotlib/examples/event_handling/idle_and_timeout.py =================================================================== --- trunk/matplotlib/examples/event_handling/idle_and_timeout.py (rev 0) +++ trunk/matplotlib/examples/event_handling/idle_and_timeout.py 2008-06-23 21:39:11 UTC (rev 5652) @@ -0,0 +1,32 @@ +""" +Demonstrate/test the idle and timeout API +""" +import matplotlib.pyplot as plt + +fig = plt.figure() +ax = fig.add_subplot(111) + +ax.plot(range(10)) + +def on_idle(canvas): + on_idle.count +=1 + print 'idle', on_idle.count + if on_idle.count==10: + canvas.mpl_source_remove(on_idle) + return True +on_idle.count = 0 + +def on_timeout(canvas): + on_timeout.count +=1 + print 'timeout', on_timeout.count + if on_timeout.count==10: + canvas.mpl_source_remove(on_timeout) + return True +on_timeout.count = 0 + +fig.canvas.mpl_idle_add(on_idle) +fig.canvas.mpl_timeout_add(100, on_timeout) + +plt.show() + + Modified: trunk/matplotlib/lib/matplotlib/afm.py =================================================================== --- trunk/matplotlib/lib/matplotlib/afm.py 2008-06-23 18:15:01 UTC (rev 5651) +++ trunk/matplotlib/lib/matplotlib/afm.py 2008-06-23 21:39:11 UTC (rev 5652) @@ -11,7 +11,7 @@ 3) Did more than I needed and it was easier to write my own than figure out how to just get what I needed from theirs -It is pretty easy to use, and requires only built-in python libs: +It is pretty easy to use, and requires only built-in python libs:: >>> from afm import AFM >>> fh = file('ptmr8a.afm') Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backend_bases.py 2008-06-23 18:15:01 UTC (rev 5651) +++ trunk/matplotlib/lib/matplotlib/backend_bases.py 2008-06-23 21:39:11 UTC (rev 5652) @@ -1316,6 +1316,33 @@ newCanvas = FigureCanvasClass(self.figure) return newCanvas + def mpl_idle_add(self, func, *args, **kwargs): + """ + add func to idle handler. The signature of func is:: + + func(canvas, *args, **kwargs) + + use :meth:`mpl_source_remove` to remove func from the idle handler. + """ + raise NotImplementedError('GUI backend must override') + + def mpl_timeout_add(self, millisec, func, *args, **kwargs): + """ + add func to timeout handler; func will be called every + millisec. The signature of func is:: + + func(canvas, *args, **kwargs) + + use :meth:`mpl_source_remove` to remove func from the timeout handler. + """ + raise NotImplementedError('GUI backend must override') + + def mpl_source_remove(self, func): + """ + remove func from idle or timeout handler + """ + raise NotImplementedError('GUI backend must override') + def mpl_connect(self, s, func): """ Connect event with string *s* to *func*. The signature of *func* is:: Modified: trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2008-06-23 18:15:01 UTC (rev 5651) +++ trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2008-06-23 21:39:11 UTC (rev 5652) @@ -172,6 +172,8 @@ self.set_flags(gtk.CAN_FOCUS) self._renderer_init() + # maps idle/timeout func -> id, remove_func for later removal + self.sourced = dict() def scroll_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() @@ -394,6 +396,53 @@ gtk.gdk.flush() gtk.gdk.threads_leave() + + def mpl_idle_add(self, func, *args, **kwargs): + """ + add func to idle handler. The signature of func is:: + + func(canvas, *args, **kwargs) + + use :meth:`mpl_source_remove` to remove func from the idle handler. + """ + idle_add = getattr(gobject, 'idle_add', getattr(gtk, 'idle_add')) + remove = getattr(gobject, 'source_remove', getattr(gtk, 'idle_remove')) + + def wrap(): + b = func(self, *args, **kwargs) + return True + + id = idle_add(wrap) + self.sourced[func] = id, remove + + + def mpl_timeout_add(self, millisec, func, *args, **kwargs): + """ + add func to timeout handler; func will be called every + millisec. The signature of func is:: + + func(canvas, *args, **kwargs) + + use :meth:`mpl_source_remove` to remove func from the timeout handler. + """ + timeout_add = getattr(gobject, 'timeout_add', getattr(gtk, 'timeout_add')) + remove = getattr(gobject, 'source_remove', getattr(gtk, 'timeout_remove')) + def wrap(): + b = func(self, *args, **kwargs) + return True + + id = timeout_add(millisec, wrap) + self.sourced[func] = id, remove + + + def mpl_source_remove(self, func): + """ + remove func from idle or timeout handler + """ + id, remove = self.sourced[func] + remove(id) + del self.sourced[func] + class FigureManagerGTK(FigureManagerBase): """ Public attributes This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |