| 1 |
from PyQt4 import QtGui, QtCore |
|---|
| 2 |
|
|---|
| 3 |
class ElexolPort(QtGui.QGroupBox): |
|---|
| 4 |
DEFAULT = 0 |
|---|
| 5 |
DIRECTION = 1 |
|---|
| 6 |
ONOFF = 2 |
|---|
| 7 |
THRESHOLD = 3 |
|---|
| 8 |
def __init__(self, parent=None, style = DEFAULT): |
|---|
| 9 |
QtGui.QGroupBox.__init__(self, parent) |
|---|
| 10 |
|
|---|
| 11 |
self.mInverted = False |
|---|
| 12 |
(on_pix, off_pix) = self.buildPixmaps(style) |
|---|
| 13 |
self.mPin0 = ElexolPin(on_pix, off_pix, self) |
|---|
| 14 |
self.mPin1 = ElexolPin(on_pix, off_pix, self) |
|---|
| 15 |
self.mPin2 = ElexolPin(on_pix, off_pix, self) |
|---|
| 16 |
self.mPin3 = ElexolPin(on_pix, off_pix, self) |
|---|
| 17 |
self.mPin4 = ElexolPin(on_pix, off_pix, self) |
|---|
| 18 |
self.mPin5 = ElexolPin(on_pix, off_pix, self) |
|---|
| 19 |
self.mPin6 = ElexolPin(on_pix, off_pix, self) |
|---|
| 20 |
self.mPin7 = ElexolPin(on_pix, off_pix, self) |
|---|
| 21 |
|
|---|
| 22 |
self.mLbl0 = QtGui.QLabel('0') |
|---|
| 23 |
self.mLbl1 = QtGui.QLabel('1') |
|---|
| 24 |
self.mLbl2 = QtGui.QLabel('2') |
|---|
| 25 |
self.mLbl3 = QtGui.QLabel('3') |
|---|
| 26 |
self.mLbl4 = QtGui.QLabel('4') |
|---|
| 27 |
self.mLbl5 = QtGui.QLabel('5') |
|---|
| 28 |
self.mLbl6 = QtGui.QLabel('6') |
|---|
| 29 |
self.mLbl7 = QtGui.QLabel('7') |
|---|
| 30 |
|
|---|
| 31 |
self.mLbl0.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 32 |
self.mLbl1.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 33 |
self.mLbl2.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 34 |
self.mLbl3.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 35 |
self.mLbl4.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 36 |
self.mLbl5.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 37 |
self.mLbl6.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 38 |
self.mLbl7.setAlignment(QtCore.Qt.AlignCenter) |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Policy(1),QtGui.QSizePolicy.Policy(1)) |
|---|
| 51 |
sizePolicy.setHorizontalStretch(0) |
|---|
| 52 |
sizePolicy.setVerticalStretch(0) |
|---|
| 53 |
|
|---|
| 54 |
self.mPin0.setSizePolicy(sizePolicy) |
|---|
| 55 |
self.mPin1.setSizePolicy(sizePolicy) |
|---|
| 56 |
self.mPin2.setSizePolicy(sizePolicy) |
|---|
| 57 |
self.mPin3.setSizePolicy(sizePolicy) |
|---|
| 58 |
self.mPin4.setSizePolicy(sizePolicy) |
|---|
| 59 |
self.mPin5.setSizePolicy(sizePolicy) |
|---|
| 60 |
self.mPin6.setSizePolicy(sizePolicy) |
|---|
| 61 |
self.mPin7.setSizePolicy(sizePolicy) |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
self.gridlayout = QtGui.QGridLayout(self) |
|---|
| 65 |
self.gridlayout.setMargin(9) |
|---|
| 66 |
self.gridlayout.setSpacing(6) |
|---|
| 67 |
|
|---|
| 68 |
self.gridlayout.addWidget(self.mLbl0,0,0,1,1) |
|---|
| 69 |
self.gridlayout.addWidget(self.mLbl1,0,1,1,1) |
|---|
| 70 |
self.gridlayout.addWidget(self.mLbl2,0,2,1,1) |
|---|
| 71 |
self.gridlayout.addWidget(self.mLbl3,0,3,1,1) |
|---|
| 72 |
self.gridlayout.addWidget(self.mPin0,1,0,1,1) |
|---|
| 73 |
self.gridlayout.addWidget(self.mPin1,1,1,1,1) |
|---|
| 74 |
self.gridlayout.addWidget(self.mPin2,1,2,1,1) |
|---|
| 75 |
self.gridlayout.addWidget(self.mPin3,1,3,1,1) |
|---|
| 76 |
self.gridlayout.addWidget(self.mLbl4,2,0,1,1) |
|---|
| 77 |
self.gridlayout.addWidget(self.mLbl5,2,1,1,1) |
|---|
| 78 |
self.gridlayout.addWidget(self.mLbl6,2,2,1,1) |
|---|
| 79 |
self.gridlayout.addWidget(self.mLbl7,2,3,1,1) |
|---|
| 80 |
self.gridlayout.addWidget(self.mPin4,3,0,1,1) |
|---|
| 81 |
self.gridlayout.addWidget(self.mPin5,3,1,1,1) |
|---|
| 82 |
self.gridlayout.addWidget(self.mPin6,3,2,1,1) |
|---|
| 83 |
self.gridlayout.addWidget(self.mPin7,3,3,1,1) |
|---|
| 84 |
|
|---|
| 85 |
self.connect(self.mPin0, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 86 |
self.connect(self.mPin1, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 87 |
self.connect(self.mPin2, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 88 |
self.connect(self.mPin3, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 89 |
self.connect(self.mPin4, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 90 |
self.connect(self.mPin5, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 91 |
self.connect(self.mPin6, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 92 |
self.connect(self.mPin7, QtCore.SIGNAL("clicked(bool)"), self.onClicked) |
|---|
| 93 |
|
|---|
| 94 |
def buildPixmaps(self, style): |
|---|
| 95 |
|
|---|
| 96 |
if ElexolPort.DIRECTION == style: |
|---|
| 97 |
highText = "In" |
|---|
| 98 |
lowText = "Out" |
|---|
| 99 |
highColor = QtGui.QColor(0, 200, 0, 200) |
|---|
| 100 |
lowColor = QtGui.QColor(200, 0, 0, 200) |
|---|
| 101 |
elif ElexolPort.ONOFF == style: |
|---|
| 102 |
highText = "On" |
|---|
| 103 |
lowText = "Off" |
|---|
| 104 |
highColor = QtGui.QColor(0, 200, 0, 200) |
|---|
| 105 |
lowColor = QtGui.QColor(200, 0, 0, 200) |
|---|
| 106 |
elif ElexolPort.THRESHOLD == style: |
|---|
| 107 |
highText = "2.5V" |
|---|
| 108 |
lowText = "1.4V" |
|---|
| 109 |
highColor = QtGui.QColor(0, 200, 0, 200) |
|---|
| 110 |
lowColor = QtGui.QColor(200, 0, 0, 200) |
|---|
| 111 |
else: |
|---|
| 112 |
highText = "On" |
|---|
| 113 |
lowText = "Off" |
|---|
| 114 |
highColor = QtGui.QColor(0, 200, 0, 200) |
|---|
| 115 |
lowColor = QtGui.QColor(200, 0, 0, 200) |
|---|
| 116 |
|
|---|
| 117 |
fm = QtGui.QFontMetrics(self.font()) |
|---|
| 118 |
on_size = fm.size(QtCore.Qt.TextSingleLine, highText) |
|---|
| 119 |
on_maxsize = max(on_size.width(), on_size.height()) |
|---|
| 120 |
off_size = fm.size(QtCore.Qt.TextSingleLine, lowText) |
|---|
| 121 |
off_maxsize = max(off_size.width(), off_size.height()) |
|---|
| 122 |
maxsize = max(on_maxsize, off_maxsize) |
|---|
| 123 |
size = QtCore.QSize(maxsize, maxsize) |
|---|
| 124 |
|
|---|
| 125 |
on_image = QtGui.QImage(size.width(), size.height(), QtGui.QImage.Format_ARGB32_Premultiplied) |
|---|
| 126 |
on_image.fill(QtGui.qRgba(0, 0, 0, 0)) |
|---|
| 127 |
off_image = QtGui.QImage(size.width(), size.height(), QtGui.QImage.Format_ARGB32_Premultiplied) |
|---|
| 128 |
off_image.fill(QtGui.qRgba(0, 0, 0, 0)) |
|---|
| 129 |
|
|---|
| 130 |
font = QtGui.QFont() |
|---|
| 131 |
font.setStyleStrategy(QtGui.QFont.ForceOutline) |
|---|
| 132 |
|
|---|
| 133 |
painter = QtGui.QPainter() |
|---|
| 134 |
painter.begin(on_image) |
|---|
| 135 |
painter.setRenderHint(QtGui.QPainter.Antialiasing) |
|---|
| 136 |
painter.setBrush(highColor) |
|---|
| 137 |
painter.setPen(highColor) |
|---|
| 138 |
painter.drawEllipse(QtCore.QRect(QtCore.QPoint(0, 0), size)) |
|---|
| 139 |
painter.setFont(font) |
|---|
| 140 |
painter.setBrush(QtCore.Qt.NoBrush) |
|---|
| 141 |
painter.setPen(QtCore.Qt.black) |
|---|
| 142 |
painter.drawText(QtCore.QRect(QtCore.QPoint(0, 0), size), |
|---|
| 143 |
QtCore.Qt.AlignCenter, highText) |
|---|
| 144 |
painter.end() |
|---|
| 145 |
|
|---|
| 146 |
painter.begin(off_image) |
|---|
| 147 |
painter.setBrush(lowColor) |
|---|
| 148 |
painter.setPen(lowColor) |
|---|
| 149 |
painter.drawEllipse(QtCore.QRect(QtCore.QPoint(0, 0), size)) |
|---|
| 150 |
painter.setFont(font) |
|---|
| 151 |
painter.setBrush(QtCore.Qt.NoBrush) |
|---|
| 152 |
painter.setPen(QtCore.Qt.black) |
|---|
| 153 |
painter.drawText(QtCore.QRect(QtCore.QPoint(0, 0), size), |
|---|
| 154 |
QtCore.Qt.AlignCenter, lowText) |
|---|
| 155 |
|
|---|
| 156 |
painter.end() |
|---|
| 157 |
on_pix = QtGui.QPixmap.fromImage(on_image) |
|---|
| 158 |
off_pix = QtGui.QPixmap.fromImage(off_image) |
|---|
| 159 |
return (on_pix, off_pix) |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
def setEnabled(self, val): |
|---|
| 163 |
if val < 0 or val > 255: |
|---|
| 164 |
print "Error: Invalid value [%s]" % (val) |
|---|
| 165 |
return |
|---|
| 166 |
self.mPin0.setEnabled(val & 0x1) |
|---|
| 167 |
self.mPin1.setEnabled(val & (0x1 << 1)) |
|---|
| 168 |
self.mPin2.setEnabled(val & (0x1 << 2)) |
|---|
| 169 |
self.mPin3.setEnabled(val & (0x1 << 3)) |
|---|
| 170 |
self.mPin4.setEnabled(val & (0x1 << 4)) |
|---|
| 171 |
self.mPin5.setEnabled(val & (0x1 << 5)) |
|---|
| 172 |
self.mPin6.setEnabled(val & (0x1 << 6)) |
|---|
| 173 |
self.mPin7.setEnabled(val & (0x1 << 7)) |
|---|
| 174 |
self.mPin0.setFlat(not(val & 0x1)) |
|---|
| 175 |
self.mPin1.setFlat(not(val & (0x1 << 1))) |
|---|
| 176 |
self.mPin2.setFlat(not(val & (0x1 << 2))) |
|---|
| 177 |
self.mPin3.setFlat(not(val & (0x1 << 3))) |
|---|
| 178 |
self.mPin4.setFlat(not(val & (0x1 << 4))) |
|---|
| 179 |
self.mPin5.setFlat(not(val & (0x1 << 5))) |
|---|
| 180 |
self.mPin6.setFlat(not(val & (0x1 << 6))) |
|---|
| 181 |
self.mPin7.setFlat(not(val & (0x1 << 7))) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
def setValue(self, val): |
|---|
| 185 |
if val < 0 or val > 255: |
|---|
| 186 |
print "Error: Invalid value [%s]" % (val) |
|---|
| 187 |
return |
|---|
| 188 |
if self.mInverted: |
|---|
| 189 |
real_val = 255-val |
|---|
| 190 |
else: |
|---|
| 191 |
real_val = val |
|---|
| 192 |
|
|---|
| 193 |
self.mPin0.setChecked(real_val & 0x1) |
|---|
| 194 |
self.mPin1.setChecked(real_val & (0x1 << 1)) |
|---|
| 195 |
self.mPin2.setChecked(real_val & (0x1 << 2)) |
|---|
| 196 |
self.mPin3.setChecked(real_val & (0x1 << 3)) |
|---|
| 197 |
self.mPin4.setChecked(real_val & (0x1 << 4)) |
|---|
| 198 |
self.mPin5.setChecked(real_val & (0x1 << 5)) |
|---|
| 199 |
self.mPin6.setChecked(real_val & (0x1 << 6)) |
|---|
| 200 |
self.mPin7.setChecked(real_val & (0x1 << 7)) |
|---|
| 201 |
|
|---|
| 202 |
def getValue(self): |
|---|
| 203 |
val = ((self.mPin0.isChecked() << 0) | |
|---|
| 204 |
(self.mPin1.isChecked() << 1) | |
|---|
| 205 |
(self.mPin2.isChecked() << 2) | |
|---|
| 206 |
(self.mPin3.isChecked() << 3) | |
|---|
| 207 |
(self.mPin4.isChecked() << 4) | |
|---|
| 208 |
(self.mPin5.isChecked() << 5) | |
|---|
| 209 |
(self.mPin6.isChecked() << 6) | |
|---|
| 210 |
(self.mPin7.isChecked() << 7)) |
|---|
| 211 |
if self.mInverted: |
|---|
| 212 |
return 255-val |
|---|
| 213 |
else: |
|---|
| 214 |
return val |
|---|
| 215 |
|
|---|
| 216 |
def onClicked(self, checked): |
|---|
| 217 |
self.emit(QtCore.SIGNAL("valueChanged(int)"), self.getValue()) |
|---|
| 218 |
|
|---|
| 219 |
class ElexolPin(QtGui.QPushButton): |
|---|
| 220 |
def __init__(self, onPix = None, offPix = None, parent = None): |
|---|
| 221 |
QtGui.QPushButton.__init__(self, parent) |
|---|
| 222 |
|
|---|
| 223 |
self.connect(self, QtCore.SIGNAL("toggled(bool)"), self.onToggle) |
|---|
| 224 |
self.setCheckable(True) |
|---|
| 225 |
|
|---|
| 226 |
self.mOnIcon = QtGui.QIcon(onPix) |
|---|
| 227 |
self.mOnIcon.addPixmap(onPix, QtGui.QIcon.Normal) |
|---|
| 228 |
self.mOnIcon.addPixmap(onPix, QtGui.QIcon.Disabled) |
|---|
| 229 |
self.mOffIcon = QtGui.QIcon(offPix) |
|---|
| 230 |
self.setIcon(self.mOffIcon) |
|---|
| 231 |
self.mOffIcon.addPixmap(offPix, QtGui.QIcon.Normal) |
|---|
| 232 |
self.mOffIcon.addPixmap(offPix, QtGui.QIcon.Disabled) |
|---|
| 233 |
|
|---|
| 234 |
def onToggle(self, clicked): |
|---|
| 235 |
if clicked: |
|---|
| 236 |
self.setIcon(self.mOnIcon) |
|---|
| 237 |
else: |
|---|
| 238 |
self.setIcon(self.mOffIcon) |
|---|
| 239 |
|
|---|
| 240 |
def onClick(self, clicked): |
|---|
| 241 |
print getValue() |
|---|