<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from PyQt4 import QtCore , QtGui <EOL> from vistrails . core import debug <EOL> from vistrails . core . vistrail . module_function import ModuleFunction <EOL> from vistrails . core . vistrail . module_param import ModuleParam <EOL> from vistrails . core . modules import module_registry <EOL> from vistrails . core . modules . basic_modules import Constant <EOL> from vistrails . core . vistrail . vistrailvariable import VistrailVariable <EOL> from vistrails . gui . common_widgets import QPromptWidget <EOL> from vistrails . gui . modules . utils import get_widget_class <EOL> from vistrails . gui . modules . constant_configuration import StandardConstantWidget <EOL> from vistrails . gui . module_palette import QModuleTreeWidget <EOL> from vistrails . gui . theme import CurrentTheme <EOL> from vistrails . gui . utils import show_question , YES_BUTTON , NO_BUTTON <EOL> import uuid <EOL> class QVariableDropBox ( QtGui . QScrollArea ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QScrollArea . __init__ ( self , parent ) <EOL> self . setAcceptDrops ( True ) <EOL> self . setWidgetResizable ( True ) <EOL> self . vWidget = QVerticalWidget ( ) <EOL> self . setWidget ( self . vWidget ) <EOL> self . updateLocked = False <EOL> self . controller = None <EOL> def dragEnterEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( event . source ( ) , QModuleTreeWidget ) : <EOL> data = event . mimeData ( ) <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> event . accept ( ) <EOL> else : <EOL> event . ignore ( ) <EOL> def dragMoveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( event . source ( ) , QModuleTreeWidget ) : <EOL> data = event . mimeData ( ) <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> event . accept ( ) <EOL> def dropEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( event . source ( ) , QModuleTreeWidget ) : <EOL> data = event . mimeData ( ) <EOL> if hasattr ( data , '<STR_LIT>' ) : <EOL> event . accept ( ) <EOL> assert len ( data . items ) == <NUM_LIT:1> <EOL> item = data . items [ <NUM_LIT:0> ] <EOL> if issubclass ( item . descriptor . module , Constant ) : <EOL> if item . descriptor and self . controller : <EOL> self . lockUpdate ( ) <EOL> ( text , ok ) = QtGui . QInputDialog . getText ( self , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> QtGui . QLineEdit . Normal , <EOL> '<STR_LIT>' ) <EOL> var_name = str ( text ) . strip ( ) <EOL> while ok and self . controller . check_vistrail_variable ( var_name ) : <EOL> msg = "<STR_LIT>" <EOL> ( text , ok ) = QtGui . QInputDialog . getText ( self , <EOL> '<STR_LIT>' , <EOL> msg , <EOL> QtGui . QLineEdit . Normal , <EOL> text ) <EOL> var_name = str ( text ) . strip ( ) <EOL> if ok : <EOL> self . vWidget . addVariable ( str ( uuid . uuid1 ( ) ) , var_name , item . descriptor ) <EOL> self . scrollContentsBy ( <NUM_LIT:0> , self . viewport ( ) . height ( ) ) <EOL> self . unlockUpdate ( ) <EOL> def updateController ( self , controller ) : <EOL> """<STR_LIT>""" <EOL> if self . controller != controller : <EOL> self . controller = controller <EOL> if self . updateLocked : return <EOL> self . vWidget . clear ( ) <EOL> if controller : <EOL> reg = module_registry . get_module_registry ( ) <EOL> for var in [ v for v in controller . vistrail . vistrail_vars ] : <EOL> try : <EOL> descriptor = reg . get_descriptor_by_name ( var . package , <EOL> var . module , <EOL> var . namespace ) <EOL> except module_registry . ModuleRegistryException : <EOL> debug . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( var . name , var . package , var . module , <EOL> var . namespace ) ) <EOL> continue <EOL> self . vWidget . addVariable ( var . uuid , var . name , descriptor , <EOL> var . value ) <EOL> self . vWidget . showPromptByChildren ( ) <EOL> else : <EOL> self . vWidget . showPrompt ( False ) <EOL> def lockUpdate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . updateLocked = True <EOL> def unlockUpdate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . updateLocked = False <EOL> class QVerticalWidget ( QPromptWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QPromptWidget . __init__ ( self , parent ) <EOL> self . setPromptText ( "<STR_LIT>" ) <EOL> self . setLayout ( QtGui . QVBoxLayout ( ) ) <EOL> self . layout ( ) . setMargin ( <NUM_LIT:0> ) <EOL> self . layout ( ) . setSpacing ( <NUM_LIT:5> ) <EOL> self . layout ( ) . setAlignment ( QtCore . Qt . AlignTop ) <EOL> self . setSizePolicy ( QtGui . QSizePolicy . Expanding , <EOL> QtGui . QSizePolicy . Expanding ) <EOL> self . setMinimumHeight ( <NUM_LIT:20> ) <EOL> self . _variable_widgets = [ ] <EOL> def addVariable ( self , uuid , name , descriptor , value = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> inputForm = QVariableInputWidget ( uuid , name , descriptor , value , self ) <EOL> self . connect ( inputForm , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . delete_form ) <EOL> self . layout ( ) . addWidget ( inputForm ) <EOL> inputForm . show ( ) <EOL> self . setMinimumHeight ( self . layout ( ) . minimumSize ( ) . height ( ) ) <EOL> self . showPrompt ( False ) <EOL> self . _variable_widgets . append ( inputForm ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . setEnabled ( False ) <EOL> for v in self . _variable_widgets : <EOL> self . disconnect ( v , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . delete_form ) <EOL> self . layout ( ) . removeWidget ( v ) <EOL> v . setParent ( None ) <EOL> v . deleteLater ( ) <EOL> self . _variable_widgets = [ ] <EOL> self . setEnabled ( True ) <EOL> def delete_form ( self , input_form ) : <EOL> self . disconnect ( input_form , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . delete_form ) <EOL> var_name = input_form . var_name <EOL> variableBox = self . parent ( ) . parent ( ) <EOL> self . layout ( ) . removeWidget ( input_form ) <EOL> self . _variable_widgets . remove ( input_form ) <EOL> input_form . setParent ( None ) <EOL> input_form . deleteLater ( ) <EOL> self . showPromptByChildren ( ) <EOL> if variableBox . controller : <EOL> variableBox . lockUpdate ( ) <EOL> variableBox . controller . set_vistrail_variable ( var_name , None ) <EOL> variableBox . unlockUpdate ( ) <EOL> self . setMinimumHeight ( self . layout ( ) . minimumSize ( ) . height ( ) ) <EOL> class QVariableInputWidget ( QtGui . QDockWidget ) : <EOL> def __init__ ( self , uuid , name , descriptor , value = '<STR_LIT>' , parent = None ) : <EOL> QtGui . QDockWidget . __init__ ( self , parent ) <EOL> self . var_uuid = uuid <EOL> self . var_name = name <EOL> self . descriptor = descriptor <EOL> self . setFeatures ( QtGui . QDockWidget . DockWidgetClosable ) <EOL> self . group_box = QVariableInputForm ( descriptor , value , self ) <EOL> self . setWidget ( self . group_box ) <EOL> title_widget = QtGui . QWidget ( ) <EOL> title_layout = QtGui . QHBoxLayout ( ) <EOL> self . closeButton = QtGui . QToolButton ( ) <EOL> self . closeButton . setAutoRaise ( True ) <EOL> self . closeButton . setIcon ( QtGui . QIcon ( self . style ( ) . standardPixmap ( QtGui . QStyle . SP_TitleBarCloseButton ) ) ) <EOL> self . closeButton . setIconSize ( QtCore . QSize ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . closeButton . setFixedWidth ( <NUM_LIT:16> ) <EOL> self . label = QHoverVariableLabel ( name ) <EOL> title_layout . addWidget ( self . label ) <EOL> title_layout . addWidget ( self . closeButton ) <EOL> title_widget . setLayout ( title_layout ) <EOL> self . setTitleBarWidget ( title_widget ) <EOL> self . connect ( self . closeButton , QtCore . SIGNAL ( '<STR_LIT>' ) , self . close ) <EOL> def renameVariable ( self , var_name ) : <EOL> variableBox = self . parent ( ) . parent ( ) . parent ( ) <EOL> if variableBox . controller : <EOL> variableBox . lockUpdate ( ) <EOL> variableBox . controller . set_vistrail_variable ( self . var_name , None , False ) <EOL> variableBox . unlockUpdate ( ) <EOL> self . var_name = var_name <EOL> self . label . setText ( var_name ) <EOL> self . group_box . updateMethod ( ) <EOL> def closeEvent ( self , event ) : <EOL> choice = show_question ( '<STR_LIT>' % self . var_name , <EOL> '<STR_LIT>' % self . var_name , <EOL> [ NO_BUTTON , YES_BUTTON ] , <EOL> NO_BUTTON ) <EOL> if choice == NO_BUTTON : <EOL> event . ignore ( ) <EOL> return <EOL> self . emit ( QtCore . SIGNAL ( '<STR_LIT>' ) , self ) <EOL> def keyPressEvent ( self , e ) : <EOL> if e . key ( ) in [ QtCore . Qt . Key_Delete , QtCore . Qt . Key_Backspace ] : <EOL> self . close ( ) <EOL> else : <EOL> QtGui . QDockWidget . keyPressEvent ( self , e ) <EOL> def check_variable ( self , name ) : <EOL> """<STR_LIT>""" <EOL> variableBox = self . parent ( ) . parent ( ) . parent ( ) <EOL> if variableBox . controller : <EOL> return variableBox . controller . check_vistrail_variable ( name ) <EOL> return False <EOL> class QVariableInputForm ( QtGui . QGroupBox ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , descriptor , var_strValue = "<STR_LIT>" , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QGroupBox . __init__ ( self , parent ) <EOL> self . setLayout ( QtGui . QGridLayout ( ) ) <EOL> self . layout ( ) . setMargin ( <NUM_LIT:5> ) <EOL> self . layout ( ) . setSpacing ( <NUM_LIT:5> ) <EOL> self . setFocusPolicy ( QtCore . Qt . ClickFocus ) <EOL> self . setSizePolicy ( QtGui . QSizePolicy . Preferred , <EOL> QtGui . QSizePolicy . Fixed ) <EOL> self . palette ( ) . setColor ( QtGui . QPalette . Window , <EOL> CurrentTheme . METHOD_SELECT_COLOR ) <EOL> p = ModuleParam ( type = descriptor . name , identifier = descriptor . identifier , <EOL> namespace = descriptor . namespace ) <EOL> p . strValue = var_strValue <EOL> widget_type = get_widget_class ( descriptor ) <EOL> self . widget = widget_type ( p , self ) <EOL> self . label = QDragVariableLabel ( p . type ) <EOL> self . layout ( ) . addWidget ( self . label , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . layout ( ) . addWidget ( self . widget , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> self . updateMethod ( ) <EOL> def focusInEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . setAutoFillBackground ( True ) <EOL> def focusOutEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . setAutoFillBackground ( False ) <EOL> def updateMethod ( self ) : <EOL> """<STR_LIT>""" <EOL> inputWidget = self . parent ( ) <EOL> variableBox = inputWidget . parent ( ) . parent ( ) . parent ( ) <EOL> if variableBox . controller : <EOL> variableBox . lockUpdate ( ) <EOL> descriptor = inputWidget . descriptor <EOL> var = VistrailVariable ( inputWidget . var_name , inputWidget . var_uuid , <EOL> descriptor . identifier , descriptor . name , <EOL> descriptor . namespace , str ( self . widget . contents ( ) ) ) <EOL> variableBox . controller . set_vistrail_variable ( inputWidget . var_name , var ) <EOL> variableBox . unlockUpdate ( ) <EOL> class QDragVariableLabel ( QtGui . QLabel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , var_type = '<STR_LIT>' , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QLabel . __init__ ( self , parent ) <EOL> self . var_type = var_type <EOL> self . setText ( var_type ) <EOL> self . setAttribute ( QtCore . Qt . WA_Hover ) <EOL> self . setCursor ( CurrentTheme . OPEN_HAND_CURSOR ) <EOL> self . setToolTip ( '<STR_LIT>' ) <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_DEFAULT_COLOR ) <EOL> def event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . type ( ) == QtCore . QEvent . HoverEnter : <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_SELECT_COLOR ) <EOL> if event . type ( ) == QtCore . QEvent . HoverLeave : <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_DEFAULT_COLOR ) <EOL> return QtGui . QLabel . event ( self , event ) <EOL> def mousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . button ( ) == QtCore . Qt . LeftButton : <EOL> inputWidget = self . parent ( ) . parent ( ) <EOL> var_name = inputWidget . var_name <EOL> var_uuid = inputWidget . var_uuid <EOL> drag_str = var_name + '<STR_LIT>' + self . var_type <EOL> drag_label = QDragVariableLabel ( drag_str ) <EOL> drag_label . adjustSize ( ) <EOL> painter = QtGui . QPainter ( ) <EOL> font = QtGui . QFont ( ) <EOL> size = drag_label . size ( ) <EOL> image = QtGui . QImage ( size . width ( ) + <NUM_LIT:4> , size . height ( ) + <NUM_LIT:4> , QtGui . QImage . Format_ARGB32_Premultiplied ) <EOL> image . fill ( <NUM_LIT:0> ) <EOL> painter . begin ( image ) <EOL> painter . setPen ( QtCore . Qt . NoPen ) <EOL> painter . setBrush ( self . palette ( ) . highlight ( ) ) <EOL> painter . drawRect ( QtCore . QRectF ( <NUM_LIT:0> , <NUM_LIT:0> , image . width ( ) , image . height ( ) ) ) <EOL> painter . setFont ( font ) <EOL> painter . setPen ( QtCore . Qt . black ) <EOL> painter . drawText ( QtCore . QRect ( QtCore . QPoint ( <NUM_LIT:2> , <NUM_LIT:2> ) , size ) , QtCore . Qt . AlignLeft | QtCore . Qt . TextSingleLine , drag_str ) <EOL> painter . end ( ) <EOL> pixmap = QtGui . QPixmap . fromImage ( image ) <EOL> mimeData = QtCore . QMimeData ( ) <EOL> portspec = inputWidget . descriptor . get_port_spec ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> mimeData . variableData = ( portspec , var_uuid , var_name ) <EOL> drag = QtGui . QDrag ( self ) <EOL> drag . setMimeData ( mimeData ) <EOL> drag . setHotSpot ( pixmap . rect ( ) . bottomRight ( ) ) <EOL> drag . setPixmap ( pixmap ) <EOL> drag . start ( QtCore . Qt . MoveAction ) <EOL> class QHoverVariableLabel ( QtGui . QLabel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , var_name = '<STR_LIT>' , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtGui . QLabel . __init__ ( self , parent ) <EOL> self . var_name = var_name <EOL> self . setText ( var_name ) <EOL> self . setAttribute ( QtCore . Qt . WA_Hover ) <EOL> self . setCursor ( QtCore . Qt . PointingHandCursor ) <EOL> self . setToolTip ( '<STR_LIT>' ) <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_DEFAULT_COLOR ) <EOL> def event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . type ( ) == QtCore . QEvent . HoverEnter : <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_SELECT_COLOR ) <EOL> if event . type ( ) == QtCore . QEvent . HoverLeave : <EOL> self . palette ( ) . setColor ( QtGui . QPalette . WindowText , <EOL> CurrentTheme . HOVER_DEFAULT_COLOR ) <EOL> return QtGui . QLabel . event ( self , event ) <EOL> def mousePressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . button ( ) == QtCore . Qt . LeftButton : <EOL> inputWidget = self . parent ( ) . parent ( ) <EOL> orig_var_name = inputWidget . var_name <EOL> ( text , ok ) = QtGui . QInputDialog . getText ( self , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> QtGui . QLineEdit . Normal , <EOL> orig_var_name ) <EOL> var_name = str ( text ) . strip ( ) <EOL> while ok and self . parent ( ) . parent ( ) . check_variable ( var_name ) : <EOL> msg = "<STR_LIT>" <EOL> ( text , ok ) = QtGui . QInputDialog . getText ( self , <EOL> '<STR_LIT>' , <EOL> msg , <EOL> QtGui . QLineEdit . Normal , <EOL> text ) <EOL> var_name = str ( text ) . strip ( ) <EOL> if ok and var_name != orig_var_name : <EOL> self . setText ( var_name ) <EOL> inputWidget . renameVariable ( var_name ) </s>
