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.

121 lines
4.2 KiB
Python

10 months ago
from PyQt5.QtCore import (QCoreApplication, Qt)
from PyQt5.QtWidgets import (QVBoxLayout, QLabel, QLineEdit,
QPushButton, QWidget, QComboBox, QMessageBox, QListView)
10 months ago
from utils.DBModels.ProtocolModel import TCPSetting
class CustomBox(QComboBox):
def __init__(self):
super(CustomBox, self).__init__()
self.setCursor(Qt.PointingHandCursor)
self.setView(QListView())
def showPopup(self):
super(CustomBox, self).showPopup()
pop = self.children()[1]
pop.move(pop.x(), pop.y() + 51)
class TCPSettingWidget(QWidget):
def __init__(self, tcpType):
super(TCPSettingWidget, self).__init__()
self.tcpType = tcpType
self.setObjectName("TCPSettingWidget")
self.setupUI()
10 months ago
def setupUI(self):
# 主布局 - 垂直布局
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(0, 0, 0, 0)
main_layout.setSpacing(8)
10 months ago
# IP地址
self.label = QLabel('IP地址')
10 months ago
self.label.setObjectName("setlabel")
self.ipEdit = QLineEdit()
10 months ago
self.ipEdit.setObjectName("setEdit")
main_layout.addWidget(self.label)
main_layout.addWidget(self.ipEdit)
10 months ago
# 端口号
self.label_2 = QLabel('端口号')
10 months ago
self.label_2.setObjectName("setlabel")
self.portEdit = QLineEdit()
self.portEdit.setObjectName("setEdit")
main_layout.addWidget(self.label_2)
main_layout.addWidget(self.portEdit)
10 months ago
# 通讯频率
self.label_3 = QLabel('通讯频率')
10 months ago
self.label_3.setObjectName("setlabel")
self.freEdit = QLineEdit()
self.freEdit.setObjectName("setEdit")
main_layout.addWidget(self.label_3)
main_layout.addWidget(self.freEdit)
10 months ago
# 起始值
self.label_4 = QLabel('起始值')
self.label_4.setObjectName('setlabel')
10 months ago
self.offsetBox = CustomBox()
self.offsetBox.addItem("1")
self.offsetBox.addItem("0")
self.offsetBox.setObjectName("setBox")
main_layout.addWidget(self.label_4)
main_layout.addWidget(self.offsetBox)
10 months ago
# 保存按钮
self.saveButton = QPushButton('保存')
10 months ago
if self.tcpType == 'master':
self.saveButton.setObjectName("tcpSaveButton")
10 months ago
else:
self.saveButton.setObjectName("setButton")
self.saveButton.clicked.connect(self.saveSetting)
main_layout.addWidget(self.saveButton)
# 添加弹性空间
main_layout.addStretch()
def loadSettings(self):
"""加载设置数据"""
try:
if self.tcpType == 'master':
query = TCPSetting.get_by_id(1)
else:
query = TCPSetting.get_by_id(2)
port = str(query.port)
host = str(query.host)
frequency = str(query.frequency)
offset = str(query.offset)
self.ipEdit.setText(host)
self.portEdit.setText(port)
self.freEdit.setText(frequency)
self.offsetBox.setCurrentText(offset)
except Exception as e:
print(f"加载TCP设置失败: {e}")
# 设置默认值
self.ipEdit.setText("127.0.0.1")
self.portEdit.setText("502")
self.freEdit.setText("1")
self.offsetBox.setCurrentText("1")
10 months ago
def saveSetting(self):
"""保存设置"""
try:
host = self.ipEdit.text()
port = self.portEdit.text()
frequency = self.freEdit.text()
offset = self.offsetBox.currentText()
if self.tcpType == 'master':
TCPSetting.update(host=host, port=port, frequency=frequency, offset=offset).where(TCPSetting.tcpType == 'master').execute()
else:
TCPSetting.update(host=host, port=port, frequency=frequency, offset=offset).where(TCPSetting.tcpType == 'slave').execute()
# 创建消息框
QMessageBox.information(self, '保存成功', f'TCP{self.tcpType}站配置已保存!')
except Exception as e:
QMessageBox.critical(self, '保存失败', f'保存TCP设置时发生错误{str(e)}')