From dc834093fe4743931f64036252c754c1b9251809 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Wed, 16 Feb 2022 00:02:56 +0100 Subject: [PATCH 1/4] :tada: Add prompt before exiting the program --- pyflow/graphics/window.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pyflow/graphics/window.py b/pyflow/graphics/window.py index 0006a3aa..2d5ee1a2 100644 --- a/pyflow/graphics/window.py +++ b/pyflow/graphics/window.py @@ -451,15 +451,17 @@ 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 closeEvent(self, event: QCloseEvent): - """Save and quit the application.""" + """Handle the event when the window is about to be closed.""" + + quit_msg = "Exit without saving?" + reply = QMessageBox.question( + self, "Message", quit_msg, QMessageBox.Yes, QMessageBox.No + ) + + if reply == QMessageBox.No: + event.ignore() + self.mdiArea.closeAllSubWindows() if self.mdiArea.currentSubWindow(): event.ignore() From a981f7743a093b58fdbf886ad627890f70a098d5 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Wed, 16 Feb 2022 00:36:01 +0100 Subject: [PATCH 2/4] :tada: Improve exit prompting --- pyflow/graphics/window.py | 72 +++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/pyflow/graphics/window.py b/pyflow/graphics/window.py index 2d5ee1a2..4f88e3b2 100644 --- a/pyflow/graphics/window.py +++ b/pyflow/graphics/window.py @@ -16,6 +16,7 @@ QMainWindow, QMessageBox, QMdiArea, + QCheckBox, ) from pyflow.graphics.widget import Widget @@ -71,6 +72,7 @@ def __init__(self): self.updateMenus() # Window properties + self.never_show_exit_prompt = False self.readSettings() self.show() @@ -454,14 +456,31 @@ def onEditRun(self): def closeEvent(self, event: QCloseEvent): """Handle the event when the window is about to be closed.""" - quit_msg = "Exit without saving?" - reply = QMessageBox.question( - self, "Message", quit_msg, QMessageBox.Yes, QMessageBox.No - ) + if self.never_show_exit_prompt: + self.closeWindow(event) + return - if reply == QMessageBox.No: + 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 + + 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() @@ -469,30 +488,30 @@ def closeEvent(self, event: QCloseEvent): self.writeSettings() event.accept() - def maybeSave(self) -> bool: - """Ask for save and returns if the file should be closed. + # 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. + # Returns: + # True if the file should be closed, False otherwise. - """ - if not self.isModified(): - return True + # """ + # 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, - ) + # 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 + # 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.""" @@ -510,6 +529,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): @@ -518,6 +539,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): From 2532e7f0a902521cc4619f495975fc84032af02e Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Wed, 16 Feb 2022 14:17:15 +0100 Subject: [PATCH 3/4] :tada: Remove exit prompt when all is saved --- pyflow/graphics/window.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pyflow/graphics/window.py b/pyflow/graphics/window.py index 4f88e3b2..b3181d24 100644 --- a/pyflow/graphics/window.py +++ b/pyflow/graphics/window.py @@ -453,13 +453,24 @@ def onEditRun(self): if len(selected_blocks) == 1: selected_blocks[0].run_left() + 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): """Handle the event when the window is about to be closed.""" - if self.never_show_exit_prompt: + 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) @@ -472,6 +483,7 @@ def closeEvent(self, event: QCloseEvent): if msgbox.checkBox().checkState() == Qt.CheckState.Checked: self.never_show_exit_prompt = True + self.writeSettings() if msgbox.result() == int(str(QMessageBox.No)): event.ignore() From 84bb7f8b146c31e09a3509454c00cc46172af90e Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Wed, 16 Feb 2022 17:38:56 +0100 Subject: [PATCH 4/4] :sparkles: Remove unused function --- pyflow/graphics/window.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/pyflow/graphics/window.py b/pyflow/graphics/window.py index b3181d24..b501a505 100644 --- a/pyflow/graphics/window.py +++ b/pyflow/graphics/window.py @@ -500,31 +500,6 @@ def closeWindow(self, event: QCloseEvent): 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()