File: pvprobe_qt.py

package info (click to toggle)
python-pyepics 3.5.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,336 kB
  • sloc: python: 10,539; makefile: 112; javascript: 104; sh: 53
file content (86 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

import epics
import sys
try:
    from PySide.QtGui import QWidget, QLabel, QLineEdit, QGridLayout, QApplication
except:
    from PyQt4.QtGui import QWidget, QLabel, QLineEdit, QGridLayout, QApplication

from epics.utils import bytes2str

class PVText(QLabel):
    def __init__(self, pvname,  **kws):
        QLabel.__init__(self, '', **kws)
        self.pv = None
        self.cb_index = None

    def SetPV(self, pvname):
        if self.pv is not None and self.cb_index is not None:
            self.pv.remove_callback(self.cb_index)

        self.pv = epics.PV(bytes2str(pvname))
        self.setText(self.pv.get(as_string=True))
        self.cb_index = self.pv.add_callback(self.onPVChange)

    def onPVChange(self, pvname=None, char_value=None, **kws):
        self.setText(char_value)

class PVLineEdit(QLineEdit):
    def __init__(self, pvname=None,  **kws):
        QLineEdit.__init__(self, **kws)
        self.returnPressed.connect(self.onReturn)
        self.pv = None
        self.cb_index = None
        if pvname is not None:
            self.SetPV(pvname)

    def SetPV(self, pvname):
        if self.pv is not None and self.cb_index is not None:
            self.pv.remove_callback(self.cb_index)

        self.pv = epics.PV(bytes2str(pvname))
        self.cb_index = self.pv.add_callback(self.onPVChange)

    def onPVChange(self, pvname=None, char_value=None, **kws):
        self.setText(char_value)

    def onReturn(self):
        self.pv.put(bytes2str(self.text()))

class PVProbe(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setWindowTitle("PyQt4 PV Probe:")

        self.pv1name = QLineEdit()
        self.pv2name = QLineEdit()
        self.value   = PVText(None)
        self.pvedit  = PVLineEdit()

        grid = QGridLayout()
        grid.addWidget(QLabel("PV1 Name (Read-only):"),   0, 0)
        grid.addWidget(QLabel("PV1 Value (Read-only):"),  1, 0)
        grid.addWidget(QLabel("PV2 Name: (Read-write):"), 2, 0)
        grid.addWidget(QLabel("PV2 Value (Read-write):"), 3, 0)
        grid.addWidget(self.pv1name,  0, 1)
        grid.addWidget(self.value,    1, 1)
        grid.addWidget(self.pv2name,  2, 1)
        grid.addWidget(self.pvedit,   3, 1)

        self.pv1name.returnPressed.connect(self.onPV1NameReturn)
        self.pv2name.returnPressed.connect(self.onPV2NameReturn)

        self.setLayout(grid)

    def onPV1NameReturn(self):
        self.value.SetPV(self.pv1name.text())

    def onPV2NameReturn(self):
        self.pvedit.SetPV(self.pv2name.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    probe = PVProbe()
    probe.show()
    sys.exit(app.exec_())