Skip to content

Commit

Permalink
fixup! ColorChooser : Maintain options state per-session
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Aug 23, 2024
1 parent ca48510 commit 87bf541
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 146 deletions.
9 changes: 9 additions & 0 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ def __init__( self, color=imath.Color3f( 1 ), **kw ) :
self.__visibleComponentsChangedSignal = Gaffer.Signals.Signal1()
self.__staticComponentChangedSignal = Gaffer.Signals.Signal1()
self.__colorFieldVisibleChangedSignal = Gaffer.Signals.Signal1()
self.__optionsMenuSignal = Gaffer.Signals.Signal2()

self.__updateUIFromColor()
self.__activateComponentIcons()
Expand Down Expand Up @@ -686,6 +687,13 @@ def colorFieldVisibleChangedSignal( self ) :

return self.__colorFieldVisibleChangedSignal

## A signal emitted whenever the options menu is opened.
# Slots should have the signature slot( ColorChooser, menuDefinition )
# and add menu items to `menuDefinition`.
def optionsMenuSignal( self ) :

return self.__optionsMenuSignal

## Returns True if a user would expect the specified sequence
# of changes to be merged into a single undoable event.
@classmethod
Expand Down Expand Up @@ -744,6 +752,7 @@ def __optionsMenuDefinition( self ) :
}
)

self.__optionsMenuSignal( self, result )

return result

Expand Down
78 changes: 67 additions & 11 deletions python/GafferUI/ColorChooserPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def __init__( self, plugs, **kw ) :
functools.partial( Gaffer.WeakMethod( self.__colorChooserColorFieldVisibleChanged ) ),
scoped = False
)
self.__colorChooser.optionsMenuSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserOptionsMenu ) ),
scoped = False
)

self.__lastChangedReason = None
self.__mergeGroupId = 0
Expand Down Expand Up @@ -120,23 +124,13 @@ def __colorChanged( self, colorChooser, reason ) :

def __colorChooserOptionChanged( self, keySuffix, value ) :

if Gaffer.Metadata.value( "colorChooser:inline:" + keySuffix, "userDefault" ) is None :
Gaffer.Metadata.deregisterValue( "colorChooser:inline:" + keySuffix, "sessionDefault" )
Gaffer.Metadata.registerValue( "colorChooser:inline:" + keySuffix, "sessionDefault", value )

for p in self.getPlugs() :
Gaffer.Metadata.deregisterValue( p, "colorChooser:inline:" + keySuffix )
Gaffer.Metadata.registerValue( p, "colorChooser:inline:" + keySuffix, value, persistent = False )

def __colorChooserOption( self, keySuffix ) :

v = sole( Gaffer.Metadata.value( p, "colorChooser:inline:" + keySuffix ) for p in self.getPlugs() )
if v is None :
v = Gaffer.Metadata.value( "colorChooser:inline:" + keySuffix, "userDefault" )
if v is None :
v = Gaffer.Metadata.value( "colorChooser:inline:" + keySuffix, "sessionDefault" )

return v
return sole( Gaffer.Metadata.value( p, "colorChooser:inline:" + keySuffix ) for p in self.getPlugs() )

def __colorChooserVisibleComponentsChanged( self, colorChooser ) :

Expand All @@ -150,6 +144,22 @@ def __colorChooserColorFieldVisibleChanged( self, colorChooser ) :

self.__colorChooserOptionChanged( "colorFieldVisible", colorChooser.getColorFieldVisible() )

def __colorChooserOptionsMenu( self, colorChooser, menuDefinition ) :

menuDefinition.append( "/__saveDefaultOptions__", { "divider": True, "label": "Defaults" } )

menuDefinition.append(
"/Save Inline Defaults",
{
"command": functools.partial(
saveDefaultOptions,
colorChooser,
"colorChooser:inline:",
self.ancestor( GafferUI.ScriptWindow ).scriptNode().applicationRoot().preferencesLocation() / "__colorChooser.py"
),
}
)

def __allComponentsEditable( self ) :

if not self._editable() :
Expand All @@ -164,3 +174,49 @@ def __allComponentsEditable( self ) :
return False

return True

def saveDefaultOptions( colorChooser, keyPrefix, scriptPath = None ) :

for k in [ "visibleComponents", "staticComponent", "colorFieldVisible" ] :
Gaffer.Metadata.deregisterValue( Gaffer.Color3fPlug, keyPrefix + k )
Gaffer.Metadata.deregisterValue( Gaffer.Color4fPlug, keyPrefix + k )

visibleComponents = colorChooser.getVisibleComponents()
Gaffer.Metadata.registerValue( Gaffer.Color3fPlug, keyPrefix + "visibleComponents", visibleComponents )
Gaffer.Metadata.registerValue( Gaffer.Color4fPlug, keyPrefix + "visibleComponents", visibleComponents )

staticComponent = colorChooser.getColorFieldStaticComponent()
Gaffer.Metadata.registerValue( Gaffer.Color3fPlug, keyPrefix + "staticComponent", staticComponent )
Gaffer.Metadata.registerValue( Gaffer.Color4fPlug, keyPrefix + "staticComponent", staticComponent )

colorFieldVisible = colorChooser.getColorFieldVisible()
Gaffer.Metadata.registerValue( Gaffer.Color3fPlug, keyPrefix + "colorFieldVisible", colorFieldVisible )
Gaffer.Metadata.registerValue( Gaffer.Color4fPlug, keyPrefix + "colorFieldVisible", colorFieldVisible )

if scriptPath is None :
return

if scriptPath.is_dir() :
raise RuntimeError( f"Cannot write Color Chooser default options script \"{scriptPath}\", a directory at that path exists.")

if scriptPath.exists() :
with open( scriptPath, "r" ) as inFile :
script = inFile.readlines()
else :
script = [
"# This file was automatically generated by Gaffer.\n",
"# Do not edit this file - it will be overwritten.\n",
"\n",
"import Gaffer\n",
"\n"
]

newScript = [l for l in script if keyPrefix not in l]

for c in [ "3", "4" ] :
newScript.append( f"Gaffer.Metadata.registerValue( Gaffer.Color{c}fPlug, \"{keyPrefix}visibleComponents\", \"{visibleComponents}\" )\n" )
newScript.append( f"Gaffer.Metadata.registerValue( Gaffer.Color{c}fPlug, \"{keyPrefix}staticComponent\", \"{staticComponent}\" )\n" )
newScript.append( f"Gaffer.Metadata.registerValue( Gaffer.Color{c}fPlug, \"{keyPrefix}colorFieldVisible\", {colorFieldVisible} )\n" )

with open( scriptPath, "w" ) as outFile :
outFile.writelines( newScript )
39 changes: 25 additions & 14 deletions python/GafferUI/ColorSwatchPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import Gaffer
import GafferUI
from GafferUI.PlugValueWidget import sole
from GafferUI.ColorChooserPlugValueWidget import saveDefaultOptions

class ColorSwatchPlugValueWidget( GafferUI.PlugValueWidget ) :

Expand Down Expand Up @@ -149,6 +150,10 @@ def __init__( self, plugs, parentWindow ) :
functools.partial( Gaffer.WeakMethod( self.__colorChooserColorFieldVisibleChanged ) ),
scoped = False
)
self.colorChooser().optionsMenuSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserOptionsMenu ) ),
scoped = False
)

self.confirmButton.clickedSignal().connect( Gaffer.WeakMethod( self.__buttonClicked ), scoped = False )
self.cancelButton.clickedSignal().connect( Gaffer.WeakMethod( self.__buttonClicked ), scoped = False )
Expand All @@ -171,15 +176,15 @@ def __init__( self, plugs, parentWindow ) :

visibleComponents = self.__colorChooserOption( "visibleComponents" )
if visibleComponents is not None :
self.__colorChooser.setVisibleComponents( visibleComponents )
self.colorChooser().setVisibleComponents( visibleComponents )

staticComponent = self.__colorChooserOption( "staticComponent" )
if staticComponent is not None :
self.__colorChooser.setColorFieldStaticComponent( staticComponent )
self.colorChooser().setColorFieldStaticComponent( staticComponent )

colorFieldVisible = self.__colorChooserOption( "colorFieldVisible" )
if colorFieldVisible is not None :
self.__colorChooser.setColorFieldVisible( colorFieldVisible )
self.colorChooser().setColorFieldVisible( colorFieldVisible )

parentWindow.addChildWindow( self, removeOnClose = True )

Expand Down Expand Up @@ -241,6 +246,22 @@ def __buttonClicked( self, button ) :
assert( not self.visible() )
GafferUI.WidgetAlgo.keepUntilIdle( self )

def __colorChooserOptionsMenu( self, colorChooser, menuDefinition ) :

menuDefinition.append( "/__saveDefaultOptions__", { "divider": True, "label": "Defaults" } )

menuDefinition.append(
"/Save Dialogue Defaults",
{
"command": functools.partial(
saveDefaultOptions,
colorChooser,
"colorChooser:dialogue:",
self.ancestor( GafferUI.ScriptWindow ).scriptNode().applicationRoot().preferencesLocation() / "__colorChooser.py"
)
}
)

def __destroy( self, *unused ) :

self.parent().removeChild( self )
Expand All @@ -250,23 +271,13 @@ def __destroy( self, *unused ) :

def __colorChooserOptionChanged( self, keySuffix, value ) :

if Gaffer.Metadata.value( "colorChooser:dialogue:" + keySuffix, "userDefault" ) is None :
Gaffer.Metadata.deregisterValue( "colorChooser:dialogue:" + keySuffix, "sessionDefault" )
Gaffer.Metadata.registerValue( "colorChooser:dialogue:" + keySuffix, "sessionDefault", value )

for p in self.__plugs :
Gaffer.Metadata.deregisterValue( p, "colorChooser:dialogue:" + keySuffix )
Gaffer.Metadata.registerValue( p, "colorChooser:dialogue:" + keySuffix, value, persistent = False )

def __colorChooserOption( self, keySuffix ) :

v = sole( Gaffer.Metadata.value( p, "colorChooser:dialogue:" + keySuffix ) for p in self.getPlugs() )
if v is None :
v = Gaffer.Metadata.value( "colorChooser:dialogue:" + keySuffix, "userDefault" )
if v is None :
v = Gaffer.Metadata.value( "colorChooser:dialogue:" + keySuffix, "sessionDefault" )

return v
return sole( Gaffer.Metadata.value( p, "colorChooser:dialogue:" + keySuffix ) for p in self.__plugs )

def __colorChooserVisibleComponentsChanged( self, colorChooser ) :

Expand Down
Loading

0 comments on commit 87bf541

Please sign in to comment.