|
|
|
import sys
|
|
|
|
import json
|
|
|
|
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, mainwindow):
|
|
|
|
super().__init__()
|
|
|
|
self.initUI()
|
|
|
|
self.mainwindow = mainwindow
|
|
|
|
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):
|
|
|
|
widgetList = self.addAreaTab()
|
|
|
|
widgetList[0].setCurrentIndex(dataType)
|
|
|
|
widgetList[1].setCurrentIndex(order)
|
|
|
|
widgetList[2].setText(channelBytes)
|
|
|
|
|
|
|
|
self.addAreaWidget(widgetList, loacl = False, init = True)
|
|
|
|
|
|
|
|
|
|
|
|
def addAreaTab(self):
|
|
|
|
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 checked, btn=okBtn: self.addAreaWidget(widgetList))
|
|
|
|
|
|
|
|
delAreaBtn.clicked.connect(self.removeAreaTab)
|
|
|
|
|
|
|
|
widgetList.extend([dataTypeCombox, orderCombox, byteLineEdit, areaLayout])
|
|
|
|
areaTabWidget.setLayout(mainLayout)
|
|
|
|
return widgetList
|
|
|
|
|
|
|
|
def removeAreaTab(self):
|
|
|
|
# 获取 QTabWidget 并从中删除标签页
|
|
|
|
deviceName = self.areaTabWidget.parent().parent().parent().parent().parent().windowTitle()
|
|
|
|
index = self.areaTabWidget.currentIndex()
|
|
|
|
|
|
|
|
alldevices = DevicesManange.getAllDevice()
|
|
|
|
for device in alldevices:
|
|
|
|
if deviceName in device:
|
|
|
|
areas = device[3]
|
|
|
|
if areas is not None:
|
|
|
|
areas = json.loads(areas)
|
|
|
|
area = areas[index]
|
|
|
|
type = area["type"]
|
|
|
|
print(area, type,'ssss')
|
|
|
|
|
|
|
|
|
|
|
|
if index != -1:
|
|
|
|
self.areaTabWidget.removeTab(index)
|
|
|
|
del self.widgetList[index]
|
|
|
|
Device.delAreas(deviceName, index)
|
|
|
|
self.mainwindow.devicesManange.getDevice(deviceName).delArea(index, type)
|
|
|
|
self.mainwindow.devicesManange.recalculateAddress()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def addAreaWidget(self, widgetList, loacl = True, init = False):
|
|
|
|
dataType = widgetList[0].currentText()
|
|
|
|
print(widgetList)
|
|
|
|
order = self.dataTypeTranslate(widgetList[1].currentText())
|
|
|
|
byteLineEdit = widgetList[2].text()
|
|
|
|
areaLayout = widgetList[3]
|
|
|
|
deviceName = self.areaTabWidget.parent().parent().parent().parent().parent().windowTitle()
|
|
|
|
widgetLists = []
|
|
|
|
|
|
|
|
while areaLayout.count():
|
|
|
|
item = areaLayout.takeAt(0)
|
|
|
|
widget = item.widget()
|
|
|
|
if widget:
|
|
|
|
widget.setParent(None)
|
|
|
|
|
|
|
|
|
|
|
|
if len(byteLineEdit) == 0:
|
|
|
|
QMessageBox.warning(self, '警告', '请输入字节长度。')
|
|
|
|
|
|
|
|
else:
|
|
|
|
widgetList[0].setEnabled(False)
|
|
|
|
if dataType in ['AI', 'AO']:
|
|
|
|
areaLabel = QLabel(dataType + str(1) + ": " + byteLineEdit + 'Byte' )
|
|
|
|
areaLabel2 =QLabel('0')
|
|
|
|
|
|
|
|
if '主站' in deviceName and dataType == 'AI' or ('从站' in deviceName and dataType == 'AO'):
|
|
|
|
areaLayout.addWidget(areaLabel, 0, 0)
|
|
|
|
areaLayout.addWidget(areaLabel2, 0, 1)
|
|
|
|
areaLayout.addItem(self.horizontalSpacer, 0, 4)
|
|
|
|
widgetLists.append([areaLabel2, areaLayout])
|
|
|
|
|
|
|
|
elif '主站' in deviceName and dataType == 'AO' or ('从站' in deviceName and dataType == 'AI'):
|
|
|
|
areaLineEdit = QLineEdit('0')
|
|
|
|
editbtn = QPushButton('强制')
|
|
|
|
areaLayout.addWidget(areaLabel, 0, 0)
|
|
|
|
areaLayout.addWidget(areaLabel2, 0, 1 )
|
|
|
|
areaLayout.addWidget(areaLineEdit, 0, 2 )
|
|
|
|
areaLayout.addWidget(editbtn, 0, 3)
|
|
|
|
areaLayout.addItem(self.horizontalSpacer, 0, 4)
|
|
|
|
editbtn.clicked.connect(lambda checked, btn=editbtn: self.wirteValue(btn))
|
|
|
|
widgetLists.append([areaLabel2, areaLineEdit, editbtn, areaLayout])
|
|
|
|
self.widgetList.append(widgetLists)
|
|
|
|
|
|
|
|
else:
|
|
|
|
channelNumber = int(byteLineEdit) * 8
|
|
|
|
if '主站' in deviceName and dataType == 'DI' or (
|
|
|
|
'从站' in deviceName and dataType == 'DO'):
|
|
|
|
for i in range(int(channelNumber)):
|
|
|
|
if i % 2 == 0:
|
|
|
|
areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte')
|
|
|
|
areaLabel2 = QLabel('0')
|
|
|
|
areaLayout.addWidget(areaLabel, i // 2, i % 2)
|
|
|
|
areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 1)
|
|
|
|
widgetLists.append([areaLabel2, areaLayout])
|
|
|
|
|
|
|
|
else:
|
|
|
|
areaLabel = QLabel(dataType + str(i + 1) + ": " + byteLineEdit + 'Byte')
|
|
|
|
areaLabel2 = QLabel('0')
|
|
|
|
areaLayout.addWidget(areaLabel, i // 2, i % 2 + 2)
|
|
|
|
areaLayout.addWidget(areaLabel2, i // 2, i % 2 + 3)
|
|
|
|
widgetLists.append([areaLabel2, areaLayout])
|
|
|
|
self.widgetList.append(widgetLists)
|
|
|
|
if '主站' in deviceName and dataType == 'DO' or (
|
|
|
|
'从站' in deviceName and dataType == 'DI'):
|
|
|
|
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.wirteValue(btn))
|
|
|
|
widgetLists.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.wirteValue(btn))
|
|
|
|
|
|
|
|
widgetLists.append([areaLabel2, areaLineEdit, editbtn])
|
|
|
|
self.widgetList.append(widgetLists)
|
|
|
|
areaLayout.addItem(self.verticalSpacer, int(byteLineEdit) * 8 ,0)
|
|
|
|
|
|
|
|
index = self.areaTabWidget.currentIndex() + 1
|
|
|
|
areaId = DevicesManange.getAreaID(deviceName)
|
|
|
|
if areaId is not None and index in areaId:
|
|
|
|
if loacl:
|
|
|
|
|
|
|
|
del self.widgetList[index - 1]
|
|
|
|
if init:
|
|
|
|
return
|
|
|
|
DevicesManange.updataAreas(dataType, order, byteLineEdit, deviceName, index)
|
|
|
|
self.mainwindow.devicesManange.getDevice(deviceName).editArea(index = index - 1, type = dataType, order = order, bytes = int(byteLineEdit))
|
|
|
|
self.mainwindow.devicesManange.recalculateAddress()
|
|
|
|
else:
|
|
|
|
DevicesManange.addAreas(dataType, order, byteLineEdit, deviceName)
|
|
|
|
self.mainwindow.devicesManange.getDevice(deviceName).addArea(type = dataType, bytes = int(byteLineEdit), nums = 1)
|
|
|
|
self.mainwindow.devicesManange.recalculateAddress()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dataTypeTranslate(self, order):
|
|
|
|
self.dataTypeDict = {'不转换': 'ABCD', '字节转换': 'DCBA', '字转换': 'CDAB', '字内转换': 'BADC'}
|
|
|
|
return self.dataTypeDict[order]
|
|
|
|
|
|
|
|
def wirteValue(self, editBtn):
|
|
|
|
deviceName = self.areaTabWidget.parent().parent().parent().parent().parent().windowTitle()
|
|
|
|
index = self.areaTabWidget.currentIndex()
|
|
|
|
valueList = []
|
|
|
|
|
|
|
|
if len(self.widgetList) > 0:
|
|
|
|
for widgetLists in self.widgetList:
|
|
|
|
for widgetList in widgetLists:
|
|
|
|
if isinstance(widgetList[1], QLineEdit):
|
|
|
|
valueList.append(int(widgetList[1].text()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def readValue(self):
|
|
|
|
deviceList = []
|
|
|
|
areaIdList = []
|
|
|
|
devices = DevicesManange.getAllDevice()
|
|
|
|
|
|
|
|
for device in devices:
|
|
|
|
deviceName = device[0]
|
|
|
|
areas = device[3]
|
|
|
|
if areas is not None:
|
|
|
|
areas = json.loads(areas)
|
|
|
|
for area in areas:
|
|
|
|
areaId = area["id"]
|
|
|
|
areaIdList.append(areaId)
|
|
|
|
deviceList.append([deviceName, areaIdList])
|
|
|
|
index = self.areaTabWidget.currentIndex()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(self.widgetList) > 0:
|
|
|
|
widgetLists = self.widgetList[index]
|
|
|
|
for widgetList in widgetLists:
|
|
|
|
widgetList[0].setText('AAA')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
window = AreaTabWidget()
|
|
|
|
window.show()
|
|
|
|
sys.exit(app.exec_())
|