From 9a13360441f4f4fc0ad15726e1c24c3bdeb96f36 Mon Sep 17 00:00:00 2001 From: John Haddon Date: Mon, 11 Dec 2023 14:34:21 +0000 Subject: [PATCH] ScriptWindow : Add "Save" option to "Discard Changes?" dialogue --- Changes.md | 1 + python/GafferUI/ScriptWindow.py | 43 ++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Changes.md b/Changes.md index 71485b4c31b..4d6d01f14fe 100644 --- a/Changes.md +++ b/Changes.md @@ -14,6 +14,7 @@ Improvements - Incomplete jobs are now killed automatically when the application is closed, after prompting to confirm that shutdown should go ahead. - Cache : Increased default computation cache size to 8Gb. Call `Gaffer.ValuePlug.setCacheMemoryLimit()` from a startup file to override this. - Dispatcher : Reduced internal overhead of `dispatch()` call, with one benchmark showing around a 3x speedup. +- ScriptWindow : Added "Save" option to dialogue shown when closing a window containing unsaved changes. Fixes ----- diff --git a/python/GafferUI/ScriptWindow.py b/python/GafferUI/ScriptWindow.py index 45b98b122ff..aa7393df199 100644 --- a/python/GafferUI/ScriptWindow.py +++ b/python/GafferUI/ScriptWindow.py @@ -118,12 +118,22 @@ def _acceptsClose( self ) : f = self.__script["fileName"].getValue() f = f.rpartition( "/" )[2] if f else "untitled" - dialogue = GafferUI.ConfirmationDialogue( + dialogue = _ChoiceDialogue( "Discard Unsaved Changes?", - "The file %s has unsaved changes. Do you want to discard them?" % f, - confirmLabel = "Discard" + f"The file \"{f}\" has unsaved changes. Do you want to discard them?", + choices = [ "Cancel", "Save", "Discard" ] ) - return dialogue.waitForConfirmation( parentWindow=self ) + choice = dialogue.waitForChoice( parentWindow=self ) + + if choice == "Discard" : + return True + elif choice == "Save" : + ## \todo Is it a bit odd that ScriptWindow should depend on FileMenu + # like this? Should the code be moved somewhere else? + GafferUI.FileMenu.save( self.menuBar() ) + return True + else : + return False def __closed( self, widget ) : @@ -194,6 +204,31 @@ def __staticScriptRemoved( scriptContainer, script ) : if not len( scriptContainer.children() ) and GafferUI.EventLoop.mainEventLoop().running() : GafferUI.EventLoop.mainEventLoop().stop() +## \todo Would this be worthy of inclusion in GafferUI? +class _ChoiceDialogue( GafferUI.Dialogue ) : + + def __init__( self, title, message, choices, **kw ) : + + GafferUI.Dialogue.__init__( self, title, sizeMode=GafferUI.Window.SizeMode.Automatic, **kw ) + + with GafferUI.ListContainer( GafferUI.ListContainer.Orientation.Vertical, spacing = 8 ) as column : + + GafferUI.Label( message ) + + self._setWidget( column ) + + for choice in choices : + self.__lastButton = self._addButton( choice ) + + def waitForChoice( self, **kw ) : + + self.__lastButton._qtWidget().setFocus() + button = self.waitForButton( **kw ) + + if button is None : + return None + else : + return button.getText() class _WindowTitleBehaviour :