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

119 lines
4.4 KiB
Python

2 years ago
from PyQt5 import QtCore
from PyQt5.QtGui import QTextDocument
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QHeaderView, QAbstractItemView, QTableView, QWidget, QDesktopWidget
2 years ago
from UI.BlockParameterModel import VarTableModel, VarButtonDelegate
2 years ago
from utils.DBModels.DeviceParModels import *
class ParamsVHeader(QHeaderView):
def __init__(self, parent, datas):
super().__init__(Qt.Vertical, parent)
self.datas = datas
2 years ago
# print(1111)
2 years ago
def resizeEvent(self, event):
"""Resize table as a whole, need this to enable resizing"""
super(QHeaderView, self).resizeEvent(event)
for index, content in enumerate(self.datas):
self.setSectionResizeMode(index, QHeaderView.Fixed)
2 years ago
height = 50 * (content[3].count('\r\n') + 1)
2 years ago
# valueHeight = 50 * (content[5].count('\r\n') + 1)
# height = desHeight if desHeight > valueHeight else valueHeight
# print(height, index)
2 years ago
if content[4] == 'DS-36':
2 years ago
height = height if height > 5 * 50 else 250
print(height)
# self.resizeSection(index, height)
2 years ago
if content[4] == 'DS-37':
2 years ago
height = height if height > 4 * 50 else 200
2 years ago
if content[4] == 'DS-39':
2 years ago
height = height if height > 7 * 50 else 350
2 years ago
if content[4] == 'DS-50':
2 years ago
height = height if height > 4 * 50 else 200
# self.resizeSection(index, height)
self.resizeSection(index, height)
2 years ago
return
2 years ago
class ParmView(QTableView):
2 years ago
def __init__(self, dbModel = None, blcokIndex = None, blockType = None):
2 years ago
super().__init__()
self.dbModel = dbModel
2 years ago
self.blcokIndex = blcokIndex
2 years ago
self.blockType = blockType
2 years ago
self.allUnitList = UnitTable.getAbleUint()
2 years ago
self.setHeader()
self.setupUi()
2 years ago
self.setData()
2 years ago
def setHeader(self):
2 years ago
self.setItemDelegateForColumn(7, VarButtonDelegate(self))
2 years ago
# self.setItemDelegateForColumn(5, ComboBoxDelegate(self))
2 years ago
2 years ago
self.model = VarTableModel(['序号','索引', '参数名', '描述', '数据类型', '访问', '当前值', '输入值'], [], table = self)
2 years ago
def setupUi(self):
self.setShowGrid(True)
self.setAlternatingRowColors(True)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
2 years ago
self.setObjectName('BlockView')
2 years ago
#设置单元格宽度比例
self.header = self.horizontalHeader()
self.header.setSectionResizeMode(QHeaderView.Interactive)
self.header.setStretchLastSection(True)
self.proxy = QtCore.QSortFilterProxyModel(self)
# # self.parameTableView.proxy = self.proxy
self.proxy.setSourceModel(self.model)
self.setModel(self.proxy)
2 years ago
self.setCustomColumnWidths([1, 1, 3, 8, 2, 2, 2, 3])
2 years ago
# self.header.checkClicked.connect(self.model.headerClick)
# self.setHorizontalHeader(self.header)
# self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
# self.header.setSectionResizeMode(0, QHeaderView.Fixed)
# self.header.resizeSection(0, 70)
def setCustomColumnWidths(self, ratios):
# 计算总比例
totalRatio = sum(ratios)
# 获取窗口宽度
screen = QDesktopWidget().screenGeometry()
width = screen.width() - 80
# 根据比例设置每列的宽度
for i, ratio in enumerate(ratios):
columnWidth = int(width * (ratio / totalRatio))
self.setColumnWidth(i, columnWidth)
2 years ago
def setData(self):
# self.datas = PressureTranslationBlock.getallParame()
2 years ago
self.datas = self.dbModel.getallParameAndID()
2 years ago
for index, data in enumerate(self.datas):
2 years ago
data[6] = ''
desc = data[3].replace('\r\n', '').replace('\n', '')
2 years ago
lines = [desc[i:i+40] + "\r\n" for i in range(0, len(desc), 40)]
2 years ago
# 合并列表为一个字符串,移除最后一个换行符
result = "".join(lines).rstrip("\r\n")
2 years ago
data[3] = result
2 years ago
data = data + ['', '', '']
self.model.append_data(data)
2 years ago
self.resizeHeader()
def resizeHeader(self):
2 years ago
# print(self.verticalHeader())
2 years ago
self.setVerticalHeader(ParamsVHeader(self, self.model.datas))
2 years ago
# QtCore.QTimer.singleShot(0, self.verticalHeader().updateGeometry)
2 years ago
2 years ago
2 years ago