Package qt_comm_channel :: Module commChannel
[hide private]
[frames] | no frames]

Source Code for Module qt_comm_channel.commChannel

  1  from python_qt_binding import QtCore, QtGui 
  2   
  3  from QtCore import Signal, QObject 
  4  from QtGui import QPushButton 
5 6 -class CommChannel(QObject):
7 ''' 8 Combines Qt signals into one place. Avoids error "... has no method 'emit'". 9 This is a static class. Usage: 10 - Subclass this class; without creating any methods, including __init__(). 11 - The only active part in the subclass is to define Qt signals. Example:: 12 13 class MySignals(CommChannel): 14 quitSig = QtCore.Signal(QPushButton) 15 taskDoneSig = QtCore.Signal() 16 17 - Before using the signals in your application, call registerSignals on CommChannel, 18 passing your subclass:: 19 20 CommChannel.registerSignals(MySignals) 21 22 - After that call, access your signals like this:: 23 24 CommChannel.getSignal('MySignals.quitSig') 25 26 Note that the signal ID is a string consisting of the name 27 of your subclass, followed by a dot, followed by the 28 signal class attribute's name. 29 30 - Other modules in the same application can access the signals the same way. 31 32 ''' 33 # Hold on to the CommChannel subclass instances that 34 # are created in registerSignals(), so that they are not 35 # GC'ed: 36 commInstances = []; 37 userSigs = {} 38 39 @staticmethod
40 - def registerSignals(signalsClass):
41 ''' 42 Add a new Qt Signal instance under a name. 43 @param signalsClass: name under which signal object is known, and can be retrieved. 44 @type signalsClass: string 45 ''' 46 if not issubclass(signalsClass, QObject): 47 raise ValueError("Class passed to registerSignals must be a subclass of CommChannel."); 48 userSignalClassVars = CommChannel.getSignalsFromClass(signalsClass) 49 signalsClassInst = signalsClass(); 50 CommChannel.commInstances.append(signalsClassInst); 51 for classVarName in userSignalClassVars: 52 try: 53 sigObj = getattr(signalsClassInst, classVarName); 54 except KeyError: 55 # Some signals will have been inherited from Object, 56 # such as the signal in class var 'destroyed'. Ignore 57 # those: 58 continue; 59 CommChannel.userSigs[signalsClass.__name__ + '.' + classVarName] = sigObj;
60 61 62 @staticmethod
63 - def getSignal(sigName):
64 ''' 65 Retrieve a previously added signal. Alternatively, 66 use dict style syntaxs: commInstance[sigName]. 67 @param sigName: name under which signal was registered. 68 @type sigName: string 69 @return: signal object, or None, if signal of given name was not registered. 70 @rtype: {QtCore.Signal | None} 71 ''' 72 return CommChannel.userSigs[sigName];
73 74 @staticmethod
75 - def __getitem__(sigName):
76 return CommChannel.userSigs[sigName];
77 78 @staticmethod
79 - def registeredSignals():
80 ''' 81 Return list of all registered signal objects. 82 ''' 83 return CommChannel.userSigs.values();
84 85 @staticmethod
87 ''' 88 Return list of all registered signal registration names. 89 ''' 90 return CommChannel.userSigs.keys();
91 92 93 # ----------------------------------------- Private --------------------------- 94 95 @staticmethod
96 - def getSignalsFromClass(cls):
97 #base_attrs = dir(type('dummy', (object,), {})) 98 this_cls_attrs = dir(cls) 99 res = [] 100 for attr in this_cls_attrs: 101 if type(getattr(cls,attr)) != Signal: 102 continue; 103 res += [attr] 104 return res
105 106 if __name__ == '__main__': 107 108 ch = CommChannel.getInstance() 109 ch.registerSignal('sig1', 'sigObj1'); 110 print(ch['sig1']) 111