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.
231 lines
9.1 KiB
Python
231 lines
9.1 KiB
Python
2 years ago
|
import sys
|
||
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QWidget, QVBoxLayout, QLabel, QPushButton, \
|
||
|
QHBoxLayout, QComboBox, QLineEdit, QSpacerItem, QSizePolicy, QGridLayout, QMessageBox
|
||
|
from PyQt5 import QtCore
|
||
|
|
||
|
from model.ProjectModel.DeviceManage import DevicesManange, Device
|
||
|
|
||
|
|
||
|
class AreaTabWidget(QMainWindow):
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.initUI()
|
||
|
def initUI(self):
|
||
|
# 创建一个 QTabWidget
|
||
|
self.widgetList = []
|
||
|
self.areaTabWidget = QTabWidget(self)
|
||
|
self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
|
||
|
self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
|
||
|
|
||
|
# 设置主窗口的中心部分为 QTabWidget
|
||
|
self.setCentralWidget(self.areaTabWidget)
|
||
|
|
||
|
def initAreaTab(self, dataType, order, channelBytes, deviceName = False):
|
||
|
areaTabWidget = QWidget()
|
||
|
tabIndex = self.areaTabWidget.count()
|
||
|
# 将标签页添加到 QTabWidget 中
|
||
|
self.areaTabWidget.addTab(areaTabWidget, str(tabIndex))
|
||
|
|
||
|
self.setCentralWidget(self.areaTabWidget)
|
||
|
widgetList = []
|
||
|
mainLayout = QVBoxLayout()
|
||
|
topLayout = QHBoxLayout()
|
||
|
areaLayout = QGridLayout()
|
||
|
|
||
|
dataTypeCombox = QComboBox()
|
||
|
dataTypeCombox.addItems(['AI', 'AO', 'DI', 'DO'])
|
||
|
dataTypeCombox.setCurrentIndex(dataType)
|
||
|
dataTypeCombox.setObjectName('dataTypeCombox')
|
||
|
|
||
|
orderCombox = QComboBox()
|
||
|
orderCombox.addItems(['不转换', '字节转换', '字转换', '字内转换'])
|
||
|
orderCombox.setCurrentIndex(order)
|
||
|
|
||
|
byteLineEdit = QLineEdit()
|
||
|
byteLineEdit.setPlaceholderText('字节长度')
|
||
|
byteLineEdit.setObjectName('byteLineEdit')
|
||
|
byteLineEdit.setText(channelBytes)
|
||
|
|
||
|
|
||
|
okBtn = QPushButton('确定')
|
||
|
delAreaBtn = QPushButton('删除')
|
||
|
|
||
|
topLayout.addWidget(dataTypeCombox, 1)
|
||
|
topLayout.addWidget(orderCombox, 1)
|
||
|
# topLayout.addWidget(byteLabel)
|
||
|
topLayout.addWidget(byteLineEdit, 2)
|
||
|
topLayout.addWidget(okBtn, 1)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addWidget(delAreaBtn, 1)
|
||
|
|
||
|
# widget = QWidget()
|
||
|
# areaLayout.addWidget(widget)
|
||
|
mainLayout.addLayout(topLayout, 1)
|
||
|
mainLayout.addLayout(areaLayout, 9)
|
||
|
|
||
|
|
||
|
delAreaBtn.clicked.connect(self.removeAreaTab)
|
||
|
|
||
|
widgetList.extend([dataTypeCombox, orderCombox, byteLineEdit, areaLayout, deviceName])
|
||
|
self.addAreaWidget(widgetList)
|
||
|
|
||
|
areaTabWidget.setLayout(mainLayout)
|
||
|
|
||
|
|
||
|
def addAreaTab(self, deviceName):
|
||
|
areaTabWidget = QWidget()
|
||
|
|
||
|
tabIndex = self.areaTabWidget.count()
|
||
|
# 将标签页添加到 QTabWidget 中
|
||
|
self.areaTabWidget.addTab(areaTabWidget, str(tabIndex))
|
||
|
self.areaTabWidget.setCurrentIndex(int(tabIndex))
|
||
|
self.setCentralWidget(self.areaTabWidget)
|
||
|
widgetList = []
|
||
|
mainLayout = QVBoxLayout()
|
||
|
topLayout = QHBoxLayout()
|
||
|
areaLayout = QGridLayout()
|
||
|
|
||
|
dataTypeCombox = QComboBox()
|
||
|
dataTypeCombox.addItems(['AI', 'AO', 'DI', 'DO'])
|
||
|
dataTypeCombox.setObjectName('dataTypeCombox')
|
||
|
|
||
|
orderCombox = QComboBox()
|
||
|
orderCombox.addItems(['不转换', '字节转换', '字转换', '字内转换'])
|
||
|
|
||
|
byteLineEdit = QLineEdit()
|
||
|
byteLineEdit.setPlaceholderText('字节长度')
|
||
|
byteLineEdit.setObjectName('byteLineEdit')
|
||
|
|
||
|
|
||
|
okBtn = QPushButton('确定')
|
||
|
delAreaBtn = QPushButton('删除')
|
||
|
|
||
|
topLayout.addWidget(dataTypeCombox, 1)
|
||
|
topLayout.addWidget(orderCombox, 1)
|
||
|
# topLayout.addWidget(byteLabel)
|
||
|
topLayout.addWidget(byteLineEdit, 2)
|
||
|
topLayout.addWidget(okBtn, 1)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addItem(self.horizontalSpacer)
|
||
|
topLayout.addWidget(delAreaBtn, 1)
|
||
|
|
||
|
widget = QWidget()
|
||
|
areaLayout.addWidget(widget)
|
||
|
mainLayout.addLayout(topLayout, 1)
|
||
|
mainLayout.addLayout(areaLayout, 9)
|
||
|
|
||
|
okBtn.clicked.connect(lambda: self.addAreaWidget(widgetList))
|
||
|
|
||
|
delAreaBtn.clicked.connect(self.removeAreaTab)
|
||
|
|
||
|
widgetList.extend([dataTypeCombox, orderCombox, byteLineEdit, areaLayout, deviceName])
|
||
|
areaTabWidget.setLayout(mainLayout)
|
||
|
|
||
|
def removeAreaTab(self):
|
||
|
# 获取 QTabWidget 并从中删除标签页
|
||
|
deviceName = self.areaTabWidget.parent().parent().parent().parent().parent().windowTitle()
|
||
|
index = self.areaTabWidget.currentIndex()
|
||
|
print(index)
|
||
|
if index != -1:
|
||
|
self.areaTabWidget.removeTab(index)
|
||
|
|
||
|
Device.delAreas(deviceName, index)
|
||
|
|
||
|
|
||
|
|
||
|
def addAreaWidget(self, widgetList):
|
||
|
dataType = widgetList[0].currentText()
|
||
|
order = self.dataTypeTranslate(widgetList[1].currentText())
|
||
|
byteLineEdit = widgetList[2].text()
|
||
|
areaLayout = widgetList[3]
|
||
|
deviceName = widgetList[4]
|
||
|
|
||
|
|
||
|
if len(byteLineEdit) == 0:
|
||
|
QMessageBox.warning(self, '警告', '请输入字节长度。')
|
||
|
else:
|
||
|
for i in range(3):
|
||
|
widgetList[i].setEnabled(False)
|
||
|
|
||
|
if dataType in ['AI', 'AO']:
|
||
|
if i % 2 == 0:
|
||
|
areaLabel = QLabel(dataType + str(i + 1 ) + ": " + byteLineEdit + 'Byte' )
|
||
|
areaLabel2 =QLabel('0')
|
||
|
areaLineEdit = QLineEdit('0')
|
||
|
editbtn = QPushButton('强制')
|
||
|
|
||
|
areaLayout.addWidget(areaLabel, i//2, i % 2)
|
||
|
areaLayout.addWidget(areaLabel2, i//2, i % 2 + 1 )
|
||
|
areaLayout.addWidget(areaLineEdit, i//2, i % 2 + 2 )
|
||
|
areaLayout.addWidget(editbtn, i // 2, i % 2 + 3)
|
||
|
areaLayout.addItem(self.horizontalSpacer, 0, 4)
|
||
|
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
|
||
|
self.widgetList.append([areaLabel2, areaLineEdit, editbtn])
|
||
|
else:
|
||
|
areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte')
|
||
|
areaLabel2 = QLabel('0')
|
||
|
areaLineEdit = QLineEdit('0')
|
||
|
editbtn = QPushButton('强制')
|
||
|
|
||
|
areaLayout.addWidget(areaLabel, i // 2, i % 2 + 4)
|
||
|
areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 6)
|
||
|
areaLayout.addWidget(areaLineEdit, i // 2, i % 2 + 6)
|
||
|
areaLayout.addWidget(editbtn, i // 2, i % 2 + 7)
|
||
|
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
|
||
|
self.widgetList.append([areaLabel2, areaLineEdit, editbtn])
|
||
|
else:
|
||
|
channelNumber = int(byteLineEdit) * 8
|
||
|
for i in range(int(channelNumber)):
|
||
|
if i % 2 == 0:
|
||
|
areaLabel = QLabel(dataType + str(i + 1 ) + ": " + byteLineEdit + 'Byte' )
|
||
|
areaLabel2 =QLabel('0')
|
||
|
areaLineEdit = QLineEdit('0')
|
||
|
editbtn = QPushButton('强制')
|
||
|
|
||
|
areaLayout.addWidget(areaLabel, i//2, i % 2)
|
||
|
areaLayout.addWidget(areaLabel2, i//2, i % 2 + 1 )
|
||
|
areaLayout.addWidget(areaLineEdit, i//2, i % 2 + 2 )
|
||
|
areaLayout.addWidget(editbtn, i // 2, i % 2 + 3)
|
||
|
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
|
||
|
self.widgetList.append([areaLabel2, areaLineEdit, editbtn])
|
||
|
else:
|
||
|
areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte')
|
||
|
areaLabel2 = QLabel('0')
|
||
|
areaLineEdit = QLineEdit('0')
|
||
|
editbtn = QPushButton('强制')
|
||
|
|
||
|
areaLayout.addWidget(areaLabel, i // 2, i % 2 + 4)
|
||
|
areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 5)
|
||
|
areaLayout.addWidget(areaLineEdit, i // 2, i % 2 + 6)
|
||
|
areaLayout.addWidget(editbtn, i // 2, i % 2 + 7)
|
||
|
editbtn.clicked.connect(lambda checked, btn=editbtn: self.forceEdit(btn))
|
||
|
|
||
|
self.widgetList.append([areaLabel2, areaLineEdit, editbtn])
|
||
|
areaLayout.addItem(self.verticalSpacer, int(byteLineEdit) * 8 ,0)
|
||
|
if deviceName:
|
||
|
DevicesManange.addAreas(dataType, order, byteLineEdit, deviceName)
|
||
|
|
||
|
def dataTypeTranslate(self, order):
|
||
|
self.dataTypeDict = {'不转换': 'ABCD', '字节转换': 'DCBA', '字转换': 'CDAB', '字内转换': 'BADC'}
|
||
|
return self.dataTypeDict[order]
|
||
|
|
||
|
def forceEdit(self, editBtn):
|
||
|
for widget in self.widgetList:
|
||
|
if editBtn in widget:
|
||
|
widget[0].setText(widget[1].text())
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app = QApplication(sys.argv)
|
||
|
window = AreaTabWidget()
|
||
|
window.show()
|
||
|
sys.exit(app.exec_())
|