a
    §ˆc4  ã                   @   s~   d 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
jej	j
jej	j
jej	j
jdœZdZe e¡ZG d	d
„ d
ejƒZdS )z5 Defines the table model used by the tabular editor.
é    N)Ú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 ¡ dfdd„Ze ¡ fdd„Ze ¡ 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 | |¡ || _dS )zInitialise the object.N)r   ÚQAbstractTableModelÚ__init__Ú_editor)ÚselfÚeditorÚparent© r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/tabular_model.pyr   (   s    zTabularModel.__init__c                 C   sÜ  | j }|j}|j|j }}| ¡ | ¡  }}|tjjj	ksH|tjjj
krX| ||||¡S |tjjjkrŒ| | ||||¡¡}	|	durˆ|	S �nL|tjjjkr¶| ||||¡}
|
r²|
S �n"|tjjjkrè| ||||¡}|duræt |¡S nð|tjjjk�r*| |||¡}t |tjjj¡}t|tjjjB ƒS |tjjjk�r‚| ||||¡}|du�rØt|t ƒ�rltj!|Ž }n
t !|¡}t "|¡S nV|tjjj#k�rØ| $||||¡}|du�rØt|t ƒ�rÄtj!|Ž }n
t !|¡}t "|¡S dS )z!Reimplemented to return the data.N)%r   ÚadapterÚobjectÚnameÚrowÚcolumnr   ÚQtÚItemDataRoleÚDisplayRoleÚEditRoleZget_textZDecorationRoleZ
_get_imageZ	get_imageZToolTipRoleZget_tooltipZFontRoleZget_fontr   ZQFontZTextAlignmentRoleZget_alignmentÚalignment_mapÚgetÚAlignmentFlagÚ	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   Údata2   sN    ÿ




zTabularModel.datac           	      C   s^   |t jjjkrdS | j}|j|j }}| ¡ | ¡  }}|j	 
|||||¡ | j ||¡ dS )z<Reimplmented to allow for modification for the object trait.FT)r   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   ÚsetDataf   s    zTabularModel.setDatac                 C   s  | j }| ¡ }| ¡ }| ¡ s(tjjjS tjjj}|j	j
rH|tjjjO }|j	jrŽd|j	jv rŽt|jdƒrŽ|j |j|j||¡rÄ|tjjjO }n6|j	jrÄd|j	jv rÄ|j |j|j|¡rÄ|tjjjO }|j |j|j|¡durê|tjjjO }|j	j�r|tjjjO }|S )z8Reimplemented to set editable status and movable status.ZeditÚget_can_edit_cellN)r   r   r   ÚisValidr   r   ZItemFlagZ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   r5   s   sB    

ÿ
þ
ýÿÿ
þýÿÿ
zTabularModel.flagsc                 C   s^   |t jjjkrdS | j}d}|t jjjkr<|j ||j	¡}n|t jjj
krZ|j ||j	¡}|S )z(Reimplemented to return the header data.N)r   r   r   r   r   ZOrientationZ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¡S )z+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 )z.Reimplemented to return the number of columns.)r   r9   r   Úcolumnsr:   r   r   r   ÚcolumnCount±   s    zTabularModel.columnCountc                 C   sX   | j }|j}|du r$| |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   sd   | j }|j}|  |||| d ¡ t|ƒD ],}| |j|j¡}| |j|j|j||¡ q*|  	¡  dS )z-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}|  |||| d ¡ t|ƒD ]}| |j|j|j|¡ q*|  ¡  |  	d¡}|j
jsx||k rl|n|d |_ng |_dS )z^Reimplemented to allow row deletion, as well as reordering via drag
        and drop.
        r   NT)r   r   ZbeginRemoveRowsrD   r@   Údeleter   r   ZendRemoveRowsr;   r0   Ú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 )z]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                    st   t dd„ |D ƒƒ}‡ fdd„|D ƒ}t |¡}t ttˆ ƒƒ d¡¡}|D ]}| d|  d¡¡ qJ| 	t
|¡ |S )zbReimplemented to generate MIME data containing the rows of the
        current selection.
        c                 S   s   h | ]}|  ¡ ’qS r   )r   )Ú.0Úindexr   r   r   Ú	<setcomp>ô   ó    z(TabularModel.mimeData.<locals>.<setcomp>c                    s&   g | ]}ˆ j j ˆ j jˆ j j|¡‘qS r   )r   r   r4   r   r   )rQ   r   rO   r   r   Ú
<listcomp>õ   s   ýÿz)TabularModel.mimeData.<locals>.<listcomp>Úutf8z %i)Úsortedr   Úcoercer   Z
QByteArrayÚstrÚidÚencodeÚappendr-   rN   )r   ZindexesÚrowsÚitemsÚ	mime_datar*   r   r   rO   r   ÚmimeDatað   s    
ü
zTabularModel.mimeDatac                    s0  |t jjjkrdS ˆdkr@| ¡ r,| ¡ ‰ntd|  d¡d ƒ‰| t	¡}| 
¡ s®|t jjjkr®dd„ | ¡  d¡ d	¡D ƒ}|d }|t| ƒkr®|dd… }	|  |	ˆ¡ d
S t |¡ ¡ }|du�r,t|tƒsÖ|g}| j}
|
j‰|
j‰|
j‰ t‡ ‡‡‡fdd„|D ƒƒ�r,t|ƒD ]}|  |ˆ¡ �qd
S dS )ú)Reimplemented to allow items to be moved.Féÿÿÿÿr   Nr   c                 S   s   g | ]}t |ƒ‘qS r   )r    )rQ   Úsr   r   r   rU     s   z-TabularModel.dropMimeData.<locals>.<listcomp>rV   ú Tc                 3   s   | ]}ˆ   ˆˆˆ|¡V  qd S )N)Zget_can_drop)rQ   Úitem©r   r   r   r   r   r   Ú	<genexpr>&  s   z,TabularModel.dropMimeData.<locals>.<genexpr>)r   r   Ú
DropActionZIgnoreActionr/   r   Úmaxr;   r*   rN   ZisNullÚ
MoveActionÚdecodeÚsplitrZ   ÚmoveRowsr   rX   Úinstancer!   Úlistr   r   r   r   ÚallÚreversedÚdropItem)r   r_   Úactionr   r   r   r*   Zid_and_rowsZtable_idÚcurrent_rowsr   re   r   rf   r   ÚdropMimeData  s>    

ÿ

ÿ
zTabularModel.dropMimeDatac                 C   s   t jjjt jjjB S )ra   )r   r   rh   rj   Z
CopyActionrO   r   r   r   ÚsupportedDropActions.  s    z!TabularModel.supportedDropActionsc                 C   sL   | j }|j}|j}|j}| ||||¡}|dkr8|d7 }| ||||¡ dS )z/Handle a Python object being dropped onto a rowÚafterr   N)r   r   r   r   Zget_droppedrA   )r   re   r   r   r   r   r   Zdestinationr   r   r   rr   6  s    zTabularModel.dropItemc                 C   s   |   |g|¡S )z(Convenience method to move a single row.)rm   )r   Zold_rowÚnew_rowr   r   r   ÚmoveRowC  s    zTabularModel.moveRowc                 C   s  | j }|dkr.t d|¡ td|  d¡d ƒ}| ¡  | ¡  |d |k rR|d7 }g }|D ]>}||krn|d8 }|j |j	|j
|¡}| d|¡ |  |¡ qZt|ƒD ]\}}| j|| |d� q¢|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.
        rb   z3Received invalid row %d. Adjusting to the last row.r   Nr   )r$   )Zmulti_selected)Úselected)r   ÚloggerÚdebugri   r;   ÚsortÚreverser   Zget_itemr   r   rA   Z	removeRowÚ	enumeraterC   r0   rI   Zsetxro   rD   r9   rK   rJ   )r   rt   rx   r   Zobjectsr   r$   rF   r   r   r   rm   G  s4    ÿÿzTabularModel.moveRows)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r-   r5   r8   r;   r=   r   ZQModelIndexrC   rG   rM   rP   r`   ru   rv   rr   ry   rm   r   r   r   r   r
   %   s$   

4*
,r
   )rƒ   ÚloggingZ	pyface.qtr   r   Ztraitsui.ui_traitsr   Z	clipboardr   r   r   r   Z
AlignRightZAlignHCenterZAlignJustifyr   rN   Ú	getLoggerr€   r{   r   r
   r   r   r   r   Ú<module>   s   ü
