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.

264 lines
11 KiB
Python

7 months ago
import sys
sys.path.append('../')
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QIcon
from utils import Globals
class MainTop(QtWidgets.QWidget):
def __init__(self, MainWindow):
super().__init__()
self.MainWindow = MainWindow
self.setupUi()
self.setAttribute(Qt.WA_StyledBackground, True)
self.setupSearchListeners()
7 months ago
# self.trendWidget = TrendWidgets()
def setupUi(self):
self.iconLabol = QtWidgets.QLabel(self)
self.iconLabol.setObjectName("iconLabol")
self.titleLabel = QtWidgets.QLabel(self)
self.titleLabel.setObjectName("titleLabel")
self.searchEdit = QtWidgets.QLineEdit()
action = QtWidgets.QAction(self)
action.setIcon(QIcon('./Static/search.png'))
7 months ago
self.searchEdit.addAction(action, QtWidgets.QLineEdit.LeadingPosition)
self.searchEdit.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.searchEdit.setObjectName('searchEdit')
font = self.searchEdit.font()
font.setPointSize(15)
self.searchEdit.setFont(font)
self.searchEdit.textChanged.connect(self.searchEditTextChanged)
# print(Globals.getValue('SearchWidget'))
self.minBtn = QtWidgets.QPushButton(QIcon('./Static/min.png'), '' ,self)
7 months ago
self.minBtn.setObjectName("minBtn")
self.maxBtn = QtWidgets.QPushButton(QIcon('./Static/normal.png'), '' ,self)
7 months ago
self.maxBtn.setObjectName("maxBtn")
self.closeBtn = QtWidgets.QPushButton(QIcon('./Static/close.png'), '' ,self)
7 months ago
self.closeBtn.setObjectName("closeBtn")
self.iconLabol.setPixmap(QPixmap('./Static/zhjt.png').scaled(50,50))
7 months ago
self.titleLabel.setText("信号发生装置")
self.closeBtn.clicked.connect(self.MainWindow.close)
self.minBtn.clicked.connect(self.MainWindow.showMinimized)
self.maxBtn.clicked.connect(self.showMax)
self.horizontalLayout = QtWidgets.QHBoxLayout(self)
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout.setSpacing(15)
self.horizontalLayout.setContentsMargins(20, 10, 20, 10)
self.horizontalLayout.addWidget(self.iconLabol)
self.horizontalLayout.addWidget(self.titleLabel)
self.horizontalLayout.addWidget(QtWidgets.QSplitter())
self.horizontalLayout.addWidget(self.searchEdit)
self.horizontalLayout.addWidget(QtWidgets.QSplitter())
self.horizontalLayout.addWidget(self.minBtn)
self.horizontalLayout.addWidget(self.maxBtn)
self.horizontalLayout.addWidget(self.closeBtn)
# self.horizontalLayout.setStretch(0, 1)
self.horizontalLayout.setStretch(1, 3)
self.horizontalLayout.setStretch(2, 3)
self.horizontalLayout.setStretch(3, 5)
self.horizontalLayout.setStretch(4, 5)
def searchEditTextChanged(self, text):
# 获取当前活动界面的索引
currentIndex = self.MainWindow.rightWidget.currentIndex()
# 检查是否在趋势界面
if currentIndex == 2: # 趋势界面
trendWidget = self.MainWindow.rightWidget.widget(2)
if hasattr(trendWidget, 'performSearch'):
trendWidget.performSearch(text)
elif hasattr(trendWidget, 'filterVarList'):
trendWidget.filterVarList(text)
return
# 检查是否在控制系统界面
if currentIndex == 6: # 控制系统界面
controlWidget = self.MainWindow.rightWidget.widget(6)
if hasattr(controlWidget, 'searchRules'):
controlWidget.searchRules(text)
return
# 获取当前活动的变量表格
currentVarWidget = self.getCurrentVarTable()
if currentVarWidget:
# 在变量管理界面,直接搜索当前表格
self.performVarTableSearch(currentVarWidget, text)
else:
# 使用原有逻辑处理其他界面
self.performLegacySearch(text)
def getCurrentVarTable(self):
"""获取当前显示的变量表格Widget"""
# 检查当前是否在变量管理界面
if self.MainWindow.rightWidget.currentIndex() != 1:
return None
# 获取变量管理TabWidget
varManageTabWidget = self.MainWindow.rightWidget.widget(1)
# 获取当前活动的Tab
currentTabIndex = varManageTabWidget.currentIndex()
currentTabWidget = varManageTabWidget.widget(currentTabIndex)
# 检查是否是支持搜索的变量表格排除Profibus
if currentTabIndex == 8: # Profibus界面不支持搜索
return None
# 检查是否有变量表格相关属性
if hasattr(currentTabWidget, 'varView') or hasattr(currentTabWidget, 'model'):
return currentTabWidget
return None
def performVarTableSearch(self, varWidget, text):
"""对变量表格执行搜索"""
try:
# 优先使用统一的performSearch接口
if hasattr(varWidget, 'performSearch'):
varWidget.performSearch(text)
# VarWidgets类型Modbus相关和其他继承类
elif hasattr(varWidget, 'proxy') and hasattr(varWidget, 'on_lineEdit_textChanged'):
# 设置搜索列为变量名列通常是第3列
if hasattr(varWidget, 'on_comboBox_currentIndexChanged'):
varWidget.on_comboBox_currentIndexChanged(3)
varWidget.on_lineEdit_textChanged(text)
except Exception as e:
print(f"搜索执行失败: {e}")
def performLegacySearch(self, text):
"""执行原有的搜索逻辑"""
7 months ago
index = Globals.getValue('SearchWidget')
7 months ago
if index == 0:
self.MainWindow.rightWidget.widget(index).on_comboBox_currentIndexChanged(1)
self.MainWindow.rightWidget.widget(index).on_lineEdit_textChanged(text)
elif index == 2: # 趋势界面
if hasattr(self.MainWindow.rightWidget.widget(index), 'filterVarList'):
self.MainWindow.rightWidget.widget(index).filterVarList(text)
7 months ago
elif index == 3:
self.MainWindow.rightWidget.widget(index).on_comboBox_currentIndexChanged(1)
self.MainWindow.rightWidget.widget(index).on_lineEdit_textChanged(text)
elif index == 6:
self.MainWindow.rightWidget.widget(index).on_comboBox_currentIndexChanged(1)
self.MainWindow.rightWidget.widget(index).on_lineEdit_textChanged(text)
elif index == 8:
self.MainWindow.rightWidget.widget(index).on_comboBox_currentIndexChanged(0)
self.MainWindow.rightWidget.widget(index).on_lineEdit_textChanged(text)
def showMax(self):
if self.MainWindow.isMaximized():
self.MainWindow.showNormal()
self.maxBtn.setIcon(QIcon('./Static/max.png'))
7 months ago
self.MainWindow.verticalLayout.setStretch(0, 1)
self.MainWindow.verticalLayout.setStretch(1, 15)
else:
self.MainWindow.showMaximized()
self.MainWindow.verticalLayout.setStretch(0, 1)
self.MainWindow.verticalLayout.setStretch(1, 18)
self.maxBtn.setIcon(QIcon('./Static/normal.png'))
7 months ago
def mouseDoubleClickEvent(self, e): # 双击
self.showMax()
def setupSearchListeners(self):
"""设置搜索相关的事件监听"""
try:
# 监听主界面切换
self.MainWindow.rightWidget.currentChanged.connect(self.onMainWidgetChanged)
# 监听变量管理Tab切换
if hasattr(self.MainWindow, 'varManageTabWidget'):
self.MainWindow.varManageTabWidget.currentChanged.connect(self.onVarTabChanged)
except Exception as e:
print(f"设置搜索监听失败: {e}")
def onMainWidgetChanged(self, index):
"""主界面切换时更新搜索状态"""
self.updateSearchPlaceholder()
self.searchEdit.clear()
def onVarTabChanged(self, index):
"""变量管理Tab切换时更新搜索状态"""
if self.MainWindow.rightWidget.currentIndex() == 1: # 在变量管理界面
self.updateSearchPlaceholder()
self.searchEdit.clear()
def updateSearchPlaceholder(self):
"""根据当前界面更新搜索框占位符和状态"""
try:
currentIndex = self.MainWindow.rightWidget.currentIndex()
if currentIndex == 2:
# 趋势界面
self.searchEdit.setPlaceholderText("搜索历史变量...")
self.searchEdit.setEnabled(True)
elif currentIndex == 1:
# 变量管理界面
currentVarWidget = self.getCurrentVarTable()
if currentVarWidget:
# 支持搜索的变量表格
placeholder = self.getVarWidgetPlaceholder(currentVarWidget)
self.searchEdit.setPlaceholderText(placeholder)
self.searchEdit.setEnabled(True)
else:
# 不支持搜索的Tab如Profibus
self.searchEdit.setPlaceholderText("当前界面不支持搜索")
self.searchEdit.setEnabled(False)
elif currentIndex == 6:
# 控制系统界面
self.searchEdit.setPlaceholderText("搜索规则...")
self.searchEdit.setEnabled(True)
else:
# 其他界面
self.searchEdit.setPlaceholderText("搜索...")
self.searchEdit.setEnabled(True)
except Exception as e:
print(f"更新搜索占位符失败: {e}")
self.searchEdit.setPlaceholderText("搜索...")
self.searchEdit.setEnabled(True)
def getVarWidgetPlaceholder(self, varWidget):
"""根据变量Widget类型获取搜索占位符"""
try:
# 获取当前Tab索引来确定类型
varManageTabWidget = self.MainWindow.rightWidget.widget(1)
currentTabIndex = varManageTabWidget.currentIndex()
placeholderMap = {
0: "搜索Modbus TCP主站变量...",
1: "搜索Modbus TCP从站变量...",
2: "搜索Modbus RTU主站变量...",
3: "搜索Modbus RTU从站变量...",
4: "搜索IO变量...",
5: "搜索TC/RTD变量...",
6: "搜索HART读取变量...",
7: "搜索HART模拟变量...",
9: "搜索远程通讯变量..."
}
return placeholderMap.get(currentTabIndex, "搜索变量...")
except Exception as e:
print(f"获取占位符失败: {e}")
return "搜索变量..."
7 months ago