import traceback from PyQt5.QtWidgets import QDialog, QWidget, QDialogButtonBox, QFileDialog, QPushButton, QMessageBox from model import Project from business import ProjectManager from properties.config import PROJECT_EXTENSION from ui.project.new_project_dialog_ui import Ui_NewProjectDialog class NewProjectDialog(QDialog): def __init__(self, projectManager : ProjectManager, parent : QWidget = None): super().__init__(parent) self.__projectManager = projectManager self.project : Project = None self.setupUi() self.setupActions() self.validate() def setupUi(self): self.ui = Ui_NewProjectDialog() self.ui.setupUi(self) self.__buttonOk : QPushButton = self.ui.buttonBox.button(QDialogButtonBox.Ok) def setupActions(self): self.ui.buttonBrowse.pressed.connect(self.browsePressed) self.ui.textProjectPath.textChanged.connect(self.projectPathChanged) self.ui.buttonBox.accepted.connect(self.okPressed) def validate(self): valid = True if not self.ui.textProjectPath.text(): valid = False self.__buttonOk.setEnabled(valid) def browsePressed(self): path = QFileDialog.getSaveFileName( self, self.tr('New project'), '', self.tr(f'Project files (*.{PROJECT_EXTENSION});;All files (*.*)')) if path[0]: self.ui.textProjectPath.setText(path[0]) def projectPathChanged(self): self.validate() def okPressed(self): try: path = self.ui.textProjectPath.text() self.project = self.__projectManager.newProject(path) self.accept() except Exception as ex: print("Failed to create project: ", traceback.format_exc()) QMessageBox.critical(self, self.tr('An error occurred'), self.tr('The project could not be created in the given location.'))