Changeset 841
- Timestamp:
- Apr 27, 2012 5:49:44 PM (12 years ago)
- Location:
- moxy/trunk/src/moxy/qtlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
moxy/trunk/src/moxy/qtlib/CaQLabel.py
r840 r841 20 20 import PySide.QtGui 21 21 import tools 22 import pprint #@UnusedImport 22 23 23 24 class CaQLabel(PySide.QtGui.QLabel): … … 27 28 28 29 def __init__(self, text=''): 30 ''' 31 :param str text: initial Label text (really, we can ignore this) 32 ''' 29 33 self.text = text 30 34 PySide.QtGui.QLabel.__init__(self, text) 31 35 36 # define the signals we'll use in the camonitor handler to update the GUI 32 37 self.labelSignal = tools.CaQSignalDef() 33 38 self.labelSignal.newBgColor.connect(self.SetBackgroundColor) … … 35 40 36 41 self.states = tools.enum('DISCONNECTED', 'CONNECTED', 'ALARM') 37 self.clut = { 38 self.states.DISCONNECTED: "#ff 0000", # red39 self.states.CONNECTED: "# 00ff00", # green40 self.states.ALARM: "# aaaaff", # blue42 self.clut = { # clut: Color LookUp Table 43 self.states.DISCONNECTED: "#ffffff", # white 44 self.states.CONNECTED: "#e0e0e0", # a bit darker than default #f0f0f0 45 self.states.ALARM: "#ff0000", # red 41 46 } 42 47 43 48 self.pv = None 49 self.ca_callback = None 50 self.ca_connect_callback = None 44 51 self.state = self.states.DISCONNECTED 45 52 self.SetBackgroundColor() 46 53 47 def connect(self, pvname ):54 def connect(self, pvname, ca_callback = None, ca_connect_callback = None): 48 55 ''' 49 56 Connect this label with the EPICS pvname 50 57 51 58 :param str pvname: EPICS Process Variable name 59 :param obj ca_callback: EPICS CA callback handler method 60 :param obj ca_connect_callback: EPICS CA connection state callback handler method 52 61 ''' 53 62 if self.pv is not None: 54 63 self.disconnect() 55 64 if len(pvname) > 0: 56 self.pv = epics.PV(pvname, callback=self.onPVChange) 65 self.ca_callback = ca_callback 66 self.ca_connect_callback = ca_connect_callback 67 self.pv = epics.PV(pvname, 68 callback=self.onPVChange, 69 connection_callback=self.onPVConnect) 57 70 self.state = self.states.CONNECTED 71 self.setToolTip(pvname) 58 72 59 73 def disconnect(self): … … 63 77 if self.pv is not None: 64 78 self.pv.remove_callback() 79 pvname = self.pv.pvname 65 80 self.pv.disconnect() 66 81 self.pv = None 82 self.ca_callback = None 83 self.ca_connect_callback = None 67 84 self.state = self.states.DISCONNECTED 68 85 self.text = '' 69 86 self.SetText() 70 87 self.SetBackgroundColor() 88 self.setToolTip(pvname + ' not connected') 71 89 72 def onPVChange(self, pvname=None, char_value=None, **kws): 90 def onPVConnect(self, *args, **kw): 91 ''' 92 respond to a PyEpics CA connection event 93 ''' 94 conn = kw['conn'] 95 self.text = { # adjust the text 96 False: '', #'disconnected', 97 True: 'connected', 98 }[conn] 99 self.labelSignal.newText.emit() # threadsafe update of the widget 100 self.state = { # adjust the state 101 False: self.states.DISCONNECTED, 102 True: self.states.CONNECTED, 103 }[conn] 104 self.labelSignal.newBgColor.emit() # threadsafe update of the widget 105 if self.ca_connect_callback is not None: 106 # caller wants to be notified of this camonitor event 107 self.ca_connect_callback(**kw) 108 109 def onPVChange(self, pvname=None, char_value=None, **kw): 73 110 ''' 74 111 respond to a PyEpics camonitor() event 75 112 ''' 76 113 self.text = char_value 77 self.state = { # toggle the state, if connected, as a demo 78 self.states.DISCONNECTED: self.states.DISCONNECTED, 79 self.states.CONNECTED: self.states.ALARM, 80 self.states.ALARM: self.states.CONNECTED, 81 }[self.state] 82 self.labelSignal.newText.emit() # threadsafe call 83 self.labelSignal.newBgColor.emit() # threadsafe call 114 self.labelSignal.newText.emit() # threadsafe update of the widget 115 if self.ca_callback is not None: 116 # caller wants to be notified of this camonitor event 117 self.ca_callback(pvname=pvname, char_value=char_value, **kw) 84 118 85 119 def SetText(self, *args, **kw): … … 94 128 95 129 130 #------------------------------------------------------------------ 131 132 133 class DemoView(PySide.QtGui.QWidget): 134 ''' 135 Show an EPICS PV connection. 136 Allow it to connect and disconnect. 137 This is a variation of EPICS PV Probe. 138 ''' 139 140 def __init__(self, parent=None, pvname=''): 141 PySide.QtGui.QWidget.__init__(self, parent) 142 self.value = CaQLabel(pvname) 143 144 grid = PySide.QtGui.QGridLayout() 145 grid.addWidget(self.value, 0,0) 146 self.setLayout(grid) 147 148 self.sig = tools.CaQSignalDef() 149 self.sig.newBgColor.connect(self.SetBackgroundColor) 150 self.toggle = False 151 152 self.setWindowTitle("Demo CaQLabel") 153 if len(pvname)>0: 154 self.connect(pvname) 155 156 def connect(self, pvname): 157 self.value.connect(pvname, ca_callback=self.callback) 158 159 def callback(self, *args, **kw): 160 self.sig.newBgColor.emit() # threadsafe update of the widget 161 162 def SetBackgroundColor(self, *args, **kw): 163 '''toggle the background color of self.value via its stylesheet''' 164 self.toggle = not self.toggle 165 color = { 166 False: "#ccc333", 167 True: "#cccccc", 168 }[self.toggle] 169 bgStyle = "QFrame {background-color: %s}" % color 170 self.value.setStyleSheet(bgStyle) 171 172 173 174 #------------------------------------------------------------------ 175 176 96 177 if __name__ == '__main__': 97 print "tba" 178 app = PySide.QtGui.QApplication(sys.argv) 179 _demo = DemoView(pvname='prj:datetime') 180 _demo.show() 181 sys.exit(app.exec_()) -
moxy/trunk/src/moxy/qtlib/pv.py
r840 r841 64 64 65 65 self.pvname.returnPressed.connect(self.onPVNameReturn) 66 self.pv = None67 66 68 67 grid = PySide.QtGui.QGridLayout() … … 78 77 79 78 def onPVNameReturn(self): 80 self.value.connect(self.pvname.text()) 79 self.value.connect(self.pvname.text(), 80 ca_callback=self.pv_update, 81 ca_connect_callback=self.connect_update) 82 83 def connect_update(self, **kw): 84 print kw['conn'] 85 86 def pv_update(self, **kw): 87 print kw['char_value'], kw['timestamp'] 81 88 82 89 -
moxy/trunk/src/moxy/qtlib/tools.py
r840 r841 34 34 class CaQSignalDef(PySide.QtCore.QObject): 35 35 ''' 36 Define signals used to communicate between the PyEpics36 Define the signals used to communicate between the PyEpics 37 37 thread and the PySide (main Qt4 GUI) thread. 38 38 ''' … … 43 43 # Use events. Look at other's code. 44 44 45 newFgColor = PySide.QtCore.Signal() 45 46 newBgColor = PySide.QtCore.Signal() 46 47 newText = PySide.QtCore.Signal() … … 54 55 typesafe enum 55 56 56 Used like so::57 EXAMPLE:: 57 58 58 59 >>> Numbers = enum('ZERO', 'ONE', 'TWO', four='IV')
Note: See TracChangeset
for help on using the changeset viewer.