113 lines
4.7 KiB
Python
113 lines
4.7 KiB
Python
import logging
|
||
from PySide6.QtCore import Signal
|
||
from PySide6.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox
|
||
from widgets.serial_settings_widget import SerialSettingsWidget
|
||
from widgets.settings_widget import SettingsWidget
|
||
from ui.electricity_settings_ui import ElectricitySettingsUI
|
||
|
||
class SettingsWindow(QDialog):
|
||
"""设置窗口,直接使用SettingsWidget中的标签页"""
|
||
|
||
# 定义信号
|
||
settings_changed = Signal()
|
||
|
||
def __init__(self, parent=None):
|
||
super().__init__(parent)
|
||
logging.info("正在初始化SettingsWindow")
|
||
|
||
# 设置窗口标题和大小
|
||
self.setWindowTitle("系统设置")
|
||
self.resize(900, 700)
|
||
self.setModal(True)
|
||
|
||
# 创建主布局
|
||
main_layout = QVBoxLayout(self)
|
||
main_layout.setContentsMargins(10, 10, 10, 10)
|
||
|
||
# 创建设置部件
|
||
self.settings_widget = SettingsWidget(self)
|
||
main_layout.addWidget(self.settings_widget)
|
||
|
||
# 添加串口设置到标签页
|
||
self.serial_settings = SerialSettingsWidget(self)
|
||
self.settings_widget.tab_widget.addTab(self.serial_settings, "串口设置")
|
||
|
||
# 应用相机刷新按钮修复
|
||
self._fix_camera_refresh_button()
|
||
|
||
# 添加按钮
|
||
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
||
self.button_box.accepted.connect(self.accept)
|
||
self.button_box.rejected.connect(self.reject)
|
||
main_layout.addWidget(self.button_box)
|
||
|
||
# 连接信号
|
||
self.serial_settings.settings_changed.connect(self.settings_changed.emit)
|
||
|
||
logging.info("SettingsWindow初始化完成")
|
||
|
||
def _fix_camera_refresh_button(self):
|
||
"""内联实现的刷新按钮修复逻辑,用于导入模块失败的情况"""
|
||
try:
|
||
logging.info("使用内联方法修复相机刷新按钮...")
|
||
|
||
# 获取设置部件
|
||
settings_widget = self.settings_widget
|
||
|
||
# 确保相机设置组件存在
|
||
if not hasattr(settings_widget, 'camera_settings'):
|
||
logging.error("设置窗口中没有camera_settings属性")
|
||
return
|
||
|
||
# 查找刷新按钮
|
||
refresh_button = None
|
||
|
||
# 方法1: 在settings_widget上查找
|
||
if hasattr(settings_widget, 'refresh_button'):
|
||
refresh_button = settings_widget.refresh_button
|
||
logging.info("在settings_widget中找到刷新按钮")
|
||
# 方法2: 在camera_tab中查找
|
||
elif hasattr(settings_widget, 'camera_tab'):
|
||
from PySide6.QtWidgets import QPushButton
|
||
for child in settings_widget.camera_tab.findChildren(QPushButton):
|
||
if child.text() == "刷新设备":
|
||
refresh_button = child
|
||
logging.info("在camera_tab中找到刷新按钮")
|
||
break
|
||
# 方法3: 在camera_settings中查找
|
||
elif hasattr(settings_widget.camera_settings, 'refresh_button'):
|
||
refresh_button = settings_widget.camera_settings.refresh_button
|
||
logging.info("在camera_settings中找到刷新按钮")
|
||
|
||
# 如果找到按钮,则绑定事件
|
||
if refresh_button:
|
||
from PySide6.QtCore import QObject
|
||
if isinstance(refresh_button, QObject):
|
||
try:
|
||
# 断开现有连接
|
||
refresh_button.clicked.disconnect()
|
||
except:
|
||
pass
|
||
|
||
# 连接到刷新方法
|
||
refresh_button.clicked.connect(settings_widget.camera_settings.refresh_devices)
|
||
logging.info("成功绑定刷新按钮到refresh_devices方法")
|
||
|
||
# 手动调用一次
|
||
settings_widget.camera_settings.refresh_devices()
|
||
logging.info("已手动调用refresh_devices初始化设备列表")
|
||
else:
|
||
logging.error(f"刷新按钮不是QObject: {type(refresh_button)}")
|
||
else:
|
||
logging.error("未找到刷新设备按钮")
|
||
|
||
except Exception as e:
|
||
logging.error(f"内联修复相机刷新按钮时发生错误: {str(e)}")
|
||
|
||
def accept(self):
|
||
"""确认按钮处理,保存所有设置并发送设置变更信号"""
|
||
# 通知设置已变更
|
||
self.settings_changed.emit()
|
||
|
||
# 调用父类方法关闭对话框
|
||
super().accept() |