<s> """<STR_LIT>""" <EOL> from traits . api import Any , Bool , Callable , Dict , Function , HasTraits , Int , List , Str , Trait , TraitError , Type <EOL> from . grid_model import GridColumn , GridModel , GridSortEvent <EOL> from . trait_grid_cell_adapter import TraitGridCellAdapter <EOL> class TraitGridColumn ( GridColumn ) : <EOL> """<STR_LIT>""" <EOL> name = Trait ( None , None , Str ) <EOL> method = Trait ( None , None , Str ) <EOL> sorter = Trait ( None , None , Callable ) <EOL> formats = Dict ( key_trait = Type , value_trait = Trait ( '<STR_LIT>' , Str , Callable ) ) <EOL> typename = Trait ( None , None , Str ) <EOL> size = Int ( - <NUM_LIT:1> ) <EOL> class TraitGridSelection ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> obj = Trait ( HasTraits ) <EOL> trait_name = Trait ( None , None , Str ) <EOL> class TraitGridModel ( GridModel ) : <EOL> """<STR_LIT>""" <EOL> data = List ( ) <EOL> columns = Trait ( None , None , List ( Trait ( None , Str , TraitGridColumn ) ) ) <EOL> row_name_trait = Trait ( None , None , Str ) <EOL> allow_column_sort = Bool ( True ) <EOL> row_factory = Trait ( None , None , Function ) <EOL> def __init__ ( self , ** traits ) : <EOL> """<STR_LIT>""" <EOL> super ( TraitGridModel , self ) . __init__ ( ** traits ) <EOL> self . _auto_columns = self . columns <EOL> if self . columns is None or len ( self . columns ) == <NUM_LIT:0> : <EOL> if self . data is not None and len ( self . data ) > <NUM_LIT:0> : <EOL> self . _auto_columns = [ ] <EOL> for name , trait in self . data [ <NUM_LIT:0> ] . traits ( ) . items ( ) : <EOL> if trait . type != '<STR_LIT>' : <EOL> self . _auto_columns . append ( TraitGridColumn ( name = name ) ) <EOL> else : <EOL> self . _auto_columns = [ ] <EOL> self . on_trait_event ( self . _on_data_changed , '<STR_LIT:data>' ) <EOL> self . on_trait_event ( self . _on_data_items_changed , '<STR_LIT>' ) <EOL> self . __manage_data_listeners ( self . data ) <EOL> self . on_trait_change ( self . _on_columns_changed , <EOL> '<STR_LIT>' ) <EOL> self . on_trait_event ( self . _on_columns_items_changed , <EOL> '<STR_LIT>' ) <EOL> self . __manage_column_listeners ( self . columns ) <EOL> self . on_trait_change ( self . _on_row_name_trait_changed , '<STR_LIT>' ) <EOL> return <EOL> def get_column_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _auto_columns ) <EOL> def get_column_name ( self , index ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> name = col = self . _auto_columns [ index ] <EOL> if isinstance ( col , TraitGridColumn ) : <EOL> if col . label is not None : <EOL> name = col . label <EOL> else : <EOL> name = col . name <EOL> except IndexError : <EOL> name = '<STR_LIT>' <EOL> return name <EOL> def get_column_size ( self , index ) : <EOL> """<STR_LIT>""" <EOL> size = - <NUM_LIT:1> <EOL> try : <EOL> col = self . _auto_columns [ index ] <EOL> if isinstance ( col , TraitGridColumn ) : <EOL> size = col . size <EOL> except IndexError : <EOL> pass <EOL> return size <EOL> def get_cols_drag_value ( self , cols ) : <EOL> """<STR_LIT>""" <EOL> value = [ ] <EOL> for col in cols : <EOL> value . append ( self . __get_data_column ( col ) ) <EOL> return value <EOL> def get_cols_selection_value ( self , cols ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for obj in self . data : <EOL> for col in cols : <EOL> values . append ( TraitGridSelection ( obj = obj , <EOL> trait_name = self . __get_column_name ( col ) ) ) <EOL> return values <EOL> def sort_by_column ( self , col , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . allow_column_sort : <EOL> return <EOL> try : <EOL> column = self . _auto_columns [ col ] <EOL> name = self . __get_column_name ( col ) <EOL> sorter = cmp <EOL> if isinstance ( column , TraitGridColumn ) and column . sorter is not None : <EOL> sorter = column . sorter <EOL> except IndexError : <EOL> return <EOL> def sort_function ( a , b ) : <EOL> if hasattr ( a , name ) : <EOL> a_trait = getattr ( a , name ) <EOL> else : <EOL> a_trait = None <EOL> if hasattr ( b , name ) : <EOL> b_trait = getattr ( b , name ) <EOL> else : <EOL> b_trait = None <EOL> return sorter ( a_trait , b_trait ) <EOL> self . data . sort ( sort_function ) <EOL> if reverse : <EOL> self . data . reverse ( ) <EOL> print '<STR_LIT>' <EOL> self . column_sorted = GridSortEvent ( index = col , reversed = reverse ) <EOL> return <EOL> def is_column_read_only ( self , index ) : <EOL> """<STR_LIT>""" <EOL> return self . __get_column_readonly ( index ) <EOL> def get_row_count ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . data is not None : <EOL> count = len ( self . data ) <EOL> else : <EOL> count = <NUM_LIT:0> <EOL> return count <EOL> def get_row_name ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if self . row_name_trait is not None : <EOL> try : <EOL> row = self . _get_row ( index ) <EOL> if hasattr ( row , self . row_name_trait ) : <EOL> name = getattr ( row , self . row_name_trait ) <EOL> except IndexError : <EOL> name = str ( index + <NUM_LIT:1> ) <EOL> else : <EOL> name = str ( index + <NUM_LIT:1> ) <EOL> return name <EOL> def get_rows_drag_value ( self , rows ) : <EOL> """<STR_LIT>""" <EOL> value = [ ] <EOL> for index in rows : <EOL> try : <EOL> value . append ( self . _get_row ( index ) ) <EOL> except IndexError : <EOL> value . append ( None ) <EOL> return value <EOL> def get_rows_selection_value ( self , rows ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for row_index in rows : <EOL> values . append ( TraitGridSelection ( obj = self . data [ row_index ] ) ) <EOL> return values <EOL> def is_row_read_only ( self , index ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def get_cell_editor ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> obj = self . data [ row ] <EOL> trait_name = self . __get_column_name ( col ) <EOL> trait = obj . base_trait ( trait_name ) <EOL> if trait is None : <EOL> return None <EOL> factory = trait . get_editor ( ) <EOL> return TraitGridCellAdapter ( factory , obj , trait_name , '<STR_LIT>' ) <EOL> def get_cell_drag_value ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> column = self . __get_column ( col ) <EOL> obj = self . _get_row ( row ) <EOL> value = self . _get_data_from_row ( obj , column ) <EOL> return value <EOL> def get_cell_selection_value ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> obj = self . data [ row ] <EOL> trait_name = self . __get_column_name ( col ) <EOL> return TraitGridSelection ( obj = obj , trait_name = trait_name ) <EOL> def resolve_selection ( self , selection_list ) : <EOL> """<STR_LIT>""" <EOL> cells = [ ] <EOL> for selection in selection_list : <EOL> try : <EOL> row = self . data . index ( selection . obj ) <EOL> except ValueError : <EOL> continue <EOL> column = - <NUM_LIT:1> <EOL> if selection . trait_name is not None : <EOL> column = self . _get_column_index_by_trait ( selection . trait_name ) <EOL> if column is None : <EOL> continue <EOL> cells . append ( ( row , column ) ) <EOL> return cells <EOL> def get_type ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> typename = self . __get_column_typename ( col ) <EOL> return typename <EOL> def get_value ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> value = self . get_cell_drag_value ( row , col ) <EOL> formats = self . __get_column_formats ( col ) <EOL> if value is not None and formats is not None and formats . has_key ( type ( value ) ) and formats [ type ( value ) ] is not None : <EOL> try : <EOL> format = formats [ type ( value ) ] <EOL> if callable ( format ) : <EOL> value = format ( value ) <EOL> else : <EOL> value = format % value <EOL> except TypeError : <EOL> pass <EOL> return value <EOL> def is_cell_empty ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> value = self . get_value ( row , col ) <EOL> return value is None <EOL> def is_cell_editable ( self , row , col ) : <EOL> """<STR_LIT>""" <EOL> return not self . is_column_read_only ( col ) <EOL> def _insert_rows ( self , pos , num_rows ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> if self . row_factory is not None : <EOL> new_data = [ ] <EOL> for i in range ( num_rows ) : <EOL> new_data . append ( self . row_factory ( ) ) <EOL> count = self . _insert_rows_into_model ( pos , new_data ) <EOL> self . rows_added = ( '<STR_LIT>' , pos , new_data ) <EOL> return count <EOL> def _delete_rows ( self , pos , num_rows ) : <EOL> """<STR_LIT>""" <EOL> if pos + num_rows >= self . get_row_count ( ) : <EOL> num_rows = self . get_rows_count ( ) - pos <EOL> return self . _delete_rows_from_model ( pos , num_rows ) <EOL> def _set_value ( self , row , col , value ) : <EOL> """<STR_LIT>""" <EOL> new_rows = <NUM_LIT:0> <EOL> column = self . __get_column ( col ) <EOL> obj = self . _get_row ( row ) <EOL> success = False <EOL> if obj is not None : <EOL> success = self . _set_data_on_row ( obj , column , value ) <EOL> else : <EOL> new_rows = self . _insert_rows ( self . get_row_count ( ) , <NUM_LIT:1> ) <EOL> if new_rows > <NUM_LIT:0> : <EOL> obj = self . _get_row ( self . get_row_count ( ) - <NUM_LIT:1> ) <EOL> success = self . _set_data_on_row ( obj , column , value ) <EOL> if not success : <EOL> pass <EOL> return new_rows <EOL> def _get_row ( self , index ) : <EOL> """<STR_LIT>""" <EOL> return self . data [ index ] <EOL> def _get_data_from_row ( self , row , column ) : <EOL> """<STR_LIT>""" <EOL> value = None <EOL> if row is not None and column is not None : <EOL> if not isinstance ( column , TraitGridColumn ) : <EOL> if hasattr ( row , column ) : <EOL> value = getattr ( row , column ) <EOL> elif column . name is not None and hasattr ( row , column . name ) : <EOL> value = getattr ( row , column . name ) <EOL> elif column . method is not None and hasattr ( row , column . method ) : <EOL> value = getattr ( row , column . method ) ( ) <EOL> return value <EOL> def _set_data_on_row ( self , row , column , value ) : <EOL> """<STR_LIT>""" <EOL> success = False <EOL> if row is not None and column is not None : <EOL> if not isinstance ( column , TraitGridColumn ) : <EOL> if hasattr ( row , column ) : <EOL> setattr ( row , column , value ) <EOL> success = True <EOL> elif column . name is not None and hasattr ( row , column . name ) : <EOL> setattr ( row , column . name , value ) <EOL> success = True <EOL> return success <EOL> def _insert_rows_into_model ( self , pos , new_data ) : <EOL> """<STR_LIT>""" <EOL> for data in new_data : <EOL> self . data . insert ( pos , data ) <EOL> pos += <NUM_LIT:1> <EOL> return <EOL> def _delete_rows_from_model ( self , pos , num_rows ) : <EOL> """<STR_LIT>""" <EOL> del self . data [ pos , pos + num_rows ] <EOL> return num_rows <EOL> def _on_row_name_trait_changed ( self , new ) : <EOL> """<STR_LIT>""" <EOL> self . fire_content_changed ( ) <EOL> return <EOL> def _on_columns_changed ( self , object , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . __manage_column_listeners ( old , remove = True ) <EOL> self . __manage_column_listeners ( self . columns ) <EOL> self . _auto_columns = self . columns <EOL> self . fire_structure_changed ( ) <EOL> return <EOL> def _on_columns_items_changed ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . __manage_column_listeners ( event . removed , remove = True ) <EOL> self . __manage_column_listeners ( event . added ) <EOL> self . fire_structure_changed ( ) <EOL> return <EOL> def _on_contained_trait_changed ( self , new ) : <EOL> """<STR_LIT>""" <EOL> self . fire_content_changed ( ) <EOL> return <EOL> def _on_data_changed ( self , object , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . __manage_data_listeners ( old , remove = True ) <EOL> self . __manage_data_listeners ( self . data ) <EOL> self . fire_structure_changed ( ) <EOL> return <EOL> def _on_data_items_changed ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . __manage_data_listeners ( event . removed , remove = True ) <EOL> self . __manage_data_listeners ( event . added ) <EOL> self . fire_content_changed ( ) <EOL> return <EOL> def __get_data_column ( self , col ) : <EOL> """<STR_LIT>""" <EOL> row_count = self . get_row_count ( ) <EOL> coldata = [ ] <EOL> for row in range ( row_count ) : <EOL> try : <EOL> coldata . append ( self . get_value ( row , col ) ) <EOL> except IndexError : <EOL> coldata . append ( None ) <EOL> return coldata <EOL> def __get_column ( self , col ) : <EOL> try : <EOL> column = self . _auto_columns [ col ] <EOL> except IndexError : <EOL> column = None <EOL> return column <EOL> def __get_column_name ( self , col ) : <EOL> name = column = self . __get_column ( col ) <EOL> if isinstance ( column , TraitGridColumn ) : <EOL> name = column . name <EOL> return name <EOL> def __get_column_typename ( self , col ) : <EOL> name = column = self . __get_column ( col ) <EOL> typename = None <EOL> if isinstance ( column , TraitGridColumn ) : <EOL> typename = column . typename <EOL> return typename <EOL> def __get_column_readonly ( self , col ) : <EOL> read_only = False <EOL> column = self . __get_column ( col ) <EOL> if isinstance ( column , TraitGridColumn ) : <EOL> read_only = column . read_only <EOL> return read_only <EOL> def __get_column_formats ( self , col ) : <EOL> formats = None <EOL> column = self . __get_column ( col ) <EOL> if isinstance ( column , TraitGridColumn ) : <EOL> formats = column . formats <EOL> return formats <EOL> def _get_column_index_by_trait ( self , trait_name ) : <EOL> cols = self . _auto_columns <EOL> for i in range ( len ( cols ) ) : <EOL> col = cols [ i ] <EOL> if isinstance ( col , TraitGridColumn ) : <EOL> col_name = col . name <EOL> else : <EOL> col_name = col <EOL> if col_name == trait_name : <EOL> return i <EOL> return None <EOL> def __manage_data_listeners ( self , list , remove = False ) : <EOL> if list is not None : <EOL> for item in list : <EOL> item . on_trait_change ( self . _on_contained_trait_changed , <EOL> remove = remove ) <EOL> return <EOL> def __manage_column_listeners ( self , collist , remove = False ) : <EOL> if collist is not None : <EOL> for col in collist : <EOL> if isinstance ( col , TraitGridColumn ) : <EOL> col . on_trait_change ( self . _on_columns_changed , <EOL> remove = remove ) <EOL> return </s>
