Package vqt :: Module main
[hide private]
[frames] | no frames]

Source Code for Module vqt.main

 1  import sys 
 2  from Queue import Queue 
 3  from threading import currentThread 
 4  
 
 5  from PyQt4 import QtCore, QtGui 
 6  
 
7 -def idlethread(func):
8 ''' 9 A decorator which causes the function to be called by the qt 10 main thread rather than synchronously... 11 12 NOTE: This makes the call async handled by the qt main 13 loop code. you can NOT return anything. 14 ''' 15 def idleadd(*args, **kwargs): 16 if currentThread().getName() == 'QtThread': 17 return func(*args, **kwargs) 18 19 return qapp.proxyOneCall(func, *args, **kwargs)
20 21 return idleadd 22
23 -def idlethreadsync(func):
24 ''' 25 Similar to idlethread except that it is synchronous and able 26 to return values. 27 ''' 28 q = Queue() 29 def dowork(*args, **kwargs): 30 try: 31 q.put(func(*args, **kwargs)) 32 except Exception, e: 33 q.put(e)
34 35 def idleadd(*args, **kwargs): 36 if currentThread().getName() == 'QtThread': 37 return func(*args, **kwargs) 38 qapp.proxyOneCall(dowork, *args, **kwargs) 39 return q.get() 40 41 return idleadd 42
43 -class VQApplication(QtGui.QApplication):
44 45 proxyCall = QtCore.pyqtSignal(name='proxyCall') 46
47 - def __init__(self, *args, **kwargs):
48 QtGui.QApplication.__init__(self, *args, **kwargs) 49 self.proxyCall.connect(self._proxyCaller) 50 self.call_proxy_queue = Queue()
51
52 - def _proxyCaller(self):
53 callable, args, kwargs = self.call_proxy_queue.get() 54 return callable(*args, **kwargs)
55
56 - def proxyOneCall(self, callable, *args, **kwargs):
57 self.call_proxy_queue.put((callable,args,kwargs)) 58 self.proxyCall.emit()
59
60 -def startup(css=None):
61 global qapp 62 qapp = VQApplication(sys.argv) 63 if css: 64 qapp.setStyleSheet( css )
65
66 -def main():
67 global qapp 68 currentThread().setName('QtThread') 69 sys.exit(qapp.exec_())
70
71 -def eatevents():
72 qapp.processEvents()
73