diff --git a/pyflow/graphics/window.py b/pyflow/graphics/window.py index 0006a3aa..b501a505 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() @@ -451,15 +453,46 @@ 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() @@ -467,31 +500,6 @@ def closeEvent(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() @@ -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): @@ -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):