<s> import os . path <EOL> from functools import reduce <EOL> from ginga . qtw . QtHelp import QtGui , QtCore , QTextCursor , QIcon , QPixmap , QImage , have_pyqt4 <EOL> from ginga . qtw import QtHelp <EOL> from ginga . misc import Callback , Bunch , LineHistory <EOL> import ginga . icons <EOL> icondir = os . path . split ( ginga . icons . __file__ ) [ <NUM_LIT:0> ] <EOL> class WidgetError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> _app = None <EOL> class WidgetBase ( Callback . Callbacks ) : <EOL> def __init__ ( self ) : <EOL> super ( WidgetBase , self ) . __init__ ( ) <EOL> self . widget = None <EOL> self . changed = False <EOL> self . extdata = Bunch . Bunch ( ) <EOL> def get_widget ( self ) : <EOL> return self . widget <EOL> def set_tooltip ( self , text ) : <EOL> self . widget . setToolTip ( text ) <EOL> def set_enabled ( self , tf ) : <EOL> self . widget . setEnabled ( tf ) <EOL> def get_size ( self ) : <EOL> wd , ht = self . widget . width ( ) , self . widget . height ( ) <EOL> return ( wd , ht ) <EOL> def get_app ( self ) : <EOL> return _app <EOL> def delete ( self ) : <EOL> self . widget . deleteLater ( ) <EOL> def focus ( self ) : <EOL> self . widget . activateWindow ( ) <EOL> self . widget . setFocus ( ) <EOL> def resize ( self , width , height ) : <EOL> self . widget . resize ( width , height ) <EOL> def show ( self ) : <EOL> self . widget . show ( ) <EOL> def hide ( self ) : <EOL> self . widget . hide ( ) <EOL> def get_font ( self , font_family , point_size ) : <EOL> font = QtHelp . get_font ( font_family , point_size ) <EOL> return font <EOL> def cfg_expand ( self , horizontal = <NUM_LIT:0> , vertical = <NUM_LIT:0> ) : <EOL> h_policy = QtGui . QSizePolicy . Policy ( horizontal ) <EOL> v_policy = QtGui . QSizePolicy . Policy ( vertical ) <EOL> self . widget . setSizePolicy ( QtGui . QSizePolicy ( h_policy , v_policy ) ) <EOL> class TextEntry ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' , editable = True ) : <EOL> super ( TextEntry , self ) . __init__ ( ) <EOL> self . widget = QtGui . QLineEdit ( ) <EOL> self . widget . setText ( text ) <EOL> self . widget . setReadOnly ( not editable ) <EOL> self . widget . returnPressed . connect ( self . _cb_redirect ) <EOL> self . widget . keyPressEvent_org = self . widget . keyPressEvent <EOL> self . widget . keyPressEvent = self . _key_press_event <EOL> self . history = LineHistory . LineHistory ( ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , * args ) : <EOL> self . history . append ( self . get_text ( ) ) <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def _key_press_event ( self , event ) : <EOL> keycode = event . key ( ) <EOL> if keycode in [ QtCore . Qt . Key_Up ] : <EOL> try : <EOL> self . set_text ( self . history . prev ( ) ) <EOL> except ValueError : <EOL> pass <EOL> elif keycode in [ QtCore . Qt . Key_Down ] : <EOL> try : <EOL> self . set_text ( self . history . next ( ) ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . widget . keyPressEvent_org ( event ) <EOL> def get_text ( self ) : <EOL> return self . widget . text ( ) <EOL> def set_text ( self , text ) : <EOL> self . widget . setText ( text ) <EOL> def set_editable ( self , tf ) : <EOL> self . widget . setReadOnly ( not tf ) <EOL> def set_font ( self , font ) : <EOL> self . widget . setFont ( font ) <EOL> def set_length ( self , numchars ) : <EOL> pass <EOL> class TextEntrySet ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' , editable = True ) : <EOL> super ( TextEntrySet , self ) . __init__ ( ) <EOL> self . widget = QtHelp . HBox ( ) <EOL> self . entry = QtGui . QLineEdit ( ) <EOL> self . entry . setText ( text ) <EOL> self . entry . setReadOnly ( not editable ) <EOL> layout = self . widget . layout ( ) <EOL> layout . addWidget ( self . entry , stretch = <NUM_LIT:1> ) <EOL> self . btn = QtGui . QPushButton ( '<STR_LIT>' ) <EOL> self . entry . returnPressed . connect ( self . _cb_redirect ) <EOL> self . btn . clicked . connect ( self . _cb_redirect ) <EOL> layout . addWidget ( self . btn , stretch = <NUM_LIT:0> ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , * args ) : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def get_text ( self ) : <EOL> return self . entry . text ( ) <EOL> def set_text ( self , text ) : <EOL> self . entry . setText ( text ) <EOL> def set_editable ( self , tf ) : <EOL> self . entry . setReadOnly ( not tf ) <EOL> def set_font ( self , font ) : <EOL> self . widget . setFont ( font ) <EOL> def set_length ( self , numchars ) : <EOL> pass <EOL> def set_enabled ( self , tf ) : <EOL> super ( TextEntrySet , self ) . set_enabled ( tf ) <EOL> self . entry . setEnabled ( tf ) <EOL> class GrowingTextEdit ( QtGui . QTextEdit ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( GrowingTextEdit , self ) . __init__ ( * args , ** kwargs ) <EOL> self . document ( ) . documentLayout ( ) . documentSizeChanged . connect ( <EOL> self . sizeChange ) <EOL> self . heightMin = <NUM_LIT:0> <EOL> self . heightMax = <NUM_LIT> <EOL> def sizeChange ( self ) : <EOL> docHeight = self . document ( ) . size ( ) . height ( ) <EOL> docHeight += <NUM_LIT:20> <EOL> if self . heightMin <= docHeight <= self . heightMax : <EOL> self . setMaximumHeight ( docHeight ) <EOL> class TextArea ( WidgetBase ) : <EOL> def __init__ ( self , wrap = False , editable = False ) : <EOL> super ( TextArea , self ) . __init__ ( ) <EOL> tw = GrowingTextEdit ( ) <EOL> tw . setReadOnly ( not editable ) <EOL> if wrap : <EOL> tw . setLineWrapMode ( QtGui . QTextEdit . WidgetWidth ) <EOL> else : <EOL> tw . setLineWrapMode ( QtGui . QTextEdit . NoWrap ) <EOL> self . widget = tw <EOL> def append_text ( self , text , autoscroll = True ) : <EOL> if text . endswith ( '<STR_LIT:\n>' ) : <EOL> text = text [ : - <NUM_LIT:1> ] <EOL> self . widget . append ( text ) <EOL> if not autoscroll : <EOL> return <EOL> self . widget . moveCursor ( QTextCursor . End ) <EOL> self . widget . moveCursor ( QTextCursor . StartOfLine ) <EOL> self . widget . ensureCursorVisible ( ) <EOL> def get_text ( self ) : <EOL> return self . widget . document ( ) . toPlainText ( ) <EOL> def clear ( self ) : <EOL> self . widget . clear ( ) <EOL> def set_text ( self , text ) : <EOL> self . clear ( ) <EOL> self . append_text ( text ) <EOL> def set_editable ( self , tf ) : <EOL> self . widget . setReadOnly ( not tf ) <EOL> def set_limit ( self , numlines ) : <EOL> pass <EOL> def set_font ( self , font ) : <EOL> self . widget . setCurrentFont ( font ) <EOL> def set_wrap ( self , tf ) : <EOL> if tf : <EOL> self . widget . setLineWrapMode ( QtGui . QTextEdit . WidgetWidth ) <EOL> else : <EOL> self . widget . setLineWrapMode ( QtGui . QTextEdit . NoWrap ) <EOL> class Label ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' , halign = '<STR_LIT:left>' , style = '<STR_LIT>' , menu = None ) : <EOL> super ( Label , self ) . __init__ ( ) <EOL> lbl = QtGui . QLabel ( text ) <EOL> if halign == '<STR_LIT:left>' : <EOL> lbl . setAlignment ( QtCore . Qt . AlignLeft ) <EOL> elif halign == '<STR_LIT>' : <EOL> lbl . setAlignment ( QtCore . Qt . AlignHCenter ) <EOL> elif halign == '<STR_LIT>' : <EOL> lbl . setAlignment ( QtCore . Qt . AlignRight ) <EOL> self . widget = lbl <EOL> lbl . mousePressEvent = self . _cb_redirect <EOL> if style == '<STR_LIT>' : <EOL> lbl . setSizePolicy ( QtGui . QSizePolicy . Minimum , <EOL> QtGui . QSizePolicy . Minimum ) <EOL> lbl . setFrameStyle ( QtGui . QFrame . Box | QtGui . QFrame . Raised ) <EOL> if menu is not None : <EOL> lbl . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu ) <EOL> menu_w = menu . get_widget ( ) <EOL> def on_context_menu ( point ) : <EOL> menu_w . exec_ ( lbl . mapToGlobal ( point ) ) <EOL> lbl . customContextMenuRequested . connect ( on_context_menu ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , event ) : <EOL> buttons = event . buttons ( ) <EOL> if buttons & QtCore . Qt . LeftButton : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def get_text ( self ) : <EOL> return self . widget . text ( ) <EOL> def set_text ( self , text ) : <EOL> self . widget . setText ( text ) <EOL> def set_font ( self , font ) : <EOL> self . widget . setFont ( font ) <EOL> def set_color ( self , fg = None , bg = None ) : <EOL> self . widget . setStyleSheet ( "<STR_LIT>" % ( bg , fg ) ) ; <EOL> class Button ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' ) : <EOL> super ( Button , self ) . __init__ ( ) <EOL> self . widget = QtGui . QPushButton ( text ) <EOL> self . widget . clicked . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , * args ) : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> class ComboBox ( WidgetBase ) : <EOL> def __init__ ( self , editable = False ) : <EOL> super ( ComboBox , self ) . __init__ ( ) <EOL> self . widget = QtHelp . ComboBox ( ) <EOL> self . widget . setSizeAdjustPolicy ( QtGui . QComboBox . AdjustToContents ) <EOL> self . widget . setEditable ( editable ) <EOL> self . widget . activated . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self ) : <EOL> idx = self . widget . currentIndex ( ) <EOL> self . make_callback ( '<STR_LIT>' , idx ) <EOL> def insert_alpha ( self , text ) : <EOL> index = <NUM_LIT:0> <EOL> while True : <EOL> itemText = self . widget . itemText ( index ) <EOL> if len ( itemText ) == <NUM_LIT:0> : <EOL> break <EOL> if itemText > text : <EOL> self . widget . insertItem ( index , text ) <EOL> return <EOL> index += <NUM_LIT:1> <EOL> self . widget . addItem ( text ) <EOL> def delete_alpha ( self , text ) : <EOL> index = self . widget . findText ( text ) <EOL> self . widget . removeItem ( index ) <EOL> def get_alpha ( self , idx ) : <EOL> return self . widget . itemText ( idx ) <EOL> def clear ( self ) : <EOL> self . widget . clear ( ) <EOL> def show_text ( self , text ) : <EOL> index = self . widget . findText ( text ) <EOL> self . set_index ( index ) <EOL> def append_text ( self , text ) : <EOL> self . widget . addItem ( text ) <EOL> def set_index ( self , index ) : <EOL> self . widget . setCurrentIndex ( index ) <EOL> def get_index ( self ) : <EOL> return self . widget . currentIndex ( ) <EOL> class SpinBox ( WidgetBase ) : <EOL> def __init__ ( self , dtype = int ) : <EOL> super ( SpinBox , self ) . __init__ ( ) <EOL> if dtype == float : <EOL> w = QtGui . QDoubleSpinBox ( ) <EOL> else : <EOL> w = QtGui . QSpinBox ( ) <EOL> w . valueChanged . connect ( self . _cb_redirect ) <EOL> w . setWrapping ( False ) <EOL> self . widget = w <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , val ) : <EOL> if self . changed : <EOL> self . changed = False <EOL> return <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def get_value ( self ) : <EOL> return self . widget . value ( ) <EOL> def set_value ( self , val ) : <EOL> self . changed = True <EOL> self . widget . setValue ( val ) <EOL> def set_decimals ( self , num ) : <EOL> self . widget . setDecimals ( num ) <EOL> def set_limits ( self , minval , maxval , incr_value = <NUM_LIT:1> ) : <EOL> adj = self . widget <EOL> adj . setRange ( minval , maxval ) <EOL> adj . setSingleStep ( incr_value ) <EOL> class Slider ( WidgetBase ) : <EOL> def __init__ ( self , orientation = '<STR_LIT>' , track = False ) : <EOL> super ( Slider , self ) . __init__ ( ) <EOL> if orientation == '<STR_LIT>' : <EOL> w = QtGui . QSlider ( QtCore . Qt . Horizontal ) <EOL> w . setTickPosition ( QtGui . QSlider . TicksBelow ) <EOL> else : <EOL> w = QtGui . QSlider ( QtCore . Qt . Vertical ) <EOL> w . setTickPosition ( QtGui . QSlider . TicksRight ) <EOL> w . setTracking ( track ) <EOL> self . widget = w <EOL> w . valueChanged . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , val ) : <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def get_value ( self ) : <EOL> return self . widget . value ( ) <EOL> def set_value ( self , val ) : <EOL> self . changed = True <EOL> self . widget . setValue ( val ) <EOL> def set_tracking ( self , tf ) : <EOL> self . widget . setTracking ( tf ) <EOL> def set_limits ( self , minval , maxval , incr_value = <NUM_LIT:1> ) : <EOL> adj = self . widget <EOL> adj . setRange ( minval , maxval ) <EOL> adj . setSingleStep ( incr_value ) <EOL> class ScrollBar ( WidgetBase ) : <EOL> def __init__ ( self , orientation = '<STR_LIT>' ) : <EOL> super ( ScrollBar , self ) . __init__ ( ) <EOL> if orientation == '<STR_LIT>' : <EOL> self . widget = QtGui . QScrollBar ( QtCore . Qt . Horizontal ) <EOL> else : <EOL> self . widget = QtGui . QScrollBar ( QtCore . Qt . Vertical ) <EOL> self . widget . valueChanged . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self ) : <EOL> val = self . widget . value ( ) <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> class CheckBox ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' ) : <EOL> super ( CheckBox , self ) . __init__ ( ) <EOL> self . widget = QtGui . QCheckBox ( text ) <EOL> self . widget . stateChanged . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , * args ) : <EOL> val = self . get_state ( ) <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def set_state ( self , tf ) : <EOL> self . widget . setChecked ( tf ) <EOL> def get_state ( self ) : <EOL> val = self . widget . checkState ( ) <EOL> return ( val != <NUM_LIT:0> ) <EOL> class ToggleButton ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' ) : <EOL> super ( ToggleButton , self ) . __init__ ( ) <EOL> self . widget = QtGui . QPushButton ( text ) <EOL> self . widget . setCheckable ( True ) <EOL> self . widget . clicked . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , val ) : <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def set_state ( self , tf ) : <EOL> self . widget . setChecked ( tf ) <EOL> def get_state ( self ) : <EOL> return self . widget . isChecked ( ) <EOL> class RadioButton ( WidgetBase ) : <EOL> def __init__ ( self , text = '<STR_LIT>' , group = None ) : <EOL> super ( RadioButton , self ) . __init__ ( ) <EOL> self . widget = QtGui . QRadioButton ( text ) <EOL> self . widget . toggled . connect ( self . _cb_redirect ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , val ) : <EOL> if self . changed : <EOL> self . changed = False <EOL> return <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def set_state ( self , tf ) : <EOL> if self . widget . isChecked ( ) != tf : <EOL> self . changed = True <EOL> self . widget . setChecked ( tf ) <EOL> def get_state ( self ) : <EOL> return self . widget . isChecked ( ) <EOL> class Image ( WidgetBase ) : <EOL> def __init__ ( self , native_image = None , style = '<STR_LIT>' , menu = None ) : <EOL> super ( Image , self ) . __init__ ( ) <EOL> lbl = QtGui . QLabel ( ) <EOL> self . widget = lbl <EOL> if native_image is not None : <EOL> self . _set_image ( native_image ) <EOL> lbl . mousePressEvent = self . _cb_redirect <EOL> if style == '<STR_LIT>' : <EOL> lbl . setSizePolicy ( QtGui . QSizePolicy . Minimum , <EOL> QtGui . QSizePolicy . Minimum ) <EOL> if menu is not None : <EOL> lbl . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu ) <EOL> menu_w = menu . get_widget ( ) <EOL> def on_context_menu ( point ) : <EOL> menu_w . exec_ ( lbl . mapToGlobal ( point ) ) <EOL> lbl . customContextMenuRequested . connect ( on_context_menu ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , event ) : <EOL> buttons = event . buttons ( ) <EOL> if buttons & QtCore . Qt . LeftButton : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def _set_image ( self , native_image ) : <EOL> pixmap = QPixmap . fromImage ( native_image ) <EOL> self . widget . setPixmap ( pixmap ) <EOL> class ProgressBar ( WidgetBase ) : <EOL> def __init__ ( self ) : <EOL> super ( ProgressBar , self ) . __init__ ( ) <EOL> w = QtGui . QProgressBar ( ) <EOL> w . setRange ( <NUM_LIT:0> , <NUM_LIT:100> ) <EOL> w . setTextVisible ( True ) <EOL> self . widget = w <EOL> def set_value ( self , pct ) : <EOL> self . widget . setValue ( int ( pct * <NUM_LIT> ) ) <EOL> class StatusBar ( WidgetBase ) : <EOL> def __init__ ( self ) : <EOL> super ( StatusBar , self ) . __init__ ( ) <EOL> sbar = QtGui . QStatusBar ( ) <EOL> self . widget = sbar <EOL> def set_message ( self , msg_str ) : <EOL> self . widget . showMessage ( msg_str , <NUM_LIT> ) <EOL> class TreeView ( WidgetBase ) : <EOL> def __init__ ( self , auto_expand = False , sortable = False , <EOL> selection = '<STR_LIT>' , use_alt_row_color = False , <EOL> dragable = False ) : <EOL> super ( TreeView , self ) . __init__ ( ) <EOL> self . auto_expand = auto_expand <EOL> self . sortable = sortable <EOL> self . dragable = dragable <EOL> self . selection = selection <EOL> self . levels = <NUM_LIT:1> <EOL> self . leaf_key = None <EOL> self . leaf_idx = <NUM_LIT:0> <EOL> self . columns = [ ] <EOL> self . datakeys = [ ] <EOL> self . shadow = { } <EOL> tv = QtGui . QTreeWidget ( ) <EOL> self . widget = tv <EOL> tv . setSelectionBehavior ( QtGui . QAbstractItemView . SelectRows ) <EOL> if selection == '<STR_LIT>' : <EOL> tv . setSelectionMode ( QtGui . QAbstractItemView . ExtendedSelection ) <EOL> tv . setAlternatingRowColors ( use_alt_row_color ) <EOL> tv . itemDoubleClicked . connect ( self . _cb_redirect ) <EOL> tv . itemSelectionChanged . connect ( self . _selection_cb ) <EOL> if self . dragable : <EOL> tv . setDragEnabled ( True ) <EOL> tv . startDrag = self . _start_drag <EOL> for cbname in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . enable_callback ( cbname ) <EOL> def setup_table ( self , columns , levels , leaf_key ) : <EOL> self . clear ( ) <EOL> self . columns = columns <EOL> self . levels = levels <EOL> self . leaf_key = leaf_key <EOL> treeview = self . widget <EOL> treeview . setColumnCount ( len ( columns ) ) <EOL> treeview . setSortingEnabled ( self . sortable ) <EOL> treeview . setUniformRowHeights ( True ) <EOL> if not isinstance ( columns [ <NUM_LIT:0> ] , str ) : <EOL> headers = [ col [ <NUM_LIT:0> ] for col in columns ] <EOL> datakeys = [ col [ <NUM_LIT:1> ] for col in columns ] <EOL> else : <EOL> headers = datakeys = columns <EOL> self . datakeys = datakeys <EOL> self . leaf_idx = datakeys . index ( self . leaf_key ) <EOL> if self . sortable : <EOL> treeview . sortByColumn ( self . leaf_idx , QtCore . Qt . AscendingOrder ) <EOL> treeview . setHeaderLabels ( headers ) <EOL> def set_tree ( self , tree_dict ) : <EOL> self . clear ( ) <EOL> self . add_tree ( tree_dict ) <EOL> def add_tree ( self , tree_dict ) : <EOL> if self . sortable : <EOL> self . widget . setSortingEnabled ( False ) <EOL> for key in tree_dict : <EOL> self . _add_subtree ( <NUM_LIT:1> , self . shadow , <EOL> self . widget , key , tree_dict [ key ] ) <EOL> if self . sortable : <EOL> self . widget . setSortingEnabled ( True ) <EOL> if self . auto_expand : <EOL> self . widget . expandAll ( ) <EOL> def _add_subtree ( self , level , shadow , parent_item , key , node ) : <EOL> if level >= self . levels : <EOL> values = [ '<STR_LIT>' if _key == '<STR_LIT>' else str ( node [ _key ] ) <EOL> for _key in self . datakeys ] <EOL> try : <EOL> bnch = shadow [ key ] <EOL> item = bnch . item <EOL> except KeyError : <EOL> item = QtGui . QTreeWidgetItem ( parent_item , values ) <EOL> if level == <NUM_LIT:1> : <EOL> parent_item . addTopLevelItem ( item ) <EOL> else : <EOL> parent_item . addChild ( item ) <EOL> shadow [ key ] = Bunch . Bunch ( node = node , item = item , terminal = True ) <EOL> if '<STR_LIT>' in node : <EOL> i = self . datakeys . index ( '<STR_LIT>' ) <EOL> item . setIcon ( i , node [ '<STR_LIT>' ] ) <EOL> item . setFlags ( item . flags ( ) & ~ QtCore . Qt . ItemIsEditable ) <EOL> else : <EOL> try : <EOL> bnch = shadow [ key ] <EOL> item = bnch . item <EOL> d = bnch . node <EOL> except KeyError : <EOL> item = QtGui . QTreeWidgetItem ( parent_item , [ str ( key ) ] ) <EOL> if level == <NUM_LIT:1> : <EOL> parent_item . addTopLevelItem ( item ) <EOL> else : <EOL> parent_item . addChild ( item ) <EOL> d = { } <EOL> shadow [ key ] = Bunch . Bunch ( node = d , item = item , terminal = False ) <EOL> for key in node : <EOL> self . _add_subtree ( level + <NUM_LIT:1> , d , item , key , node [ key ] ) <EOL> def _selection_cb ( self ) : <EOL> res_dict = self . get_selected ( ) <EOL> self . make_callback ( '<STR_LIT>' , res_dict ) <EOL> def _cb_redirect ( self , item ) : <EOL> res_dict = { } <EOL> self . _get_item ( res_dict , item ) <EOL> self . make_callback ( '<STR_LIT>' , res_dict ) <EOL> def _get_path ( self , item ) : <EOL> if item is None : <EOL> return [ ] <EOL> if item . childCount ( ) == <NUM_LIT:0> : <EOL> path_rest = self . _get_path ( item . parent ( ) ) <EOL> myname = item . text ( self . leaf_idx ) <EOL> path_rest . append ( myname ) <EOL> return path_rest <EOL> myname = item . text ( <NUM_LIT:0> ) <EOL> path_rest = self . _get_path ( item . parent ( ) ) <EOL> path_rest . append ( myname ) <EOL> return path_rest <EOL> def _get_item ( self , res_dict , item ) : <EOL> path = self . _get_path ( item ) <EOL> d , s = res_dict , self . shadow <EOL> for name in path [ : - <NUM_LIT:1> ] : <EOL> d = d . setdefault ( name , { } ) <EOL> s = s [ name ] . node <EOL> dst_key = path [ - <NUM_LIT:1> ] <EOL> d [ dst_key ] = s [ dst_key ] . node <EOL> def get_selected ( self ) : <EOL> items = list ( self . widget . selectedItems ( ) ) <EOL> res_dict = { } <EOL> for item in items : <EOL> if item . childCount ( ) > <NUM_LIT:0> : <EOL> continue <EOL> self . _get_item ( res_dict , item ) <EOL> return res_dict <EOL> def clear ( self ) : <EOL> self . widget . clear ( ) <EOL> self . shadow = { } <EOL> def clear_selection ( self ) : <EOL> self . widget . clearSelection ( ) <EOL> def _path_to_item ( self , path ) : <EOL> s = self . shadow <EOL> for name in path [ : - <NUM_LIT:1> ] : <EOL> s = s [ name ] . node <EOL> item = s [ path [ - <NUM_LIT:1> ] ] . item <EOL> return item <EOL> def select_path ( self , path ) : <EOL> item = self . _path_to_item ( path ) <EOL> self . widget . setItemSelected ( item , True ) <EOL> def highlight_path ( self , path , onoff , font_color = '<STR_LIT>' ) : <EOL> item = self . _path_to_item ( path ) <EOL> font = QtHelp . QFont ( ) <EOL> if not onoff : <EOL> color = QtHelp . QColor ( '<STR_LIT>' ) <EOL> else : <EOL> font . setBold ( True ) <EOL> color = QtHelp . QColor ( font_color ) <EOL> brush = QtHelp . QBrush ( color ) <EOL> for i in range ( item . columnCount ( ) ) : <EOL> item . setForeground ( i , brush ) <EOL> item . setFont ( i , font ) <EOL> def scroll_to_path ( self , path ) : <EOL> item = self . _path_to_item ( path ) <EOL> row = self . widget . indexOfTopLevelItem ( item ) <EOL> midx = self . widget . indexAt ( QtCore . QPoint ( row , <NUM_LIT:0> ) ) <EOL> self . widget . scrollTo ( midx , QtGui . QAbstractItemView . PositionAtCenter ) <EOL> def sort_on_column ( self , i ) : <EOL> self . widget . sortByColumn ( i , QtCore . Qt . AscendingOrder ) <EOL> def set_column_width ( self , i , width ) : <EOL> self . widget . setColumnWidth ( i , width ) <EOL> def set_column_widths ( self , lwidths ) : <EOL> for i , width in enumerate ( lwidths ) : <EOL> if width is not None : <EOL> self . set_column_width ( i , width ) <EOL> def set_optimal_column_widths ( self ) : <EOL> for i in range ( len ( self . columns ) ) : <EOL> self . widget . resizeColumnToContents ( i ) <EOL> def _start_drag ( self , event ) : <EOL> res_dict = self . get_selected ( ) <EOL> drag_pkg = DragPackage ( self . widget ) <EOL> self . make_callback ( '<STR_LIT>' , drag_pkg , res_dict ) <EOL> drag_pkg . start_drag ( ) <EOL> class ContainerBase ( WidgetBase ) : <EOL> def __init__ ( self ) : <EOL> super ( ContainerBase , self ) . __init__ ( ) <EOL> self . children = [ ] <EOL> def add_ref ( self , ref ) : <EOL> self . children . append ( ref ) <EOL> def _remove ( self , childw , delete = False ) : <EOL> layout = self . widget . layout ( ) <EOL> if layout is not None : <EOL> layout . removeWidget ( childw ) <EOL> childw . setParent ( None ) <EOL> if delete : <EOL> childw . deleteLater ( ) <EOL> def remove ( self , w , delete = False ) : <EOL> if not w in self . children : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . children . remove ( w ) <EOL> self . _remove ( w . get_widget ( ) , delete = delete ) <EOL> def remove_all ( self , delete = False ) : <EOL> for w in list ( self . children ) : <EOL> self . remove ( w , delete = delete ) <EOL> def get_children ( self ) : <EOL> return self . children <EOL> def num_children ( self ) : <EOL> return len ( self . children ) <EOL> def _get_native_children ( self ) : <EOL> return [ child . get_widget ( ) for child in self . children ] <EOL> def _get_native_index ( self , nchild ) : <EOL> l = self . _get_native_children ( ) <EOL> try : <EOL> return l . index ( nchild ) <EOL> except ( IndexError , ValueError ) as e : <EOL> return - <NUM_LIT:1> <EOL> def _native_to_child ( self , nchild ) : <EOL> idx = self . _get_native_index ( nchild ) <EOL> if idx < <NUM_LIT:0> : <EOL> return None <EOL> return self . children [ idx ] <EOL> def set_margins ( self , left , right , top , bottom ) : <EOL> layout = self . widget . layout ( ) <EOL> layout . setContentsMargins ( left , right , top , bottom ) <EOL> def set_border_width ( self , pix ) : <EOL> layout = self . widget . layout ( ) <EOL> layout . setContentsMargins ( pix , pix , pix , pix ) <EOL> class Box ( ContainerBase ) : <EOL> def __init__ ( self , orientation = '<STR_LIT>' ) : <EOL> super ( Box , self ) . __init__ ( ) <EOL> self . widget = QtGui . QWidget ( ) <EOL> self . orientation = orientation <EOL> if orientation == '<STR_LIT>' : <EOL> self . layout = QtGui . QHBoxLayout ( ) <EOL> else : <EOL> self . layout = QtGui . QVBoxLayout ( ) <EOL> self . layout . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . widget . setLayout ( self . layout ) <EOL> def add_widget ( self , child , stretch = <NUM_LIT:0.0> ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> if self . orientation == '<STR_LIT>' : <EOL> self . layout . addWidget ( child_w , stretch = stretch , <EOL> alignment = QtCore . Qt . AlignLeft ) <EOL> else : <EOL> self . layout . addWidget ( child_w , stretch = stretch ) <EOL> def set_spacing ( self , val ) : <EOL> self . layout . setSpacing ( val ) <EOL> class HBox ( Box ) : <EOL> def __init__ ( self ) : <EOL> super ( HBox , self ) . __init__ ( orientation = '<STR_LIT>' ) <EOL> class VBox ( Box ) : <EOL> def __init__ ( self ) : <EOL> super ( VBox , self ) . __init__ ( orientation = '<STR_LIT>' ) <EOL> class Frame ( ContainerBase ) : <EOL> def __init__ ( self , title = None ) : <EOL> super ( Frame , self ) . __init__ ( ) <EOL> self . widget = QtGui . QFrame ( ) <EOL> self . widget . setFrameStyle ( QtGui . QFrame . Box | QtGui . QFrame . Raised ) <EOL> vbox = QtGui . QVBoxLayout ( ) <EOL> self . layout = vbox <EOL> vbox . setContentsMargins ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> self . widget . setLayout ( vbox ) <EOL> if title : <EOL> lbl = QtGui . QLabel ( title ) <EOL> lbl . setAlignment ( QtCore . Qt . AlignHCenter ) <EOL> vbox . addWidget ( lbl , stretch = <NUM_LIT:0> ) <EOL> self . label = lbl <EOL> else : <EOL> self . label = None <EOL> def set_widget ( self , child , stretch = <NUM_LIT:1> ) : <EOL> self . remove_all ( ) <EOL> self . add_ref ( child ) <EOL> self . widget . layout ( ) . addWidget ( child . get_widget ( ) , stretch = stretch ) <EOL> class Expander ( ContainerBase ) : <EOL> r_arrow = None <EOL> d_arrow = None <EOL> widget_style = """<STR_LIT>""" <EOL> def __init__ ( self , title = '<STR_LIT>' ) : <EOL> super ( Expander , self ) . __init__ ( ) <EOL> if Expander . r_arrow is None : <EOL> Expander . r_arrow = QtHelp . get_icon ( os . path . join ( icondir , <EOL> '<STR_LIT>' ) , <EOL> size = ( <NUM_LIT:12> , <NUM_LIT:12> ) ) <EOL> if Expander . d_arrow is None : <EOL> Expander . d_arrow = QtHelp . get_icon ( os . path . join ( icondir , <EOL> '<STR_LIT>' ) , <EOL> size = ( <NUM_LIT:12> , <NUM_LIT:12> ) ) <EOL> self . widget = QtGui . QWidget ( ) <EOL> vbox = QtGui . QVBoxLayout ( ) <EOL> vbox . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> vbox . setSpacing ( <NUM_LIT:0> ) <EOL> self . layout = vbox <EOL> self . toggle = QtGui . QPushButton ( Expander . r_arrow , title ) <EOL> self . toggle . setStyleSheet ( Expander . widget_style ) <EOL> self . toggle . clicked . connect ( self . _toggle_widget ) <EOL> vbox . addWidget ( self . toggle , stretch = <NUM_LIT:0> ) <EOL> self . widget . setLayout ( vbox ) <EOL> def set_widget ( self , child , stretch = <NUM_LIT:1> ) : <EOL> self . remove_all ( ) <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> self . widget . layout ( ) . addWidget ( child_w , stretch = stretch ) <EOL> child_w . setVisible ( False ) <EOL> def _toggle_widget ( self ) : <EOL> child = self . get_children ( ) [ <NUM_LIT:0> ] <EOL> child_w = child . get_widget ( ) <EOL> if child_w . isVisible ( ) : <EOL> self . toggle . setIcon ( Expander . r_arrow ) <EOL> child_w . setVisible ( False ) <EOL> else : <EOL> self . toggle . setIcon ( Expander . d_arrow ) <EOL> child_w . setVisible ( True ) <EOL> class TabWidget ( ContainerBase ) : <EOL> def __init__ ( self , tabpos = '<STR_LIT>' , reorderable = False , detachable = False , <EOL> group = <NUM_LIT:0> ) : <EOL> super ( TabWidget , self ) . __init__ ( ) <EOL> self . reorderable = reorderable <EOL> self . detachable = detachable <EOL> w = QtGui . QTabWidget ( ) <EOL> w . currentChanged . connect ( self . _cb_redirect ) <EOL> w . tabCloseRequested . connect ( self . _tab_close ) <EOL> w . setUsesScrollButtons ( True ) <EOL> if self . reorderable : <EOL> w . setMovable ( True ) <EOL> self . widget = w <EOL> self . set_tab_position ( tabpos ) <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . enable_callback ( name ) <EOL> def set_tab_position ( self , tabpos ) : <EOL> w = self . widget <EOL> if tabpos == '<STR_LIT>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . North ) <EOL> elif tabpos == '<STR_LIT>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . South ) <EOL> elif tabpos == '<STR_LIT:left>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . West ) <EOL> elif tabpos == '<STR_LIT:right>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . East ) <EOL> def _cb_redirect ( self , index ) : <EOL> index = self . get_index ( ) <EOL> child = self . index_to_widget ( index ) <EOL> if child is not None : <EOL> self . make_callback ( '<STR_LIT>' , child ) <EOL> def _tab_close ( self , index ) : <EOL> child = self . index_to_widget ( index ) <EOL> self . make_callback ( '<STR_LIT>' , child ) <EOL> def add_widget ( self , child , title = '<STR_LIT>' ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> self . widget . addTab ( child_w , title ) <EOL> child . extdata . tab_title = title <EOL> def _remove ( self , nchild , delete = False ) : <EOL> idx = self . widget . indexOf ( nchild ) <EOL> self . widget . removeTab ( idx ) <EOL> nchild . setParent ( None ) <EOL> if delete : <EOL> nchild . deleteLater ( ) <EOL> def get_index ( self ) : <EOL> return self . widget . currentIndex ( ) <EOL> def set_index ( self , idx ) : <EOL> self . widget . setCurrentIndex ( idx ) <EOL> child = self . index_to_widget ( idx ) <EOL> def index_of ( self , child ) : <EOL> return self . widget . indexOf ( child . get_widget ( ) ) <EOL> def index_to_widget ( self , idx ) : <EOL> """<STR_LIT>""" <EOL> nchild = self . widget . widget ( idx ) <EOL> if nchild is None : <EOL> return nchild <EOL> return self . _native_to_child ( nchild ) <EOL> def highlight_tab ( self , idx , tf ) : <EOL> tabbar = self . widget . tabBar ( ) <EOL> if not tf : <EOL> color = QtHelp . QColor ( '<STR_LIT>' ) <EOL> else : <EOL> color = QtHelp . QColor ( '<STR_LIT>' ) <EOL> tabbar . setTabTextColor ( idx , color ) <EOL> class StackWidget ( ContainerBase ) : <EOL> def __init__ ( self ) : <EOL> super ( StackWidget , self ) . __init__ ( ) <EOL> self . widget = QtGui . QStackedWidget ( ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def add_widget ( self , child , title = '<STR_LIT>' ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> self . widget . addWidget ( child_w ) <EOL> child . extdata . tab_title = title <EOL> def get_index ( self ) : <EOL> return self . widget . currentIndex ( ) <EOL> def set_index ( self , idx ) : <EOL> self . widget . setCurrentIndex ( idx ) <EOL> def index_of ( self , child ) : <EOL> return self . widget . indexOf ( child . get_widget ( ) ) <EOL> def index_to_widget ( self , idx ) : <EOL> nchild = self . widget . widget ( idx ) <EOL> return self . _native_to_child ( nchild ) <EOL> class MDIWidget ( ContainerBase ) : <EOL> def __init__ ( self , tabpos = '<STR_LIT>' , mode = '<STR_LIT>' ) : <EOL> super ( MDIWidget , self ) . __init__ ( ) <EOL> w = QtGui . QMdiArea ( ) <EOL> w . setVerticalScrollBarPolicy ( QtCore . Qt . ScrollBarAsNeeded ) <EOL> w . setHorizontalScrollBarPolicy ( QtCore . Qt . ScrollBarAsNeeded ) <EOL> w . subWindowActivated . connect ( self . _cb_redirect ) <EOL> w . setTabsClosable ( True ) <EOL> w . setTabsMovable ( False ) <EOL> self . widget = w <EOL> self . true_mdi = True <EOL> self . cur_index = - <NUM_LIT:1> <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . enable_callback ( name ) <EOL> self . set_tab_position ( tabpos ) <EOL> self . set_mode ( mode ) <EOL> def set_tab_position ( self , tabpos ) : <EOL> w = self . widget <EOL> if tabpos == '<STR_LIT>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . North ) <EOL> elif tabpos == '<STR_LIT>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . South ) <EOL> elif tabpos == '<STR_LIT:left>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . West ) <EOL> elif tabpos == '<STR_LIT:right>' : <EOL> w . setTabPosition ( QtGui . QTabWidget . East ) <EOL> def get_mode ( self ) : <EOL> if self . widget . viewMode ( ) == QtGui . QMdiArea . TabbedView : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def set_mode ( self , mode ) : <EOL> mode = mode . lower ( ) <EOL> if mode == '<STR_LIT>' : <EOL> self . widget . setViewMode ( QtGui . QMdiArea . TabbedView ) <EOL> elif mode == '<STR_LIT>' : <EOL> self . widget . setViewMode ( QtGui . QMdiArea . SubWindowView ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( mode ) ) <EOL> def _cb_redirect ( self , subwin ) : <EOL> if subwin is not None : <EOL> nchild = subwin . widget ( ) <EOL> child = self . _native_to_child ( nchild ) <EOL> self . cur_index = self . children . index ( child ) <EOL> self . make_callback ( '<STR_LIT>' , child ) <EOL> def _window_resized ( self , event , subwin , widget ) : <EOL> qsize = event . size ( ) <EOL> wd , ht = qsize . width ( ) , qsize . height ( ) <EOL> widget . extdata . mdi_size = ( wd , ht ) <EOL> subwin . _resizeEvent ( event ) <EOL> def _window_moved ( self , event , subwin , widget ) : <EOL> qpos = event . pos ( ) <EOL> x , y = qpos . x ( ) , qpos . y ( ) <EOL> widget . extdata . mdi_pos = ( x , y ) <EOL> subwin . _moveEvent ( event ) <EOL> def _window_closed ( self , event , subwin , widget ) : <EOL> nchild = subwin . widget ( ) <EOL> child = self . _native_to_child ( nchild ) <EOL> event . ignore ( ) <EOL> self . make_callback ( '<STR_LIT>' , child ) <EOL> def add_widget ( self , child , title = '<STR_LIT>' ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> subwin = QtGui . QMdiSubWindow ( self . widget ) <EOL> subwin . setWidget ( child_w ) <EOL> child . extdata . tab_title = title <EOL> w = self . widget . addSubWindow ( subwin ) <EOL> w . _closeEvent = w . closeEvent <EOL> w . closeEvent = lambda event : self . _window_closed ( event , w , child ) <EOL> size = child . extdata . get ( '<STR_LIT>' , None ) <EOL> if size is not None : <EOL> wd , ht = size <EOL> w . resize ( wd , ht ) <EOL> pos = child . extdata . get ( '<STR_LIT>' , None ) <EOL> if pos is not None : <EOL> x , y = pos <EOL> w . move ( x , y ) <EOL> w . _resizeEvent = w . resizeEvent <EOL> w . resizeEvent = lambda event : self . _window_resized ( event , w , child ) <EOL> w . _moveEvent = w . moveEvent <EOL> w . moveEvent = lambda event : self . _window_moved ( event , w , child ) <EOL> w . setWindowTitle ( title ) <EOL> child_w . show ( ) <EOL> w . show ( ) <EOL> def _remove ( self , nchild , delete = False ) : <EOL> subwins = list ( self . widget . subWindowList ( ) ) <EOL> l = [ sw . widget ( ) for sw in subwins ] <EOL> try : <EOL> idx = l . index ( nchild ) <EOL> subwin = subwins [ idx ] <EOL> except ( IndexError , ValueError ) as e : <EOL> subwin = None <EOL> if subwin is not None : <EOL> self . widget . removeSubWindow ( subwin ) <EOL> subwin . deleteLater ( ) <EOL> nchild . setParent ( None ) <EOL> if delete : <EOL> nchild . deleteLater ( ) <EOL> def get_index ( self ) : <EOL> subwin = self . widget . activeSubWindow ( ) <EOL> if subwin is not None : <EOL> return self . _get_native_index ( subwin . widget ( ) ) <EOL> return self . cur_index <EOL> def _get_subwin ( self , widget ) : <EOL> for subwin in list ( self . widget . subWindowList ( ) ) : <EOL> if subwin . widget ( ) == widget : <EOL> return subwin <EOL> return None <EOL> def set_index ( self , idx ) : <EOL> if <NUM_LIT:0> <= idx < len ( self . children ) : <EOL> child = self . children [ idx ] <EOL> subwin = self . _get_subwin ( child . widget ) <EOL> if subwin is not None : <EOL> self . widget . setActiveSubWindow ( subwin ) <EOL> def index_of ( self , child ) : <EOL> nchild = child . get_widget ( ) <EOL> return self . _get_native_index ( nchild ) <EOL> def index_to_widget ( self , idx ) : <EOL> if <NUM_LIT:0> <= idx < len ( self . children ) : <EOL> return self . children [ idx ] <EOL> return None <EOL> def tile_panes ( self ) : <EOL> self . widget . tileSubWindows ( ) <EOL> def cascade_panes ( self ) : <EOL> self . widget . cascadeSubWindows ( ) <EOL> def use_tabs ( self , tf ) : <EOL> if tf : <EOL> self . widget . setViewMode ( QtGui . QMdiArea . TabbedView ) <EOL> else : <EOL> self . widget . setViewMode ( QtGui . QMdiArea . SubWindowView ) <EOL> class ScrollArea ( ContainerBase ) : <EOL> def __init__ ( self ) : <EOL> super ( ScrollArea , self ) . __init__ ( ) <EOL> self . widget = QtGui . QScrollArea ( ) <EOL> self . widget . setWidgetResizable ( True ) <EOL> self . widget . _resizeEvent = self . widget . resizeEvent <EOL> self . widget . resizeEvent = self . _resize_cb <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _resize_cb ( self , event ) : <EOL> self . widget . _resizeEvent ( event ) <EOL> rect = self . widget . geometry ( ) <EOL> x1 , y1 , x2 , y2 = rect . getCoords ( ) <EOL> width = x2 - x1 <EOL> height = y2 - y1 <EOL> self . make_callback ( '<STR_LIT>' , width , height ) <EOL> def set_widget ( self , child ) : <EOL> self . add_ref ( child ) <EOL> self . widget . setWidget ( child . get_widget ( ) ) <EOL> def scroll_to_end ( self , vertical = True , horizontal = False ) : <EOL> area = self . widget <EOL> if vertical : <EOL> area . verticalScrollBar ( ) . setValue ( area . verticalScrollBar ( ) . maximum ( ) ) <EOL> if horizontal : <EOL> area . horizontalScrollBar ( ) . setValue ( area . horizontalScrollBar ( ) . maximum ( ) ) <EOL> class Splitter ( ContainerBase ) : <EOL> def __init__ ( self , orientation = '<STR_LIT>' ) : <EOL> super ( Splitter , self ) . __init__ ( ) <EOL> w = QtGui . QSplitter ( ) <EOL> self . orientation = orientation <EOL> if orientation == '<STR_LIT>' : <EOL> w . setOrientation ( QtCore . Qt . Horizontal ) <EOL> else : <EOL> w . setOrientation ( QtCore . Qt . Vertical ) <EOL> self . widget = w <EOL> w . setStretchFactor ( <NUM_LIT:0> , <NUM_LIT:0.5> ) <EOL> w . setStretchFactor ( <NUM_LIT:1> , <NUM_LIT:0.5> ) <EOL> def add_widget ( self , child ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> self . widget . addWidget ( child_w ) <EOL> def get_sizes ( self ) : <EOL> return list ( self . widget . sizes ( ) ) <EOL> def set_sizes ( self , sizes ) : <EOL> return self . widget . setSizes ( sizes ) <EOL> class GridBox ( ContainerBase ) : <EOL> def __init__ ( self , rows = <NUM_LIT:1> , columns = <NUM_LIT:1> ) : <EOL> super ( GridBox , self ) . __init__ ( ) <EOL> w = QtGui . QWidget ( ) <EOL> layout = QtGui . QGridLayout ( ) <EOL> w . setLayout ( layout ) <EOL> self . widget = w <EOL> def resize_grid ( self , rows , columns ) : <EOL> pass <EOL> def set_row_spacing ( self , val ) : <EOL> self . widget . layout ( ) . setVerticalSpacing ( val ) <EOL> def set_column_spacing ( self , val ) : <EOL> self . widget . layout ( ) . setHorizontalSpacing ( val ) <EOL> def set_spacing ( self , val ) : <EOL> self . set_row_spacing ( val ) <EOL> self . set_column_spacing ( val ) <EOL> def add_widget ( self , child , row , col , stretch = <NUM_LIT:0> ) : <EOL> self . add_ref ( child ) <EOL> w = child . get_widget ( ) <EOL> self . widget . layout ( ) . addWidget ( w , row , col ) <EOL> class ToolbarAction ( WidgetBase ) : <EOL> def __init__ ( self ) : <EOL> super ( ToolbarAction , self ) . __init__ ( ) <EOL> self . widget = None <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , * args ) : <EOL> if self . widget . isCheckable ( ) : <EOL> tf = self . widget . isChecked ( ) <EOL> self . make_callback ( '<STR_LIT>' , tf ) <EOL> else : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def set_state ( self , tf ) : <EOL> self . widget . setChecked ( tf ) <EOL> def get_state ( self ) : <EOL> return self . widget . isChecked ( ) <EOL> class Toolbar ( ContainerBase ) : <EOL> def __init__ ( self , orientation = '<STR_LIT>' ) : <EOL> super ( Toolbar , self ) . __init__ ( ) <EOL> w = QtGui . QToolBar ( ) <EOL> if orientation == '<STR_LIT>' : <EOL> w . setOrientation ( QtCore . Qt . Horizontal ) <EOL> else : <EOL> w . setOrientation ( QtCore . Qt . Vertical ) <EOL> self . widget = w <EOL> def add_action ( self , text , toggle = False , iconpath = None ) : <EOL> child = ToolbarAction ( ) <EOL> if iconpath : <EOL> image = QImage ( iconpath ) <EOL> qsize = QtCore . QSize ( <NUM_LIT> , <NUM_LIT> ) <EOL> image = image . scaled ( qsize ) <EOL> pixmap = QPixmap . fromImage ( image ) <EOL> iconw = QIcon ( pixmap ) <EOL> action = self . widget . addAction ( iconw , text , <EOL> child . _cb_redirect ) <EOL> else : <EOL> action = self . widget . addAction ( text , child . _cb_redirect ) <EOL> action . setCheckable ( toggle ) <EOL> child . widget = action <EOL> self . add_ref ( child ) <EOL> return child <EOL> def add_widget ( self , child ) : <EOL> self . add_ref ( child ) <EOL> w = child . get_widget ( ) <EOL> self . widget . addWidget ( w ) <EOL> def add_menu ( self , text , menu = None ) : <EOL> if menu is None : <EOL> menu = Menu ( ) <EOL> child = self . add_action ( text ) <EOL> child . add_callback ( '<STR_LIT>' , lambda w : menu . popup ( ) ) <EOL> return menu <EOL> def add_separator ( self ) : <EOL> self . widget . addSeparator ( ) <EOL> class MenuAction ( WidgetBase ) : <EOL> def __init__ ( self , text = None , checkable = False ) : <EOL> super ( MenuAction , self ) . __init__ ( ) <EOL> self . widget = None <EOL> self . text = text <EOL> self . checkable = checkable <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def set_state ( self , tf ) : <EOL> if not self . checkable : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . widget . setChecked ( tf ) <EOL> def get_state ( self ) : <EOL> return self . widget . isChecked ( ) <EOL> def _cb_redirect ( self , * args ) : <EOL> if self . widget . isCheckable ( ) : <EOL> tf = self . widget . isChecked ( ) <EOL> self . make_callback ( '<STR_LIT>' , tf ) <EOL> else : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> class Menu ( ContainerBase ) : <EOL> def __init__ ( self ) : <EOL> super ( Menu , self ) . __init__ ( ) <EOL> self . widget = QtGui . QMenu ( ) <EOL> def add_widget ( self , child ) : <EOL> w = self . widget . addAction ( child . text , lambda : child . _cb_redirect ( ) ) <EOL> if child . checkable : <EOL> w . setCheckable ( True ) <EOL> child . widget = w <EOL> self . add_ref ( child ) <EOL> def add_name ( self , name , checkable = False ) : <EOL> child = MenuAction ( text = name , checkable = checkable ) <EOL> self . add_widget ( child ) <EOL> return child <EOL> def add_separator ( self ) : <EOL> self . widget . addSeparator ( ) <EOL> def popup ( self , widget = None ) : <EOL> if widget is not None : <EOL> w = widget . get_widget ( ) <EOL> self . widget . exec_ ( w . mapToGlobal ( QtCore . QPoint ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> else : <EOL> self . widget . exec_ ( QtGui . QCursor . pos ( ) ) <EOL> class Menubar ( ContainerBase ) : <EOL> def __init__ ( self ) : <EOL> super ( Menubar , self ) . __init__ ( ) <EOL> self . widget = QtGui . QMenuBar ( ) <EOL> self . menus = Bunch . Bunch ( caseless = True ) <EOL> def add_widget ( self , child ) : <EOL> menu_w = child . get_widget ( ) <EOL> self . widget . addMenu ( menu_w ) <EOL> self . add_ref ( child ) <EOL> def add_name ( self , name ) : <EOL> menu_w = self . widget . addMenu ( name ) <EOL> child = Menu ( ) <EOL> child . widget = menu_w <EOL> self . add_ref ( child ) <EOL> self . menus [ name ] = child <EOL> return child <EOL> def get_menu ( self , name ) : <EOL> return self . menus [ name ] <EOL> class TopLevel ( ContainerBase ) : <EOL> def __init__ ( self , title = None ) : <EOL> super ( TopLevel , self ) . __init__ ( ) <EOL> widget = QtHelp . TopLevel ( ) <EOL> self . widget = widget <EOL> box = QtGui . QVBoxLayout ( ) <EOL> box . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> box . setSpacing ( <NUM_LIT:0> ) <EOL> widget . setLayout ( box ) <EOL> widget . closeEvent = lambda event : self . _quit ( event ) <EOL> widget . destroyed = self . _destroyed_cb <EOL> if not title is None : <EOL> widget . setWindowTitle ( title ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def set_widget ( self , child ) : <EOL> self . add_ref ( child ) <EOL> child_w = child . get_widget ( ) <EOL> self . widget . layout ( ) . addWidget ( child_w ) <EOL> def _quit ( self , event ) : <EOL> event . ignore ( ) <EOL> self . close ( ) <EOL> def _closeEvent ( * args ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> self . make_callback ( '<STR_LIT>' ) <EOL> def _destroyed_cb ( self , * args ) : <EOL> event . accept ( ) <EOL> def raise_ ( self ) : <EOL> self . widget . raise_ ( ) <EOL> self . widget . activateWindow ( ) <EOL> def lower ( self ) : <EOL> self . widget . lower ( ) <EOL> def focus ( self ) : <EOL> self . widget . raise_ ( ) <EOL> self . widget . activateWindow ( ) <EOL> def move ( self , x , y ) : <EOL> self . widget . move ( x , y ) <EOL> def maximize ( self ) : <EOL> self . widget . showMaximized ( ) <EOL> def unmaximize ( self ) : <EOL> self . widget . showNormal ( ) <EOL> def fullscreen ( self ) : <EOL> self . widget . showFullScreen ( ) <EOL> def unfullscreen ( self ) : <EOL> self . widget . showNormal ( ) <EOL> def is_fullscreen ( self ) : <EOL> return self . widget . isFullScreen ( ) <EOL> def iconify ( self ) : <EOL> self . hide ( ) <EOL> def uniconify ( self ) : <EOL> self . widget . showNormal ( ) <EOL> def set_title ( self , title ) : <EOL> self . widget . setWindowTitle ( title ) <EOL> class Application ( Callback . Callbacks ) : <EOL> def __init__ ( self , logger = None ) : <EOL> global _app <EOL> super ( Application , self ) . __init__ ( ) <EOL> self . logger = logger <EOL> self . window_list = [ ] <EOL> self . window_dict = { } <EOL> self . wincnt = <NUM_LIT:0> <EOL> if have_pyqt4 : <EOL> QtGui . QApplication . setGraphicsSystem ( '<STR_LIT>' ) <EOL> app = QtGui . QApplication ( [ ] ) <EOL> self . _qtapp = app <EOL> _app = self <EOL> desktop = self . _qtapp . desktop ( ) <EOL> rect = desktop . availableGeometry ( ) <EOL> size = rect . size ( ) <EOL> self . screen_wd = size . width ( ) <EOL> self . screen_ht = size . height ( ) <EOL> for name in ( '<STR_LIT>' , ) : <EOL> self . enable_callback ( name ) <EOL> def get_screen_size ( self ) : <EOL> return ( self . screen_wd , self . screen_ht ) <EOL> def process_events ( self ) : <EOL> self . _qtapp . processEvents ( ) <EOL> def process_end ( self ) : <EOL> self . _qtapp . quit ( ) <EOL> def add_window ( self , window , wid = None ) : <EOL> if wid is None : <EOL> wid = '<STR_LIT>' % ( self . wincnt ) <EOL> self . wincnt += <NUM_LIT:1> <EOL> window . wid = wid <EOL> window . url = '<STR_LIT>' <EOL> window . app = self <EOL> self . window_dict [ wid ] = window <EOL> def get_window ( self , wid ) : <EOL> return self . window_dict [ wid ] <EOL> def has_window ( self , wid ) : <EOL> return wid in self . window_dict <EOL> def get_wids ( self ) : <EOL> return list ( self . window_dict . keys ( ) ) <EOL> def make_window ( self , title = None ) : <EOL> w = TopLevel ( title = title ) <EOL> self . add_window ( w ) <EOL> return w <EOL> class Dialog ( WidgetBase ) : <EOL> def __init__ ( self , title = None , flags = None , buttons = None , <EOL> parent = None , modal = False ) : <EOL> super ( Dialog , self ) . __init__ ( ) <EOL> self . widget = QtGui . QDialog ( parent . get_widget ( ) ) <EOL> self . widget . setModal ( modal ) <EOL> vbox = QtGui . QVBoxLayout ( ) <EOL> self . widget . setLayout ( vbox ) <EOL> self . content = VBox ( ) <EOL> vbox . addWidget ( self . content . get_widget ( ) , stretch = <NUM_LIT:1> ) <EOL> hbox_w = QtGui . QWidget ( ) <EOL> hbox = QtGui . QHBoxLayout ( ) <EOL> hbox_w . setLayout ( hbox ) <EOL> for name , val in buttons : <EOL> btn = QtGui . QPushButton ( name ) <EOL> def cb ( val ) : <EOL> return lambda : self . _cb_redirect ( val ) <EOL> btn . clicked . connect ( cb ( val ) ) <EOL> hbox . addWidget ( btn , stretch = <NUM_LIT:0> ) <EOL> vbox . addWidget ( hbox_w , stretch = <NUM_LIT:0> ) <EOL> self . enable_callback ( '<STR_LIT>' ) <EOL> def _cb_redirect ( self , val ) : <EOL> self . make_callback ( '<STR_LIT>' , val ) <EOL> def get_content_area ( self ) : <EOL> return self . content <EOL> def raise_ ( self ) : <EOL> self . widget . raise_ ( ) <EOL> self . widget . activateWindow ( ) <EOL> class SaveDialog ( QtGui . QFileDialog ) : <EOL> def __init__ ( self , title = None , selectedfilter = None ) : <EOL> super ( SaveDialog , self ) . __init__ ( ) <EOL> self . selectedfilter = selectedfilter <EOL> self . widget = self . getSaveFileName ( self , title , '<STR_LIT>' , selectedfilter ) <EOL> def get_path ( self ) : <EOL> if self . widget and self . selectedfilter is not None and not self . widget . endswith ( self . selectedfilter [ <NUM_LIT:1> : ] ) : <EOL> self . widget += self . selectedfilter [ <NUM_LIT:1> : ] <EOL> return self . widget <EOL> class DragPackage ( object ) : <EOL> def __init__ ( self , src_widget ) : <EOL> self . src_widget = src_widget <EOL> self . _drag = QtHelp . QDrag ( self . src_widget ) <EOL> def set_urls ( self , urls ) : <EOL> mimeData = QtCore . QMimeData ( ) <EOL> _urls = [ QtCore . QUrl ( url ) for url in urls ] <EOL> mimeData . setUrls ( _urls ) <EOL> self . _drag . setMimeData ( mimeData ) <EOL> def start_drag ( self ) : <EOL> if QtHelp . have_pyqt5 : <EOL> result = self . _drag . exec_ ( QtCore . Qt . MoveAction ) <EOL> else : <EOL> result = self . _drag . start ( QtCore . Qt . MoveAction ) <EOL> def name_mangle ( name , pfx = '<STR_LIT>' ) : <EOL> newname = [ ] <EOL> for c in name . lower ( ) : <EOL> if not ( c . isalpha ( ) or c . isdigit ( ) or ( c == '<STR_LIT:_>' ) ) : <EOL> newname . append ( '<STR_LIT:_>' ) <EOL> else : <EOL> newname . append ( c ) <EOL> return pfx + '<STR_LIT>' . join ( newname ) <EOL> def make_widget ( title , wtype ) : <EOL> if wtype == '<STR_LIT:label>' : <EOL> w = Label ( title ) <EOL> w . widget . setAlignment ( QtCore . Qt . AlignRight ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Label ( title ) <EOL> w . widget . setAlignment ( QtCore . Qt . AlignLeft ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = TextEntry ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = TextEntrySet ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = ComboBox ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = SpinBox ( dtype = int ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = SpinBox ( dtype = float ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = VBox ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = HBox ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Slider ( orientation = '<STR_LIT>' ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Slider ( orientation = '<STR_LIT>' ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = CheckBox ( title ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = RadioButton ( title ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = ToggleButton ( title ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Button ( title ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Label ( '<STR_LIT>' ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = TextArea ( editable = True ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Toolbar ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = ProgressBar ( ) <EOL> elif wtype == '<STR_LIT>' : <EOL> w = Menubar ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % wtype ) <EOL> return w <EOL> def hadjust ( w , orientation ) : <EOL> if orientation != '<STR_LIT>' : <EOL> return w <EOL> vbox = VBox ( ) <EOL> vbox . add_widget ( w ) <EOL> vbox . add_widget ( Label ( '<STR_LIT>' ) , stretch = <NUM_LIT:1> ) <EOL> return vbox <EOL> def build_info ( captions , orientation = '<STR_LIT>' ) : <EOL> numrows = len ( captions ) <EOL> numcols = reduce ( lambda acc , tup : max ( acc , len ( tup ) ) , captions , <NUM_LIT:0> ) <EOL> if ( numcols % <NUM_LIT:2> ) != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> numcols = int ( numcols // <NUM_LIT:2> ) <EOL> widget = QtGui . QWidget ( ) <EOL> table = QtGui . QGridLayout ( ) <EOL> widget . setLayout ( table ) <EOL> table . setVerticalSpacing ( <NUM_LIT:2> ) <EOL> table . setHorizontalSpacing ( <NUM_LIT:4> ) <EOL> table . setContentsMargins ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> wb = Bunch . Bunch ( ) <EOL> row = <NUM_LIT:0> <EOL> for tup in captions : <EOL> col = <NUM_LIT:0> <EOL> while col < numcols : <EOL> idx = col * <NUM_LIT:2> <EOL> if idx < len ( tup ) : <EOL> title , wtype = tup [ idx : idx + <NUM_LIT:2> ] <EOL> if not title . endswith ( '<STR_LIT::>' ) : <EOL> name = name_mangle ( title ) <EOL> else : <EOL> name = name_mangle ( '<STR_LIT>' + title [ : - <NUM_LIT:1> ] ) <EOL> w = make_widget ( title , wtype ) <EOL> table . addWidget ( w . widget , row , col ) <EOL> wb [ name ] = w <EOL> col += <NUM_LIT:1> <EOL> row += <NUM_LIT:1> <EOL> w = wrap ( widget ) <EOL> w = hadjust ( w , orientation = orientation ) <EOL> return w , wb <EOL> def wrap ( native_widget ) : <EOL> wrapper = WidgetBase ( ) <EOL> wrapper . widget = native_widget <EOL> return wrapper <EOL> def get_orientation ( container ) : <EOL> if not hasattr ( container , '<STR_LIT:size>' ) : <EOL> return '<STR_LIT>' <EOL> ( wd , ht ) = container . size <EOL> if wd < ht : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def get_oriented_box ( container , scrolled = True , fill = False ) : <EOL> orientation = get_orientation ( container ) <EOL> if orientation == '<STR_LIT>' : <EOL> box1 = VBox ( ) <EOL> box2 = VBox ( ) <EOL> else : <EOL> box1 = HBox ( ) <EOL> box2 = VBox ( ) <EOL> box2 . add_widget ( box1 , stretch = <NUM_LIT:0> ) <EOL> if not fill : <EOL> box2 . add_widget ( Label ( '<STR_LIT>' ) , stretch = <NUM_LIT:1> ) <EOL> if scrolled : <EOL> sw = ScrollArea ( ) <EOL> sw . set_widget ( box2 ) <EOL> else : <EOL> sw = box2 <EOL> return box1 , sw , orientation </s>
