Changeset 841


Ignore:
Timestamp:
Apr 27, 2012 5:49:44 PM (10 years ago)
Author:
jemian
Message:

now we have a way to finish building the pv.py example, the CaQLabel widget is pretty good now

Location:
moxy/trunk/src/moxy/qtlib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • moxy/trunk/src/moxy/qtlib/CaQLabel.py

    r840 r841  
    2020import PySide.QtGui
    2121import tools
     22import pprint           #@UnusedImport
    2223
    2324class CaQLabel(PySide.QtGui.QLabel):
     
    2728
    2829    def __init__(self, text=''):
     30        '''
     31        :param str text: initial Label text (really, we can ignore this)
     32        '''
    2933        self.text = text
    3034        PySide.QtGui.QLabel.__init__(self, text)
    3135
     36        # define the signals we'll use in the camonitor handler to update the GUI
    3237        self.labelSignal = tools.CaQSignalDef()
    3338        self.labelSignal.newBgColor.connect(self.SetBackgroundColor)
     
    3540       
    3641        self.states = tools.enum('DISCONNECTED', 'CONNECTED', 'ALARM')
    37         self.clut = {
    38             self.states.DISCONNECTED:     "#ff0000",      # red
    39             self.states.CONNECTED:        "#00ff00",      # green
    40             self.states.ALARM:            "#aaaaff",      # blue
     42        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
    4146        }
    4247
    4348        self.pv = None
     49        self.ca_callback = None
     50        self.ca_connect_callback = None
    4451        self.state = self.states.DISCONNECTED
    4552        self.SetBackgroundColor()
    4653
    47     def connect(self, pvname):
     54    def connect(self, pvname, ca_callback = None, ca_connect_callback = None):
    4855        '''
    4956        Connect this label with the EPICS pvname
    5057       
    5158        :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
    5261        '''
    5362        if self.pv is not None:
    5463            self.disconnect()
    5564        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)
    5770            self.state = self.states.CONNECTED
     71            self.setToolTip(pvname)
    5872   
    5973    def disconnect(self):
     
    6377        if self.pv is not None:
    6478            self.pv.remove_callback()
     79            pvname = self.pv.pvname
    6580            self.pv.disconnect()
    6681            self.pv = None
     82            self.ca_callback = None
     83            self.ca_connect_callback = None
    6784            self.state = self.states.DISCONNECTED
    6885            self.text = ''
    6986            self.SetText()
    7087            self.SetBackgroundColor()
     88            self.setToolTip(pvname + ' not connected')
    7189
    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):
    73110        '''
    74111        respond to a PyEpics camonitor() event
    75112        '''
    76113        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)
    84118
    85119    def SetText(self, *args, **kw):
     
    94128
    95129
     130#------------------------------------------------------------------
     131
     132
     133class 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
    96177if __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  
    6464
    6565        self.pvname.returnPressed.connect(self.onPVNameReturn)
    66         self.pv = None
    6766
    6867        grid = PySide.QtGui.QGridLayout()
     
    7877
    7978    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']
    8188
    8289
  • moxy/trunk/src/moxy/qtlib/tools.py

    r840 r841  
    3434class CaQSignalDef(PySide.QtCore.QObject):
    3535    '''
    36     Define signals used to communicate between the PyEpics
     36    Define the signals used to communicate between the PyEpics
    3737    thread and the PySide (main Qt4 GUI) thread.
    3838    '''
     
    4343    # Use events.  Look at other's code.
    4444   
     45    newFgColor = PySide.QtCore.Signal()
    4546    newBgColor = PySide.QtCore.Signal()
    4647    newText    = PySide.QtCore.Signal()
     
    5455    typesafe enum
    5556   
    56     Used like so::
     57    EXAMPLE::
    5758
    5859        >>> Numbers = enum('ZERO', 'ONE', 'TWO', four='IV')
Note: See TracChangeset for help on using the changeset viewer.