""" 配置分类UI组件演示程序 """ import sys from pathlib import Path from PySide6.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QTextEdit, QLabel, QPushButton, QMessageBox ) from PySide6.QtCore import Qt from config_category_widget import ConfigCategoryWidget class DemoWindow(QMainWindow): """演示窗口""" def __init__(self): super().__init__() self.setWindowTitle("配置分类功能演示") self.resize(800, 600) # 中心部件 central = QWidget() layout = QVBoxLayout() # 标题 title = QLabel("配置分类功能演示") title.setStyleSheet("font-size: 18px; font-weight: bold; padding: 10px;") title.setAlignment(Qt.AlignCenter) layout.addWidget(title) # 配置分类选择器 self.category_widget = ConfigCategoryWidget() self.category_widget.category_changed.connect(self._on_category_changed) layout.addWidget(self.category_widget) # 信息显示区域 info_label = QLabel("当前分类信息:") info_label.setStyleSheet("font-weight: bold; margin-top: 20px;") layout.addWidget(info_label) self.info_text = QTextEdit() self.info_text.setReadOnly(True) self.info_text.setMaximumHeight(200) layout.addWidget(self.info_text) # 配置内容显示区域 config_label = QLabel("配置文件内容:") config_label.setStyleSheet("font-weight: bold; margin-top: 20px;") layout.addWidget(config_label) self.config_text = QTextEdit() self.config_text.setReadOnly(True) layout.addWidget(self.config_text) # 操作按钮 btn_layout = QVBoxLayout() refresh_btn = QPushButton("刷新分类列表") refresh_btn.clicked.connect(self._refresh_categories) btn_layout.addWidget(refresh_btn) load_btn = QPushButton("加载当前分类配置") load_btn.clicked.connect(self._load_current_config) btn_layout.addWidget(load_btn) layout.addLayout(btn_layout) central.setLayout(layout) self.setCentralWidget(central) # 初始加载 self._load_current_config() def _on_category_changed(self, category_name: str, config_path: Path, template_path): """分类改变时的处理""" info = f"分类名称: {category_name}\n" info += f"配置路径: {config_path}\n" info += f"配置存在: {'是' if config_path.exists() else '否'}\n" if template_path: info += f"模板路径: {template_path}\n" info += f"模板存在: {'是' if template_path.exists() else '否'}\n" else: info += "模板路径: (未配置)\n" self.info_text.setText(info) # 自动加载配置内容 self._load_config_content(config_path) def _load_config_content(self, config_path: Path): """加载配置文件内容""" try: if not config_path.exists(): self.config_text.setText("配置文件不存在") return with config_path.open('r', encoding='utf-8') as f: content = f.read() self.config_text.setText(content) except Exception as e: self.config_text.setText(f"读取配置文件失败: {e}") def _refresh_categories(self): """刷新分类列表""" self.category_widget._load_categories() QMessageBox.information(self, "刷新", "分类列表已刷新") def _load_current_config(self): """加载当前分类配置""" category = self.category_widget.get_current_category() if category is None: self.info_text.setText("未选择分类") self.config_text.setText("") return self._on_category_changed( category.name, category.config_path, category.template_path ) def main(): """主函数""" app = QApplication(sys.argv) # 设置样式 app.setStyle("Fusion") window = DemoWindow() window.show() sys.exit(app.exec()) if __name__ == "__main__": main()