<s> import IECore <EOL> import Gaffer <EOL> import GafferUI <EOL> QtCore = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> QtGui = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> class SplineWidget ( GafferUI . Widget ) : <EOL> DrawMode = IECore . Enum . create ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , spline = None , drawMode = DrawMode . Splines , parenting = None ) : <EOL> GafferUI . Widget . __init__ ( self , QtGui . QFrame ( ) , parenting = parenting ) <EOL> self . _qtWidget ( ) . setSizePolicy ( QtGui . QSizePolicy . Expanding , QtGui . QSizePolicy . Expanding ) <EOL> self . _qtWidget ( ) . setObjectName ( "<STR_LIT>" ) <EOL> self . setDrawMode ( drawMode ) <EOL> if spline == None : <EOL> spline = IECore . Splineff ( <EOL> IECore . CubicBasisf . catmullRom ( ) , <EOL> ( <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> ) <EOL> ) <EOL> self . setSpline ( spline ) <EOL> self . __displayTransformChangedConnection = GafferUI . DisplayTransform . changedSignal ( ) . connect ( Gaffer . WeakMethod ( self . __displayTransformChanged ) ) <EOL> self . _qtWidget ( ) . paintEvent = Gaffer . WeakMethod ( self . __paintEvent ) <EOL> def setSpline ( self , spline ) : <EOL> try : <EOL> if spline == self . __spline : <EOL> return <EOL> except : <EOL> pass <EOL> self . __spline = spline <EOL> self . __splinesToDraw = None <EOL> self . __gradientToDraw = None <EOL> self . _qtWidget ( ) . update ( ) <EOL> def getSpline ( self ) : <EOL> return self . __spline <EOL> def setDrawMode ( self , drawMode ) : <EOL> try : <EOL> if drawMode == self . __drawMode : <EOL> return <EOL> except : <EOL> pass <EOL> self . __drawMode = drawMode <EOL> self . _qtWidget ( ) . update ( ) <EOL> def getDrawMode ( self ) : <EOL> return self . __drawMode <EOL> def __paintEvent ( self , event ) : <EOL> painter = QtGui . QPainter ( self . _qtWidget ( ) ) <EOL> painter . setRenderHint ( QtGui . QPainter . Antialiasing ) <EOL> o = QtGui . QStyleOption ( ) <EOL> o . initFrom ( self . _qtWidget ( ) ) <EOL> self . _qtWidget ( ) . style ( ) . drawPrimitive ( QtGui . QStyle . PE_Widget , o , painter , self . _qtWidget ( ) ) <EOL> if self . __drawMode == self . DrawMode . Ramp : <EOL> self . __paintRamp ( painter ) <EOL> elif self . __drawMode == self . DrawMode . Splines : <EOL> self . __paintSplines ( painter ) <EOL> def __paintRamp ( self , painter ) : <EOL> if self . __gradientToDraw is None : <EOL> self . __gradientToDraw = QtGui . QLinearGradient ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . __gradientToDraw . setCoordinateMode ( self . __gradientToDraw . ObjectBoundingMode ) <EOL> displayTransform = GafferUI . DisplayTransform . get ( ) <EOL> numStops = <NUM_LIT> <EOL> for i in range ( <NUM_LIT:0> , numStops ) : <EOL> t = float ( i ) / ( numStops - <NUM_LIT:1> ) <EOL> c = self . __spline ( t ) <EOL> if isinstance ( c , float ) : <EOL> c = IECore . Color3f ( c , c , c ) <EOL> else : <EOL> c = IECore . Color3f ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:1> ] , c [ <NUM_LIT:2> ] ) <EOL> c = displayTransform ( c ) <EOL> self . __gradientToDraw . setColorAt ( t , self . _qtColor ( c ) ) <EOL> brush = QtGui . QBrush ( self . __gradientToDraw ) <EOL> rect = self . _qtWidget ( ) . contentsRect ( ) <EOL> painter . fillRect ( rect , brush ) <EOL> def __paintSplines ( self , painter ) : <EOL> numPoints = <NUM_LIT:50> <EOL> if not self . __splinesToDraw : <EOL> self . __splinesToDraw = [ ] <EOL> interval = self . __spline . interval ( ) <EOL> if isinstance ( self . __spline , IECore . Splineff ) : <EOL> spline = IECore . Struct ( ) <EOL> spline . color = IECore . Color3f ( <NUM_LIT:1> ) <EOL> spline . path = QtGui . QPainterPath ( ) <EOL> for i in range ( <NUM_LIT:0> , numPoints ) : <EOL> t = float ( i ) / ( numPoints - <NUM_LIT:1> ) <EOL> tt = interval [ <NUM_LIT:0> ] + ( interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] ) * t <EOL> c = self . __spline ( tt ) <EOL> if i == <NUM_LIT:0> : <EOL> spline . path . moveTo ( t , c ) <EOL> else : <EOL> spline . path . lineTo ( t , c ) <EOL> self . __splinesToDraw . append ( spline ) <EOL> else : <EOL> for i in range ( <NUM_LIT:0> , self . __spline ( <NUM_LIT:0> ) . dimensions ( ) ) : <EOL> spline = IECore . Struct ( ) <EOL> if i == <NUM_LIT:3> : <EOL> spline . color = IECore . Color3f ( <NUM_LIT:1> ) <EOL> else : <EOL> c = IECore . Color3f ( <NUM_LIT:0> ) <EOL> c [ i ] = <NUM_LIT:1> <EOL> spline . color = c <EOL> spline . path = QtGui . QPainterPath ( ) <EOL> self . __splinesToDraw . append ( spline ) <EOL> for i in range ( <NUM_LIT:0> , numPoints ) : <EOL> t = float ( i ) / ( numPoints - <NUM_LIT:1> ) <EOL> tt = interval [ <NUM_LIT:0> ] + ( interval [ <NUM_LIT:1> ] - interval [ <NUM_LIT:0> ] ) * t <EOL> c = self . __spline ( tt ) <EOL> for j in range ( <NUM_LIT:0> , c . dimensions ( ) ) : <EOL> if i == <NUM_LIT:0> : <EOL> self . __splinesToDraw [ j ] . path . moveTo ( t , c [ j ] ) <EOL> else : <EOL> self . __splinesToDraw [ j ] . path . lineTo ( t , c [ j ] ) <EOL> self . __splineBound = QtCore . QRectF ( ) <EOL> for s in self . __splinesToDraw : <EOL> self . __splineBound = self . __splineBound . united ( s . path . controlPointRect ( ) ) <EOL> rect = self . _qtWidget ( ) . contentsRect ( ) <EOL> transform = QtGui . QTransform ( ) <EOL> if self . __splineBound . width ( ) : <EOL> transform . translate ( rect . x ( ) , <NUM_LIT:0> ) <EOL> transform . scale ( rect . width ( ) / self . __splineBound . width ( ) , <NUM_LIT:1> ) <EOL> if self . __splineBound . height ( ) : <EOL> transform . translate ( <NUM_LIT:0> , rect . y ( ) + rect . height ( ) ) <EOL> transform . scale ( <NUM_LIT:1> , - rect . height ( ) / self . __splineBound . height ( ) ) <EOL> painter . setTransform ( transform ) <EOL> for s in self . __splinesToDraw : <EOL> pen = QtGui . QPen ( self . _qtColor ( s . color ) ) <EOL> painter . setPen ( pen ) <EOL> painter . drawPath ( s . path ) <EOL> def __displayTransformChanged ( self ) : <EOL> self . _qtWidget ( ) . update ( ) </s>
