Skip to content

Commit

Permalink
fixup! ColorChooser : Static color slider widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Nov 1, 2024
1 parent d59ece3 commit fcbb9c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
44 changes: 22 additions & 22 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _drawIndicator( painter, position ) :
# A custom slider for drawing the backgrounds.
class _ComponentSlider( GafferUI.Slider ) :

def __init__( self, color, component, dynamicColors = False, **kw ) :
def __init__( self, color, component, dynamicBackground = False, **kw ) :

GafferUI.Slider.__init__(
self, 0.0,
Expand All @@ -128,7 +128,7 @@ def __init__( self, color, component, dynamicColors = False, **kw ) :

self.color = color
self.component = component
self.__dynamicColors = dynamicColors
self.__dynamicBackground = dynamicBackground
self.__gradientToDraw = None
self.__size = self.size()

Expand All @@ -137,8 +137,8 @@ def __init__( self, color, component, dynamicColors = False, **kw ) :
def setColor( self, color ) :

if (
( self.__dynamicColors and color != self.color ) or
( not self.__dynamicColors and self.component == "s" )
( self.__dynamicBackground and color != self.color ) or
( not self.__dynamicBackground and self.component == "s" )
) :
self.__gradientToDraw = None
self.color = color
Expand All @@ -148,15 +148,15 @@ def getColor( self ) :

return self.color

def setDynamicColors( self, dynamic ) :
def setDynamicBackground( self, dynamicBackground ) :

self.__dynamicColors = dynamic
self.__dynamicBackground = dynamicBackground
self.__gradientToDraw = None
self._qtWidget().update()

def getDynamicColors( self ) :
def getDynamicBackground( self ) :

return self.__dynamicColors
return self.__dynamicBackground

def _drawBackground( self, painter ) :

Expand All @@ -171,7 +171,7 @@ def _drawBackground( self, painter ) :
c1 = imath.Color3f( 0 )
c2 = imath.Color3f( 1 )
else :
if self.__dynamicColors :
if self.__dynamicBackground :
c1 = imath.Color3f( self.color[0], self.color[1], self.color[2] )
elif self.component in "rgbvi" :
c1 = imath.Color3f( 0 )
Expand Down Expand Up @@ -887,7 +887,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.__dynamicColorsChangedSignal = Gaffer.Signals.Signal1()
self.__dynamicSliderBackgroundsChangedSignal = Gaffer.Signals.Signal1()
self.__optionsMenuSignal = Gaffer.Signals.Signal2()

self.__colorFieldPrimaryIcon = GafferUI.Image( "colorFieldPrimaryIcon.png" )
Expand All @@ -898,7 +898,7 @@ def __init__( self, color=imath.Color3f( 1 ), **kw ) :
self.__updateUIFromColor()
self.__updateComponentIcons()

self.setDynamicColors( False )
self.setDynamicSliderBackgrounds( False )

## The default color starts as the value passed when creating the widget.
# It is represented with a swatch which when clicked will revert the current
Expand Down Expand Up @@ -975,17 +975,17 @@ def getColorFieldVisible( self ) :

return self.__colorField.getVisible()

def setDynamicColors( self, dynamic ) :
def setDynamicSliderBackgrounds( self, dynamic ) :

for component, slider in self.__sliders.items() :
slider.setDynamicColors( dynamic )
slider.setDynamicBackground( dynamic )
self.__colorField.setDynamicColors( dynamic )

self.__dynamicColorsChangedSignal( self )
self.__dynamicSliderBackgroundsChangedSignal( self )

def getDynamicColors( self ) :
def getDynamicSliderBackgrounds( self ) :

return self.__sliders["r"].getDynamicColors()
return self.__sliders["r"].getDynamicBackground()

## A signal emitted whenever the color is changed. Slots should
# have the signature slot( ColorChooser, reason ). The reason
Expand Down Expand Up @@ -1016,9 +1016,9 @@ def colorFieldVisibleChangedSignal( self ) :

## A signal emitted whenever the dynamic colors option is changed.
# Slots should have the signature slot( ColorChooser ).
def dynamicColorsChangedSignal( self ) :
def dynamicSliderBackgroundsChangedSignal( self ) :

return self.__dynamicColorsChangedSignal
return self.__dynamicSliderBackgroundsChangedSignal

## A signal emitted whenever the options menu is opened.
# Slots should have the signature slot( ColorChooser, menuDefinition )
Expand Down Expand Up @@ -1086,13 +1086,13 @@ def __optionsMenuDefinition( self ) :
}
)

result.append( "/__appearance__", { "divider": True, "label": "Appearance" } )
result.append( "/__sliders__", { "divider": True, "label": "Sliders" } )

result.append(
"/Dynamic Colors",
"/Dynamic Backgrounds",
{
"command": Gaffer.WeakMethod( self.setDynamicColors ),
"checkBox": self.getDynamicColors(),
"command": Gaffer.WeakMethod( self.setDynamicSliderBackgrounds ),
"checkBox": self.getDynamicSliderBackgrounds(),
}
)

Expand Down
22 changes: 11 additions & 11 deletions python/GafferUI/ColorChooserPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def __init__( self, plugs, **kw ) :
if colorFieldVisible is not None :
self.__colorChooser.setColorFieldVisible( colorFieldVisible )

dynamicColors = self.__colorChooserOption( "dynamicColors" )
if dynamicColors is not None :
self.__colorChooser.setDynamicColors( dynamicColors )
dynamicSliderBackgrounds = self.__colorChooserOption( "dynamicSliderBackgrounds" )
if dynamicSliderBackgrounds is not None :
self.__colorChooser.setDynamicSliderBackgrounds( dynamicSliderBackgrounds )

self.__colorChangedConnection = self.__colorChooser.colorChangedSignal().connect(
Gaffer.WeakMethod( self.__colorChanged )
Expand All @@ -82,8 +82,8 @@ def __init__( self, plugs, **kw ) :
self.__colorChooser.colorFieldVisibleChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserColorFieldVisibleChanged ) )
)
self.__colorChooser.dynamicColorsChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__dynamicColorsChanged ) )
self.__colorChooser.dynamicSliderBackgroundsChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__dynamicSliderBackgroundsChanged ) )
)
self.__colorChooser.optionsMenuSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserOptionsMenu ) ),
Expand Down Expand Up @@ -148,9 +148,9 @@ def __colorChooserColorFieldVisibleChanged( self, colorChooser ) :

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

def __dynamicColorsChanged( self, colorChooser ) :
def __dynamicSliderBackgroundsChanged( self, colorChooser ) :

self.__colorChooserOptionChanged( "dynamicColors", colorChooser.getDynamicColors() )
self.__colorChooserOptionChanged( "dynamicSliderBackgrounds", colorChooser.getDynamicSliderBackgrounds() )

def __colorChooserOptionsMenu( self, colorChooser, menuDefinition ) :

Expand Down Expand Up @@ -188,16 +188,16 @@ def saveDefaultOptions( colorChooser, keyPrefix, scriptPath = None ) :
visibleComponents = colorChooser.getVisibleComponents()
staticComponent = colorChooser.getColorFieldStaticComponent()
colorFieldVisible = colorChooser.getColorFieldVisible()
dynamicColors = colorChooser.getDynamicColors()
dynamicSliderBackgrounds = colorChooser.getDynamicSliderBackgrounds()

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

Gaffer.Metadata.registerValue( p, keyPrefix + "visibleComponents", visibleComponents )
Gaffer.Metadata.registerValue( p, keyPrefix + "staticComponent", staticComponent )
Gaffer.Metadata.registerValue( p, keyPrefix + "colorFieldVisible", colorFieldVisible )
Gaffer.Metadata.registerValue( p, keyPrefix + "dynamicColors", dynamicColors )
Gaffer.Metadata.registerValue( p, keyPrefix + "dynamicSliderBackgrounds", dynamicSliderBackgrounds )

if scriptPath is None :
return
Expand All @@ -223,7 +223,7 @@ def saveDefaultOptions( colorChooser, keyPrefix, scriptPath = None ) :
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" )
newScript.append( f"Gaffer.Metadata.registerValue( Gaffer.Color{c}fPlug, \"{keyPrefix}dynamicColors\", {dynamicColors} )\n" )
newScript.append( f"Gaffer.Metadata.registerValue( Gaffer.Color{c}fPlug, \"{keyPrefix}dynamicSliderBackgrounds\", {dynamicSliderBackgrounds} )\n" )

with open( scriptPath, "w" ) as outFile :
outFile.writelines( newScript )
14 changes: 7 additions & 7 deletions python/GafferUI/ColorSwatchPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def __init__( self, plugs, parentWindow ) :
self.colorChooser().colorFieldVisibleChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserColorFieldVisibleChanged ) )
)
self.colorChooser().dynamicColorsChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__dynamicColorsChanged ) )
self.colorChooser().dynamicSliderBackgroundsChangedSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__dynamicSliderBackgroundsChanged ) )
)
self.colorChooser().optionsMenuSignal().connect(
functools.partial( Gaffer.WeakMethod( self.__colorChooserOptionsMenu ) ),
Expand Down Expand Up @@ -186,9 +186,9 @@ def __init__( self, plugs, parentWindow ) :
if colorFieldVisible is not None :
self.colorChooser().setColorFieldVisible( colorFieldVisible )

dynamicColors = self.__colorChooserOption( "dynamicColors" )
if dynamicColors is not None :
self.colorChooser().setDynamicColors( dynamicColors )
dynamicSliderBackgrounds = self.__colorChooserOption( "dynamicSliderBackgrounds" )
if dynamicSliderBackgrounds is not None :
self.colorChooser().setDynamicSliderBackgrounds( dynamicSliderBackgrounds )

parentWindow.addChildWindow( self, removeOnClose = True )

Expand Down Expand Up @@ -295,6 +295,6 @@ def __colorChooserColorFieldVisibleChanged( self, colorChooser ) :

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

def __dynamicColorsChanged( self, colorChooser ) :
def __dynamicSliderBackgroundsChanged( self, colorChooser ) :

self.__colorChooserOptionChanged( "dynamicColors", colorChooser.getDynamicColors() )
self.__colorChooserOptionChanged( "dynamicSliderBackgrounds", colorChooser.getDynamicSliderBackgrounds() )
38 changes: 19 additions & 19 deletions python/GafferUITest/ColorChooserTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ def __getColorFieldVisibility( self, widget ) :
c = self.__colorChooserFromWidget( widget )
return c.getColorFieldVisible()

def __setDynamicColors( self, widget, dynamic ) :
def __setDynamicSliderBackgrounds( self, widget, dynamic ) :

c = self.__colorChooserFromWidget( widget )
c.setDynamicColors( dynamic )
c.setDynamicSliderBackgrounds( dynamic )

def __getDynamicColors( self, widget ) :
def __getDynamicSliderBackgrounds( self, widget ) :

c = self.__colorChooserFromWidget( widget )
return c.getDynamicColors()
return c.getDynamicSliderBackgrounds()

def testMetadata( self ) :

Expand All @@ -155,33 +155,33 @@ def testMetadata( self ) :
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:visibleComponents" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:staticComponent" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:colorFieldVisible" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicColors" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicSliderBackgrounds" ) )

# Modify widget

self.__setVisibleComponents( widget, "rgbtmi" )
self.__setStaticComponent( widget, "t" )
self.__setColorFieldVisibility( widget, False )
self.__setDynamicColors( widget, True )
self.__setDynamicSliderBackgrounds( widget, True )

for c in "rgbtmi" :
self.assertTrue( self.__sliderFromWidget( widget, c ).getVisible() )
for c in "hsv" :
self.assertFalse( self.__sliderFromWidget( widget, c ).getVisible() )
self.assertEqual( self.__getStaticComponent( widget ), "t" )
self.assertFalse( self.__getColorFieldVisibility( widget ) )
self.assertTrue( self.__getDynamicColors( widget ) )
self.assertTrue( self.__getDynamicSliderBackgrounds( widget ) )

for p in [ "rgbPlug2" ] :
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:visibleComponents" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:staticComponent" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:colorFieldVisible" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicColors" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicSliderBackgrounds" ) )

self.assertEqual( set( Gaffer.Metadata.value( script["node"]["rgbPlug1"], "colorChooser:inline:visibleComponents" ) ), set( "rgbtmi" ) )
self.assertEqual( Gaffer.Metadata.value( script["node"]["rgbPlug1"], "colorChooser:inline:staticComponent" ), "t" )
self.assertFalse( Gaffer.Metadata.value( script["node"]["rgbPlug1"], "colorChooser:inline:colorFieldVisible" ) )
self.assertTrue( Gaffer.Metadata.value( script["node"]["rgbPlug1"], "colorChooser:inline:dynamicColors" ) )
self.assertTrue( Gaffer.Metadata.value( script["node"]["rgbPlug1"], "colorChooser:inline:dynamicSliderBackgrounds" ) )

# Recreate widget and should have the same state

Expand All @@ -195,7 +195,7 @@ def testMetadata( self ) :
self.assertFalse( self.__sliderFromWidget( widget, c ).getVisible() )
self.assertEqual( self.__getStaticComponent( widget ), "t" )
self.assertFalse( self.__getColorFieldVisibility( widget ) )
self.assertTrue( self.__getDynamicColors( widget ) )
self.assertTrue( self.__getDynamicSliderBackgrounds( widget ) )

# We haven't saved the defaults, so a widget for a second plug
# gets the original defaults.
Expand All @@ -207,13 +207,13 @@ def testMetadata( self ) :
self.assertTrue( self.__sliderFromWidget( widget2, c ).getVisible() )
self.assertEqual( self.__getStaticComponent( widget2 ), "v" )
self.assertTrue( self.__getColorFieldVisibility( widget2 ) )
self.assertFalse( self.__getDynamicColors( widget2 ) )
self.assertFalse( self.__getDynamicSliderBackgrounds( widget2 ) )

for p in [ "rgbPlug2" ] :
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:visibleComponents" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:staticComponent" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:colorFieldVisible" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicColors" ) )
self.assertIsNone( Gaffer.Metadata.value( script["node"][p], "colorChooser:inline:dynamicSliderBackgrounds" ) )

# Don't serialize state

Expand All @@ -229,13 +229,13 @@ def testMetadata( self ) :
self.assertTrue( self.__sliderFromWidget( widget, c ).getVisible() )
self.assertEqual( self.__getStaticComponent( widget ), "v" )
self.assertTrue( self.__getColorFieldVisibility( widget ) )
self.assertFalse( self.__getDynamicColors( widget ) )
self.assertFalse( self.__getDynamicSliderBackgrounds( widget ) )

for p in [ "rgbPlug1", "rgbPlug2" ] :
self.assertIsNone( Gaffer.Metadata.value( script2["node"][p], "colorChooser:inline:visibleComponents" ) )
self.assertIsNone( Gaffer.Metadata.value( script2["node"][p], "colorChooser:inline:staticComponent" ) )
self.assertIsNone( Gaffer.Metadata.value( script2["node"][p], "colorChooser:inline:colorFieldVisible" ) )
self.assertIsNone( Gaffer.Metadata.value( script2["node"][p], "colorChooser:inline:dynamicColors" ) )
self.assertIsNone( Gaffer.Metadata.value( script2["node"][p], "colorChooser:inline:dynamicSliderBackgrounds" ) )

def testSaveDefaultOptions( self ) :

Expand Down Expand Up @@ -263,15 +263,15 @@ def testSaveDefaultOptions( self ) :
self.assertEqual( self.__getStaticComponent( rgbaWidget ), "v" )
self.assertTrue( self.__getColorFieldVisibility( rgbWidget ) )
self.assertTrue( self.__getColorFieldVisibility( rgbaWidget ) )
self.assertFalse( self.__getDynamicColors( rgbWidget ) )
self.assertFalse( self.__getDynamicColors( rgbaWidget ) )
self.assertFalse( self.__getDynamicSliderBackgrounds( rgbWidget ) )
self.assertFalse( self.__getDynamicSliderBackgrounds( rgbaWidget ) )

# Modify `rgbWidget`

self.__setVisibleComponents( rgbWidget, "rgbhsv" )
self.__setStaticComponent( rgbWidget, "g" )
self.__setColorFieldVisibility( rgbWidget, False )
self.__setDynamicColors( rgbWidget, True )
self.__setDynamicSliderBackgrounds( rgbWidget, True )

# Save defaults
colorChooser = self.__colorChooserFromWidget( rgbWidget )
Expand Down Expand Up @@ -300,8 +300,8 @@ def testSaveDefaultOptions( self ) :
self.assertEqual( self.__getStaticComponent( rgbaWidget ), "g" )
self.assertFalse( self.__getColorFieldVisibility( rgbWidget ) )
self.assertFalse( self.__getColorFieldVisibility( rgbaWidget ) )
self.assertTrue( self.__getDynamicColors( rgbWidget ) )
self.assertTrue( self.__getDynamicColors( rgbaWidget ) )
self.assertTrue( self.__getDynamicSliderBackgrounds( rgbWidget ) )
self.assertTrue( self.__getDynamicSliderBackgrounds( rgbaWidget ) )

if __name__ == "__main__" :
unittest.main()

0 comments on commit fcbb9c3

Please sign in to comment.