diff --git a/database/db_manager.py b/database/db_manager.py index 1f71d8c..6043e76 100644 --- a/database/db_manager.py +++ b/database/db_manager.py @@ -322,3 +322,34 @@ class DatabaseManager: (project_id,) ) return [ConfigData(*row) for row in cursor.fetchall()] + + def delete_project(self, project_id: int) -> bool: + """删除项目及其关联数据""" + try: + with self.get_connection() as conn: + # 使用事务确保数据一致性 + conn.executescript(f""" + DELETE FROM filters WHERE config_id IN + (SELECT id FROM configs WHERE project_id = {project_id}); + DELETE FROM params WHERE config_id IN + (SELECT id FROM configs WHERE project_id = {project_id}); + DELETE FROM configs WHERE project_id = {project_id}; + DELETE FROM projects WHERE id = {project_id}; + """) + conn.commit() + return True + except Exception: + return False + + def update_project_description(self, project_id: int, new_description: str) -> bool: + """更新项目描述""" + try: + with self.get_connection() as conn: + cursor = conn.execute( + "UPDATE projects SET description = ? WHERE id = ?", + (new_description, project_id) + ) + conn.commit() + return cursor.rowcount > 0 + except Exception: + return False diff --git a/widgets/project_controller.py b/widgets/project_controller.py new file mode 100644 index 0000000..d93f68e --- /dev/null +++ b/widgets/project_controller.py @@ -0,0 +1,124 @@ +from PySide6.QtCore import QObject, Signal +from PySide6.QtWidgets import QMessageBox +from database.db_manager import DatabaseManager +from database.models import ProjectData +from typing import Optional +from widgets.project_dialog import AddProjectDialog + +class ProjectController(QObject): + # 定义信号 + project_changed = Signal(ProjectData) # 项目变更信号 + project_renamed = Signal(str) # 项目重命名信号 + project_deleted = Signal() # 项目删除信号 + + def __init__(self, db_manager: DatabaseManager, view: 'AVAS_WIDGET'): + super().__init__() + self.db_manager = db_manager + self.view = view + self.current_project: Optional[ProjectData] = None + + def _notify_project_changed(self): + """通知项目变更""" + self.project_changed.emit(self.current_project) + if self.current_project: + self.project_renamed.emit(self.current_project.name) + + def create_project(self) -> bool: + """创建新项目""" + dialog = AddProjectDialog(self.view) + if dialog.exec(): + name, description = dialog.get_project_data() + + if not name: + QMessageBox.warning(self.view, "警告", "项目名称不能为空!") + return False + + try: + project_id = self.db_manager.add_project(name, description) + self.current_project = ProjectData( + id=project_id, + name=name, + description=description, + created_at=None + ) + self._notify_project_changed() + return True + + except Exception as e: + QMessageBox.critical(self.view, "错误", f"创建项目失败:{str(e)}") + return False + return False + + def select_project(self, project_id: int) -> bool: + """选择项目""" + projects = self.db_manager.get_all_projects() + for project in projects: + if project.id == project_id: + self.current_project = project + self._notify_project_changed() + return True + return False + + def rename_project(self, new_name: str) -> bool: + """重命名项目""" + if not self.current_project: + QMessageBox.warning(self.view, "警告", "请先选择项目!") + return False + + if not new_name: + QMessageBox.warning(self.view, "警告", "项目名称不能为空!") + return False + + try: + if self.db_manager.update_project_name(self.current_project.id, new_name): + self.current_project.name = new_name + self._notify_project_changed() + return True + else: + QMessageBox.warning(self.view, "警告", "项目名称已存在!") + return False + except Exception as e: + QMessageBox.critical(self.view, "错误", f"重命名项目失败:{str(e)}") + return False + + def update_project_description(self, new_description: str) -> bool: + """更新项目描述""" + if not self.current_project: + QMessageBox.warning(self.view, "警告", "请先选择项目!") + return False + + try: + if self.db_manager.update_project_description(self.current_project.id, new_description): + self.current_project.description = new_description + self._notify_project_changed() + return True + return False + except Exception as e: + QMessageBox.critical(self.view, "错误", f"更新项目描述失败:{str(e)}") + return False + + def delete_project(self) -> bool: + """删除项目""" + if not self.current_project: + QMessageBox.warning(self.view, "警告", "请先选择项目!") + return False + + reply = QMessageBox.question( + self.view, + "确认删除", + f"确定要删除项目 '{self.current_project.name}' 吗?\n此操作将同时删除项目下的所有配置!", + QMessageBox.Yes | QMessageBox.No, + QMessageBox.No + ) + + if reply == QMessageBox.Yes: + try: + if self.db_manager.delete_project(self.current_project.id): + self.current_project = None + self.project_deleted.emit() + return True + return False + except Exception as e: + QMessageBox.critical(self.view, "错误", f"删除项目失败:{str(e)}") + return False + return False \ No newline at end of file