forked from GafferHQ/gaffer
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a GafferUI.Metadata class which is used to register descriptions for the various node and plug types. Used it to implement tooltip help for plugs, with the tooltip coming from the "parameterName.help" annotation in the case of RenderManShader nodes. We still need to register appropriate metadata for all existing nodes and generate documentation from it.
- Loading branch information
1 parent
ffb2bc7
commit 37c8c85
Showing
7 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
########################################################################## | ||
# | ||
# Copyright (c) 2013, Image Engine Design Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above | ||
# copyright notice, this list of conditions and the following | ||
# disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following | ||
# disclaimer in the documentation and/or other materials provided with | ||
# the distribution. | ||
# | ||
# * Neither the name of John Haddon nor the names of | ||
# any other contributors to this software may be used to endorse or | ||
# promote products derived from this software without specific prior | ||
# written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
########################################################################## | ||
|
||
import re | ||
import fnmatch | ||
|
||
import IECore | ||
|
||
import Gaffer | ||
|
||
## The Metadata class provides a registry of metadata for the different types | ||
# of Nodes and Plugs. This metadata assists in creating UIs and can be used to | ||
# generate documentation. | ||
class Metadata : | ||
|
||
## Registers a textual description for nodes of the specified type. | ||
# The description may either be a string or a callable which will compute | ||
# a description when passed a node instance. | ||
@classmethod | ||
def registerNodeDescription( cls, nodeType, description ) : | ||
|
||
if isinstance( nodeType, IECore.TypeId ) : | ||
nodeTypeId = nodeType | ||
else : | ||
nodeTypeId = nodeType.staticTypeId() | ||
|
||
cls.__nodeDescriptions[nodeTypeId] = description | ||
|
||
## Returns a description for the specified node instance. | ||
@classmethod | ||
def nodeDescription( cls, node ) : | ||
|
||
nodeTypeId = node.typeId() | ||
while nodeTypeId != IECore.TypeId.Invalid : | ||
|
||
description = cls.__nodeDescriptions.get( nodeTypeId, None ) | ||
if description is not None : | ||
if callable( description ) : | ||
return description( node ) | ||
else : | ||
return description | ||
|
||
nodeTypeId = IECore.RunTimeTyped.baseTypeId( nodeTypeId ) | ||
|
||
return "" | ||
|
||
## Registers a textual description for the specified plug on nodes of the | ||
# specified type. The plugPath may be a string optionally containing fnmatch | ||
# wildcard characters or be a regex for performing more complex matches. | ||
# The description may either be a string or a callable which will compute | ||
# a description when passed a node instance. | ||
@classmethod | ||
def registerPlugDescription( cls, nodeType, plugPath, stringOrCallable ) : | ||
|
||
if isinstance( nodeType, IECore.TypeId ) : | ||
nodeTypeId = nodeType | ||
else : | ||
nodeTypeId = nodeType.staticTypeId() | ||
|
||
if isinstance( plugPath, basestring ) : | ||
plugPath = re.compile( fnmatch.translate( plugPath ) ) | ||
else : | ||
assert( type( plugPath ) is type( re.compile( "" ) ) ) | ||
|
||
plugDescriptions = cls.__plugDescriptions.setdefault( nodeTypeId, [] ) | ||
plugDescriptions.insert( | ||
0, | ||
IECore.Struct( | ||
plugPathMatcher = plugPath, | ||
description = stringOrCallable | ||
) | ||
) | ||
|
||
## Returns a description for the specified plug instance. | ||
@classmethod | ||
def plugDescription( cls, plug ) : | ||
|
||
node = plug.node() | ||
if node is None : | ||
return "" | ||
|
||
plugPath = plug.relativeName( node ) | ||
|
||
nodeTypeId = node.typeId() | ||
while nodeTypeId != IECore.TypeId.Invalid : | ||
plugDescriptions = cls.__plugDescriptions.get( nodeTypeId, None ) | ||
if plugDescriptions is not None : | ||
for d in plugDescriptions : | ||
if d.plugPathMatcher.match( plugPath ) : | ||
if callable( d.description ) : | ||
return d.description( plug ) | ||
else : | ||
return d.description | ||
|
||
nodeTypeId = IECore.RunTimeTyped.baseTypeId( nodeTypeId ) | ||
|
||
return "" | ||
|
||
__nodeDescriptions = {} | ||
__plugDescriptions = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
########################################################################## | ||
# | ||
# Copyright (c) 2013, Image Engine Design Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above | ||
# copyright notice, this list of conditions and the following | ||
# disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following | ||
# disclaimer in the documentation and/or other materials provided with | ||
# the distribution. | ||
# | ||
# * Neither the name of John Haddon nor the names of | ||
# any other contributors to this software may be used to endorse or | ||
# promote products derived from this software without specific prior | ||
# written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
########################################################################## | ||
|
||
import unittest | ||
|
||
import IECore | ||
|
||
import Gaffer | ||
import GafferTest | ||
import GafferUI | ||
import GafferUITest | ||
|
||
class MetadataTest( GafferUITest.TestCase ) : | ||
|
||
class DerivedAddNode( GafferTest.AddNode ) : | ||
|
||
def __init__( self, name="DerivedAddNode" ) : | ||
|
||
GafferTest.AddNode.__init__( self, name ) | ||
|
||
IECore.registerRunTimeTyped( DerivedAddNode ) | ||
|
||
def testNodeDescription( self ) : | ||
|
||
add = GafferTest.AddNode() | ||
|
||
self.assertEqual( GafferUI.Metadata.nodeDescription( add ), "" ) | ||
|
||
GafferUI.Metadata.registerNodeDescription( GafferTest.AddNode, "description" ) | ||
self.assertEqual( GafferUI.Metadata.nodeDescription( add ), "description" ) | ||
|
||
GafferUI.Metadata.registerNodeDescription( GafferTest.AddNode, lambda node : node.getName() ) | ||
self.assertEqual( GafferUI.Metadata.nodeDescription( add ), "AddNode" ) | ||
|
||
derivedAdd = self.DerivedAddNode() | ||
self.assertEqual( GafferUI.Metadata.nodeDescription( derivedAdd ), "DerivedAddNode" ) | ||
|
||
GafferUI.Metadata.registerNodeDescription( self.DerivedAddNode.staticTypeId(), "a not very helpful description" ) | ||
self.assertEqual( GafferUI.Metadata.nodeDescription( derivedAdd ), "a not very helpful description" ) | ||
self.assertEqual( GafferUI.Metadata.nodeDescription( add ), "AddNode" ) | ||
|
||
def testPlugDescription( self ) : | ||
|
||
add = GafferTest.AddNode() | ||
|
||
self.assertEqual( GafferUI.Metadata.plugDescription( add["op1"] ), "" ) | ||
|
||
GafferUI.Metadata.registerPlugDescription( GafferTest.AddNode.staticTypeId(), "op1", "The first operand" ) | ||
self.assertEqual( GafferUI.Metadata.plugDescription( add["op1"] ), "The first operand" ) | ||
|
||
GafferUI.Metadata.registerPlugDescription( GafferTest.AddNode.staticTypeId(), "op1", lambda plug : plug.getName() + " description" ) | ||
self.assertEqual( GafferUI.Metadata.plugDescription( add["op1"] ), "op1 description" ) | ||
|
||
derivedAdd = self.DerivedAddNode() | ||
self.assertEqual( GafferUI.Metadata.plugDescription( derivedAdd["op1"] ), "op1 description" ) | ||
|
||
GafferUI.Metadata.registerPlugDescription( self.DerivedAddNode, "op*", "derived class description" ) | ||
self.assertEqual( GafferUI.Metadata.plugDescription( derivedAdd["op1"] ), "derived class description" ) | ||
self.assertEqual( GafferUI.Metadata.plugDescription( derivedAdd["op2"] ), "derived class description" ) | ||
|
||
self.assertEqual( GafferUI.Metadata.plugDescription( add["op1"] ), "op1 description" ) | ||
self.assertEqual( GafferUI.Metadata.plugDescription( add["op2"] ), "" ) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters