Skip to content

Commit

Permalink
🎉 Add prompt before exiting the program (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisFederico authored Feb 16, 2022
2 parents 2768c1a + 84bb7f8 commit 584b84c
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions pyflow/graphics/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
QMainWindow,
QMessageBox,
QMdiArea,
QCheckBox,
)

from pyflow.graphics.widget import Widget
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(self):
self.updateMenus()

# Window properties
self.never_show_exit_prompt = False
self.readSettings()
self.show()

Expand Down Expand Up @@ -451,47 +453,53 @@ def onEditRun(self):
if len(selected_blocks) == 1:
selected_blocks[0].run_left()

# def closeEvent(self, event:QEvent):
# """ Save and quit the application. """
# if self.maybeSave():
# event.accept()
# else:
# event.ignore()
def allWidgetsAreSaved(self):
"""Return true if all widgets are saved."""

for widget in self.mdiArea.subWindowList():
if isinstance(widget.widget(), Widget):
if widget.widget().isModified():
return False

return True

def closeEvent(self, event: QCloseEvent):
"""Save and quit the application."""
"""Handle the event when the window is about to be closed."""

if self.allWidgetsAreSaved() or self.never_show_exit_prompt:
self.closeWindow(event)
return

# Show the exit without saving prompt
quit_msg = "Exit without saving?"
msgbox = QMessageBox(self)
msgbox.setText(quit_msg)
msgbox.setWindowTitle("Exit?")
msgbox.addButton(QMessageBox.Yes)
msgbox.addButton(QMessageBox.No)
cb = QCheckBox("Never show this again")
msgbox.setCheckBox(cb)
msgbox.exec()

if msgbox.checkBox().checkState() == Qt.CheckState.Checked:
self.never_show_exit_prompt = True
self.writeSettings()

if msgbox.result() == int(str(QMessageBox.No)):
event.ignore()
return

self.closeWindow(event)

def closeWindow(self, event: QCloseEvent):
"""Close the window."""
self.mdiArea.closeAllSubWindows()
if self.mdiArea.currentSubWindow():
event.ignore()
else:
self.writeSettings()
event.accept()

def maybeSave(self) -> bool:
"""Ask for save and returns if the file should be closed.
Returns:
True if the file should be closed, False otherwise.
"""
if not self.isModified():
return True

answer = QMessageBox.warning(
self,
"About to loose you work?",
"The file has been modified.\n" "Do you want to save your changes?",
QMessageBox.StandardButton.Save
| QMessageBox.StandardButton.Discard
| QMessageBox.StandardButton.Cancel,
)

if answer == QMessageBox.StandardButton.Save:
return self.onFileSave()
if answer == QMessageBox.StandardButton.Discard:
return True
return False

def activeMdiChild(self) -> Widget:
"""Get the active Widget if existing."""
activeSubWindow = self.mdiArea.activeSubWindow()
Expand All @@ -508,6 +516,8 @@ def readSettings(self):
self.resize(size)
if settings.value("isMaximized", False) == "true":
self.showMaximized()
if settings.value("NeverShowExitPrompt", False) == "true":
self.never_show_exit_prompt = True
LOGGER.info("Loaded settings under Bycelium/Pyflow")

def writeSettings(self):
Expand All @@ -516,6 +526,7 @@ def writeSettings(self):
settings.setValue("pos", self.pos())
settings.setValue("size", self.size())
settings.setValue("isMaximized", self.isMaximized())
settings.setValue("NeverShowExitPrompt", self.never_show_exit_prompt)
LOGGER.info("Saved settings under Bycelium/Pyflow")

def setActiveSubWindow(self, window):
Expand Down

0 comments on commit 584b84c

Please sign in to comment.