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.

286 lines
12 KiB
Python

7 months ago
import qtawesome
from PyQt5.QtCore import Qt, QVariant, QSize
from PyQt5.QtWidgets import QHBoxLayout, QWidget, QMessageBox, QComboBox
7 months ago
from protocol.TCP.TemToMv import temToMv
from model.ProjectModel.VarManage import *
from UI.VarManages.ModbusModel import *
from utils import Globals
import re
class TcRtdModel(VarTableModel):
def __init__(self, header, data: list, table = None):
'''
header : 表头变量
data : 表格内容
table : 缺省参数
'''
VarTableModel.__init__(self, header, data, table = table)
def initTable(self):
self.datas = []
# proType = Globals.getValue('currentProType')
# if proType in ['5']:
varDatas = TcRtdManage.getAllVar()
7 months ago
if not varDatas:
# self.layoutChanged.emit()
self.table.proxy.invalidate()
return
for x in varDatas:
x.insert(1, '')
x.insert(2, '')
self.datas.append(x)
x.append('')
self.checkList = ['Unchecked'] * len(self.datas)
# self.layoutChanged.emit()
self.table.proxy.invalidate()
def data(self, QModelIndex, role=None):
# print(Qt.__dict__.items())
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
if not QModelIndex.isValid():
print("行或者列有问题")
return QVariant()
if role == Qt.BackgroundColorRole:
if QModelIndex.row() % 2 == 0 and self.datas[QModelIndex.row()][3] not in Globals.getValue('forceVars'):
return QtGui.QColor('#EFEFEF')
if self.datas[QModelIndex.row()][3] in Globals.getValue('forceVars'):
return QtGui.QColor('#00FF7F')
if role == Qt.TextColorRole:
return QtGui.QColor('#1A1A1A')
if role == Qt.CheckStateRole:
if QModelIndex.column() == 0:
return Qt.Checked if self.checkList[QModelIndex.row()] == 'Checked' else Qt.Unchecked
else:
return None
if role == Qt.ToolTipRole:
if QModelIndex.column() == 0:
return self.checkList[QModelIndex.row()]
if role == Qt.DisplayRole or role == Qt.EditRole:
if QModelIndex.row() in self.editableList:
return self.datas[QModelIndex.row()][QModelIndex.column()]
if role != Qt.DisplayRole:
return QVariant()
# # 获取变量值并插入表格
if QModelIndex.column() == 2:
return self.table.valueList[QModelIndex.row()]# # 获取变量值并插入表格
# if QModelIndex.column() == 2:
return QVariant(self.datas[QModelIndex.row()][QModelIndex.column()])
def flags(self, index):
if index.column() == 0:
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
if index.row() in self.editableList and index.column() or index.column() == 1 or index.column() == 8:
if index.column() == 4:
return Qt.ItemIsEnabled
7 months ago
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsEditable
return Qt.ItemIsEnabled
7 months ago
class TcRtdButtonDelegate(VarButtonDelegate):
"""该类用于向单元格中添加按钮 任务表格"""
def __init__(self, parent=None):
super(TcRtdButtonDelegate, self).__init__(parent)
def paint(self, painter, option, index):
if not self.parent().indexWidget(index):
button1 = QPushButton(
qtawesome.icon('fa.play', color='#1fbb6f'),
"",
self.parent(),
clicked=self.start_action
)
button1.setIconSize(QSize(15, 15))
button1.setStyleSheet("border:none;")
button2 = QPushButton(
qtawesome.icon('fa.pencil', color='#4c8cf2'),
"",
self.parent(),
clicked=self.edit_action
)
button1.clicked.connect(self.start_action)
button2.clicked.connect(self.edit_action)
button2.oldName = False
button2.isEdit = True
button1.index = [index.row(), index.column()]
button2.index = [index.row(), index.column()]
data = self.parent().model.datas[index.row()]
for x in data[:-1]:
if x != '':
break
else:
button2.isEdit = False
button2.setIcon(qtawesome.icon('fa.save', color='#1fbb6f'))
self.parent().model.editableList.append(button2.index[0])
h_box_layout = QHBoxLayout()
h_box_layout.addWidget(button1)
h_box_layout.addWidget(button2)
h_box_layout.setContentsMargins(2, 0, 0, 2)
h_box_layout.setAlignment(Qt.AlignCenter)
widget = QWidget()
widget.setLayout(h_box_layout)
self.parent().setIndexWidget(
index,
widget
)
def start_action(self):
sender = self.sender()
model = self.parent().model
value = model.datas[sender.index[0]][1]
varType = model.datas[sender.index[0]][5]
min = model.datas[sender.index[0]][6]
max = model.datas[sender.index[0]][7]
compensation = model.datas[sender.index[0]][8]
pattern = re.compile(r'[^0-9\.-]+')
if not value or re.findall(pattern, str(value) + str(compensation)):
reply = QMessageBox.question(self.parent(),
'警告',
"请输入强制值或数字",
QMessageBox.Yes)
return
if min and max:
if float(value) < float(min) or float(value) > float(max):
reply = QMessageBox.question(self.parent(),
'警告',
"超出量程范围",
QMessageBox.Yes)
return
elif min and not max:
if float(value) < float(min):
reply = QMessageBox.question(self.parent(),
'警告',
"超出量程范围",
QMessageBox.Yes)
return
elif max and not min:
if float(value) > float(max):
reply = QMessageBox.question(self.parent(),
'警告',
"超出量程范围",
QMessageBox.Yes)
return
mv = temToMv(varType, float(value))
if not mv and mv != 0:
reply = QMessageBox.question(self.parent(),
'警告',
"输入值有误",
QMessageBox.Yes)
return
mv += float(compensation)
model.table.mvList[sender.index[0]] = mv
model.table.valueList[sender.index[0]] = float(value)
forceVars = Globals.getValue('forceVars')
forceVars.add(model.datas[sender.index[0]][3])
Globals.setValue('forceVars', forceVars)
def edit_action(self):
sender = self.sender()
model = self.parent().model
if sender.isEdit:
sender.setIcon(qtawesome.icon('fa.save', color='#1fbb6f'))
sender.isEdit = False
sender.oldName = model.datas[sender.index[0]][3]
model.editableList.append(sender.index[0])
else:
varMes = model.datas[sender.index[0]]
name, channelNumber, des, varType, min, max, compensationVar = str(varMes[3]), str(varMes[4]), str(varMes[5]),str(varMes[6]), str(varMes[7]), str(varMes[8]), str(varMes[9])
7 months ago
if not name or not varType:
reply = QMessageBox.question(self.parent(),
'警告',
"有字段为空",
QMessageBox.Yes)
return
sender.isEdit = True
model.editableList.remove(sender.index[0])
if sender.oldName:
if sender.oldName == name:
TcRtdManage.editVar(name=sender.oldName, Nname=name, channelNumber = channelNumber, des=des,
varType=varType, min=min, max=max, compensationVar = compensationVar)
else:
if GlobalVarManager.isVarNameExist(name):
QMessageBox.information(self.parent(), '提示', '已有同名变量')
return
else:
TcRtdManage.editVar(name=sender.oldName, Nname=name, channelNumber = channelNumber, des=des,
varType=varType, min=min, max=max, compensationVar = compensationVar)
7 months ago
else:
if GlobalVarManager.isVarNameExist(name):
QMessageBox.information(self.parent(), '提示', '已有同名变量')
return
else:
TcRtdManage.createVar(varName=name, channelNumber = channelNumber, varType=varType, des=des,
min=min, max=max, compensationVar = compensationVar)
7 months ago
sender.setIcon(qtawesome.icon('fa.pencil', color='#4c8cf2'))
rowIndex = sender.index[0]
varMes = TcRtdManage.getByName(name)
varMes.insert(1, '')
varMes.insert(2, '')
varMes.append('')
model.insert_data(varMes, rowIndex)
model.remove_row(rowIndex + 1)
class TcRtdTypeDelegate(TcRtdButtonDelegate):
def __init__(self, parent=None):
super(TcRtdTypeDelegate, self).__init__(parent)
def paint(self, painter, option, index):
if not self.parent().indexWidget(index):
comboBox = QComboBox(self.parent())
if index.row() < 8:
item = ['R', 'S', 'B', 'J', 'T','E', 'K', 'N', 'C', 'A']
comboBox.addItems(item)
i = item.index(self.parent().model.datas[index.row()][index.column()])
comboBox.setCurrentIndex(i)
else:
item = ['PT100']
comboBox.addItems(item)
comboBox.currentIndexChanged.connect(self.indexChange)
comboBox.setObjectName('TcRtdTypeBox')
comboBox.setEditable(True)
comboBox.lineEdit().setAlignment(Qt.AlignCenter)
h_box_layout = QHBoxLayout()
h_box_layout.addWidget(comboBox)
h_box_layout.setContentsMargins(0, 0, 0, 0)
comboBox.index = [index.row(), index.column()]
widget = QWidget()
widget.setLayout(h_box_layout)
row = index.row()
if row % 2 == 0:
comboBox.setStyleSheet("QComboBox { background-color: #EFEFEF }")
else:
comboBox.setStyleSheet("QComboBox { background-color: #e9e7e3 }")
self.parent().setIndexWidget(
index,
widget
)
def indexChange(self):
sender = self.sender()
index = sender.currentIndex()
varType = sender.itemText(index)
name = self.parent().model.datas[sender.index[0]][3]
TcRtdManage.editvarType(name, varType)
self.parent().model.datas[sender.index[0]][sender.index[1]] = varType