1 """GNUmed wx.Timer proxy object.
2
3 @copyright: author(s)
4 """
5
6
7
8 __version__ = "$Revision: 1.13 $"
9 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
10 __licence__ = "GPL v2 or later (details at http://www.gnu.org)"
11
12
13 import logging
14
15
16 import wx
17
18
19 _log = logging.getLogger('gm.timers')
20 _timers = []
21
29
31 """wx.Timer proxy.
32
33 It would be quite useful to tune the delay
34 according to current network speed either at
35 application startup or even during runtime.
36 """
37 - def __init__(self, callback = None, delay = 300, cookie = None):
38 """Set up our timer with reasonable defaults.
39
40 - delay default is 300ms as per Richard Terry's experience
41 - delay should be tailored to network speed/user speed
42 - <cookie> is passed to <callback> when <delay> is up
43 """
44
45 if not callable(callback):
46 raise ValueError("[%s]: <callback> %s is not a callable()" % (self.__class__.__name__, callback))
47
48 if cookie is None:
49 self.cookie = id(self)
50 else:
51 self.cookie = cookie
52 self.__callback = callback
53 self.__delay = delay
54
55 wx.Timer.__init__(self)
56
57 _log.debug('setting up timer: cookie [%s], delay %sms', self.cookie, self.__delay)
58
59 global _timers
60 _timers.append(self)
61
62 - def Start(self, milliseconds=-1, oneShot=False):
63 if milliseconds == -1:
64 milliseconds = self.__delay
65 wx.Timer.Start(self, milliseconds=milliseconds, oneShot=oneShot)
66
68 self.__callback(self.cookie)
69
71 if cookie is None:
72 self.cookie = id(self)
73 else:
74 self.cookie = cookie
75
76 if __name__ == '__main__':
77 import time
78
79
81 print "timer <%s> fired" % cookie
82
85 print "setting up timer"
86 timer = cTimer(callback = cb_timer)
87 print "starting timer"
88 timer.Start()
89 return True
90
91 app = cApp(0)
92
93 app.MainLoop()
94 print "waiting 10 seconds for timer to trigger"
95 time.sleep(10)
96
97