diff --git a/src/main/python/pyes/dialogs.py b/src/main/python/pyes/dialogs.py index 6228a69..c0b078e 100644 --- a/src/main/python/pyes/dialogs.py +++ b/src/main/python/pyes/dialogs.py @@ -29,6 +29,29 @@ def __init__(self, parent=None): self.ui.setupUi(self) +class NotSavedDialog(QMessageBox): + def __init__(self, parent=None): + """ + Dialog for asking confirmation of intention of closing the file when unsaved modification are present + """ + super().__init__(parent) + # QMessageBox( + # QMessageBox.Icon.Question, + # "Unsaved changes detected", + # "Unsaved changes detected.\nDo you want to save?", + # ) + self.setIcon(QMessageBox.Icon.Question) + self.setWindowTitle("Unsaved changes detected") + self.setText("The document has been modified.") + self.setInformativeText("Do you want to save your changes?") + self.setStandardButtons( + QMessageBox.StandardButton.Save + | QMessageBox.StandardButton.Discard + | QMessageBox.StandardButton.Cancel + ) + self.setDefaultButton(QMessageBox.StandardButton.Save) + + class WrongFileDialog(QMessageBox): def __init__(self, parent=None): """ diff --git a/src/main/python/pyes/windows/window.py b/src/main/python/pyes/windows/window.py index c0ebe2f..8222ff1 100644 --- a/src/main/python/pyes/windows/window.py +++ b/src/main/python/pyes/windows/window.py @@ -22,6 +22,7 @@ IonicStrengthInfoDialog, IssuesLoadingDialog, NewDialog, + NotSavedDialog, UncertaintyInfoDialog, WrongFileDialog, ) @@ -285,28 +286,35 @@ def file_new(self): """ Display a prompt asking if you want to create a new project """ - dialog = NewDialog(self) - if dialog.exec(): - self.resetFields() - self.project_path = None - self.setWindowTitle("PyES - New Project") + if not self.undostack.isClean(): + choice = NotSavedDialog().exec() - # Resets results - self.result = {} + if choice != QMessageBox.StandardButton.Discard: + return False + else: + choice = NewDialog().exec() - # Disable results windows - if self.PlotWindow: - self.PlotWindow.close() - if self.ExportWindow: - self.ExportWindow.close() + if not choice: + return False - # Disable buttons to show results - self.exportButton.setEnabled(False) - self.plotDistButton.setEnabled(False) - self.actionExport_Results.setEnabled(False) - self.actionPlot_Results.setEnabled(False) - else: - pass + self.resetFields() + self.project_path = None + self.setWindowTitle("PyES - New Project") + + # Resets results + self.result = {} + + # Disable results windows + if self.PlotWindow: + self.PlotWindow.close() + if self.ExportWindow: + self.ExportWindow.close() + + # Disable buttons to show results + self.exportButton.setEnabled(False) + self.plotDistButton.setEnabled(False) + self.actionExport_Results.setEnabled(False) + self.actionPlot_Results.setEnabled(False) def help_about(self): """ @@ -378,6 +386,17 @@ def file_open(self): """ Load a previously saved project """ + if not self.undostack.isClean(): + choice = NotSavedDialog().exec() + + if choice == QMessageBox.StandardButton.Cancel: + return False + elif choice == QMessageBox.StandardButton.Discard: + pass + elif choice == QMessageBox.StandardButton.Save: + if not self.file_save(): + return False + if self.project_path: input_path, _ = QFileDialog.getOpenFileName( self, @@ -1091,20 +1110,7 @@ def closeEvent(self, event): Cleanup before closing. """ if not self.undostack.isClean(): - dlg = QMessageBox( - QMessageBox.Icon.Question, - "Unsaved changes detected", - "Unsaved changes detected.\nDo you want to save?", - ) - dlg.setText("The document has been modified.") - dlg.setInformativeText("Do you want to save your changes?") - dlg.setStandardButtons( - QMessageBox.StandardButton.Save - | QMessageBox.StandardButton.Discard - | QMessageBox.StandardButton.Cancel - ) - dlg.setDefaultButton(QMessageBox.StandardButton.Save) - choice = dlg.exec() + choice = NotSavedDialog().exec() if choice == QMessageBox.StandardButton.Cancel: event.ignore()