3
8ßL]Ú3  ã               @   s|   d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	 ddl
mZ ejjejjejjejjdœZd	ZG d
d„ dejƒZdS )z5 Defines the table model used by the tabular editor.
é    )Úabsolute_importÚunicode_literalsN)ÚQtCoreÚQtGui)ÚSequenceTypesé   )Ú
PyMimeData)ÚleftÚrightÚcenterZjustifyztraits-ui-tabular-editorc               @   s´   e Zd ZdZd%dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
ejƒ dfdd„Zejƒ fdd„Zejƒ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ ZdS )&ÚTabularModelz The model for tabular data.Nc             C   s   t jj| |ƒ || _dS )z  Initialise the object.
        N)r   ÚQAbstractTableModelÚ__init__Ú_editor)ÚselfÚeditorÚparent© r   ú@/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/tabular_model.pyr   9   s    zTabularModel.__init__c             C   sÈ  | j }|j}|j|j }}|jƒ |jƒ  }}|tjjksD|tjj	krT|j
||||ƒS |tjjkr†|j|j||||ƒƒ}	|	dk	r‚|	S �n>|tjjkr®|j||||ƒ}
|
rª|
S �n|tjjkrÞ|j||||ƒ}|dk	rÜtj|ƒS næ|tjjk�r|j|||ƒ}tj|tjjƒ}t|tjjB ƒS |tjjk�rp|j||||ƒ}|dk	�rÄt|tƒ�rZtj|Ž }n
tj|ƒ}tj |ƒS nT|tjj!k�rÄ|j"||||ƒ}|dk	�rÄt|tƒ�r°tj|Ž }n
tj|ƒ}tj |ƒS dS )z+ Reimplemented to return the data.
        N)#r   ÚadapterÚobjectÚnameÚrowÚcolumnr   ÚQtÚDisplayRoleÚEditRoleZget_textZDecorationRoleZ
_get_imageZ	get_imageZToolTipRoleZget_tooltipZFontRoleZget_fontr   ZQFontZTextAlignmentRoleZget_alignmentÚalignment_mapÚgetÚ	AlignLeftÚintZAlignVCenterZBackgroundRoleZget_bg_colorÚ
isinstancer   ZQColorZQBrushZForegroundRoleZget_text_color)r   ÚmiÚroler   r   Úobjr   r   r   ÚimageZtooltipÚfontÚstringÚ	alignmentÚcolorZq_colorr   r   r   ÚdataD   sN    




zTabularModel.datac       	      C   s\   |t jjkrdS | j}|j|j }}|jƒ |jƒ  }}|jj	|||||ƒ | j
j||ƒ dS )zF Reimplmented to allow for modification for the object trait.
        FT)r   r   r   r   r   r   r   r   r   Zset_textZdataChangedÚemit)	r   r"   Úvaluer#   r   r$   r   r   r   r   r   r   ÚsetDatay   s    zTabularModel.setDatac             C   sö   | j }|jƒ }|jƒ }|jƒ s&tjjS tjj}|jj	rB|tjj
O }|jjr†d|jjkr†t|jdƒr†|jj|j|j||ƒrº|tjjO }n4|jjrºd|jjkrº|jj|j|j|ƒrº|tjjO }|jj|j|j|ƒdk	rÞ|tjjO }|jjrò|tjjO }|S )zB Reimplemented to set editable status and movable status.
        ÚeditÚget_can_edit_cellN)r   r   r   ÚisValidr   r   ZItemIsDropEnabledZItemIsEnabledÚfactoryZ
selectableZItemIsSelectableÚeditableÚ
operationsÚhasattrr   r/   r   r   ZItemIsEditableZget_can_editÚget_dragZItemIsDragEnabled)r   r"   r   r   r   Úflagsr   r   r   r6   ‡   s0    
zTabularModel.flagsc             C   sX   |t jjkrdS | j}d}|t jjkr8|jj||jƒ}n|t jjkrT|jj	||jƒ}|S )z2 Reimplemented to return the header data.
        N)
r   r   r   r   ZVerticalr   Zget_row_labelr   Z
HorizontalZ	get_label)r   ÚsectionZorientationr#   r   Úlabelr   r   r   Ú
headerData«   s    zTabularModel.headerDatac             C   s   | j }|jj|j|jƒS )z5 Reimplemented to return the number of rows.
        )r   r   Úlenr   r   )r   r"   r   r   r   r   ÚrowCount»   s    zTabularModel.rowCountc             C   s   | j }t|jjƒS )z8 Reimplemented to return the number of columns.
        )r   r:   r   Úcolumns)r   r"   r   r   r   r   ÚcolumnCountÁ   s    zTabularModel.columnCountc             C   sX   | j }|j}|dkr$|j|j|jƒ}| j|||ƒ |j|jj|j|j||ƒ | jƒ  dS )zŠ Reimplemented to allow creation of new rows. Added an optional
            arg to allow the insertion of an existing row object.
        NT)	r   r   Úget_default_valuer   r   ÚbeginInsertRowsÚcallxÚinsertÚendInsertRows)r   r   r   r$   r   r   r   r   r   Ú	insertRowÇ   s    zTabularModel.insertRowc             C   sh   | j }|j}| j|||| d ƒ x8t|ƒD ],}|j|j|jƒ}|j|j|j|j||ƒ q,W | j	ƒ  dS )z7 Reimplemented to allow creation of new items.
        r   T)
r   r   r?   Úranger>   r   r   r@   rA   rB   )r   r   Úcountr   r   r   Úir,   r   r   r   Ú
insertRowsÚ   s    
zTabularModel.insertRowsc             C   s†   | j }|j}| j|||| d ƒ x&t|ƒD ]}|j|j|j|j|ƒ q,W | jƒ  | j	dƒ}|j
js|||k rp|n|d |_ng |_dS )zc Reimplemented to allow row deletion, as well as reordering via drag
            and drop.
        r   NT)r   r   ZbeginRemoveRowsrD   r@   Údeleter   r   ZendRemoveRowsr;   r1   Úmulti_selectÚselected_rowÚmulti_selected_rows)r   r   rE   r   r   r   rF   Únr   r   r   Ú
removeRowsì   s    
zTabularModel.removeRowsc             C   s   t tjtjgS )zb Reimplemented to expose our internal MIME type for drag and drop
            operations.
        )Útabular_mime_typer   Z	MIME_TYPEZNOPICKLE_MIME_TYPE)r   r   r   r   Ú	mimeTypesþ   s    zTabularModel.mimeTypesc                sz   t dd„ |D ƒƒ}‡ fdd„|D ƒ}tj|ƒ}tjtjtˆ ƒƒjdƒƒ}x |D ]}|j	d| jdƒƒ qNW |j
t|ƒ |S )zg Reimplemented to generate MIME data containing the rows of the
            current selection.
        c             S   s   h | ]}|j ƒ ’qS r   )r   )Ú.0Úindexr   r   r   ú	<setcomp>	  s    z(TabularModel.mimeData.<locals>.<setcomp>c                s&   g | ]}ˆ j jjˆ j jˆ j j|ƒ‘qS r   )r   r   r5   r   r   )rP   r   )r   r   r   ú
<listcomp>
  s   z)TabularModel.mimeData.<locals>.<listcomp>Úutf8z %i)Úsortedr   Úcoercer   Z
QByteArrayÚsixÚ	text_typeÚidÚencodeÚappendr-   rN   )r   ÚindexesZrowsÚitemsÚ	mime_datar*   r   r   )r   r   ÚmimeData  s    


zTabularModel.mimeDatac                s<  |t jjkrdS |jtƒ}|jƒ  r‚|t jjkr‚dd„ |jƒ jdƒjdƒD ƒ}|d }|t	| ƒkr‚|dd… }	| j
|	|jƒ ƒ d	S tj|ƒjƒ }|dk	�r8t|tƒsª|g}| j}
|
j‰|
j‰|
j‰ ˆdkrÚ|jƒ rÚ|jƒ ‰ˆdkröˆ jˆˆƒdkröd‰t‡ ‡‡‡fd
d„|D ƒƒ�r8xt|ƒD ]}| j|ˆƒ �qW d	S dS )z3 Reimplemented to allow items to be moved.
        Fc             S   s   g | ]}t |ƒ‘qS r   )r    )rP   Úsr   r   r   rS     s    z-TabularModel.dropMimeData.<locals>.<listcomp>rT   ú r   r   NTc             3   s   | ]}ˆ j ˆˆˆ|ƒV  qd S )N)Zget_can_drop)rP   Úitem)r   r   r   r   r   r   ú	<genexpr>4  s   z,TabularModel.dropMimeData.<locals>.<genexpr>éÿÿÿÿrd   )r   r   ZIgnoreActionr*   rN   ZisNullÚ
MoveActionÚdecodeÚsplitrY   ÚmoveRowsr   r   rV   Úinstancer!   Úlistr   r   r   r   r0   r:   ÚallÚreversedÚdropItem)r   r^   Úactionr   r   r   r*   Zid_and_rowsZtable_idÚcurrent_rowsr   rb   r   )r   r   r   r   r   ÚdropMimeData  s8    


zTabularModel.dropMimeDatac             C   s   t jjt jjB S )z3 Reimplemented to allow items to be moved.
        )r   r   re   Z
CopyAction)r   r   r   r   ÚsupportedDropActions;  s    z!TabularModel.supportedDropActionsc             C   sL   | j }|j}|j}|j}|j||||ƒ}|dkr8|d7 }|j||||ƒ dS )z1 Handle a Python object being dropped onto a row Úafterr   N)r   r   r   r   Zget_droppedrA   )r   rb   r   r   r   r   r   Zdestinationr   r   r   rm   D  s    zTabularModel.dropItemc             C   s   | j |g|ƒS )z2 Convenience method to move a single row.
        )rh   )r   Zold_rowÚnew_rowr   r   r   ÚmoveRowQ  s    zTabularModel.moveRowc             C   sæ   | j }|jƒ  |jƒ  |d |k r*|d7 }g }xF|D ]>}||krH|d8 }|jj|j|j|ƒ}|jd|ƒ | j|ƒ q4W x&t	|ƒD ]\}}| j
|| |d� q€W |jjrÌ|j|d� tt||t|ƒ ƒƒ|_n|j|d d� ||_dS )z` Moves a sequence of rows (provided as a list of row indexes) to a
            new row.
        r   r   )r$   )Zmulti_selected)ÚselectedNrd   )r   ÚsortÚreverser   Zget_itemr   r   rA   Z	removeRowÚ	enumeraterC   r1   rI   Zsetxrj   rD   r:   rK   rJ   )r   ro   rs   r   Zobjectsr   r$   rF   r   r   r   rh   V  s&    
zTabularModel.moveRows)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r-   r6   r9   r;   r=   r   ZQModelIndexrC   rG   rM   rO   r_   rp   rq   rm   rt   rh   r   r   r   r   r   6   s$   
5$'	r   )r|   Ú
__future__r   r   rW   Z	pyface.qtr   r   Ztraitsui.ui_traitsr   Z	clipboardr   r   r   Z
AlignRightZAlignHCenterZAlignJustifyr   rN   r   r   r   r   r   r   Ú<module>   s   