<s> """<STR_LIT>""" <EOL> from pyface . qt import QtCore , QtGui <EOL> from traitsui . ui_traits import SequenceTypes <EOL> from . clipboard import PyMimeData <EOL> alignment_map = { <EOL> '<STR_LIT:left>' : QtCore . Qt . AlignLeft , <EOL> '<STR_LIT:right>' : QtCore . Qt . AlignRight , <EOL> '<STR_LIT>' : QtCore . Qt . AlignHCenter , <EOL> '<STR_LIT>' : QtCore . Qt . AlignJustify <EOL> } <EOL> tabular_mime_type = '<STR_LIT>' <EOL> class TabularModel ( QtCore . QAbstractTableModel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , editor , parent = None ) : <EOL> """<STR_LIT>""" <EOL> QtCore . QAbstractTableModel . __init__ ( self , parent ) <EOL> self . _editor = editor <EOL> def data ( self , mi , role ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> adapter = editor . adapter <EOL> obj , name = editor . object , editor . name <EOL> row , column = mi . row ( ) , mi . column ( ) <EOL> if role == QtCore . Qt . DisplayRole or role == QtCore . Qt . EditRole : <EOL> return adapter . get_text ( obj , name , row , column ) <EOL> elif role == QtCore . Qt . DecorationRole : <EOL> image = editor . _get_image ( adapter . get_image ( obj , name , row , column ) ) <EOL> if image is not None : <EOL> return image <EOL> elif role == QtCore . Qt . ToolTipRole : <EOL> tooltip = adapter . get_tooltip ( obj , name , row , column ) <EOL> if tooltip : <EOL> return tooltip <EOL> elif role == QtCore . Qt . FontRole : <EOL> font = adapter . get_font ( obj , name , row , column ) <EOL> if font is not None : <EOL> return QtGui . QFont ( font ) <EOL> elif role == QtCore . Qt . TextAlignmentRole : <EOL> string = adapter . get_alignment ( obj , name , column ) <EOL> alignment = alignment_map . get ( string , QtCore . Qt . AlignLeft ) <EOL> return int ( alignment | QtCore . Qt . AlignVCenter ) <EOL> elif role == QtCore . Qt . BackgroundRole : <EOL> color = adapter . get_bg_color ( obj , name , row , column ) <EOL> if color is not None : <EOL> if isinstance ( color , SequenceTypes ) : <EOL> q_color = QtGui . QColor ( * color ) <EOL> else : <EOL> q_color = QtGui . QColor ( color ) <EOL> return QtGui . QBrush ( q_color ) <EOL> elif role == QtCore . Qt . ForegroundRole : <EOL> color = adapter . get_text_color ( obj , name , row , column ) <EOL> if color is not None : <EOL> if isinstance ( color , SequenceTypes ) : <EOL> q_color = QtGui . QColor ( * color ) <EOL> else : <EOL> q_color = QtGui . QColor ( color ) <EOL> return QtGui . QBrush ( q_color ) <EOL> return None <EOL> def setData ( self , mi , value , role ) : <EOL> """<STR_LIT>""" <EOL> if role != QtCore . Qt . EditRole : <EOL> return False <EOL> editor = self . _editor <EOL> obj , name = editor . object , editor . name <EOL> row , column = mi . row ( ) , mi . column ( ) <EOL> editor . adapter . set_text ( obj , name , row , column , value ) <EOL> signal = QtCore . SIGNAL ( '<STR_LIT>' ) <EOL> self . emit ( signal , mi , mi ) <EOL> return True <EOL> def flags ( self , mi ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> row = mi . row ( ) <EOL> column = mi . column ( ) <EOL> if not mi . isValid ( ) : <EOL> return QtCore . Qt . ItemIsDropEnabled <EOL> flags = QtCore . Qt . ItemIsEnabled <EOL> if editor . factory . selectable : <EOL> flags |= QtCore . Qt . ItemIsSelectable <EOL> if ( editor . factory . editable and '<STR_LIT>' in editor . factory . operations and <EOL> hasattr ( editor . adapter , '<STR_LIT>' ) ) : <EOL> if editor . adapter . get_can_edit_cell ( editor . object , editor . name , <EOL> row , column ) : <EOL> flags |= QtCore . Qt . ItemIsEditable <EOL> elif ( editor . factory . editable and '<STR_LIT>' in editor . factory . operations and <EOL> editor . adapter . get_can_edit ( editor . object , editor . name , row ) ) : <EOL> flags |= QtCore . Qt . ItemIsEditable <EOL> if editor . adapter . get_drag ( editor . object , editor . name , row ) is not None : <EOL> flags |= QtCore . Qt . ItemIsDragEnabled <EOL> if editor . factory . editable : <EOL> flags |= QtCore . Qt . ItemIsDropEnabled <EOL> return flags <EOL> def headerData ( self , section , orientation , role ) : <EOL> """<STR_LIT>""" <EOL> if role != QtCore . Qt . DisplayRole : <EOL> return None <EOL> editor = self . _editor <EOL> label = None <EOL> if orientation == QtCore . Qt . Vertical : <EOL> label = editor . adapter . get_row_label ( section , editor . object ) <EOL> elif orientation == QtCore . Qt . Horizontal : <EOL> label = editor . adapter . get_label ( section , editor . object ) <EOL> return label <EOL> def rowCount ( self , mi ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> return editor . adapter . len ( editor . object , editor . name ) <EOL> def columnCount ( self , mi ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> return len ( editor . adapter . columns ) <EOL> def insertRow ( self , row , parent = QtCore . QModelIndex ( ) , obj = None ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> adapter = editor . adapter <EOL> if obj is None : <EOL> obj = adapter . get_default_value ( editor . object , editor . name ) <EOL> self . beginInsertRows ( parent , row , row ) <EOL> editor . callx ( editor . adapter . insert , editor . object , editor . name , row , obj ) <EOL> self . endInsertRows ( ) <EOL> return True <EOL> def insertRows ( self , row , count , parent = QtCore . QModelIndex ( ) ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> adapter = editor . adapter <EOL> self . beginInsertRows ( parent , row , row + count - <NUM_LIT:1> ) <EOL> for i in xrange ( count ) : <EOL> value = adapter . get_default_value ( editor . object , editor . name ) <EOL> editor . callx ( adapter . insert , editor . object , editor . name , row , value ) <EOL> self . endInsertRows ( ) <EOL> return True <EOL> def removeRows ( self , row , count , parent = QtCore . QModelIndex ( ) ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> adapter = editor . adapter <EOL> self . beginRemoveRows ( parent , row , row + count - <NUM_LIT:1> ) <EOL> for i in xrange ( count ) : <EOL> editor . callx ( adapter . delete , editor . object , editor . name , row ) <EOL> self . endRemoveRows ( ) <EOL> n = self . rowCount ( None ) <EOL> if not editor . factory . multi_select : <EOL> editor . selected_row = row if row < n else row - <NUM_LIT:1> <EOL> else : <EOL> editor . multi_selected_rows = [ ] <EOL> return True <EOL> def mimeTypes ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ tabular_mime_type , PyMimeData . MIME_TYPE , <EOL> PyMimeData . NOPICKLE_MIME_TYPE ] <EOL> def mimeData ( self , indexes ) : <EOL> """<STR_LIT>""" <EOL> rows = sorted ( set ( [ index . row ( ) for index in indexes ] ) ) <EOL> items = [ self . _editor . adapter . get_drag ( <EOL> self . _editor . object , self . _editor . name , row ) <EOL> for row in rows ] <EOL> mime_data = PyMimeData . coerce ( items ) <EOL> data = QtCore . QByteArray ( str ( id ( self ) ) ) <EOL> for row in rows : <EOL> data . append ( '<STR_LIT>' % row ) <EOL> mime_data . setData ( tabular_mime_type , data ) <EOL> return mime_data <EOL> def dropMimeData ( self , mime_data , action , row , column , parent ) : <EOL> """<STR_LIT>""" <EOL> if action == QtCore . Qt . IgnoreAction : <EOL> return False <EOL> data = mime_data . data ( tabular_mime_type ) <EOL> if not data . isNull ( ) and action == QtCore . Qt . MoveAction : <EOL> id_and_rows = map ( int , str ( data ) . split ( '<STR_LIT:U+0020>' ) ) <EOL> table_id = id_and_rows [ <NUM_LIT:0> ] <EOL> if table_id == id ( self ) : <EOL> current_rows = id_and_rows [ <NUM_LIT:1> : ] <EOL> self . moveRows ( current_rows , parent . row ( ) ) <EOL> return True <EOL> data = PyMimeData . coerce ( mime_data ) . instance ( ) <EOL> if data is not None : <EOL> if not isinstance ( data , list ) : <EOL> data = [ data ] <EOL> editor = self . _editor <EOL> object = editor . object <EOL> name = editor . name <EOL> adapter = editor . adapter <EOL> if row == - <NUM_LIT:1> and parent . isValid ( ) : <EOL> row = parent . row ( ) <EOL> if row == - <NUM_LIT:1> and adapter . len ( object , name ) == <NUM_LIT:0> : <EOL> row = <NUM_LIT:0> <EOL> if all ( adapter . get_can_drop ( object , name , row , item ) <EOL> for item in data ) : <EOL> for item in reversed ( data ) : <EOL> self . dropItem ( item , row ) <EOL> return True <EOL> return False <EOL> def supportedDropActions ( self ) : <EOL> """<STR_LIT>""" <EOL> return QtCore . Qt . MoveAction <EOL> def dropItem ( self , item , row ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> object = editor . object <EOL> name = editor . name <EOL> adapter = editor . adapter <EOL> destination = adapter . get_dropped ( object , name , row , item ) <EOL> if destination == '<STR_LIT>' : <EOL> row += <NUM_LIT:1> <EOL> adapter . insert ( object , name , row , item ) <EOL> def moveRow ( self , old_row , new_row ) : <EOL> """<STR_LIT>""" <EOL> return self . moveRows ( [ old_row ] , new_row ) <EOL> def moveRows ( self , current_rows , new_row ) : <EOL> """<STR_LIT>""" <EOL> editor = self . _editor <EOL> current_rows . sort ( ) <EOL> current_rows . reverse ( ) <EOL> if current_rows [ - <NUM_LIT:1> ] < new_row : <EOL> new_row += <NUM_LIT:1> <EOL> objects = [ ] <EOL> for row in current_rows : <EOL> if row <= new_row : <EOL> new_row -= <NUM_LIT:1> <EOL> obj = editor . adapter . get_item ( editor . object , editor . name , row ) <EOL> objects . insert ( <NUM_LIT:0> , obj ) <EOL> self . removeRow ( row ) <EOL> for i , obj in enumerate ( objects ) : <EOL> self . insertRow ( new_row + i , obj = obj ) <EOL> if editor . factory . multi_select : <EOL> editor . setx ( multi_selected = objects ) <EOL> editor . multi_selected_rows = range ( new_row , new_row + len ( objects ) ) <EOL> else : <EOL> editor . setx ( selected = objects [ <NUM_LIT:0> ] ) <EOL> editor . selected_row = new_row </s>
