<s> """<STR_LIT>""" <EOL> import wx <EOL> from traits . api import Property <EOL> from traitsui . editors . enum_editor import ToolkitEditorFactory <EOL> from editor import Editor <EOL> from constants import OKColor , ErrorColor <EOL> from helper import enum_values_changed , TraitsUIPanel , disconnect , disconnect_no_id <EOL> capitalize = lambda s : s . capitalize ( ) <EOL> class BaseEditor ( Editor ) : <EOL> """<STR_LIT>""" <EOL> names = Property <EOL> mapping = Property <EOL> inverse_mapping = Property <EOL> def init ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> factory = self . factory <EOL> if factory . name != '<STR_LIT>' : <EOL> self . _object , self . _name , self . _value = self . parse_extended_name ( factory . name ) <EOL> self . values_changed ( ) <EOL> self . _object . on_trait_change ( self . _values_changed , <EOL> '<STR_LIT:U+0020>' + self . _name , dispatch = '<STR_LIT>' ) <EOL> else : <EOL> factory . on_trait_change ( self . rebuild_editor , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) <EOL> def _get_names ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _object is None : <EOL> return self . factory . _names <EOL> return self . _names <EOL> def _get_mapping ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _object is None : <EOL> return self . factory . _mapping <EOL> return self . _mapping <EOL> def _get_inverse_mapping ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _object is None : <EOL> return self . factory . _inverse_mapping <EOL> return self . _inverse_mapping <EOL> def rebuild_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def values_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _names , self . _mapping , self . _inverse_mapping = enum_values_changed ( self . _value ( ) ) <EOL> def _values_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . values_changed ( ) <EOL> self . rebuild_editor ( ) <EOL> def dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _object is not None : <EOL> self . _object . on_trait_change ( self . _values_changed , <EOL> '<STR_LIT:U+0020>' + self . _name , remove = True ) <EOL> else : <EOL> self . factory . on_trait_change ( self . rebuild_editor , <EOL> '<STR_LIT>' , remove = True ) <EOL> super ( BaseEditor , self ) . dispose ( ) <EOL> class SimpleEditor ( BaseEditor ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> super ( SimpleEditor , self ) . init ( parent ) <EOL> factory = self . factory <EOL> if factory . evaluate is None : <EOL> self . control = control = wx . Choice ( parent , - <NUM_LIT:1> , wx . Point ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> wx . Size ( - <NUM_LIT:1> , - <NUM_LIT:1> ) , self . names ) <EOL> wx . EVT_CHOICE ( parent , self . control . GetId ( ) , self . update_object ) <EOL> else : <EOL> self . control = control = wx . ComboBox ( parent , - <NUM_LIT:1> , '<STR_LIT>' , <EOL> wx . Point ( <NUM_LIT:0> , <NUM_LIT:0> ) , wx . Size ( - <NUM_LIT:1> , - <NUM_LIT:1> ) , self . names , <EOL> style = wx . CB_DROPDOWN ) <EOL> wx . EVT_COMBOBOX ( parent , control . GetId ( ) , self . update_object ) <EOL> wx . EVT_TEXT_ENTER ( parent , control . GetId ( ) , <EOL> self . update_text_object ) <EOL> wx . EVT_KILL_FOCUS ( control , self . on_kill_focus ) <EOL> if ( not factory . is_grid_cell ) and factory . auto_set : <EOL> wx . EVT_TEXT ( parent , control . GetId ( ) , self . update_text_object ) <EOL> self . _no_enum_update = <NUM_LIT:0> <EOL> self . set_tooltip ( ) <EOL> def dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> disconnect ( self . control , <EOL> wx . EVT_COMBOBOX , wx . EVT_TEXT_ENTER , wx . EVT_TEXT ) <EOL> disconnect_no_id ( self . control , wx . EVT_KILL_FOCUS ) <EOL> super ( SimpleEditor , self ) . dispose ( ) <EOL> def update_object ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . _no_enum_update += <NUM_LIT:1> <EOL> try : <EOL> new_value = self . mapping [ event . GetString ( ) ] <EOL> if new_value == self . value and self . factory . is_grid_cell : <EOL> grid = self . control . Parent . Parent <EOL> grid . EnableEditing ( False ) <EOL> grid . EnableEditing ( True ) <EOL> self . value = new_value <EOL> except : <EOL> from traitsui . api import raise_to_debug <EOL> raise_to_debug ( ) <EOL> self . _no_enum_update -= <NUM_LIT:1> <EOL> def update_text_object ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . _no_enum_update == <NUM_LIT:0> : <EOL> value = self . control . GetValue ( ) <EOL> try : <EOL> value = self . mapping [ value ] <EOL> except : <EOL> try : <EOL> value = self . factory . evaluate ( value ) <EOL> except Exception as excp : <EOL> self . error ( excp ) <EOL> return <EOL> self . _no_enum_update += <NUM_LIT:1> <EOL> try : <EOL> self . value = value <EOL> self . control . SetBackgroundColour ( OKColor ) <EOL> self . control . Refresh ( ) <EOL> except : <EOL> pass <EOL> self . _no_enum_update -= <NUM_LIT:1> <EOL> def on_kill_focus ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . update_text_object ( event ) <EOL> event . Skip ( ) <EOL> def update_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _no_enum_update == <NUM_LIT:0> : <EOL> if self . factory . evaluate is None : <EOL> try : <EOL> self . control . SetStringSelection ( <EOL> self . inverse_mapping [ self . value ] ) <EOL> except : <EOL> pass <EOL> else : <EOL> try : <EOL> self . control . SetValue ( self . str_value ) <EOL> except : <EOL> pass <EOL> def error ( self , excp ) : <EOL> """<STR_LIT>""" <EOL> self . control . SetBackgroundColour ( ErrorColor ) <EOL> self . control . Refresh ( ) <EOL> def rebuild_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> control = self . control <EOL> clear = True <EOL> cur_name = None <EOL> if self . factory . evaluate is not None : <EOL> n = control . GetCount ( ) <EOL> cur_names = [ control . GetString ( i ) for i in range ( n ) ] <EOL> cur_name = control . GetValue ( ) <EOL> if cur_name in self . names : <EOL> clear = False <EOL> include = True <EOL> for i in range ( n - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if cur_name == cur_names [ i ] : <EOL> include = False <EOL> else : <EOL> control . Delete ( i ) <EOL> for name in self . names : <EOL> if include or ( name != cur_name ) : <EOL> control . Append ( name ) <EOL> cur_name = None <EOL> else : <EOL> point = control . GetInsertionPoint ( ) <EOL> if clear : <EOL> control . Clear ( ) <EOL> control . AppendItems ( self . names ) <EOL> if cur_name is not None : <EOL> self . _no_enum_update += <NUM_LIT:1> <EOL> control . SetValue ( cur_name ) <EOL> control . SetInsertionPoint ( point ) <EOL> self . _no_enum_update -= <NUM_LIT:1> <EOL> self . update_editor ( ) <EOL> class RadioEditor ( BaseEditor ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> super ( RadioEditor , self ) . init ( parent ) <EOL> self . control = TraitsUIPanel ( parent , - <NUM_LIT:1> ) <EOL> self . rebuild_editor ( ) <EOL> def update_object ( self , event ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . value = event . GetEventObject ( ) . value <EOL> except : <EOL> pass <EOL> def update_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> value = self . value <EOL> for button in self . control . GetChildren ( ) : <EOL> state = ( button . value == value ) <EOL> button . SetValue ( state ) <EOL> if state : <EOL> button . SetFocus ( ) <EOL> def rebuild_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> panel = self . control <EOL> panel . SetSizer ( None ) <EOL> panel . DestroyChildren ( ) <EOL> cur_name = self . str_value <EOL> names = self . names <EOL> mapping = self . mapping <EOL> n = len ( names ) <EOL> cols = self . factory . cols <EOL> rows = ( n + cols - <NUM_LIT:1> ) / cols <EOL> incr = [ n / cols ] * cols <EOL> rem = n % cols <EOL> for i in range ( cols ) : <EOL> incr [ i ] += ( rem > i ) <EOL> incr [ - <NUM_LIT:1> ] = - ( reduce ( lambda x , y : x + y , incr [ : - <NUM_LIT:1> ] , <NUM_LIT:0> ) - <NUM_LIT:1> ) <EOL> if cols > <NUM_LIT:1> : <EOL> sizer = wx . GridSizer ( <NUM_LIT:0> , cols , <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> else : <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> style = wx . RB_GROUP <EOL> index = <NUM_LIT:0> <EOL> for i in range ( rows ) : <EOL> for j in range ( cols ) : <EOL> if n > <NUM_LIT:0> : <EOL> name = label = names [ index ] <EOL> label = self . string_value ( label , capitalize ) <EOL> control = wx . RadioButton ( panel , - <NUM_LIT:1> , label , style = style ) <EOL> control . value = mapping [ name ] <EOL> style = <NUM_LIT:0> <EOL> control . SetValue ( name == cur_name ) <EOL> wx . EVT_RADIOBUTTON ( panel , control . GetId ( ) , <EOL> self . update_object ) <EOL> self . set_tooltip ( control ) <EOL> index += incr [ j ] <EOL> n -= <NUM_LIT:1> <EOL> else : <EOL> control = wx . RadioButton ( panel , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> control . value = '<STR_LIT>' <EOL> control . Show ( False ) <EOL> sizer . Add ( control , <NUM_LIT:0> , wx . NORTH , <NUM_LIT:5> ) <EOL> panel . SetSizerAndFit ( sizer ) <EOL> class ListEditor ( BaseEditor ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> super ( ListEditor , self ) . init ( parent ) <EOL> self . control = wx . ListBox ( parent , - <NUM_LIT:1> , wx . Point ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> wx . Size ( - <NUM_LIT:1> , - <NUM_LIT:1> ) , self . names , <EOL> style = wx . LB_SINGLE | wx . LB_NEEDED_SB ) <EOL> wx . EVT_LISTBOX ( parent , self . control . GetId ( ) , self . update_object ) <EOL> self . set_tooltip ( ) <EOL> def dispose ( self ) : <EOL> """<STR_LIT>""" <EOL> disconnect ( self . control , wx . EVT_LISTBOX ) <EOL> super ( ListEditor , self ) . dispose ( ) <EOL> def update_object ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if not self . _ignore_update : <EOL> value = self . control . GetStringSelection ( ) <EOL> try : <EOL> value = self . mapping [ value ] <EOL> except : <EOL> try : <EOL> value = self . factory . evaluate ( value ) <EOL> except : <EOL> pass <EOL> try : <EOL> self . value = value <EOL> except : <EOL> pass <EOL> def update_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> control = self . control <EOL> try : <EOL> index = control . FindString ( self . inverse_mapping [ self . value ] ) <EOL> if index >= <NUM_LIT:0> : <EOL> control . SetSelection ( index ) <EOL> except : <EOL> pass <EOL> def rebuild_editor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ignore_update = True <EOL> self . control . Clear ( ) <EOL> self . control . AppendItems ( self . names ) <EOL> self . _ignore_update = False <EOL> self . update_editor ( ) </s>
