3
8ßL]ù- ã               @   sd  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZmZmZmZmZmZ ddlmZmZmZm Z  ddl!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1Z1ej2e3ƒZ4e0ƒ Z5G dd„ de,ƒZ6G dd„ de
j7ƒZ8G dd„ de
j9ƒZ:dS )z> Defines the tree editor for the PyQt user interface toolkit.
é    )Úabsolute_importN)Úzip_longest)ÚQtCoreÚQtGui)ÚImageResource)Úconvert_image)Údo_later)ÚAnyÚEventÚInt)Ú
CopyActionÚ	CutActionÚDeleteActionÚ	NewActionÚPasteActionÚRenameAction)ÚITreeNodeAdapterBridgeÚMultiTreeNodeÚObjectTreeNodeÚTreeNode)ÚMenuÚActionÚ	Separator)ÚSequenceTypes)ÚListUndoItemé   )Ú	clipboardÚ
PyMimeData)ÚEditor)Úpixmap_cache)ÚWordWrapRendererc                   sî  e Zd ZdZdZeZeZeZ	eZ
eZeZeZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡ fdd„Zd¦dd„Zdd„ Zdd„ Zdd„ Zdd„ Zd§dd„Zd¨dd„Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Z d+d,„ Z!e"j#j$e"j#j%e"j#j&d-œZ'd©d/d0„Z(d1d2„ Z)d3d4„ Z*d5d6„ Z+dªd8d9„Z,d«d:d;„Z-d<d=„ Z.d>d?„ Z/d@dA„ Z0dBdC„ Z1dDdE„ Z2dFdG„ Z3dHdI„ Z4d¬dJdK„Z5d­dLdM„Z6dNdO„ Z7d®dPdQ„Z8dRdS„ Z9e:dTdU„ ƒZ;e:dVdW„ ƒZ<dXdY„ Z=d¯dZd[„Z>d°d\d]„Z?d^d_„ Z@d`da„ ZAdbdc„ ZBddde„ ZCdfdg„ ZDdhdi„ ZEdjdk„ ZFdldm„ ZGdndo„ ZHdpdq„ ZIdrds„ ZJdtdu„ ZKdvdw„ ZLdxdy„ ZMdzd{„ ZNd±d|d}„ZOd~d„ ZPd€d�„ ZQd‚dƒ„ ZRd„d…„ ZSd²d†d‡„ZTdˆd‰„ ZUdŠd‹„ ZVdŒd�„ ZWdŽd�„ ZXd�d‘„ ZYd’d“„ ZZd”d•„ Z[d–d—„ Z\d³d˜d™„Z]d´dšd›„Z^dœd�„ Z_dždŸ„ Z`d d¡„ Zad¢d£„ Zbd¤d¥„ Zc‡  ZdS )µÚSimpleEditorz" Simple style of tree editor.
    Tc             C   s(  | j }d| _|j�rŽ|jrø|j}|dkr | |_tjƒ  | _}|jtj	j
ƒ |jdƒ d | j_| j_|j}|dk	rœx"|jD ]}|j| jkrz| j|_qzW d|_dS |j}|dksº|j| jk	rÞ|j}|dkrÒg  |_}|j| ƒ n|j| _t| ƒ | _| _n”t| ƒ| _tjƒ  | _}|jtj	j
ƒ |jdƒ d |_|_|jdk�rJtjj}ntjj}tj|ƒ | _}	|	jtjjtjjƒ |	j| jƒ |	j|ƒ nt| ƒ | _| _tƒ }
| |
_| jj|
ƒ |
| _i | _ g | _!| j"|j#dƒ | j"|j$dƒ | j"|j%ddƒ | j"|j&ddƒ | j"|j'd	dƒ | j"|j(d
dƒ dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        NTZ
horizontalÚrefreshÚselectedÚ	activatedZtoÚclickÚdclickÚvetoÚfrom))ÚfactoryÚ_editorÚeditableZshared_editorÚeditorr   ZQScrollAreaÚcontrolZsetFrameShapeZQFrameZNoFrameZsetWidgetResizableÚ_node_uiÚ_editor_nidZ_shared_editorsÚuiÚappendÚ_TreeWidgetÚ_treeZorientationr   ÚQtZ
HorizontalZVerticalÚ	QSplitterZsetSizePolicyZQSizePolicyZ	ExpandingZ	addWidgetÚTreeItemDelegateZsetItemDelegateZ_item_delegateÚ_mapÚ	_undoableZ
sync_valuer"   r#   r$   r%   r&   r'   )ÚselfÚparentr)   Zfactory_editorÚsaZeditorsr,   Zshared_editorsZorientZsplitterZdelegate© r<   ú>/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/tree_editor.pyÚinit_   sh    






zSimpleEditor.initc          
   C   s°   yŒ| j }t|tjƒ rvt|tjƒrvtjƒ }x4|D ],}| j|ƒd }|j	|ƒ}|j
tj|ƒƒ q0W |jƒ j|tjjƒ n|j| j|ƒd ƒ W n   ddlm} |ƒ  Y nX dS )z* Handles the **selection** event.
        é   r   )Úraise_to_debugN)r3   Ú
isinstanceÚsixÚstring_typesÚcollectionsÚIterabler   ZQItemSelectionÚ_object_infoÚindexFromItemr1   ZQItemSelectionRangeZselectionModelÚselectZQItemSelectionModelZClearAndSelectÚsetCurrentItemÚtraitsui.apir@   )r9   Ú	selectionÚtreeZitem_selectionÚselÚitemÚidxr@   r<   r<   r=   Ú_selection_changedË   s    

zSimpleEditor._selection_changedc             C   s   | j s| j|ƒ dS )z7 Handles the **selected** trait being changed.
        N)Ú_no_update_selectedrP   )r9   r#   r<   r<   r=   Ú_selected_changedå   s    zSimpleEditor._selected_changedc             C   s
   d| _ dS )z/ Handles the 'veto' event being fired.
        TN)Ú_veto)r9   r<   r<   r=   Ú_veto_changedï   s    zSimpleEditor._veto_changedc             C   s   | j jƒ jƒ  dS )z Update the viewport.
        N)r3   ÚviewportÚupdate)r9   r<   r<   r=   Ú_refresh_changedô   s    zSimpleEditor._refresh_changedc                s>   | j dk	r,| j jdƒ | j| j jƒ ƒ d| _ tt| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        NT)r3   ÚblockSignalsÚ_delete_nodeÚinvisibleRootItemÚsuperr!   Údispose)r9   )Ú	__class__r<   r=   r\   ý   s
    
zSimpleEditor.disposec             C   sd   |dkr`| j |ƒ\}}}| j||ƒr`| j|ƒ |r<|jdƒ x"| j|ƒD ]}| j||d ƒ qHW dS )zM Expands from the specified node the specified number of sub-levels.
        r   Tr   N)Ú_get_node_dataÚ_has_childrenÚ_expand_nodeÚsetExpandedÚ
_nodes_forÚexpand_levels)r9   ÚnidZlevelsÚexpandÚexpandedÚnodeÚobjectÚcnidr<   r<   r=   rc     s    

zSimpleEditor.expand_levelsc       	      C   s€  | j }|dkrdS i }| j| jƒ\}}| j||j|ƒƒ}|rF| j|ƒ | j| jƒ\}}| j||j|ƒƒ}|rv| j|ƒ |jƒ  i | _| j| jƒ\}}|dk	�rH| j	j
r°|jƒ }n| j|||ƒ}|j|ƒ|fg| jt|ƒ< | j||ƒ | j|d||fƒ | j	j
�s| j||ƒ�r6| j|ƒ | j	j
�s6|jdƒ |j|ƒ | j|| j	jdƒ | j jƒ }|dk�r|xt|ƒD ]}| j j|ƒ �qfW dS )z` Updates the editor when the object trait changes externally to the
            editor.
        NFTr   )r3   Ú	_node_forÚ	old_valueÚ_get_object_nidÚget_children_idrY   Úclearr7   Úvaluer)   Ú	hide_rootrZ   Ú_create_itemÚidÚ_add_listenersÚ_set_node_datar_   r`   ra   rI   rc   Ú	auto_openZcolumnCountÚrangeZresizeColumnToContents)	r9   rL   Úsaved_staterh   rg   Zold_nidrd   ZncolumnsÚir<   r<   r=   Úupdate_editor  s@    









zSimpleEditor.update_editorc             C   s   | j S )zC Returns the editor's control for indicating error status.
        )r3   )r9   r<   r<   r=   Úget_error_controlO  s    zSimpleEditor.get_error_controlc             C   s*   t |tƒrtj|Ž }n
tj|ƒ}tj|ƒS )N)rA   r   r   ÚQColorZQBrush)r9   ÚcolorZq_colorr<   r<   r=   Ú
_get_brushT  s    

zSimpleEditor._get_brushc       
      C   s„   |j |ƒ}xttt| jjdd… |ƒdƒD ]T\}\}}|j||ƒ}|rJ|jnd}	|dksb|dksb|	rp|j|dƒ q(|j||ƒ q(W dS )z Set the column labels. r   NFÚ )Zget_column_labelsÚ	enumerater   r)   Úcolumn_headersÚget_rendererÚhandles_textÚsetText)
r9   rd   rg   rh   Zcolumn_labelsrx   ÚheaderÚlabelÚrendererr‚   r<   r<   r=   Ú_set_column_labels[  s    
&zSimpleEditor._set_column_labelsNc       
      C   sä   |dkrt j|ƒ}nt jƒ }|j||ƒ |j|ƒ}t|ddƒ}t|ddƒ}| jjpT|sh|jd|j|ƒƒ |s€|j	d| j
||ƒƒ |jd|j|ƒƒ | j|||ƒ |j|ƒ}	|	rÀ|jd| j|	ƒƒ |j|ƒ}	|	rà|jd| j|	ƒƒ |S )z¨ Create  a new TreeWidgetItem as per word_wrap policy.

        Index is the index of the new node in the parent:
            None implies append the child to the end. Nr‚   FÚhandles_iconr   )r   ÚQTreeWidgetItemÚinsertChildr�   Úgetattrr)   Ú	word_wraprƒ   Ú	get_labelÚsetIconÚ	_get_iconZ
setToolTipZget_tooltipr‡   Zget_backgroundZsetBackgroundr}   Zget_foregroundZsetForeground)
r9   rd   rg   rh   Úindexri   r†   r‚   rˆ   r|   r<   r<   r=   rq   l  s(    


zSimpleEditor._create_itemr   c             C   sb   |dkrdS | j |ƒ\}}}|j|ƒ}t|ddƒ}| jjs>|rL|j|dƒ n|j||j|ƒƒ dS )z% Set the label of the specified item r   Nr‚   Fr~   )r^   r�   r‹   r)   rŒ   rƒ   r�   )r9   rd   Úcolrf   rg   rh   r†   r‚   r<   r<   r=   Ú
_set_label‹  s    
zSimpleEditor._set_labelc             C   s   | j |d||ƒS )z3 Appends a new node to the specified node.
        N)Ú_insert_node)r9   rd   rg   rh   r<   r<   r=   Ú_append_nodeœ  s    zSimpleEditor._append_nodec             C   s†   | j ||||ƒ}| j||ƒ}| j|d||fƒ | jjt|ƒg ƒj|j|ƒ|fƒ | j||ƒ |r‚|j	|ƒrv|j
dƒ ntj|ƒ|_|S )zj Inserts a new node before a specified index into the children of the
            specified node.
        FT)rq   r_   rt   r7   Ú
setdefaultrr   r1   rm   rs   Úcan_auto_openra   r   r‰   Ú_dummy)r9   rd   r�   rg   rh   ri   Úhas_childrenr<   r<   r=   r“   ¥  s    
zSimpleEditor._insert_nodec             C   s4  x| j |ƒD ]}| j|ƒ qW |jƒ }|dk	rPt|ddƒ|krP|j|ƒ |`dS y| j|ƒ\}}}W n tk
rx   Y nfX t|ƒ}| j	| }x0t
|ƒD ]$\}	}
t|ƒt|
d ƒkr–||	= P q–W t|ƒdkrÞ| j||ƒ | j	|= |dkrü| jj| jj|ƒƒ n
|j|ƒ | jdk	�r0t|ƒt| jjƒk�r0| jƒ  dS )z= Deletes a specified tree node and all its children.
        Nr—   r   r   )rb   rY   r:   r‹   ÚremoveChildr—   r^   ÚAttributeErrorrr   r7   r   ÚlenÚ_remove_listenersr3   ZtakeTopLevelItemZindexOfTopLevelItemr*   r/   Ú_clear_editor)r9   rd   ri   Úpnidrf   rg   rh   Z	id_objectZobject_inforx   Úinfor<   r<   r=   rY   Ä  s4    


zSimpleEditor._delete_nodec             C   s„   | j |ƒ\}}}|s€t|ddƒ}|dk	r6|j|ƒ |`x6|j|ƒD ](}| j|ƒ\}}|dk	rB| j|||ƒ qBW | j|d||fƒ dS )zA Expands the contents of a specified node (if required).
        r—   NT)r^   r‹   r™   r—   Úget_childrenrj   r”   rt   )r9   rd   rf   rg   rh   ÚdummyÚchildÚ
child_noder<   r<   r=   r`   ó  s    
zSimpleEditor._expand_nodec                s   ‡ fdd„t ˆ jƒ ƒD ƒS )z< Returns all child node ids of a specified node id.
        c                s   g | ]}ˆ j |ƒ‘qS r<   )r¢   )Ú.0rx   )rd   r<   r=   ú
<listcomp>  s    z+SimpleEditor._nodes_for.<locals>.<listcomp>)rv   Ú
childCount)r9   rd   r<   )rd   r=   rb     s    zSimpleEditor._nodes_forc             C   st   |j ƒ }|d kr.| jjr"| jjƒ }|d kr.dS x@t|jƒ ƒD ],}|j|ƒ|kr<| j|ƒ\}}}|||fS q<W dS d S )N)NNN)NNN)	r:   r)   rp   r3   rZ   rv   r¦   r¢   r^   )r9   rd   rž   rx   Ú_ZpnodeZpobjectr<   r<   r=   Ú_node_index  s    
zSimpleEditor._node_indexc             C   s   |j |ƒo|j|ƒS )z> Returns whether a specified object has any children.
        )Úallows_childrenr˜   )r9   rg   rh   r<   r<   r=   r_   )  s    zSimpleEditor._has_children)z<item>z<group>z<open>Fc       
      C   s&  | j jstjƒ S |j||ƒ}t|tjƒrž|jdƒrDt	|dƒ}|j
ƒ S || jkrh| j| }| jjƒ j|ƒS |j|ƒ}t|tjƒrˆ||g}n
||g }t||ƒ}ntt|tƒr®|}ndt|tƒrèt|ƒdkrÒtjj|Ž }ntjj|Ž }| j|ƒS t|tjƒ�r | j|ƒS tddj|ƒ ƒ‚|j}	tjt|	ƒƒS )z9 Returns the index of the specified object icon.
        ú@é   g      ð?z7Icon value must be a string or color or color tuple or zIImageResource instance: z
given {!r}zPIcon value must be a string or color or color tuple or IImageResource instance: )r)   Z
show_iconsr   ÚQIconZget_iconrA   rB   rC   Ú
startswithr   Zcreate_iconÚSTD_ICON_MAPr3   ÚstyleZstandardIconZget_icon_pathr   ÚtupleÚmaxr{   ZfromRgbFZfromRgbÚ_icon_from_colorÚ
ValueErrorÚformatZabsolute_pathr   )
r9   rg   rh   Zis_expandedZ	icon_nameZimage_resourceZiconÚpathr|   Ú	file_namer<   r<   r=   r�   8  s:    










zSimpleEditor._get_iconc             C   s$   t j| jjƒ ƒ}|j|ƒ t j|ƒS )z3 Create a square icon filled with the given color. )r   ÚQPixmapr3   ZiconSizeÚfillr¬   )r9   r|   Zpixmapr<   r<   r=   r²   h  s    
zSimpleEditor._icon_from_colorc             C   sN   |j |ƒr*|j|| jdƒ |j|| jdƒ |j|| jdƒ |j|| jdƒ dS )z: Adds the event listeners for a specified object.
        FN)	r©   Úwhen_children_replacedÚ_children_replacedÚwhen_children_changedÚ_children_updatedÚwhen_label_changedÚ_label_updatedÚwhen_column_labels_changeÚ_column_labels_updated)r9   rg   rh   r<   r<   r=   rs   r  s    
zSimpleEditor._add_listenersc             C   sN   |j |ƒr*|j|| jdƒ |j|| jdƒ |j|| jdƒ |j|| jdƒ dS )z> Removes any event listeners from a specified object.
        TN)	r©   r¹   rº   r»   r¼   r½   r¾   r¿   rÀ   )r9   rg   rh   r<   r<   r=   rœ   ‚  s    
zSimpleEditor._remove_listenersr~   c       	      C   sP   | j t|ƒ }x&|D ]\}}||krP qW |d d }| j|ƒ\}}}|||fS )zl Returns the tree node data for a specified object in the form
            ( expanded, node, nid ).
        r   r   )r7   rr   r^   )	r9   rh   ÚnamerŸ   Úname2rd   rf   rg   Úignorer<   r<   r=   rF   “  s    zSimpleEditor._object_infoc       	      C   sL   g }xB| j t|ƒ D ]0\}}||kr| j|ƒ\}}}|j|||fƒ qW |S )z€ Returns the tree node data for a specified object as a list of the
            form: [ ( expanded, node, nid ), ... ].
        )r7   rr   r^   r1   )	r9   rh   rÁ   ÚresultrÂ   rd   rf   rg   rÃ   r<   r<   r=   Ú_object_info_for¢  s    zSimpleEditor._object_info_forc       	         s&  t ˆtƒr(tˆƒdkr(t ˆd tƒr(ˆS | j}‡fdd„|jD ƒ}t|ƒdkrZˆ|d fS t|ƒdkrtˆtˆd�fS |d j‰ ‡ fdd„|D ƒ}t|ƒdkr¨ˆ|d fS d}x2t|ƒD ]\}}|j	d	kr¶|}||= P q¶W |d }|ft|ƒ }||j
k�rˆ|j
| fS t||d
� |j
|< }ˆ|fS )zB Returns the TreeNode associated with a specified object.
        r?   r   c                s   g | ]}|j ˆ ƒr|‘qS r<   )Zis_node_for)r¤   rg   )rh   r<   r=   r¥   »  s    z*SimpleEditor._node_for.<locals>.<listcomp>r   )Úadapterc                s   g | ]}ˆ |j kr|‘qS r<   )Únode_for)r¤   rg   )Úbaser<   r=   r¥   É  s    Nr~   )Ú	root_nodeÚnodes)rA   r°   r›   r   r)   rÊ   r   rÇ   r   ÚchildrenZmulti_nodesr   )	r9   rh   r)   rÊ   rÉ   rx   rg   ÚkeyZ
multi_noder<   )rÈ   rh   r=   rj   ²  s6    

zSimpleEditor._node_forc             C   s*   x$| j jD ]}t|t|jƒƒr
|S q
W dS )zA Returns the TreeNode associated with a specified class.
        N)r)   rÊ   Ú
issubclassr°   rÇ   )r9   Úklassrg   r<   r<   r=   Ú_node_for_classì  s    zSimpleEditor._node_for_classc             C   s8   x2| j jD ]&}x |jD ]}||jkr||fS qW q
W dS )zL Returns the node and class associated with a specified class name.
        N)NN)r)   rÊ   rÇ   Ú__name__)r9   Ú
class_namerg   rÎ   r<   r<   r=   Ú_node_for_class_nameø  s
    
z!SimpleEditor._node_for_class_namec             C   sV   | j |ƒ\}}}|j|ƒ}|dks*|j rB|jd| j|||ƒƒ n|jdtjƒ ƒ dS )z0 Updates the icon for a specified node.
        Nr   )r^   r�   rˆ   rŽ   r�   r   r¬   )r9   rd   rf   rg   rh   r†   r<   r<   r=   Ú_update_icon  s
    
zSimpleEditor._update_iconc             C   s6   | j }| jj|jƒ |jdkr2|jdk	r2|jj|_dS )z+ Begins an "undoable" transaction.
        r   Néÿÿÿÿ)r0   r8   r1   ÚhistoryÚnow)r9   r0   r<   r<   r=   Ú_begin_undo  s    zSimpleEditor._begin_undoc             C   s   | j jƒ dkrd| j_ d S )Nr   rÔ   rÔ   )r8   Úpopr0   )r9   r<   r<   r=   Ú	_end_undo  s    zSimpleEditor._end_undoc             C   s   t |||j|j|jd�S )N)rh   rÁ   r�   ÚaddedÚremoved)r   r�   rÚ   rÛ   )r9   rh   rÁ   Úeventr<   r<   r=   Ú_get_undo_item'  s
    zSimpleEditor._get_undo_itemc          
   C   s6   z&| j ƒ  |rtj|ƒ}|j||ƒ W d| jƒ  X dS )z0 Performs an undoable append operation.
        N)r×   ÚcopyÚdeepcopyZappend_childrÙ   )r9   rg   rh   ÚdataÚ	make_copyr<   r<   r=   Ú_undoable_append2  s    
zSimpleEditor._undoable_appendc          
   C   s8   z(| j ƒ  |rtj|ƒ}|j|||ƒ W d| jƒ  X dS )z0 Performs an undoable insert operation.
        N)r×   rÞ   rß   Zinsert_childrÙ   )r9   rg   rh   r�   rà   rá   r<   r<   r=   Ú_undoable_insertA  s    
zSimpleEditor._undoable_insertc          
   C   s(   z| j ƒ  |j||ƒ W d| jƒ  X dS )z0 Performs an undoable delete operation.
        N)r×   Zdelete_childrÙ   )r9   rg   rh   r�   r<   r<   r=   Ú_undoable_deleteP  s    zSimpleEditor._undoable_deletec             C   sJ   | j jt|ƒƒ}|dkrdS x(|D ]\}}||kr"|S q"W |d d S dS )zB Gets the ID associated with a specified object (if any).
        Nr   r   )r7   Úgetrr   )r9   rh   rÁ   rŸ   rÂ   rd   r<   r<   r=   rl   ]  s    zSimpleEditor._get_object_nidc             C   s4   | j }|jdk	r0|jdƒ |jjƒ  d |_|_dS )z2 Clears the current editor pane (if any).
        N)r*   r.   Ú	setWidgetr\   r/   )r9   r,   r<   r<   r=   r�   m  s
    


zSimpleEditor._clear_editorc             C   s   | j S )z Gets the node specific data. )Ú_py_data)rd   r<   r<   r=   r^   z  s    zSimpleEditor._get_node_datac             C   s
   || _ dS )z Sets the node specific data. N)rç   )rd   rà   r<   r<   r=   rt     s    zSimpleEditor._set_node_datac             C   s   | j |ƒd S )z; Gets the object associated with a specified node.
        r?   )r^   )r9   rd   r<   r<   r=   Ú
get_objectŠ  s    zSimpleEditor.get_objectc             C   s8   | j ||ƒ}|dk	r4|jƒ }|| jjƒ k	r4| j|ƒS dS )zi Returns the object that is the immmediate parent of a specified
            object in the tree.
        N)rl   r:   r3   rZ   rè   )r9   rh   rÁ   rd   rž   r<   r<   r=   Ú
get_parent”  s    
zSimpleEditor.get_parentc             C   s&   | j ||ƒ}|dk	r"| j|ƒd S dS )z> Returns the node associated with a specified object.
        Nr   )rl   r^   )r9   rh   rÁ   rd   r<   r<   r=   Úget_node£  s    zSimpleEditor.get_nodec             C   sh   | j |ƒ\}}}|j|ƒrP|jƒ }|dk	rPx$| j|ƒD ]}||k	r6|jdƒ q6W | j|ƒ | j|ƒ dS )z- Handles a tree node being expanded.
        NF)r^   Zcan_auto_closer:   rb   ra   r`   rÓ   )r9   rd   rf   rg   rh   r:   Zsnidr<   r<   r=   Ú_on_item_expanded±  s    

zSimpleEditor._on_item_expandedc             C   s   | j |ƒ dS )z. Handles a tree node being collapsed.
        N)rÓ   )r9   rd   r<   r<   r=   Ú_on_item_collapsedÊ  s    zSimpleEditor._on_item_collapsedc             C   sF   | j |ƒ\}}}|j|ƒdkr<| jjdk	r<| jj| jj|ƒ || _dS )z, Handles a tree item being clicked.
        TN)r^   r%   r)   Zon_clickr0   Úevaluate)r9   rd   r‘   r§   rg   rh   r<   r<   r=   Ú_on_item_clickedÓ  s    zSimpleEditor._on_item_clickedc             C   sT   | j |ƒ\}}}|j|ƒdkrD| jjdk	rJ| jj| jj|ƒ d| _nd| _|| _dS )z3 Handles a tree item being double-clicked.
        TN)r^   r&   r)   Z	on_dclickr0   rí   rS   )r9   rd   r‘   r§   rg   rh   r<   r<   r=   Ú_on_item_dclickedâ  s    zSimpleEditor._on_item_dclickedc             C   sT   | j |ƒ\}}}|j|ƒdkrD| jjdk	rJ| jj| jj|ƒ d| _nd| _|| _dS )z. Handles a tree item being activated.
        TN)r^   r$   r)   Zon_activatedr0   rí   rS   )r9   rd   r‘   r§   rg   rh   r<   r<   r=   Ú_on_item_activatedõ  s    zSimpleEditor._on_item_activatedc             C   s¤  | j jƒ }g }t|ƒdkrfxV|D ]@}| j|ƒ\}}}|j|ƒ t|ƒt|d ƒkr |}|j|ƒ}q W nd}d}d}| jjdkr’d| _	|| _
d| _	nd| _	|| _
d| _	|dkr¾| jj| jj|ƒ | j}	|	dk	�r |	jdƒ | jƒ  |dk	�r–|j|ƒ}
|
dk�s
t|
tƒ�r|j|
ƒ}
| jjdk	�s.|
jdk�r@|j|	|
dd�}n|j|	|
dd�}| j|_||	_||	_|jjƒ dk	�rŠ|jjƒ jddddƒ |	j|jƒ |	jdƒ dS )	z- Handles a tree node being selected.
        r   NTZsingleFZsubpanel)r:   ÚviewÚkindZpanel)r3   ZselectedItemsr›   r^   r1   rr   rH   r)   Úselection_moderQ   r#   r0   rí   Z	on_selectr*   ZsetUpdatesEnabledr�   Zget_viewrA   ÚstrZ
trait_viewrÕ   rò   Úedit_traitsr:   r.   r/   r-   ZlayoutZsetContentsMarginsræ   )r9   Únidsr#   rd   rf   rg   Z
sel_objectrh   Znot_handledr,   rñ   r0   r<   r<   r=   Ú_on_tree_sel_changed  sV    








z!SimpleEditor._on_tree_sel_changedc             C   s`  | j j|ƒ}|dkrdS | j|ƒ\}}}|||f| _|| || jj| jjdœ| _|jƒ }|dksl|| j j	ƒ krvd }}n| j|ƒ\}}}|| _
|| _|| _|j|ƒ}	|	dkr¸| j||ƒ}	n\t|	tƒ�r|	jtƒ}
|
dk	�rd|
_| j||ƒ}t|ƒdk�r|
jdt|ddiŽƒ nd}	|	dk	�r>|	j| j | ƒ}|j| j j|ƒƒ d | _ | _ | _
 | _| _dS )zR Handles the user requesting a context menuright clicking on a tree node.
        N)rh   r,   rg   rŸ   Úhandlerr~   r   rÁ   ÚNew)r3   ÚitemAtr^   Ú_datar0   rŸ   rø   Ú_contextr:   rZ   Ú
_menu_nodeÚ_menu_parent_nodeÚ_menu_parent_objectZget_menuÚ_standard_menurA   r   Z
find_groupr   rr   Ú_new_actionsr›   ÚinsertZcreate_menuÚexec_ZmapToGlobal)r9   Úposrd   r§   rg   rh   rž   Zparent_nodeZparent_objectÚmenuÚgroupÚactionsZqmenur<   r<   r=   Ú_on_context_menu\  s@    




zSimpleEditor._on_context_menuc             C   sR   t tttƒ ttƒ tg}| j||ƒ}t|ƒdkrJt|ddiŽtƒ g|dd…< t|Ž S )z6 Returns the standard contextual pop-up menu.
        r   rÁ   rù   )	r   r   r   r   r   r   r  r›   r   )r9   rg   rh   r  Úitemsr<   r<   r=   r   ™  s    
zSimpleEditor._standard_menuc       
      C   s”   | j d }g }|j|ƒ}t|ƒdkr�xj|D ]b}d}t|tƒrD|\}}| j|ƒ}|dk	r*|j}|j|ƒ}	|	dkrr|}	|jt	|	d||f d�ƒ q*W |S )zA Returns a list of Actions that will create new objects.
        r   r   FNr~   zeditor._menu_new_node('%s',%s))rÁ   Úaction)
rû   Zget_addr›   rA   r°   rÏ   rÐ   Úget_namer1   r   )
r9   rg   rh   r	  ÚaddrÎ   ÚpromptÚadd_noderÑ   rÁ   r<   r<   r=   r  ª  s&    





zSimpleEditor._new_actionsc             C   s.   | j }t|tƒr|j| jƒS |d k	o,|j|ƒS )N)rþ   rA   r   Úcan_copyrÿ   )r9   rh   r:   r<   r<   r=   Ú_is_copyableÅ  s    
zSimpleEditor._is_copyablec             C   sV   | j }t|tƒr*|j| jƒo&|j| jƒ}n|d k	oD|j|ƒoD|j|ƒ}|oT| jj|ƒS )N)rþ   rA   r   r  rÿ   Ú
can_deleterý   Úcan_delete_me)r9   rh   r:   Zcan_cutr<   r<   r=   Ú_is_cutableË  s    


zSimpleEditor._is_cutablec             C   s   | j j|tjƒS )N)rý   Úcan_addr   Zinstance_type)r9   rh   r<   r<   r=   Ú_is_pasteableÖ  s    zSimpleEditor._is_pasteablec             C   s@   | j }t|tƒr|j| jƒ}n|d k	o.|j|ƒ}|o>| jj|ƒS )N)rþ   rA   r   r  rÿ   rý   r  )r9   rh   r:   r  r<   r<   r=   Ú_is_deletableÙ  s
    
zSimpleEditor._is_deletablec             C   s€   | j }t|tƒr|j| jƒ}n|d k	o.|j|ƒ}|o>| jj|ƒ}| j|ƒ}|jƒ }|rd|t	j
jO }n|t	j
j M }|j|ƒ |S )N)rþ   rA   r   Ú
can_renamerÿ   rý   Zcan_rename_merl   Úflagsr   r4   ZItemIsEditableZsetFlags)r9   rh   r:   r  rd   r  r<   r<   r=   Ú_is_renameableá  s    


zSimpleEditor._is_renameablec             C   s    |r|j |ƒ rdS |j||ƒS )zB Returns whether a given object is droppable on the node.
        F)Z
can_insertr  )r9   rg   rh   Ú
add_objectZ
for_insertr<   r<   r=   Ú_is_droppableõ  s    zSimpleEditor._is_droppablec             C   s(   |j ||ƒ}||k	s| r|S tj|ƒS )z< Returns a droppable version of a specified object.
        )Zdrop_objectrÞ   rß   )r9   rg   rh   Zdropped_objectrá   Ú
new_objectr<   r<   r=   Ú_drop_objectý  s    zSimpleEditor._drop_objectc             C   s,   |j j}| j|j|dƒ | j|j|dƒ dS )z= Adds a menu item to the menu bar being constructed.
        ZenabledÚcheckedN)rN   r
  Ú	eval_whenZenabled_whenZchecked_when)r9   Z	menu_itemr
  r<   r<   r=   Úadd_to_menu  s    zSimpleEditor.add_to_menuc             C   s   | j |ƒ dS )z? Adds a toolbar item to the toolbar being constructed.
        N)r   )r9   Ztoolbar_itemr<   r<   r=   Úadd_to_toolbar  s    zSimpleEditor.add_to_toolbarc             C   sD   |j dkr t|j tƒ | jƒs dS |jdkr@t|jtƒ | jƒs@dS dS )zM Returns whether the action should be defined in the user interface.
        r~   FT)Zdefined_whenÚevalÚglobalsrü   Zvisible_when)r9   r
  r<   r<   r=   Úcan_add_to_menu   s    

zSimpleEditor.can_add_to_menuc             C   s
   | j |ƒS )za Returns whether the toolbar action should be defined in the user
            interface.
        )r$  )r9   r
  r<   r<   r=   Úcan_add_to_toolbar2  s    zSimpleEditor.can_add_to_toolbarc             C   s   | j j| j|ƒ dS )zE Performs the action described by a specified Action object.
        N)r0   Zdo_undoableÚ_perform)r9   r
  Zaction_eventr<   r<   r=   Úperform<  s    zSimpleEditor.performc       
      C   sÂ   | j \}}}|j}| jj}| jj}|jdƒdkrˆ|jdƒdk rF|d7 }yt|tƒ || |||dœƒ W n   ddlm	} |ƒ  Y nX d S t
||d ƒ}	|	d k	rª|	||ƒ d S |jd k	r¾|j|ƒ d S )NÚ.r   ú(z())rh   r,   rg   rŸ   rø   )r@   )rû   r
  r0   rŸ   rø   Úfindr"  r#  rJ   r@   r‹   Z
on_perform)
r9   r
  rg   rh   rd   Úmethod_namerŸ   rø   r@   Úmethodr<   r<   r=   r&  A  s0    

zSimpleEditor._performc             C   sj   |dkrfd}yt |tƒ | jƒs"d}W n4 tk
rX } ztjd||f ƒ W Y dd}~X nX t|||ƒ dS )z¢ Evaluates a condition within a defined context, and sets a
        specified object trait based on the result, which is assumed to be a
        Boolean.
        r~   TFzGException (%s) raised when evaluating the condition %s. Returning True.N)r"  r#  rü   Ú	ExceptionÚloggerÚwarningÚsetattr)r9   Ú	conditionrh   Ztraitro   Úer<   r<   r=   r  e  s    zSimpleEditor.eval_whenc             C   s   | j d t_d| _ dS )zB Copies the current tree node object to the paste buffer.
        r   N)rû   r   Úinstance)r9   r<   r<   r=   Ú_menu_copy_node|  s    zSimpleEditor._menu_copy_nodec             C   s,   | j \}}}|t_d| _ | j| j|ƒŽ  dS )zB Cuts the current tree node object into the paste buffer.
        N)rû   r   r3  rä   r¨   )r9   rg   rh   rd   r<   r<   r=   Ú_menu_cut_node†  s    zSimpleEditor._menu_cut_nodec             C   s(   | j \}}}d| _ | j||tjdƒ dS )z\ Pastes the current contents of the paste buffer into the current
            node.
        NT)rû   râ   r   r3  )r9   rg   rh   rd   r<   r<   r=   Ú_menu_paste_node’  s    zSimpleEditor._menu_paste_nodec             C   s†   | j \}}}d| _ |j|ƒ}|dk	r‚|dk	rr| jjdkrrtjj| jdd|j|ƒ tjj	tjj
B ƒ}|tjj	krrdS | j| j|ƒŽ  dS )z1 Deletes the current node from the tree.
        NFTzConfirm Deletionz#Are you sure you want to delete %s?)rû   Zconfirm_deleter0   rÕ   r   ZQMessageBoxZquestionr3   r�   ZYesZNorä   r¨   )r9   rg   rh   rd   ÚrcZbutnr<   r<   r=   Ú_menu_delete_nodež  s    
zSimpleEditor._menu_delete_nodec             C   s"   | j \}}}d| _ | jj|ƒ dS )z" Rename the current node.
        N)rû   r3   ZeditItem)r9   r§   rd   r<   r<   r=   Ú_menu_rename_node´  s    zSimpleEditor._menu_rename_nodec          	   C   sh   y| j |ƒ\}}}W n
   dS tj|j|ƒƒ}|j|ƒ}||krd|dkrX|j||ƒ n| j||ƒ dS )z* Handle changes to a widget item.
        Nr~   )r^   rB   Ú	text_typeÚtextr�   Z	set_labelr’   )r9   rd   r‘   r§   rg   rh   Z	new_labelZ	old_labelr<   r<   r=   Ú_on_nid_changed»  s    
zSimpleEditor._on_nid_changedc       	      C   st   | j \}}}d| _ | j|ƒ\}}|ƒ }| s>|j| jdd�jrp| j|||dƒ | jjrp| jj	|j
|jƒ d ƒƒ dS )z0 Adds a new object to the current node.
        NZ	livemodal)r:   rò   Fr   )rû   rÒ   rõ   r-   rÄ   râ   r)   r+   r3   rI   r¢   r¦   )	r9   rÑ   r  rg   rh   rd   Únew_nodeÚ	new_classr  r<   r<   r=   Ú_menu_new_nodeÒ  s    
zSimpleEditor._menu_new_nodec             C   sê   | j }xÞ| j||ƒD ]Î\}}}|j|ƒ}|r~x| j|ƒD ]}	| j|	ƒ q8W x‚|D ](}
| j|
ƒ\}
}|dk	rP| j|||
ƒ qPW nPt|ddƒ}|dkr¬t|ƒdkr¬t	j
|ƒ|_n"|dk	rÎt|ƒdkrÎ|j|ƒ |`|j|ƒr|jdƒ qW dS )zC Handles the children of a node being completely replaced.
        Nr—   r   T)r3   rÅ   r    rb   rY   rj   r”   r‹   r›   r   r‰   r—   r™   r–   ra   )r9   rh   rÁ   ÚnewrL   rf   rg   rd   rË   ri   r¢   r£   r¡   r<   r<   r=   rº   è  s$    



zSimpleEditor._children_replacedc             C   sp  |dd… }| j | j|||ƒ |j}t|jƒ}|t|jƒ }| j}�x&| j||ƒD �]\}}	}
|	j|ƒ}|rüx$| j	|
ƒ||… D ]}| j
|ƒ q€W t|ƒt|jƒ }d}xª|jD ]F}| j|ƒ\}}|dk	r°||krÚ|| nd}| j|
|||ƒ |d7 }q°W nXt|
ddƒ}|dk�r.t|ƒdk�r.tj|
ƒ|
_n&|dk	�rTt|ƒdk�rT|
j|ƒ |
`|	j|ƒrR|
jdƒ qRW dS )z7 Handles the children of a node being changed.
        Né   r   r   r—   Tiúÿÿÿ)Z
log_changerÝ   r�   r›   rÚ   rÛ   r3   rÅ   r    rb   rY   rj   r“   r‹   r   r‰   r—   r™   r–   ra   )r9   rh   rÁ   rÜ   ÚstartÚnÚendrL   rf   rg   rd   rË   ri   Ú	remainingZchild_indexr¢   r£   Zinsert_indexr¡   r<   r<   r=   r¼     s8    

	


zSimpleEditor._children_updatedc       	      C   sz   | j jdƒ}zZg }xP| jt|ƒ D ]>\}}||kr"|j|ƒ | j|ƒd }| j|dƒ | j|ƒ q"W W d| j j|ƒ X dS )z8  Handles the label of an object being changed.
        Tr   r   N)r3   rX   r7   rr   r1   r^   r’   rÓ   )	r9   rh   rÁ   r…   Úblkrö   rÂ   rd   rg   r<   r<   r=   r¾   G  s    
zSimpleEditor._label_updatedc       	      C   sr   | j jdƒ}zRg }xH| jt|ƒ D ]6\}}||kr"|j|ƒ | j|ƒd }| j|||ƒ q"W W d| j j|ƒ X dS )z@  Handles the column labels of an object being changed.
        Tr   N)r3   rX   r7   rr   r1   r^   r‡   )	r9   rh   rÁ   r@  rF  rö   rÂ   rd   rg   r<   r<   r=   rÀ   [  s    
z#SimpleEditor._column_labels_updatedc             C   sp   t | jtjƒr4t |tƒr$|jdƒ}n|}| jj|ƒ | jjƒ }| jj	| j
jƒ |dk	rld|krl|j|d ƒ dS )z` Restores any saved user preference information associated with the
            editor.
        Ú	structureNÚcolumn_state)rA   r-   r   r5   Údictrå   ZrestoreStater3   r„   ZsetExpandsOnDoubleClickr)   Zexpands_on_dclick)r9   ÚprefsrG  r„   r<   r<   r=   Úrestore_prefsq  s    

zSimpleEditor.restore_prefsc             C   sJ   i }t | jtjƒr$| jjƒ jƒ |d< | jjƒ }|dk	rF|jƒ jƒ |d< |S )zM Returns any user preference information associated with the editor.
        rG  NrH  )rA   r-   r   r5   Z	saveStaterà   r3   r„   )r9   rJ  r„   r<   r<   r=   Ú
save_prefs†  s    
zSimpleEditor.save_prefs)T)N)r   )F)r~   )r~   )T)T)r~   )r~   )r~   )T)N)F)r~   N)erÐ   Ú
__module__Ú__qualname__Ú__doc__Z
scrollabler
   rK   r	   r#   r$   r%   r&   r'   r"   r>   rP   rR   rT   rW   r\   rc   ry   rz   r}   r‡   rq   r’   r”   r“   rY   r`   rb   r¨   r_   r   ÚQStyleZSP_FileIconZSP_DirClosedIconZSP_DirOpenIconr®   r�   r²   rs   rœ   rF   rÅ   rj   rÏ   rÒ   rÓ   r×   rÙ   rÝ   râ   rã   rä   rl   r�   Ústaticmethodr^   rt   rè   ré   rê   rë   rì   rî   rï   rð   r÷   r  r   r  r  r  r  r  r  r  r  r   r!  r$  r%  r'  r&  r  r4  r5  r6  r8  r9  r<  r?  rº   r¼   r¾   rÀ   rK  rL  Ú__classcell__r<   r<   )r]   r=   r!   ;   s¶   l
	
1

	/	

0


:





	T=
	

$


%:r!   c                   sR   e Zd ZdZddd„Z‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )r2   z¥ The _TreeWidget class is a specialised QTreeWidget that reimplements
        the drag'n'drop support so that it hooks into the provided Traits
        support.
    Nc             C   sP  t jj| |ƒ | jtjjƒ | jdƒ | jdƒ | j	tj
|jjŽ ƒ t|jjƒ}|dkr|| jdƒ | j|ƒ | j|jjƒ n
| jdƒ | j|jjƒ |jj}|dkr¶| jd||f ƒ |jjdkrÐ| jt jjƒ | jj|jƒ | jj|jƒ | jj|j ƒ | j!j|j"ƒ | j#j|j$ƒ | j%j|j&ƒ | j'j|j(ƒ | j)j|j*ƒ || _+d| _,dS )z% Initialise the tree widget.
        Tr   Fz‚
            QTreeView::item {
                padding-top: %spx;
                padding-bottom: %spx;
            }
            ÚextendedN)-r   ÚQTreeWidgetÚ__init__ZsetContextMenuPolicyr   r4   ZCustomContextMenuZsetDragEnabledZsetAcceptDropsZsetIconSizeÚQSizer)   Z	icon_sizer›   r€   ZsetHeaderHiddenZsetColumnCountZsetHeaderLabelsZsetAlternatingRowColorsZalternating_row_colorsZvertical_paddingZsetStyleSheetró   ZsetSelectionModeZQAbstractItemViewZExtendedSelectionZitemExpandedÚconnectrë   ZitemCollapsedrì   ZitemClickedrî   ZitemDoubleClickedrï   ZitemActivatedrð   ZitemSelectionChangedr÷   ZcustomContextMenuRequestedr  ZitemChangedr<  r*   Ú	_dragging)r9   r,   r:   Zcolumn_countÚpaddingr<   r<   r=   rU  Ÿ  s8    




z_TreeWidget.__init__c                s\   | j jjrFx:t| jƒ ƒD ]*}| j| j|ƒƒ}| j|ƒ}|jj	|ƒ qW t
| j| ƒj|ƒ dS )zA Overridden to emit sizeHintChanged() of items for word wrapping N)r*   r)   rŒ   rv   ZtopLevelItemCountrG   ZtopLevelItemÚitemDelegateÚsizeHintChangedÚemitr[   r]   ÚresizeEvent)r9   rÜ   rx   Úmirr   )r]   r<   r=   r]  Ë  s    

z_TreeWidget.resizeEventc             C   s4  | j ƒ }|dkrdS || _| j|ƒ}| jƒ jtjjƒ ƒ|jƒ  }| j	j
|ƒ\}}}|j|ƒ}tj|ƒ}	|j| jƒ jƒ ƒ}
tj|
jƒ ƒ}|j| jƒ jƒ jƒ ƒ tj|ƒ}| jƒ }| jtjjO  _tj|jƒ |
jƒ  |jƒ ƒ|_| jƒ j||| j|ƒƒ |j ƒ  tj!| ƒ}|j"|	ƒ |j#|ƒ |j$|ƒ |j%|ƒ dS )z@ Reimplemented to start the drag of a tree widget item.
        N)&ZcurrentItemrX  ZvisualItemRectrU   ZmapFromGlobalr   ZQCursorr  ZtopLeftr*   r^   Zget_drag_objectr   ÚcoerceZintersectedÚrectr·   Úsizer¸   ZpaletterÈ   r|   ZQPainterZviewOptionsÚstaterP  ZState_Selectedr   ZQRectrZ  ÚpaintrG   rD  ZQDragZsetMimeDataZ	setPixmapZ
setHotSpotr  )r9   r  rd   Znid_rectZhsposr§   rg   rh   Zdrag_objectZmdr`  ZpmÚpainterÚoptionZdragr<   r<   r=   Ú	startDragÔ  s4    







z_TreeWidget.startDragc             C   s2   |j ƒ  tj|jƒ ƒjƒ }|dkr&dS |jƒ  dS )zZ Reimplemented to see if the current drag can be handled by the
            tree.
        N)rÃ   r   r_  ÚmimeDatar3  ÚacceptProposedAction)r9   r2  rà   r<   r<   r=   ÚdragEnterEventÿ  s
    z_TreeWidget.dragEnterEventc             C   s0   |j ƒ  | j|ƒ\}}}}}|dk	r,|jƒ  dS )z‡ Reimplemented to see if the current drag can be handled by the
            particular tree widget item underneath the cursor.
        N)rÃ   Ú_get_actionrh  )r9   r2  r
  Úto_nodeÚ	to_objectÚto_indexrà   r<   r<   r=   ÚdragMoveEvent  s    z_TreeWidget.dragMoveEventc             C   s�  |j ƒ  | j}| j}d| _| j|ƒ\}}}}}|dkr¼|dk	r’| jj|||dƒ}|dk	rºz,|jƒ  |j|j|ƒŽ  |j|||dƒ W d|j	ƒ  X n(|j|||dƒ}|dk	rº|j|||dƒ nÈ|dk�r€|dk	�rP|j|||dƒ}|dk	�r~|j|ƒ\}	}
}||
k�r||k�r|d8 }z,|jƒ  |j|	|
|ƒ |j
||||dƒ W d|j	ƒ  X n.| jj|||dƒ}|dk	�r„|j
||||dƒ ndS |jƒ  dS )z5 Reimplemented to update the model and tree.
        Nr1   FTr  r   )rÃ   r*   rX  rj  r  r×   rä   r¨   râ   rÙ   rã   rh  )r9   r2  r,   Zdraggingr
  rk  rl  rm  rà   Z	from_nodeZfrom_objectZ
from_indexr<   r<   r=   Ú	dropEvent  sT    








z_TreeWidget.dropEventc             C   s@  d}d}d}d}d}| j }| j|jƒ ƒ}|dkrP|jjrB| jƒ }n|||||fS | jdk	rŒ|}	x,|	dk	rŠ|	| jkr€|||||fS |	jƒ }	q`W tj	|j
ƒ ƒjƒ }|j|ƒ\}
}}|jƒ tjjkrà|j|||dƒràd}|}|}d}nR|j|ƒ\}}}|dkrþd}n4|j|||dƒ�rd}n|j|||dƒ�r.d}nd}|||||fS )zI Work out what action on what object to perform for a drop event
        NFr1   Tr  )r*   rú   r  r)   rp   rZ   rX  r:   r   r_  rg  r3  r^   ZproposedActionr   r4   Z
MoveActionr  r¨   )r9   rÜ   r
  rk  rl  rm  rà   r,   rd   rž   r§   rg   rh   r<   r<   r=   rj  U  sD    



z_TreeWidget._get_action)N)rÐ   rM  rN  rO  rU  r]  rf  ri  rn  ro  rj  rR  r<   r<   )r]   r=   r2   ™  s   
,	+:r2   c                   s4   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )r6   z. A delegate class to draw wrapped text labels c                s–   |j ƒ }| jjj|ƒ}| jj|ƒ\}}}|j ƒ }|jt|d�}|dkrXtt| ƒj	||ƒS ||f}	|j
| j||||	ƒ}
|
dkrˆtjddƒS tj|
Ž S dS )z! returns area taken by the text. )ÚcolumnNr   é   )rp  r,   r3   ÚitemFromIndexr^   r�   rh   r[   r6   ÚsizeHintra  r   rV  )r9   re  r�   rp  rN   rf   rg   r3  r†   Zsize_contextra  )r]   r<   r=   rs  š  s    zTreeItemDelegate.sizeHintc             C   s   |j |jƒ dS )z' Update the editor's geometry.
        N)ZsetGeometryr`  )r9   r,   re  r�   r<   r<   r=   ÚupdateEditorGeometry®  s    z%TreeItemDelegate.updateEditorGeometryc                s¼   | j jj|ƒ}| j j|ƒ\}}}|jƒ }|jt|d�}	|	dkrL| j jjrLt	}	|	dkrjt
t| ƒj|||ƒ nN|	js„t
t| ƒj|||ƒ |||f}
|	j| j ||||
ƒ}|dk	r¸t| jj|ƒ dS )z" Render the contents of the item. )rp  N)r,   r3   rr  r^   rp  r�   rh   r)   rŒ   ÚDEFAULT_WRAP_RENDERERr[   r6   rc  Zhandles_allr   r[  r\  )r9   rd  re  r�   rN   rf   rg   r3  rp  r†   Zpaint_contextra  )r]   r<   r=   rc  ³  s    
zTreeItemDelegate.paint)rÐ   rM  rN  rO  rs  rt  rc  rR  r<   r<   )r]   r=   r6   �  s   
r6   );rO  Ú
__future__r   rÞ   rD   ÚloggingZ	six.movesr   Z	pyface.qtr   r   Z
pyface.apir   Zpyface.ui_traitsr   Zpyface.timer.apir   Z
traits.apir	   r
   r   Ztraitsui.editors.tree_editorr   r   r   r   r   r   Ztraitsui.tree_noder   r   r   r   Ztraitsui.menur   r   r   Ztraitsui.ui_traitsr   Ztraitsui.undor   r   r   r,   r   Úhelperr   Ztree_node_renderersr    rB   Ú	getLoggerrÐ   r.  ru  r!   rT  r2   ZQStyledItemDelegater6   r<   r<   r<   r=   Ú<module>   sL    
            j w