You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PROFIBUS/UI/ButtonLayoutWidget.py

141 lines
5.9 KiB
Python

from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QWidget, QVBoxLayout, QLabel, QPushButton, QLayout, \
QHBoxLayout, QComboBox, QLineEdit, QSpacerItem, QSizePolicy, QGridLayout, QMessageBox, QSplitter, QFrame
import re
class forceButton(QPushButton):
def __init__(self, valueLabel = None, valueEdit = None):
super().__init__()
self.valueEdit = valueEdit
self.valueLabel= valueLabel
self.setObjectName('forceBtn')
self.setText('强制')
class ButtonLayout(QWidget):
def __init__(self, areaWidget, order, byteLineEdit, dataType, deviceName):
super().__init__()
self.areaLineEditWidgets = []
self.areaLabelWidget = []
self.areaWidget = areaWidget
self.order = order
self.byteLineEdit = byteLineEdit
self.dataType = dataType
self.deviceName = deviceName
self.areaValueManage = self.areaWidget.areaValueManage
self.settingValue = areaWidget.settingValue
self.addLayout()
self.initValues()
def addLayout(self):
self.setLayout(self.areaLayout())
def areaLayout(self):
areaLayout = QVBoxLayout()
print(self.dataType)
if self.dataType in ['AI','AO']:
hLayout = QHBoxLayout()
hLayout.addLayout(self.dateLayout(1))
hLayout.addWidget(QSplitter())
areaLayout.addLayout(hLayout)
areaLayout.addWidget(QSplitter())
else:
byteLineEdit = int(self.byteLineEdit) * 4
for i in range(int(byteLineEdit)):
hLayout = QHBoxLayout()
hLayout.addLayout(self.dateLayout(number = (i + 1)*2 - 1))
# hLayout.addWidget(QSplitter())
hLayout.addLayout(self.dateLayout(number = (i + 1)*2))
areaLayout.addLayout(hLayout)
areaLayout.addWidget(QSplitter())
return areaLayout
def dateLayout(self, number, force = True):
number = number
forceLayout = QHBoxLayout()
areaMessLabel = QLabel(str(self.dataType) + str(number) + ": " + '字节长度: ' + str(self.byteLineEdit) + ' 当前值: ' )
areaMessLabel.setObjectName('areaMessLabel')
areaValueLabel = QLabel('0')
areaValueLabel.setObjectName('areaValueLabel')
forceLayout.addWidget(areaMessLabel)
forceLayout.addWidget(areaValueLabel)
if '主站' in self.deviceName and self.dataType == 'AI' or ('从站' in self.deviceName and self.dataType == 'AO'):
force = False
self.areaLabelWidget.append(areaValueLabel)
elif '主站' in self.deviceName and self.dataType == 'DI' or ('从站' in self.deviceName and self.dataType == 'DO'):
force = False
self.areaLabelWidget.append(areaValueLabel)
if force:
areaLineEdit = QLineEdit('0')
areaLineEdit.setObjectName('areaLineEdit')
forceBtn = forceButton(valueLabel=areaValueLabel, valueEdit=areaLineEdit)
forceBtn.clicked.connect(self.forceValues)
forceLayout.addWidget(areaLineEdit)
forceLayout.addWidget(forceBtn)
self.areaLineEditWidgets.append(forceBtn)
return forceLayout
def wirteAreaLineEditValue(self,dataTypeAndModel):
areaValueList = []
if dataTypeAndModel in ['主站AO','从站AI']:
for areaLineEdit in self.areaLineEditWidgets:
editValue = areaLineEdit.valueEdit.text()
pattern = re.compile(r'^\d+(\.\d+)?$')
match = pattern.match(editValue)
if not match:
QMessageBox.warning(self, '提示', '请输入数字。')
return
else:
areaLineEdit.valueLabel.setText(editValue)
areaValueList.append(editValue)
return areaValueList
#判断输入值是否为0和1
elif dataTypeAndModel in ['主站DO','从站DI']:
for areaLineEdit in self.areaLineEditWidgets:
editValue = areaLineEdit.valueEdit.text()
pattern = re.compile(r'^[01]$')
match = pattern.match(editValue)
if not match:
QMessageBox.warning(self, '提示', '请输入0或1。')
return
else:
areaLineEdit.valueLabel.setText(editValue)
areaValueList.append(editValue)
else:
areaValueList = self.areaLabelWidget
return areaValueList
def readValues(self,valueList):
widgets = self.areaLabelWidget
for value, widgets in zip(valueList, widgets):
widgets.setText(str(value))
def forceValues(self):
dataTypeAndModel = self.deviceName[-2:] + self.dataType
curIndex = self.areaWidget.areaTabWidget.currentIndex()
valueList = self.wirteAreaLineEditValue(dataTypeAndModel)
# if valueList is None:
# return
# else:
self.areaValueManage.updataAreaValue(deviceName = self.deviceName, curIndex = curIndex, valueList = valueList, dataTypeAndModel = dataTypeAndModel)
self.areaWidget.wirteValue(deviceName = self.deviceName, dataTypeAndModel = dataTypeAndModel)
def initValues(self):
dataTypeAndModel = self.deviceName[-2:] + self.dataType
valueList = self.wirteAreaLineEditValue(dataTypeAndModel)
curIndex = self.areaWidget.areaTabWidget.currentIndex()
if self.settingValue is not None:
self.areaValueManage.initAreaValue(deviceName = self.deviceName, curIndex= curIndex, valueList = valueList, dataTypeAndModel = dataTypeAndModel)
else:
self.areaValueManage.updataAreaValue(deviceName = self.deviceName, curIndex = curIndex, valueList = valueList, dataTypeAndModel = dataTypeAndModel)