168 lines
6.7 KiB
Python
168 lines
6.7 KiB
Python
from PySide6.QtWidgets import QMessageBox, QVBoxLayout
|
||
import logging
|
||
from ui.settings_ui import SettingsUI
|
||
from utils.sql_utils import SQLUtils
|
||
from widgets.inspection_settings_widget import InspectionSettingsWidget
|
||
|
||
class SettingsWidget(SettingsUI):
|
||
def __init__(self, parent=None):
|
||
super().__init__(parent)
|
||
self.parent = parent
|
||
|
||
logging.info("正在初始化SettingsWidget")
|
||
|
||
# 创建检验设置部件
|
||
logging.info("创建InspectionSettingsWidget实例")
|
||
self.inspection_settings = InspectionSettingsWidget()
|
||
|
||
# 移除临时占位符标签并添加检验设置部件
|
||
if hasattr(self, 'inspection_placeholder'):
|
||
logging.info("移除临时占位符")
|
||
self.inspection_layout.removeWidget(self.inspection_placeholder)
|
||
self.inspection_placeholder.hide()
|
||
self.inspection_placeholder.deleteLater()
|
||
else:
|
||
logging.warning("未找到临时占位符标签")
|
||
|
||
# 检查布局是否可用
|
||
if hasattr(self, 'inspection_layout'):
|
||
logging.info("添加检验设置部件到布局")
|
||
self.inspection_layout.addWidget(self.inspection_settings)
|
||
else:
|
||
logging.error("无法找到inspection_layout布局")
|
||
|
||
# 连接信号和槽
|
||
self.connect_signals()
|
||
|
||
# 初始化数据库类型UI状态
|
||
self.update_db_ui_state()
|
||
|
||
logging.info("SettingsWidget初始化完成")
|
||
|
||
def connect_signals(self):
|
||
# 数据库类型选择
|
||
self.sqlite_radio.toggled.connect(self.update_db_ui_state)
|
||
self.pgsql_radio.toggled.connect(self.update_db_ui_state)
|
||
self.mysql_radio.toggled.connect(self.update_db_ui_state)
|
||
|
||
# 按钮动作
|
||
self.test_conn_button.clicked.connect(self.test_connection)
|
||
self.save_db_button.clicked.connect(self.save_db_settings)
|
||
|
||
# 检验配置变更信号
|
||
self.inspection_settings.signal_configs_changed.connect(self.handle_inspection_configs_changed)
|
||
|
||
def update_db_ui_state(self):
|
||
"""根据选择的数据库类型更新UI状态"""
|
||
if self.sqlite_radio.isChecked():
|
||
# SQLite模式下,只需要数据库文件路径
|
||
self.host_input.setEnabled(False)
|
||
self.host_input.setText("")
|
||
self.user_input.setEnabled(False)
|
||
self.user_input.setText("")
|
||
self.password_input.setEnabled(False)
|
||
self.password_input.setText("")
|
||
self.port_input.setEnabled(False)
|
||
self.port_input.setText("")
|
||
self.database_input.setEnabled(True)
|
||
self.database_input.setText("db/jtDB.db")
|
||
elif self.pgsql_radio.isChecked():
|
||
# PostgreSQL模式下,需要完整的连接信息
|
||
self.host_input.setEnabled(True)
|
||
self.host_input.setText("localhost")
|
||
self.user_input.setEnabled(True)
|
||
self.user_input.setText("postgres")
|
||
self.password_input.setEnabled(True)
|
||
self.password_input.setText("")
|
||
self.port_input.setEnabled(True)
|
||
self.port_input.setText("5432")
|
||
self.database_input.setEnabled(True)
|
||
self.database_input.setText("jtDB")
|
||
elif self.mysql_radio.isChecked():
|
||
# MySQL模式下,需要完整的连接信息
|
||
self.host_input.setEnabled(True)
|
||
self.host_input.setText("localhost")
|
||
self.user_input.setEnabled(True)
|
||
self.user_input.setText("root")
|
||
self.password_input.setEnabled(True)
|
||
self.password_input.setText("")
|
||
self.port_input.setEnabled(True)
|
||
self.port_input.setText("3306")
|
||
self.database_input.setEnabled(True)
|
||
self.database_input.setText("jtDB")
|
||
|
||
def get_db_type(self):
|
||
"""获取当前选择的数据库类型"""
|
||
if self.sqlite_radio.isChecked():
|
||
return "sqlite"
|
||
elif self.pgsql_radio.isChecked():
|
||
return "pgsql"
|
||
elif self.mysql_radio.isChecked():
|
||
return "mysql"
|
||
return "sqlite" # 默认返回sqlite
|
||
|
||
def get_connection_params(self):
|
||
"""获取数据库连接参数"""
|
||
db_type = self.get_db_type()
|
||
params = {
|
||
"database": self.database_input.text().strip()
|
||
}
|
||
|
||
if db_type != "sqlite":
|
||
params.update({
|
||
"host": self.host_input.text().strip(),
|
||
"user": self.user_input.text().strip(),
|
||
"password": self.password_input.text().strip(),
|
||
"port": self.port_input.text().strip()
|
||
})
|
||
|
||
return db_type, params
|
||
|
||
def test_connection(self):
|
||
"""测试数据库连接"""
|
||
db_type, params = self.get_connection_params()
|
||
|
||
try:
|
||
# 创建数据库连接
|
||
db = SQLUtils(db_type, **params)
|
||
|
||
# 测试连接
|
||
db.cursor.execute("SELECT 1")
|
||
|
||
# 关闭连接
|
||
db.close()
|
||
|
||
# 显示成功消息
|
||
QMessageBox.information(self, "连接成功", "数据库连接测试成功!")
|
||
logging.info(f"数据库连接测试成功,类型: {db_type}")
|
||
except Exception as e:
|
||
# 显示错误消息
|
||
QMessageBox.critical(self, "连接失败", f"数据库连接测试失败!\n\n错误: {str(e)}")
|
||
logging.error(f"数据库连接测试失败,类型: {db_type}, 错误: {str(e)}")
|
||
|
||
def save_db_settings(self):
|
||
"""保存数据库设置"""
|
||
db_type = self.get_db_type()
|
||
params = self.get_connection_params()[1]
|
||
desc = self.desc_input.text().strip()
|
||
|
||
# 这里应该将设置保存到配置文件中
|
||
# 为了简单起见,这里只显示一个消息框
|
||
settings_info = f"数据库类型: {db_type}\n"
|
||
for key, value in params.items():
|
||
if key != "password":
|
||
settings_info += f"{key}: {value}\n"
|
||
else:
|
||
settings_info += f"{key}: {'*' * len(value)}\n"
|
||
|
||
settings_info += f"说明: {desc}"
|
||
|
||
QMessageBox.information(self, "设置已保存", f"数据库设置已保存!\n\n{settings_info}")
|
||
logging.info(f"数据库设置已保存,类型: {db_type}")
|
||
|
||
def handle_inspection_configs_changed(self):
|
||
"""处理检验配置变更"""
|
||
logging.info("检验配置已更新")
|
||
# 如果有父窗口,通知父窗口更新检验配置
|
||
if self.parent and hasattr(self.parent, 'update_inspection_columns'):
|
||
self.parent.update_inspection_columns() |