368 lines
15 KiB
Python
368 lines
15 KiB
Python
import logging
|
|
import json
|
|
from PySide6.QtWidgets import QMessageBox, QLabel
|
|
from PySide6.QtCore import QObject, Signal
|
|
from PySide6.QtCore import Qt
|
|
|
|
from ui.inspection_settings_ui import InspectionSettingsUI
|
|
from utils.inspection_config_manager import InspectionConfigManager
|
|
from dao.inspection_dao import InspectionDAO
|
|
|
|
class InspectionSettingsWidget(InspectionSettingsUI):
|
|
"""检验设置部件"""
|
|
|
|
# 定义信号
|
|
signal_configs_changed = Signal() # 配置变更信号
|
|
settings_changed = Signal() # 设置变更信号,与 SettingsWindow 兼容
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.parent = parent
|
|
|
|
# 添加一个明显的标签,方便测试
|
|
self.test_label = QLabel("检验配置已加载", self)
|
|
self.test_label.setStyleSheet("color: green; font-size: 16px; font-weight: bold; background-color: #e0f7e0; padding: 5px; border-radius: 5px;")
|
|
self.test_label.setAlignment(Qt.AlignCenter)
|
|
self.main_layout.insertWidget(0, self.test_label)
|
|
|
|
# 初始化检验配置管理器
|
|
self.inspection_manager = InspectionConfigManager.get_instance()
|
|
|
|
# 保存配置ID
|
|
self.config_ids = [None] * 6
|
|
|
|
# 连接信号和槽
|
|
self.connect_signals()
|
|
|
|
# 加载配置
|
|
self.load_configs()
|
|
|
|
def connect_signals(self):
|
|
"""连接信号和槽"""
|
|
# 保存按钮
|
|
self.save_button.clicked.connect(self.save_configs)
|
|
|
|
# 重置按钮
|
|
self.reset_button.clicked.connect(self.load_configs)
|
|
|
|
# 数据类型下拉框
|
|
for i in range(6):
|
|
position = i + 1
|
|
combo = self.findChild(QObject, f"data_type_combo_{position}")
|
|
if combo:
|
|
combo.currentIndexChanged.connect(lambda idx, pos=position: self.update_form_by_data_type(pos))
|
|
|
|
def load_configs(self):
|
|
"""加载检验配置"""
|
|
try:
|
|
# 设置表单禁用,避免加载过程中的错误操作
|
|
self.set_form_enabled(False)
|
|
|
|
# 重新加载配置
|
|
self.inspection_manager.reload_configs()
|
|
|
|
# 获取所有配置(包括禁用的)
|
|
configs = self.inspection_manager.get_configs(include_disabled=True)
|
|
|
|
# 添加详细日志用于调试
|
|
logging.info(f"加载检验配置成功,共 {len(configs)} 项: {[c.get('name', 'unknown') for c in configs]}")
|
|
self.test_label.setText(f"检验配置已加载({len(configs)}项)")
|
|
|
|
# 清空当前配置ID
|
|
self.config_ids = [None] * 6
|
|
|
|
# 按位置加载配置
|
|
for config in configs:
|
|
position = config.get('position')
|
|
if 1 <= position <= 6:
|
|
self.config_ids[position - 1] = config.get('id')
|
|
self.load_config_to_form(position, config)
|
|
|
|
# 恢复表单可用
|
|
self.set_form_enabled(True)
|
|
|
|
logging.info("已加载检验配置")
|
|
except Exception as e:
|
|
logging.error(f"加载检验配置失败: {str(e)}")
|
|
self.test_label.setText(f"加载检验配置失败: {str(e)}")
|
|
self.test_label.setStyleSheet("color: red; font-size: 16px; font-weight: bold; background-color: #ffe0e0; padding: 5px; border-radius: 5px;")
|
|
QMessageBox.critical(self, "错误", f"加载检验配置失败: {str(e)}")
|
|
# 恢复表单可用
|
|
self.set_form_enabled(True)
|
|
|
|
def load_config_to_form(self, position, config):
|
|
"""将配置加载到表单
|
|
|
|
Args:
|
|
position: 位置序号 (1-6)
|
|
config: 配置数据
|
|
"""
|
|
# 获取分组
|
|
group = self.config_groups[position - 1]
|
|
|
|
# 设置启用状态
|
|
group.setChecked(config.get('enabled', False))
|
|
|
|
# 设置表单值
|
|
# 名称
|
|
name_input = self.findChild(QObject, f"name_input_{position}")
|
|
if name_input:
|
|
name_input.setText(config.get('name', ''))
|
|
|
|
# 显示名称
|
|
display_name_input = self.findChild(QObject, f"display_name_input_{position}")
|
|
if display_name_input:
|
|
display_name_input.setText(config.get('display_name', ''))
|
|
|
|
# 数据类型
|
|
data_type_combo = self.findChild(QObject, f"data_type_combo_{position}")
|
|
if data_type_combo:
|
|
data_type = config.get('data_type', 'text')
|
|
index = data_type_combo.findData(data_type)
|
|
if index >= 0:
|
|
data_type_combo.setCurrentIndex(index)
|
|
|
|
# 单位
|
|
unit_input = self.findChild(QObject, f"unit_input_{position}")
|
|
if unit_input:
|
|
unit_input.setText(config.get('unit', ''))
|
|
|
|
# 最小值
|
|
min_value_spin = self.findChild(QObject, f"min_value_spin_{position}")
|
|
if min_value_spin:
|
|
min_value = config.get('min_value')
|
|
if min_value is not None:
|
|
min_value_spin.setValue(float(min_value))
|
|
else:
|
|
min_value_spin.setValue(0)
|
|
|
|
# 最大值
|
|
max_value_spin = self.findChild(QObject, f"max_value_spin_{position}")
|
|
if max_value_spin:
|
|
max_value = config.get('max_value')
|
|
if max_value is not None:
|
|
max_value_spin.setValue(float(max_value))
|
|
else:
|
|
max_value_spin.setValue(100)
|
|
|
|
# 枚举值
|
|
enum_values_input = self.findChild(QObject, f"enum_values_input_{position}")
|
|
if enum_values_input:
|
|
enum_values = config.get('enum_values')
|
|
if enum_values:
|
|
if isinstance(enum_values, list):
|
|
enum_values_input.setText(','.join(enum_values))
|
|
elif isinstance(enum_values, str):
|
|
try:
|
|
values = json.loads(enum_values)
|
|
if isinstance(values, list):
|
|
enum_values_input.setText(','.join(values))
|
|
else:
|
|
enum_values_input.setText(str(enum_values))
|
|
except:
|
|
enum_values_input.setText(str(enum_values))
|
|
else:
|
|
enum_values_input.setText('')
|
|
|
|
# 是否必填
|
|
required_check = self.findChild(QObject, f"required_check_{position}")
|
|
if required_check:
|
|
required_check.setChecked(config.get('required', False))
|
|
|
|
# 根据数据类型更新表单
|
|
self.update_form_by_data_type(position)
|
|
|
|
def update_form_by_data_type(self, position):
|
|
"""根据数据类型更新表单项的可用性
|
|
|
|
Args:
|
|
position: 位置序号 (1-6)
|
|
"""
|
|
# 获取数据类型
|
|
data_type_combo = self.findChild(QObject, f"data_type_combo_{position}")
|
|
if not data_type_combo:
|
|
return
|
|
|
|
data_type = data_type_combo.currentData()
|
|
|
|
# 获取相关控件
|
|
unit_input = self.findChild(QObject, f"unit_input_{position}")
|
|
min_value_spin = self.findChild(QObject, f"min_value_spin_{position}")
|
|
max_value_spin = self.findChild(QObject, f"max_value_spin_{position}")
|
|
enum_values_input = self.findChild(QObject, f"enum_values_input_{position}")
|
|
|
|
# 根据数据类型设置控件可用性
|
|
if data_type == 'number':
|
|
# 数值类型:启用单位、最小值、最大值,禁用枚举值
|
|
if unit_input:
|
|
unit_input.setEnabled(True)
|
|
if min_value_spin:
|
|
min_value_spin.setEnabled(True)
|
|
if max_value_spin:
|
|
max_value_spin.setEnabled(True)
|
|
if enum_values_input:
|
|
enum_values_input.setEnabled(False)
|
|
enum_values_input.setText('')
|
|
elif data_type == 'enum':
|
|
# 枚举类型:禁用单位、最小值、最大值,启用枚举值
|
|
if unit_input:
|
|
unit_input.setEnabled(False)
|
|
unit_input.setText('')
|
|
if min_value_spin:
|
|
min_value_spin.setEnabled(False)
|
|
min_value_spin.setValue(0)
|
|
if max_value_spin:
|
|
max_value_spin.setEnabled(False)
|
|
max_value_spin.setValue(0)
|
|
if enum_values_input:
|
|
enum_values_input.setEnabled(True)
|
|
else: # text 或其他
|
|
# 文本类型:禁用所有特殊字段
|
|
if unit_input:
|
|
unit_input.setEnabled(False)
|
|
unit_input.setText('')
|
|
if min_value_spin:
|
|
min_value_spin.setEnabled(False)
|
|
min_value_spin.setValue(0)
|
|
if max_value_spin:
|
|
max_value_spin.setEnabled(False)
|
|
max_value_spin.setValue(0)
|
|
if enum_values_input:
|
|
enum_values_input.setEnabled(False)
|
|
enum_values_input.setText('')
|
|
|
|
def get_form_data(self, position):
|
|
"""获取表单数据
|
|
|
|
Args:
|
|
position: 位置序号 (1-6)
|
|
|
|
Returns:
|
|
dict: 表单数据
|
|
"""
|
|
# 获取分组
|
|
group = self.config_groups[position - 1]
|
|
|
|
# 获取控件
|
|
name_input = self.findChild(QObject, f"name_input_{position}")
|
|
display_name_input = self.findChild(QObject, f"display_name_input_{position}")
|
|
data_type_combo = self.findChild(QObject, f"data_type_combo_{position}")
|
|
unit_input = self.findChild(QObject, f"unit_input_{position}")
|
|
min_value_spin = self.findChild(QObject, f"min_value_spin_{position}")
|
|
max_value_spin = self.findChild(QObject, f"max_value_spin_{position}")
|
|
enum_values_input = self.findChild(QObject, f"enum_values_input_{position}")
|
|
required_check = self.findChild(QObject, f"required_check_{position}")
|
|
|
|
# 获取数据
|
|
enabled = group.isChecked()
|
|
name = name_input.text() if name_input else ''
|
|
display_name = display_name_input.text() if display_name_input else ''
|
|
data_type = data_type_combo.currentData() if data_type_combo else 'text'
|
|
unit = unit_input.text() if unit_input and data_type == 'number' else ''
|
|
min_value = min_value_spin.value() if min_value_spin and data_type == 'number' else None
|
|
max_value = max_value_spin.value() if max_value_spin and data_type == 'number' else None
|
|
|
|
# 处理枚举值
|
|
enum_values = None
|
|
if enum_values_input and data_type == 'enum':
|
|
enum_text = enum_values_input.text().strip()
|
|
if enum_text:
|
|
enum_values = [v.strip() for v in enum_text.split(',') if v.strip()]
|
|
|
|
required = required_check.isChecked() if required_check else False
|
|
|
|
# 构建数据
|
|
data = {
|
|
'position': position,
|
|
'name': name,
|
|
'display_name': display_name,
|
|
'enabled': enabled,
|
|
'data_type': data_type,
|
|
'required': required,
|
|
'sort_order': position
|
|
}
|
|
|
|
# 根据数据类型添加特定字段
|
|
if data_type == 'number':
|
|
data['min_value'] = min_value
|
|
data['max_value'] = max_value
|
|
data['unit'] = unit
|
|
elif data_type == 'enum':
|
|
data['enum_values'] = enum_values
|
|
|
|
return data
|
|
|
|
def validate_form(self):
|
|
"""验证表单数据
|
|
|
|
Returns:
|
|
bool: 验证是否通过
|
|
"""
|
|
# 检查是否至少启用一个检验项
|
|
enabled_count = 0
|
|
for group in self.config_groups:
|
|
if group.isChecked():
|
|
enabled_count += 1
|
|
|
|
if enabled_count == 0:
|
|
QMessageBox.warning(self, "验证失败", "请至少启用一个检验项目!")
|
|
return False
|
|
|
|
# 检查每个启用的检验项是否填写了必要信息
|
|
for i in range(6):
|
|
position = i + 1
|
|
group = self.config_groups[i]
|
|
|
|
if group.isChecked():
|
|
# 获取控件
|
|
name_input = self.findChild(QObject, f"name_input_{position}")
|
|
display_name_input = self.findChild(QObject, f"display_name_input_{position}")
|
|
data_type_combo = self.findChild(QObject, f"data_type_combo_{position}")
|
|
enum_values_input = self.findChild(QObject, f"enum_values_input_{position}")
|
|
|
|
# 验证名称和显示名称
|
|
if not name_input.text().strip():
|
|
QMessageBox.warning(self, "验证失败", f"检验项目 {position} 的项目名称不能为空!")
|
|
return False
|
|
|
|
if not display_name_input.text().strip():
|
|
QMessageBox.warning(self, "验证失败", f"检验项目 {position} 的显示名称不能为空!")
|
|
return False
|
|
|
|
# 验证枚举类型的枚举值
|
|
data_type = data_type_combo.currentData()
|
|
if data_type == 'enum' and not enum_values_input.text().strip():
|
|
QMessageBox.warning(self, "验证失败", f"检验项目 {position} 是枚举类型,枚举值不能为空!")
|
|
return False
|
|
|
|
return True
|
|
|
|
def save_configs(self):
|
|
"""保存检验配置"""
|
|
try:
|
|
# 验证表单
|
|
if not self.validate_form():
|
|
return
|
|
|
|
# 获取表单数据
|
|
configs = []
|
|
for position in range(1, 7):
|
|
group = self.config_groups[position - 1]
|
|
if group.isChecked():
|
|
config = self.get_form_data(position)
|
|
configs.append(config)
|
|
|
|
# 保存配置
|
|
success = self.inspection_manager.save_configs(configs)
|
|
|
|
if success:
|
|
QMessageBox.information(self, "成功", "检验配置已保存")
|
|
self.signal_configs_changed.emit()
|
|
self.settings_changed.emit() # 发送设置变更信号
|
|
logging.info("检验配置已保存")
|
|
else:
|
|
QMessageBox.critical(self, "错误", "保存检验配置失败")
|
|
logging.error("保存检验配置失败")
|
|
except Exception as e:
|
|
logging.error(f"保存检验配置失败: {str(e)}")
|
|
QMessageBox.critical(self, "错误", f"保存检验配置失败: {str(e)}") |