Skip to content

Commit

Permalink
[enh] Aske for confirmation when changing projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Kastakin committed Jun 15, 2023
1 parent 3871ee8 commit d70732b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
23 changes: 23 additions & 0 deletions src/main/python/pyes/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
72 changes: 39 additions & 33 deletions src/main/python/pyes/windows/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
IonicStrengthInfoDialog,
IssuesLoadingDialog,
NewDialog,
NotSavedDialog,
UncertaintyInfoDialog,
WrongFileDialog,
)
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d70732b

Please sign in to comment.