275 lines
9.5 KiB
Python
275 lines
9.5 KiB
Python
"""
|
||
配置分类功能集成代码示例
|
||
直接复制到 ui_main.py 中使用
|
||
"""
|
||
|
||
# ============================================================
|
||
# 1. 在文件顶部添加导入
|
||
# ============================================================
|
||
|
||
from config_category_widget import ConfigCategoryWidget
|
||
from config_category import get_default_category_manager, ConfigCategory
|
||
|
||
|
||
# ============================================================
|
||
# 2. 在 MainWindow.__init__ 中添加分类选择器
|
||
# ============================================================
|
||
|
||
# 在工具栏布局中,self.cfg_btn 之前添加:
|
||
|
||
# 配置分类选择器
|
||
self.category_widget = ConfigCategoryWidget(on_change=self.schedule_autosave)
|
||
self.category_widget.category_changed.connect(self._on_category_changed)
|
||
toolbar_layout.addWidget(self.category_widget)
|
||
|
||
# 添加分隔符(可选)
|
||
separator = QFrame()
|
||
separator.setFrameShape(QFrame.VLine)
|
||
separator.setFrameShadow(QFrame.Sunken)
|
||
toolbar_layout.addWidget(separator)
|
||
|
||
|
||
# ============================================================
|
||
# 3. 添加分类切换处理方法
|
||
# ============================================================
|
||
|
||
def _on_category_changed(self, category_name: str, config_path: Path, template_path) -> None:
|
||
"""
|
||
分类切换时的处理
|
||
|
||
Args:
|
||
category_name: 分类名称
|
||
config_path: 配置文件路径
|
||
template_path: 模板文件路径(可能为None)
|
||
"""
|
||
try:
|
||
self.logger.info(f"切换到配置分类: {category_name}")
|
||
|
||
# 加载配置
|
||
self.config = AppConfig.load(config_path)
|
||
self._autosave_path = config_path
|
||
|
||
# 加载模板
|
||
if template_path and template_path.exists():
|
||
self.template_path = template_path
|
||
self.logger.info(f"已加载模板: {template_path}")
|
||
else:
|
||
self.logger.warning(f"分类 {category_name} 没有模板文件")
|
||
|
||
# 更新UI显示
|
||
self.apply_config_to_ui()
|
||
|
||
# 保存状态
|
||
self._save_last_state(template=self.template_path, config=self._autosave_path)
|
||
|
||
# 提示用户
|
||
msg = f"已切换到配置分类: {category_name}\n"
|
||
msg += f"配置文件: {config_path.name}\n"
|
||
if template_path:
|
||
msg += f"模板文件: {template_path.name}"
|
||
else:
|
||
msg += "模板文件: (未配置)"
|
||
|
||
QMessageBox.information(self, "分类切换", msg)
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"切换配置分类失败: {e}")
|
||
QMessageBox.warning(self, "错误", f"切换配置分类失败:\n{e}")
|
||
|
||
|
||
# ============================================================
|
||
# 4. 修改 _do_autosave 方法
|
||
# ============================================================
|
||
|
||
def _do_autosave(self) -> None:
|
||
"""自动保存配置"""
|
||
# 如果没有设置保存路径,尝试使用当前分类的配置路径
|
||
if self._autosave_path is None:
|
||
category = self.category_widget.get_current_category()
|
||
if category:
|
||
self._autosave_path = category.config_path
|
||
self.logger.info(f"使用分类配置路径: {self._autosave_path}")
|
||
|
||
# 原有的保存逻辑
|
||
if self._autosave_path:
|
||
try:
|
||
self.apply_ui_to_config()
|
||
self.config.save(self._autosave_path)
|
||
self.logger.info(f"配置已自动保存到: {self._autosave_path}")
|
||
except Exception as e:
|
||
self.logger.error(f"自动保存配置失败: {e}")
|
||
|
||
|
||
# ============================================================
|
||
# 5. 修改 _load_default_config_if_exists 方法
|
||
# ============================================================
|
||
|
||
def _load_default_config_if_exists(self) -> None:
|
||
"""加载默认配置,优先使用分类配置"""
|
||
|
||
# 检查是否有可用的配置分类
|
||
if self.category_widget.has_categories():
|
||
# 有分类,使用第一个分类的配置
|
||
category = self.category_widget.get_current_category()
|
||
if category:
|
||
try:
|
||
self.config = AppConfig.load(category.config_path)
|
||
self._autosave_path = category.config_path
|
||
|
||
if category.template_path:
|
||
self.template_path = category.template_path
|
||
|
||
self.logger.info(f"已加载分类配置: {category.name}")
|
||
return
|
||
except Exception as e:
|
||
self.logger.error(f"加载分类配置失败: {e}")
|
||
|
||
# 没有分类或加载失败,使用根目录的 default.json(向后兼容)
|
||
default_path = APP_DIR / DEFAULT_CONFIG_FILENAME
|
||
if default_path.exists():
|
||
try:
|
||
self.config = AppConfig.load(default_path)
|
||
self._autosave_path = default_path
|
||
self.logger.info("已加载根目录配置: default.json")
|
||
except Exception as e:
|
||
self.logger.error(f"加载default.json失败: {e}")
|
||
else:
|
||
self.logger.info("未找到配置文件,等待用户配置")
|
||
|
||
|
||
# ============================================================
|
||
# 6. 在配置对话框中显示当前分类信息(可选)
|
||
# ============================================================
|
||
|
||
def open_config_dialog(self, preview: bool = False) -> None:
|
||
# ... 现有代码 ...
|
||
|
||
# 在对话框顶部显示当前分类信息
|
||
if hasattr(self, 'category_widget'):
|
||
category = self.category_widget.get_current_category()
|
||
if category:
|
||
category_info = QLabel(f"当前分类: {category.name}")
|
||
category_info.setStyleSheet(
|
||
"background-color: #e3f2fd; padding: 8px; "
|
||
"border-radius: 4px; font-weight: bold; color: #1976d2;"
|
||
)
|
||
# 将此标签添加到对话框布局的顶部
|
||
# dlg_layout.insertWidget(0, category_info)
|
||
|
||
|
||
# ============================================================
|
||
# 7. 完整的集成检查清单
|
||
# ============================================================
|
||
|
||
"""
|
||
集成检查清单:
|
||
|
||
□ 1. 已添加导入语句
|
||
□ 2. 已在工具栏添加分类选择器
|
||
□ 3. 已添加 _on_category_changed 方法
|
||
□ 4. 已修改 _do_autosave 方法
|
||
□ 5. 已修改 _load_default_config_if_exists 方法
|
||
□ 6. 已运行 setup_config_categories.py 初始化
|
||
□ 7. 已运行 test_config_category.py 验证
|
||
□ 8. 已测试分类切换功能
|
||
□ 9. 已测试配置保存功能
|
||
□ 10. 已测试程序启动加载
|
||
|
||
测试步骤:
|
||
1. 启动程序,检查分类选择器是否显示
|
||
2. 切换分类,检查配置和模板是否正确加载
|
||
3. 修改配置,检查是否保存到正确的分类文件
|
||
4. 重启程序,检查是否加载上次使用的分类
|
||
5. 开始实验,检查是否使用当前分类的配置
|
||
6. 生成报告,检查是否使用当前分类的模板
|
||
"""
|
||
|
||
|
||
# ============================================================
|
||
# 8. 调试辅助代码(可选)
|
||
# ============================================================
|
||
|
||
def _debug_category_info(self):
|
||
"""打印当前分类信息(用于调试)"""
|
||
if hasattr(self, 'category_widget'):
|
||
category = self.category_widget.get_current_category()
|
||
if category:
|
||
print(f"当前分类: {category.name}")
|
||
print(f"配置路径: {category.config_path}")
|
||
print(f"模板路径: {category.template_path}")
|
||
print(f"自动保存路径: {self._autosave_path}")
|
||
else:
|
||
print("未选择分类")
|
||
else:
|
||
print("分类选择器未初始化")
|
||
|
||
|
||
# ============================================================
|
||
# 9. 错误处理示例
|
||
# ============================================================
|
||
|
||
def _safe_load_category_config(self, category: ConfigCategory) -> bool:
|
||
"""
|
||
安全加载分类配置(带错误处理)
|
||
|
||
Returns:
|
||
是否成功加载
|
||
"""
|
||
try:
|
||
# 检查配置文件是否存在
|
||
if not category.config_path.exists():
|
||
self.logger.error(f"配置文件不存在: {category.config_path}")
|
||
QMessageBox.warning(
|
||
self,
|
||
"配置错误",
|
||
f"分类 {category.name} 的配置文件不存在"
|
||
)
|
||
return False
|
||
|
||
# 加载配置
|
||
self.config = AppConfig.load(category.config_path)
|
||
self._autosave_path = category.config_path
|
||
|
||
# 加载模板(可选)
|
||
if category.template_path and category.template_path.exists():
|
||
self.template_path = category.template_path
|
||
|
||
# 更新UI
|
||
self.apply_config_to_ui()
|
||
|
||
self.logger.info(f"成功加载分类配置: {category.name}")
|
||
return True
|
||
|
||
except Exception as e:
|
||
self.logger.error(f"加载分类配置失败: {e}")
|
||
QMessageBox.critical(
|
||
self,
|
||
"加载失败",
|
||
f"加载分类 {category.name} 的配置失败:\n{e}"
|
||
)
|
||
return False
|
||
|
||
|
||
# ============================================================
|
||
# 10. 使用提示
|
||
# ============================================================
|
||
|
||
"""
|
||
使用提示:
|
||
|
||
1. 复制需要的代码片段到 ui_main.py 对应位置
|
||
2. 根据实际情况调整代码(如变量名、方法名等)
|
||
3. 确保导入语句在文件顶部
|
||
4. 确保方法添加到 MainWindow 类中
|
||
5. 运行初始化脚本创建配置分类
|
||
6. 测试所有功能是否正常
|
||
|
||
注意事项:
|
||
|
||
- 保持代码缩进一致
|
||
- 检查方法签名是否匹配
|
||
- 确保信号连接正确
|
||
- 添加适当的日志记录
|
||
- 处理可能的异常情况
|
||
"""
|