importosimportsysimportdillfrompathlibimportPathfromtypingimportOptional,List,Dict,AnyfromPySide6.QtWidgetsimportQDialog,QFileDialog,QMessageBox,QWidgetfromPySide6.QtCoreimportSignal,QEvent# UI importsfromsrc.ui.project_creation_dialog_uiimportUi_FormasUi_ProjectCreationDialog# Core importsfromsrc.core.settingsimport(add_recent_project_path,add_recent_project_creation_path,get_recent_project_creation_paths,)# Utils importsfromsrc.utils.logger_utilityimportlogger# Add the parent directory of 'src' to sys.pathcurrent_dir=Path(__file__).resolve().parentparent_dir=current_dir.parent.parent# Adjust according to your project structuresys.path.append(str(parent_dir))
[docs]classProjectCreationDialog(QDialog):"""Dialog for creating a new project"""projectCreated=Signal(str)@logger.catchdef__init__(self,parent:Optional[QWidget]=None)->None:"""Initializer"""super(ProjectCreationDialog,self).__init__(parent)self.ui:Ui_ProjectCreationDialog=Ui_ProjectCreationDialog()self.ui.setupUi(self)self.loadRecentProjectPaths()self.ui.folderSelectComboBox.mouseDoubleClickEvent=self.selectFolderself.ui.createProjectPushButton.clicked.connect(self.createProject)
[docs]@logger.catchdefloadRecentProjectPaths(self)->None:"""Load the recent project paths into the combo box"""recentPaths:List[str]=get_recent_project_creation_paths()forpathinrecentPaths:self.ui.folderSelectComboBox.addItem(path)
[docs]@logger.catchdefselectFolder(self,event:Optional[QEvent]=None)->None:"""Select a folder for the project"""folder:str=QFileDialog.getExistingDirectory(self,"Select Project Folder")iffolder:self.ui.folderSelectComboBox.addItem(folder)
[docs]@logger.catchdefcreateProject(self)->None:"""Create a new project and emit the projectCreated signal"""projectName:str=self.ui.projectNameLineEdit.text()projectFolder:str=self.ui.folderSelectComboBox.currentText()ifnotprojectNameornotprojectFolder:# Show an error messageQMessageBox.critical(self,"Error","Please enter a project name and select a folder")logger.error("Project name or folder not entered")returnprojectPath:str=os.path.join(projectFolder,projectName)os.makedirs(projectPath,exist_ok=True)# Create project directoryinitialState:Dict[Any,Any]={}# Define your initial state herewithopen(os.path.join(projectPath,f"{projectName}.mstp"),"wb")asfile:dill.dump(initialState,file)# Serialize initial stateself.projectCreated.emit(os.path.join(projectPath,f"{projectName}.mstp"))# Emit signal with project file pathadd_recent_project_creation_path(projectFolder)# Update your settings with the new pathadd_recent_project_path(os.path.join(projectPath,f"{projectName}.mstp"))# Close the dialog and optionally proceed to open the project in the editorself.accept()