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/BlockParameterManageWidget.py

343 lines
14 KiB
Python

1 year ago
import re
import qtawesome
1 year ago
from PyQt5.QtGui import QPixmap, QPainter
1 year ago
from PyQt5.QtCore import Qt
1 year ago
from PyQt5.QtWidgets import QStyledItemDelegate, QStyle, QComboBox, QMessageBox, QPushButton,QStackedWidget, QLineEdit \
, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QSplitter, QButtonGroup, QDialog
1 year ago
from utils.DBModels.DeviceParModels import *
1 year ago
from UI.BlockParameterView import ParmView
1 year ago
from UI.SearchAddressWidget import SearchAddressWidget
1 year ago
from utils.DBModels.DeviceParModels import *
1 year ago
from utils import Globals
1 year ago
from UI.LoadingDataWidget import LoadingDataWidget
1 year ago
from UI.EditAddressWidget import EditAddressWidget
1 year ago
1 year ago
from model.ProjectModel.BlockManage import BlockManage, BlockType, TBType
1 year ago
1 year ago
class ShowImage(QWidget):
def __init__(self, image_path):
super().__init__()
self.image_path = image_path
self.pixmap = QPixmap(self.image_path)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.pixmap)
1 year ago
1 year ago
1 year ago
class HideTextDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
# 仅在下拉列表中绘制文本避免在ComboBox显示区域绘制
if option.state & QStyle.State_Enabled:
super().paint(painter, option, index)
def sizeHint(self, option, index):
# 返回原始大小确保下拉列表项不受ComboBox宽度影响
return super().sizeHint(option, index)
class TbCombox(QComboBox):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建ComboBox
1 year ago
self.addItems(["压力转换块 ", "温度转换块", "物位转换块", "流量转换块"])
1 year ago
self.setEditable(True) # 设置为可编辑以应用样式表隐藏文本
self.setObjectName('tbcombox')
# 设置样式隐藏文本
self.lineEdit().setStyleSheet("color: transparent; border:none; background-color: #f0f0f0")
self.lineEdit().setCursor(Qt.ArrowCursor)
self.lineEdit().setReadOnly(True)
# 设置自定义代理以调整下拉列表行为
self.delegate = HideTextDelegate(self)
self.setItemDelegate(self.delegate)
# 设置ComboBox的固定大小
self.setFixedSize(30, 40)
# 确保下拉列表宽度适应最长项
1 year ago
# max_width = max(self.fontMetrics().width(self.itemText(i)) for i in range(self.count()))
1 year ago
self.view().setMinimumWidth(130)
1 year ago
class DynamicAddBlock(QHBoxLayout):
def __init__(self, blocklist):
super().__init__()
self.blocklist = blocklist
self.buttonlist = []
self.blockViewlist = []
1 year ago
self.tbList = ['PressureTranslationBlock', 'TemperatureTranslationBlock', 'LevelTranslationBlock', 'FlowTranslationBlock'] #存放四个转换块
1 year ago
self.enumList = [TBType.pressureTB, TBType.tempTB, TBType.levelTB, TBType.flowTB]
1 year ago
self.initUI()
def initUI(self):
pbNumber = self.blocklist[0]
tbNumber = self.blocklist[1]
fbNumber = self.blocklist[2]
for i in range(pbNumber):
pblockBtn = QPushButton('物理块')
pblockBtn.setCheckable(True)
1 year ago
# pblockBtn.setFixedSize(90, 43)
1 year ago
pblockBtn.setObjectName("parameBtn")
pblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f'))
# pblockBtn.setCheckable(True)
pblockBtn.clicked.connect(lambda _, pbbtn = pblockBtn: self.switchParameterWidget(pbbtn))
1 year ago
self.addWidget(pblockBtn, 3)
1 year ago
self.buttonlist.append(pblockBtn)
1 year ago
physicalBlockView = ParmView(PhysicalBlock, i, BlockType.PB)
1 year ago
self.blockViewlist.append(physicalBlockView)
if i == 0:
pblockBtn.setChecked(True)
for i in range(fbNumber):
fblockBtn = QPushButton('功能块' + str(i + 1))
1 year ago
# fblockBtn.setFixedSize(90, 43)
1 year ago
fblockBtn.setCheckable(True)
fblockBtn.setObjectName("parameBtn")
fblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f'))
fblockBtn.clicked.connect(lambda _, fbbtn = fblockBtn: self.switchParameterWidget(fbbtn))
1 year ago
self.addWidget(fblockBtn, 3)
1 year ago
self.buttonlist.append(fblockBtn)
1 year ago
aiFunctionBlockView = ParmView(AIFunctionBlock, i, BlockType.FB)
1 year ago
self.blockViewlist.append(aiFunctionBlockView)
1 year ago
1 year ago
for i in range(tbNumber):
tbwidget = QWidget()
tbwidget.setObjectName('tbwidget')
tblayout = QHBoxLayout()
tbwidget.setLayout(tblayout)
tbcombox = TbCombox()
1 year ago
tbcombox.setCurrentIndex(0)
tblockBtn = QPushButton('压力转换块' + str(i + 1))
tblockBtn.setObjectName("tbparameBtn")
# tblockBtn.setFixedSize(130, 43)
1 year ago
tblockBtn.setCheckable(True)
tblockBtn.setIcon(qtawesome.icon('fa.th-large', color='#1fbb6f'))
1 year ago
# tblockBtn.clicked.connect(lambda _, tbbtn = tblockBtn: self.switchParameterWidget(tbbtn))
tblockBtn.clicked.connect(lambda _, combox = tbcombox, tbbtn = tblockBtn : self.switchTbtype(combox.currentIndex(), combox, tbbtn))
tbcombox.currentIndexChanged.connect(lambda index, combox = tbcombox, tbbtn = tblockBtn : self.switchTbtype(index, combox, tbbtn))
tblayout.addWidget(tblockBtn, 6)
tblayout.addWidget(tbcombox, 1)
tblayout.setSpacing(0)
self.addWidget(tbwidget, 5)
1 year ago
self.buttonlist.append(tblockBtn)
1 year ago
for tb, enum in zip(self.tbList, self.enumList):
tbBlockView = ParmView(globals()[tb], i, enum)
1 year ago
self.blockViewlist.append(tbBlockView)
1 year ago
self.parameterButtonGroup = QButtonGroup()
self.parameterButtonGroup.setExclusive(True)
for button in self.buttonlist:
self.parameterButtonGroup.addButton(button)
self.parameStackWidget = QStackedWidget()
for view in self.blockViewlist:
self.parameStackWidget.addWidget(view)
1 year ago
def switchParameterWidget(self, buttonType):
for index , button in enumerate(self.buttonlist):
if button == buttonType:
self.parameStackWidget.setCurrentIndex(index)
1 year ago
def switchTbtype(self, index, combox, button):
1 year ago
tbType = combox.itemText(index)
1 year ago
buttonNumber = button.text()[-1:]
button.setText(tbType + str(buttonNumber))
if button.isChecked():
stackIndex = (int(self.blocklist[0]) + int(self.blocklist[1]) + index) + (int(buttonNumber) - 1) * 4
self.parameStackWidget.setCurrentIndex(stackIndex)
1 year ago
def deleteLater(self):
while self.count():
item = self.takeAt(0)
widget = item.widget()
if widget:
widget.deleteLater()
layout = item.layout()
if layout:
layout.deleteLater()
if self.parameStackWidget:
while self.parameStackWidget.count():
widget = self.parameStackWidget.widget(0)
self.parameStackWidget.removeWidget(widget)
widget.deleteLater()
self.parameStackWidget.deleteLater()
super().deleteLater()
1 year ago
1 year ago
class BlockParameterManageWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
1 year ago
self.initUIstat = True
1 year ago
def initUI(self):
self.mainlayout = QVBoxLayout()
1 year ago
1 year ago
self.settingLayout = QHBoxLayout()
self.deviceAddressLabel = QLabel('从站地址')
self.deviceAddressLabel.setObjectName('deviceAddressLabel')
1 year ago
self.deviceAddressEdit = QLineEdit()
1 year ago
self.deviceAddressEdit.returnPressed.connect(self.loadBlackData)
self.deviceAddressEdit.setObjectName("deviceAddressEdit")
1 year ago
1 year ago
self.confirmBtn = QPushButton('连接设备')
self.confirmBtn.setIcon(qtawesome.icon('mdi.connection', color='#1fbb6f'))
1 year ago
self.confirmBtn.clicked.connect(self.loadBlackData)
self.confirmBtn.setObjectName("parameBtn")
1 year ago
1 year ago
self.refershDataBtn = QPushButton('加载数据')
1 year ago
self.refershDataBtn.setIcon(qtawesome.icon('mdi6.reload', color='#1fbb6f'))
1 year ago
self.refershDataBtn.clicked.connect(self.refreshData)
self.refershDataBtn.setObjectName("parameBtn")
1 year ago
self.deviceAddressSearchBtn = QPushButton('搜索站地址')
self.deviceAddressSearchBtn.setIcon(qtawesome.icon('fa.search', color='#1fbb6f'))
1 year ago
self.deviceAddressSearchBtn.clicked.connect(self.searchAddress)
self.deviceAddressSearchBtn.setObjectName("parameBtn")
1 year ago
1 year ago
self.editAddressBtn = QPushButton('修改站地址')
1 year ago
self.editAddressBtn.setIcon(qtawesome.icon('fa.edit', color='#1fbb6f'))
1 year ago
self.editAddressBtn.clicked.connect(self.editAddress)
self.editAddressBtn.setObjectName("parameBtn")
1 year ago
self.settingLayout.addWidget(self.deviceAddressLabel, 1)
self.settingLayout.addWidget(self.deviceAddressEdit, 1)
self.settingLayout.addWidget(self.confirmBtn, 1)
self.settingLayout.addWidget(self.deviceAddressSearchBtn, 1)
1 year ago
self.settingLayout.addWidget(self.editAddressBtn, 1)
1 year ago
self.settingLayout.addWidget(self.refershDataBtn, 1)
1 year ago
# self.settingLayout.addLayout(self.blockLayout)
self.splitter = QSplitter()
self.settingLayout.addWidget(self.splitter, 18)
1 year ago
self.mainlayout.addLayout(self.settingLayout,1)
1 year ago
self.widget = ShowImage('Static/pict.png')
1 year ago
self.mainlayout.addWidget(self.widget, 20)
1 year ago
self.setLayout(self.mainlayout)
1 year ago
def loadBlackData(self):
1 year ago
if self.initUIstat:
address = self.deviceAddressEdit.text()
# print(address)
if address:
1 year ago
pattern = re.compile(r'^(?:[1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-6])$')
1 year ago
match = pattern.match(address)
if not match:
1 year ago
QMessageBox.warning(self, '提示', '请输入2 - 126。')
1 year ago
return
if self.initUIstat:
1 year ago
try:
self.blockManage = BlockManage(int(address))
except Exception as e:
reply = QMessageBox.question(self.parent(),
'警告',
f"发生错误: {e}",
QMessageBox.Yes)
return
1 year ago
self.splitter.deleteLater()
self.widget.deleteLater()
self.splitter = QSplitter()
Globals.setValue('blockManage', self.blockManage)
blocklist = self.blockManage.getBlockNums()
self.blockLayout = DynamicAddBlock(blocklist)
self.settingLayout.addLayout(self.blockLayout, 7)
self.settingLayout.addWidget(self.splitter, 11)
self.mainlayout.addWidget(self.blockLayout.parameStackWidget, 20)
self.initUIstat = False
else:
reply = QMessageBox.question(self.parent(),
'警告',
"请输入从站地址",
QMessageBox.Yes)
1 year ago
return
1 year ago
else:
reply = QMessageBox.question(self, '确定', '确定更换站地址吗?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.blockLayout.deleteLater()
1 year ago
self.splitter.deleteLater()
1 year ago
self.splitter = QSplitter()
1 year ago
blocklist = self.blockManage.getBlockNums()
self.blockLayout = DynamicAddBlock(blocklist)
1 year ago
self.settingLayout.addLayout(self.blockLayout, 7)
1 year ago
self.settingLayout.addWidget(self.splitter, 11)
1 year ago
self.mainlayout.addWidget(self.blockLayout.parameStackWidget, 20)
1 year ago
else:
return
1 year ago
def refreshData(self):
1 year ago
if self.initUIstat:
reply = QMessageBox.question(self.parent(),
'警告',
"请先连接设备",
QMessageBox.Yes)
return
1 year ago
1 year ago
self.loadingDataWidget = LoadingDataWidget()
1 year ago
self.loadingDataWidget.show()
# self.loadingDataWidget.loadData()
1 year ago
blockView = self.blockLayout.parameStackWidget.currentWidget()
model = blockView.model
1 year ago
# model.updateColumn(5, '查询中sdadadsda\r\nsdasdsasasad\r\nasdsadsad...')
# blockView.resizeHeader()
blockType = blockView.blockType
1 year ago
blcokIndex = blockView.blcokIndex
1 year ago
self.blockManage.getBlockValues(blockType = blockType, blockIndex = blcokIndex, callback = self.loadingDataWidget.loadData, callback2 = model.updateValue)
# print(valueList)
# model.updateValue(valueList)
# print(blockType, blcokIndex)
1 year ago
1 year ago
def editAddress(self):
1 year ago
# if self.initUIstat:
# reply = QMessageBox.question(self.parent(),
# '警告',
# "请先连接设备",
# QMessageBox.Yes)
# return
1 year ago
1 year ago
oldAddress = int(self.blockManage.address)
1 year ago
self.editAddressWidget = EditAddressWidget(oldAddress)
if self.editAddressWidget.exec_() == QDialog.Accepted:
1 year ago
newAddress = int(self.editAddressWidget.editAddressEdit.text())
1 year ago
self.blockManage.DPV1Master.editDevAddress(oldAddress, newAddress)
1 year ago
1 year ago
def searchAddress(self):
self.searchAddressWidget = SearchAddressWidget(self.deviceAddressEdit)
self.searchAddressWidget.show()