<s> """<STR_LIT>""" <EOL> from pyface . qt import QtCore , QtGui <EOL> from . gradient_editor import ( ColorControlPoint , ChannelBase , FunctionControl , <EOL> GradientEditorWidget ) <EOL> class QGradientControl ( QtGui . QWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , gradient_table , width , height ) : <EOL> """<STR_LIT>""" <EOL> super ( QGradientControl , self ) . __init__ ( parent = parent ) <EOL> self . resize ( width , height ) <EOL> self . setAttribute ( QtCore . Qt . WA_OpaquePaintEvent , True ) <EOL> self . width = width <EOL> self . height = height <EOL> self . gradient_table = gradient_table <EOL> assert ( gradient_table . size == width ) <EOL> self . setMinimumSize ( <NUM_LIT:100> , <NUM_LIT:50> ) <EOL> def paintEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( QGradientControl , self ) . paintEvent ( event ) <EOL> painter = QtGui . QPainter ( self ) <EOL> brush = QtGui . QBrush ( QtGui . QColor ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> painter . setBrush ( brush ) <EOL> painter . setBackgroundMode ( QtCore . Qt . OpaqueMode ) <EOL> sz = self . size ( ) <EOL> width , height = sz . width ( ) , sz . height ( ) <EOL> xform = self . gradient_table . scaling_function <EOL> start_y = <NUM_LIT:0> <EOL> end_y = height <EOL> if xform : <EOL> start_y = height / <NUM_LIT:2> <EOL> color = QtGui . QColor ( ) <EOL> for x in range ( width ) : <EOL> ( r , g , b , a ) = self . gradient_table . get_pos_rgba_color_lerped ( float ( x ) / ( width - <NUM_LIT:1> ) ) <EOL> color . setRgb ( int ( <NUM_LIT:255> * r ) , int ( <NUM_LIT:255> * g ) , int ( <NUM_LIT:255> * b ) ) <EOL> painter . setPen ( color ) <EOL> brush . setColor ( color ) <EOL> painter . drawLine ( x , start_y , x , end_y ) <EOL> if xform : <EOL> end_y = start_y <EOL> start_y = <NUM_LIT:0> <EOL> for x in range ( width ) : <EOL> f = float ( x ) / ( width - <NUM_LIT:1> ) <EOL> ( r , g , b , a ) = self . gradient_table . get_pos_rgba_color_lerped ( xform ( f ) ) <EOL> color . set ( int ( <NUM_LIT:255> * r ) , int ( <NUM_LIT:255> * g ) , int ( <NUM_LIT:255> * b ) ) <EOL> brush . setColor ( color ) <EOL> painter . drawLine ( x , start_y , x , end_y ) <EOL> class Channel ( ChannelBase ) : <EOL> def paint ( self , painter ) : <EOL> """<STR_LIT>""" <EOL> table = self . control . table <EOL> relevant_control_points = [ <EOL> x for x in table . control_points if self . name in x . active_channels <EOL> ] <EOL> color = QtGui . QColor ( * self . rgb_color ) <EOL> painter . setPen ( color ) <EOL> brush = QtGui . QBrush ( color ) <EOL> painter . setBrush ( brush ) <EOL> painter . setBackgroundMode ( QtCore . Qt . OpaqueMode ) <EOL> for k in range ( len ( relevant_control_points ) - <NUM_LIT:1> ) : <EOL> cur_point = relevant_control_points [ k ] <EOL> next_point = relevant_control_points [ <NUM_LIT:1> + k ] <EOL> painter . drawLine ( self . get_pos_index ( cur_point . pos ) , <EOL> self . get_value_index ( cur_point . color ) , <EOL> self . get_pos_index ( next_point . pos ) , <EOL> self . get_value_index ( next_point . color ) ) <EOL> color = QtCore . Qt . black <EOL> painter . setPen ( color ) <EOL> for control_point in relevant_control_points : <EOL> x = self . get_pos_index ( control_point . pos ) <EOL> y = self . get_value_index ( control_point . color ) <EOL> radius = <NUM_LIT:6> <EOL> painter . drawRect ( x - ( radius / <NUM_LIT> ) , y - ( radius / <NUM_LIT> ) , radius , radius ) <EOL> painter . drawRect ( <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:6> ) <EOL> class QFunctionControl ( QtGui . QWidget , FunctionControl ) : <EOL> """<STR_LIT>""" <EOL> control_pt_click_tolerance = <NUM_LIT:4> <EOL> ChannelFactory = Channel <EOL> def __init__ ( self , master , gradient_table , color_space , width , height ) : <EOL> """<STR_LIT>""" <EOL> FunctionControl . __init__ ( self , master , gradient_table , color_space , <EOL> width , height ) <EOL> QtGui . QWidget . __init__ ( self , parent = master ) <EOL> self . resize ( width , height ) <EOL> self . setMinimumSize ( <NUM_LIT:100> , <NUM_LIT:50> ) <EOL> def paintEvent ( self , event ) : <EOL> super ( QFunctionControl , self ) . paintEvent ( event ) <EOL> painter = QtGui . QPainter ( self ) <EOL> brush = QtGui . QBrush ( QtGui . QColor ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> painter . setBrush ( brush ) <EOL> width , height = self . size ( ) . width ( ) , self . size ( ) . height ( ) <EOL> painter . drawRect ( <NUM_LIT:0> , <NUM_LIT:0> , width , height ) <EOL> for channel in self . channels : <EOL> channel . paint ( painter ) <EOL> def mousePressEvent ( self , event ) : <EOL> if event . button ( ) == QtCore . Qt . LeftButton : <EOL> self . cur_drag = self . find_control_point ( event . x ( ) , event . y ( ) ) <EOL> super ( QFunctionControl , self ) . mousePressEvent ( event ) <EOL> def mouseReleaseEvent ( self , event ) : <EOL> if event . button ( ) == QtCore . Qt . LeftButton : <EOL> if self . cur_drag : <EOL> self . table_config_changed ( final_update = True ) <EOL> self . cur_drag = None <EOL> elif event . button ( ) == QtCore . Qt . RightButton : <EOL> cur_control_point = self . find_control_point ( event . x ( ) , None ) <EOL> if cur_control_point : <EOL> if ( cur_control_point [ <NUM_LIT:1> ] . fixed ) : <EOL> return <EOL> self . table . control_points . remove ( cur_control_point [ <NUM_LIT:1> ] ) <EOL> self . table_config_changed ( final_update = True ) <EOL> else : <EOL> new_control_point = ColorControlPoint ( active_channels = self . active_channels_string ) <EOL> new_control_point . set_pos ( self . channels [ <NUM_LIT:0> ] . get_index_pos ( event . x ( ) ) ) <EOL> new_control_point . color = self . table . get_pos_color ( new_control_point . pos ) <EOL> self . table . insert_control_point ( new_control_point ) <EOL> self . table_config_changed ( final_update = True ) <EOL> if isinstance ( event , QtGui . QMouseEvent ) : <EOL> super ( QFunctionControl , self ) . mouseReleaseEvent ( event ) <EOL> def leaveEvent ( self , event ) : <EOL> if self . cur_drag : <EOL> self . table_config_changed ( final_update = True ) <EOL> self . cur_drag = None <EOL> super ( QFunctionControl , self ) . leaveEvent ( event ) <EOL> def resizeEvent ( self , event ) : <EOL> sz = self . size ( ) <EOL> self . width = sz . width ( ) <EOL> self . height = sz . height ( ) <EOL> def mouseMoveEvent ( self , event ) : <EOL> channel = None <EOL> point = None <EOL> if self . cur_drag : <EOL> channel = self . cur_drag [ <NUM_LIT:0> ] <EOL> point = self . cur_drag [ <NUM_LIT:1> ] <EOL> if ( not point . fixed ) : <EOL> point . set_pos ( channel . get_index_pos ( event . x ( ) ) ) <EOL> point . activate_channels ( self . active_channels_string ) <EOL> self . table . sort_control_points ( ) <EOL> channel . set_value_index ( point . color , event . y ( ) ) <EOL> self . table_config_changed ( final_update = False ) <EOL> screenX = event . x ( ) <EOL> screenY = event . y ( ) <EOL> width , height = self . size ( ) . width ( ) , self . size ( ) . height ( ) <EOL> master = self . master <EOL> s1 , s2 = master . get_table_range ( ) <EOL> if channel is not None : <EOL> name = self . text_map [ channel . name ] <EOL> pos = s1 + ( s2 - s1 ) * point . pos <EOL> val = channel . get_value ( point . color ) <EOL> txt = '<STR_LIT>' % ( name , pos , val ) <EOL> else : <EOL> x = s1 + ( s2 - s1 ) * float ( screenX ) / ( width - <NUM_LIT:1> ) <EOL> y = <NUM_LIT:1.0> - float ( screenY ) / ( height - <NUM_LIT:1> ) <EOL> txt = "<STR_LIT>" % ( x , y ) <EOL> self . master . set_status_text ( txt ) <EOL> class QGradientEditorWidget ( QtGui . QWidget , GradientEditorWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , master , vtk_table , on_change_color_table = None , <EOL> colors = None ) : <EOL> """<STR_LIT>""" <EOL> GradientEditorWidget . __init__ ( self , master , vtk_table , <EOL> on_change_color_table , colors ) <EOL> QtGui . QWidget . __init__ ( self , master ) <EOL> gradient_preview_width = self . gradient_preview_width <EOL> gradient_preview_height = self . gradient_preview_height <EOL> channel_function_width = self . channel_function_width <EOL> channel_function_height = self . channel_function_height <EOL> grid = QtGui . QGridLayout ( ) <EOL> grid . setColumnStretch ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> grid . setColumnStretch ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . gradient_control = QGradientControl ( self , <EOL> self . gradient_table , <EOL> gradient_preview_width , <EOL> gradient_preview_height ) <EOL> self . setToolTip ( '<STR_LIT>' ) <EOL> grid . addWidget ( QtGui . QLabel ( "<STR_LIT>" , self ) , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> grid . addWidget ( self . gradient_control , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> gc = self . gradient_control <EOL> gc . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu ) <EOL> gc . customContextMenuRequested . connect ( self . contextMenuEventOnGradient ) <EOL> function_controls = self . function_controls <EOL> editor_data = self . editor_data <EOL> row = <NUM_LIT:1> <EOL> for color in self . colors : <EOL> data = editor_data [ color ] <EOL> control = QFunctionControl ( self , self . gradient_table , color , <EOL> channel_function_width , <EOL> channel_function_height ) <EOL> txt = data [ <NUM_LIT:0> ] + self . tooltip_text <EOL> control . setToolTip ( txt ) <EOL> grid . addWidget ( QtGui . QLabel ( data [ <NUM_LIT:1> ] , self ) , row , <NUM_LIT:0> ) <EOL> grid . addWidget ( control , row , <NUM_LIT:1> ) <EOL> function_controls . append ( control ) <EOL> row += <NUM_LIT:1> <EOL> self . text = QtGui . QLabel ( '<STR_LIT:status>' , self ) <EOL> grid . addWidget ( self . text , row , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . setLayout ( grid ) <EOL> self . show ( ) <EOL> def set_status_text ( self , msg ) : <EOL> self . text . setText ( msg ) <EOL> def contextMenuEventOnGradient ( self , pos ) : <EOL> menu = QtGui . QMenu ( self ) <EOL> saveAction = menu . addAction ( "<STR_LIT>" ) <EOL> loadAction = menu . addAction ( "<STR_LIT>" ) <EOL> action = menu . exec_ ( self . mapToGlobal ( pos ) ) <EOL> if action == saveAction : <EOL> self . on_save ( ) <EOL> elif action == loadAction : <EOL> self . on_load ( ) <EOL> def on_save ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> wildcard = "<STR_LIT>" <EOL> filename , filter = QtGui . QFileDialog . getSaveFileName ( self , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> wildcard ) <EOL> if filename : <EOL> self . save ( filename ) <EOL> def on_load ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> wildcard = "<STR_LIT>" <EOL> filename , filter = QtGui . QFileDialog . getOpenFileName ( self , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> wildcard ) <EOL> if filename : <EOL> self . load ( filename ) <EOL> class QGradientEditor ( QtGui . QMainWindow ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , vtk_table , on_change_color_table = None , colors = None ) : <EOL> """<STR_LIT>""" <EOL> super ( QGradientEditor , self ) . __init__ ( ) <EOL> self . setWindowTitle ( "<STR_LIT>" ) <EOL> self . widget = QGradientEditorWidget ( self , vtk_table , <EOL> on_change_color_table , <EOL> colors ) <EOL> self . setCentralWidget ( self . widget ) <EOL> self . resize ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . statusBar ( ) <EOL> menu = self . menuBar ( ) <EOL> file_menu = menu . addMenu ( "<STR_LIT>" ) <EOL> file_action = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> file_action . setStatusTip ( "<STR_LIT>" ) <EOL> file_action . triggered . connect ( self . widget . on_save ) <EOL> file_menu . addAction ( file_action ) <EOL> load_action = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> load_action . setStatusTip ( "<STR_LIT>" ) <EOL> load_action . triggered . connect ( self . widget . on_load ) <EOL> file_menu . addAction ( load_action ) <EOL> quit_action = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> quit_action . setStatusTip ( "<STR_LIT>" ) <EOL> quit_action . triggered . connect ( QtGui . QApplication . instance ( ) . quit ) <EOL> file_menu . addAction ( quit_action ) <EOL> help_menu = menu . addMenu ( "<STR_LIT>" ) <EOL> action = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> action . setStatusTip ( "<STR_LIT>" ) <EOL> action . triggered . connect ( self . on_help ) <EOL> help_menu . addAction ( action ) <EOL> action = QtGui . QAction ( "<STR_LIT>" , self ) <EOL> action . setStatusTip ( "<STR_LIT>" ) <EOL> action . triggered . connect ( self . on_about ) <EOL> help_menu . addAction ( action ) <EOL> def on_help ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> QtGui . QMessageBox . information ( self , '<STR_LIT>' , message ) <EOL> def on_about ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> message = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> QtGui . QMessageBox . information ( self , '<STR_LIT>' , message ) <EOL> def main ( ) : <EOL> from . traitsui_gradient_editor import make_test_table <EOL> import sys <EOL> table , ctf , otf = make_test_table ( lut = False ) <EOL> def on_color_table_changed ( ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> app = QtGui . QApplication . instance ( ) <EOL> editor = QGradientEditor ( table , <EOL> on_color_table_changed , <EOL> colors = [ '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:h>' , '<STR_LIT:s>' , '<STR_LIT:v>' ] , <EOL> ) <EOL> editor . setWindowTitle ( "<STR_LIT>" ) <EOL> editor . show ( ) <EOL> sys . exit ( app . exec_ ( ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
