Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 Add prompt before exiting the program #271

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
MathisFederico marked this conversation as resolved.
Show resolved Hide resolved

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