a
    §ˆc¶ë  ã                   @   sb  d Z ddlZddlmZ ddlZddlZzddlmZmZ W n   d ZZY n0 ddl	m
Z
 ddl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mZmZ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& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 dZ6G dd„ de2ƒZ7G dd„ deƒZ8dS )zB Defines the tree editor for the wxPython user interface toolkit.
é    N)Úpartial)ÚPythonDropSourceÚPythonDropTarget)Ú	ImageList)ÚHasStrictTraitsÚAnyÚStrÚEventÚ
TraitError)ÚViewÚTreeNodeÚObjectTreeNodeÚMultiTreeNode)Ú
CopyActionÚ	CutActionÚDeleteActionÚ	NewActionÚPasteActionÚRenameAction)ÚListUndoItem)ÚITreeNodeAdapterBridge)ÚMenuÚActionÚ	Separator)ÚImageResource)Úconvert_image)Ú
DockWindowÚ	DockSizerÚDockSectionÚ
DockRegionÚDockControlé   )ÚOKColor)ÚEditor)ÚTraitsUIPanelÚTraitsUIScrolledPanelc                       s$  e Zd ZdZdZeƒ Zeƒ Zeƒ Z	eƒ Z
eƒ Zeƒ Zdd„ Z‡ 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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d'd(„ Zd)d*„ Z d+d,„ Z!d-d.„ Z"d¹d/d0„Z#dºd2d3„Z$d4d5„ Z%d6d7„ Z&d»d9d:„Z'd¼d;d<„Z(d=d>„ Z)d?d@„ Z*dAdB„ Z+dCdD„ Z,dEdF„ Z-dGdH„ Z.dIdJ„ Z/dKdL„ Z0dMdN„ Z1d½dOdP„Z2d¾dQdR„Z3dSdT„ Z4d¿dUdV„Z5dWdX„ Z6dYdZ„ Z7d[d\„ Z8d]d^„ Z9dÀd_d`„Z:dÁdadb„Z;dcdd„ Z<dedf„ Z=dgdh„ Z>didj„ Z?dÂdldm„Z@dndo„ ZAdpdq„ ZBdrds„ ZCdtdu„ ZDdvdw„ ZEdxdy„ ZFdzd{„ ZGd|d}„ ZHd~d„ ZId€d�„ ZJd‚dƒ„ ZKd„d…„ ZLd†d‡„ ZMdˆd‰„ ZNdŠd‹„ ZOdŒd�„ ZPdŽd�„ ZQd�d‘„ ZRd’d“„ ZSd”d•„ ZTd–d—„ ZUd˜d™„ ZVdšd›„ ZWdÃdœd�„ZXdždŸ„ ZYd d¡„ ZZd¢d£„ Z[d¤d¥„ Z\d¦d§„ Z]d¨d©„ Z^dªd«„ Z_dÄd¬d­„Z`dÅd®d¯„Zad°d±„ Zbd²d³„ Zcd´dµ„ Zdd¶d·„ Ze‡  ZfS )ÆÚSimpleEditorzSimple style of tree editor.Tc                 C   s�  | j }|  ¡ }|j�rþ|jrê|j}|du r„| |_t|dƒ| _d | j_| j_	|j
}|dur€|j
D ]}|j| ju r`| j|_q`d|_
dS |j}|du sž|j| jurÂ|j
}|du r¶g  |_
}| | ¡ n|j| _tj|d|d� | _ | _}�qd| _|jpþ| jjj}	t||	d�j | _}
tj|
d|d� | _}t|
ƒ | _}| t tj¡¡ | dd¡ | t dd¡¡ d | j_| j_	| j}d }}d	}|j}|dk�r°|d
 }|d }|j}|
 ttt t!|d||d�gd�t t!|d| j|d�gd�g|j"dkd�d�¡ ntj|d|d� | _ | _}|j#�r:t$|j%Ž | _&| '| j&¡ i | _(g | _)| *tj+| j,¡ | *tj-| j.¡ | *tj/| j0¡ | *tj1| j2¡ | *tj3| j4¡ | *tj5| j6¡ | *tj7| j8¡ | *tj9| j:¡ | *tj;| j<¡ | *tj=| j>¡ | *tj?| j@¡ | *tjA| jB¡ | *tjC| jD¡ | *tjE| jF¡ |  G|jHd¡ |  G|jIdd¡ |  G|jJdd¡ |  G|jKdd¡ |  G|jLdd¡ tMdu�rŒ| NtM| ƒ¡ dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Néÿÿÿÿ©ÚstyleT)Úthemeé   éd   Ú Úfixedz
 Hierarchyz EditorÚtree)ÚnameÚidÚcontrolr)   )ÚcontentsÚeditorZ
horizontal)r3   Zis_rowÚselectedÚ	activatedÚtoÚclickÚdclickÚvetoÚfrom)OÚfactoryÚ
_get_styleÚeditableZshared_editorr4   Ú_editorr$   r2   Ú_node_uiÚ_editor_nidZ_shared_editorsÚuiÚappendÚwxZTreeCtrlÚ_treeÚ_is_dock_windowZ
dock_themeÚitemÚ	containerr   r%   ÚSetSizerÚBoxSizerÚVERTICALZSetScrollRateZ
SetMinSizeÚSizeÚlabelZdockr   r   r   r    ZorientationZ
show_iconsr   Z	icon_sizeÚ_image_listZAssignImageListÚ_mapÚ	_undoableZBindÚEVT_LEFT_DOWNÚ_on_left_downÚEVT_RIGHT_DOWNÚ_on_right_downÚEVT_LEFT_DCLICKÚ_on_left_dclickÚEVT_TREE_ITEM_EXPANDINGÚ_on_tree_item_expandingÚEVT_TREE_ITEM_EXPANDEDÚ_on_tree_item_expandedÚEVT_TREE_ITEM_COLLAPSINGÚ_on_tree_item_collapsingÚEVT_TREE_ITEM_COLLAPSEDZ_on_tree_item_collapseÚEVT_TREE_ITEM_ACTIVATEDÚ_on_tree_item_activatedÚEVT_TREE_SEL_CHANGEDÚ_on_tree_sel_changedÚEVT_TREE_BEGIN_DRAGÚ_on_tree_begin_dragÚEVT_TREE_BEGIN_LABEL_EDITÚ_on_tree_begin_label_editÚEVT_TREE_END_LABEL_EDITÚ_on_tree_end_label_editÚEVT_TREE_ITEM_GETTOOLTIPÚ_on_tree_item_gettooltipÚ
EVT_MOTIONÚ	_on_hoverZ
sync_valuer5   r6   r8   r9   r:   r   ZSetDropTarget)ÚselfÚparentr<   r)   Zfactory_editorZeditorsr4   Zshared_editorsr/   r*   ZsplitterrG   Zhierarchy_nameZeditor_namer0   © rn   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/tree_editor.pyÚinitb   sÈ    


ÿÿ
üÿÿ
üÿÿõéÿÿÿ
zSimpleEditor.initc                    sæ   | j }|durÒ| tj¡ | tj¡ | tj¡ | tj¡ | tj¡ | tj¡ | tj	¡ | tj
¡ | tj¡ | tj¡ | tj¡ | tj¡ | tj¡ | tj¡ | j  ¡ }| ¡ rÒ|  |¡ d| _ tƒ  ¡  dS )z&Disposes of the contents of an editor.N)rE   ZUnbindrD   rQ   rS   rU   rW   rY   r[   r]   r^   r`   rb   rd   rf   rh   rj   ÚGetRootItemÚIsOkÚ_delete_nodeÚsuperÚdispose)rl   r/   Únid©Ú	__class__rn   ro   ru     s*    

zSimpleEditor.disposec                 C   s2   z| j  |  |¡d ¡ W n ty,   Y n0 dS )z Handles the **selection** event.é   N)rE   Ú
SelectItemÚ_object_infoÚ	Exception)rl   Ú	selectionrn   rn   ro   Ú_selection_changed  s    zSimpleEditor._selection_changedc                 C   s   | j s|  |¡ dS )z-Handles the **selected** trait being changed.N)Ú_no_update_selectedr~   )rl   r5   rn   rn   ro   Ú_selected_changed$  s    zSimpleEditor._selected_changedc                 C   s
   d| _ dS )z%Handles the 'veto' event being fired.TN)Ú_veto©rl   rn   rn   ro   Ú_veto_changed)  s    zSimpleEditor._veto_changedc                 C   st   | j }tjtjB tjB }|jdks6|jdkr@tjdkr@|tjO }|j	rV|tj
tjB O }|jdkrp|tjtjB O }|S )z;Returns the style settings used for displaying the wx tree.ÚoffZ
appearanceÚposixÚsingle)r<   rD   ZTR_EDIT_LABELSZTR_HAS_BUTTONSZCLIP_CHILDRENZ
lines_modeÚosr0   ZTR_NO_LINESÚ	hide_rootZTR_HIDE_ROOTZTR_LINES_AT_ROOTÚselection_modeZTR_MULTIPLEZTR_EXTENDED)rl   r<   r)   rn   rn   ro   r=   -  s    
ÿÿ

zSimpleEditor._get_stylec                 C   s<   z$|   ¡ | _| j t¡ | j ¡  W n ty6   Y n0 dS )z9Handles the user entering input data in the edit control.N)Ú
_get_valueÚvaluer2   ZSetBackgroundColourr"   ÚRefreshr
   ©rl   Úeventrn   rn   ro   Úupdate_object@  s    
zSimpleEditor.update_objectc           	      C   sŽ   | j }| ¡ }i }| ¡ rŠ|g}|rŠ| ¡ }|  |¡}z| |¡}W n   d}Y n0 |d |f|t|d ƒ< |  |¡D ]}| |¡ qxq |S )NTr   r'   )	rE   rq   rr   ÚpopÚ_get_node_dataZ
IsExpandedÚhashÚ_nodesrC   )	rl   r/   rv   ÚstateÚnodes_to_doÚnodeÚdataÚis_expandedÚcnidrn   rn   ro   Ú_save_stateI  s     

zSimpleEditor._save_statec                 C   s–   |sd S | j }| ¡ }| ¡ r’|g}|r’| ¡ }|  |¡D ]T}|  |¡}t|d ƒ}||v r:|| \}	}
|	r:|  |¡ |
r„| |¡ | 	|¡ q:q$d S ©Nr'   )
rE   rq   rr   r�   r“   r‘   r’   Ú_expand_nodeÚExpandrC   )rl   r”   r/   rv   r•   r–   r™   r—   ÚkeyZwas_expandedZcurrent_statern   rn   ro   Ú_restore_state[  s$    


zSimpleEditor._restore_statec                    sf   ˆ j ‰‡ ‡fdd„}ˆ ¡ }| ¡ rb|g}|rb| ¡ }||ƒ ˆ  |¡D ]}||ƒ | |¡ qHq*dS )z3Expands all nodes, starting from the selected node.c                    s@   ˆ   | ¡\}}}ˆ  ||¡r<ˆ | d¡ ˆ  | ¡ ˆ | ¡ d S ©NT)r‘   Ú_has_childrenÚSetItemHasChildrenrœ   r�   )rv   Úexpandedr–   Úobject©rl   r/   rn   ro   Ú
_do_expands  s
    
z+SimpleEditor.expand_all.<locals>._do_expandN)rE   ZGetSelectionrr   r�   r“   rC   )rl   r¦   rv   r•   r–   Únrn   r¥   ro   Ú
expand_allo  s    zSimpleEditor.expand_allc                 C   sp   |dkrl|   |¡\}}}|  ||¡rl| j |d¡ |  |¡ |rL| j |¡ |  |¡D ]}|  ||d ¡ qVdS )zCExpands from the specified node the specified number of sub-levels.r   Tr!   N)r‘   r¡   rE   r¢   rœ   r�   r“   Úexpand_levels)rl   rv   ÚlevelsÚexpandr£   r–   r¤   r™   rn   rn   ro   r©   „  s    
zSimpleEditor.expand_levelsc                 C   s  | j }i }|du�r| ¡ }| ¡ r.|  |¡ |  | j¡\}}|du�r|  ||¡}| | |¡||¡ | _	}| 
|¡|fg| jt|ƒ< |  ||¡ |  |d||f¡ | jjsº|  ||¡rô| |d¡ |  |¡ | jjsô| |¡ | |¡ |  ¡  |  || jjd¡ | ¡  dS )z[Updates the editor when the object trait changes externally to the
        editor.
        NFT)rE   rq   rr   rs   Ú	_node_forr‹   Ú	_get_iconZAddRootÚ	get_labelÚ	_root_nidÚget_children_idrO   r1   Ú_add_listenersÚ_set_node_datar<   rˆ   r¡   r¢   rœ   r�   rz   ra   r©   Ú	auto_openrŒ   )rl   r/   Úsaved_staterv   r¤   r–   Úiconrn   rn   ro   Úupdate_editor�  s0    


ÿ



zSimpleEditor.update_editorc                 C   s   | j S )z9Returns the editor's control for indicating error status.)rE   r‚   rn   rn   ro   Úget_error_control³  s    zSimpleEditor.get_error_controlc                 C   s   |   |d||¡S )z)Appends a new node to the specified node.N)Ú_insert_node)rl   rv   r–   r¤   rn   rn   ro   Ú_append_node·  s    zSimpleEditor._append_nodec           
      C   s¼   | j }|  ||¡}| |¡}|du r6| ||||¡}n| |||||¡}|  ||¡}	| ||	¡ |  |d||f¡ | j 	t
|ƒg ¡ | |¡|f¡ |  ||¡ |	r¸| |¡r¸| |¡ |S )zeInserts a new node before a specified index into the children of the
        specified node.
        NF)rE   r­   r®   Z
AppendItemZ
InsertItemr¡   r¢   r²   rO   Ú
setdefaultr1   rC   r°   r±   Úcan_auto_openr�   )
rl   rv   Úindexr–   r¤   r/   rµ   rM   r™   Úhas_childrenrn   rn   ro   r¸   »  s     
ÿ
zSimpleEditor._insert_nodec           
      C   s¾   |   |¡D ]}|  |¡ q
|  |¡\}}}t|ƒ}| j| }t|ƒD ]\}}	||	d krD||=  qdqDt|ƒdkr„|  ||¡ | j|= d| _| j	 
|¡ d| _| jdurº|| jjkrº|  ¡  dS )z6Deletes a specified tree node and all of its children.r!   r   TFN)Ú
_nodes_forrs   r‘   r1   rO   Ú	enumerateÚlenÚ_remove_listenersÚ_lockedrE   ÚDeleter?   rA   Ú_clear_editor)
rl   rv   r™   r£   r–   r¤   Z	id_objectZobject_infoÚiÚinforn   rn   ro   rs   Õ  s"    
zSimpleEditor._delete_nodec                 C   s^   |   |¡\}}}|sZ| |¡D ](}|  |¡\}}|dur|  |||¡ q|  |d||f¡ dS )z7Expands the contents of a specified node (if required).NT)r‘   Úget_childrenr¬   r¹   r²   )rl   rv   r£   r–   r¤   ÚchildÚ
child_nodern   rn   ro   rœ   ò  s    zSimpleEditor._expand_nodec                 c   s8   | j }| |¡\}}| ¡ r4|V  | ||¡\}}qdS )z4Returns each of the child nodes of a specified node.N)rE   ZGetFirstChildrr   ZGetNextChild)rl   rv   r/   r™   Úcookiern   rn   ro   r“      s
    zSimpleEditor._nodesc                 C   s   dd„ |   |¡D ƒS )z2Returns all child node ids of a specified node id.c                 S   s   g | ]}|‘qS rn   rn   )Ú.0r™   rn   rn   ro   Ú
<listcomp>
  ó    z+SimpleEditor._nodes_for.<locals>.<listcomp>)r“   ©rl   rv   rn   rn   ro   r¾     s    zSimpleEditor._nodes_forc                 C   sZ   | j  |¡}| ¡ sdS t|  |¡ƒD ].\}}||kr&|  |¡\}}}|||f  S q&d S )N)NNN)rE   ÚGetItemParentrr   r¿   r“   r‘   )rl   rv   ÚpnidrÅ   r™   ÚignoreZpnodeZpobjectrn   rn   ro   Ú_node_index  s    zSimpleEditor._node_indexc                 C   s   |  |¡o| |¡S )z4Returns whether a specified object has any children.)Úallows_childrenr½   ©rl   r–   r¤   rn   rn   ro   r¡     s    zSimpleEditor._has_childrenc                 C   s   |r|  |¡sdS | ||¡S )z8Returns whether a given object is droppable on the node.F)Z
can_insertÚcan_add)rl   r–   r¤   Ú
add_objectZ
for_insertrn   rn   ro   Ú_is_droppable  s    zSimpleEditor._is_droppablec                 C   s&   |  ||¡}||us|s|S t |¡S )z2Returns a droppable version of a specified object.)Zdrop_objectÚcopyÚdeepcopy)rl   r–   r¤   Zdropped_objectÚ	make_copyÚ
new_objectrn   rn   ro   Ú_drop_object"  s    zSimpleEditor._drop_objectFc                 C   sÎ   | j du rdS | ||¡}t|tƒrž| d¡rFt|dƒ}|du rœdS qÂ|dd… dkrh|dd… }| }n(| |¡}t|tƒr†||g}n
| |¡ t||ƒj	}n$t|tƒr°|j	}nt
dd |¡ ƒ‚| j  |¡S )	z/Returns the index of the specified object icon.Nr'   ú@é   r!   ú<z8Icon value must be a string or IImageResource instance: z
given {!r})rN   Zget_iconÚ
isinstanceÚstrÚ
startswithr   Zget_icon_pathrC   r   Zabsolute_pathÚ
ValueErrorÚformatZGetIndex)rl   r–   r¤   r˜   Z	icon_nameÚimageÚpathrn   rn   ro   r­   *  s0    








ÿÿzSimpleEditor._get_iconc                 C   s>   |  |¡r*| || jd¡ | || jd¡ | || jd¡ dS )z0Adds the event listeners for a specified object.FN©rÓ   Zwhen_children_replacedÚ_children_replacedZwhen_children_changedÚ_children_updatedZwhen_label_changedÚ_label_updatedrÔ   rn   rn   ro   r±   J  s    
zSimpleEditor._add_listenersc                 C   s>   |  |¡r*| || jd¡ | || jd¡ | || jd¡ dS )z4Removes any event listeners from a specified object.TNrç   rÔ   rn   rn   ro   rÁ   R  s    
zSimpleEditor._remove_listenersr-   c           	      C   sN   | j t|ƒ }|D ]\}}||kr q4q|d d }|  |¡\}}}|||fS )zgReturns the tree node data for a specified object in the form
        ( expanded, node, nid ).
        r   r!   )rO   r1   r‘   )	rl   r¤   r0   rÆ   Úname2rv   r£   r–   rÑ   rn   rn   ro   r{   Z  s    zSimpleEditor._object_infoc           	      C   sH   g }| j t|ƒ D ]0\}}||kr|  |¡\}}}| |||f¡ q|S )z{Returns the tree node data for a specified object as a list of the
        form: [ ( expanded, node, nid ), ... ].
        )rO   r1   r‘   rC   )	rl   r¤   r0   Úresultrë   rv   r£   r–   rÑ   rn   rn   ro   Ú_object_info_fori  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}t|ƒD ]$\}}| 	ˆ¡d	kr´|}||=  qâq´|d }|ft|ƒ }||j
v �r
ˆ|j
| fS t||d
� |j
|< }ˆ|fS )z8Returns the TreeNode associated with a specified object.ry   r!   c                    s"   g | ]}ˆ d ur|  ˆ ¡r|‘qS ©N)Zis_node_for©rË   r–   )r¤   rn   ro   rÌ   €  s   þz*SimpleEditor._node_for.<locals>.<listcomp>r   )Úadapterc                    s   g | ]}ˆ |j kr|‘qS rn   )Únode_forrï   )Úbasern   ro   rÌ   ‘  rÍ   Nr-   )Ú	root_nodeÚnodes)rà   ÚtuplerÀ   r   r<   rô   r   rñ   r¿   r°   Zmulti_nodesr   )	rl   r¤   r<   rô   ró   rÅ   r–   rž   Z
multi_nodern   )rò   r¤   ro   r¬   u  sB    ÿ
þý
þ
ÿzSimpleEditor._node_forc                 C   s*   | j jD ]}t|t|jƒƒr|  S qdS )z7Returns the TreeNode associated with a specified class.N)r<   rô   Ú
issubclassrõ   rñ   )rl   Úklassr–   rn   rn   ro   Ú_node_for_class°  s    
zSimpleEditor._node_for_classc                 C   s   |   | ¡ ¡ dS )z&Updates the icon for a specified node.N)Ú_update_icon_for_nidÚGetItem)rl   rŽ   r˜   rn   rn   ro   Ú_update_icon·  s    zSimpleEditor._update_iconc                 C   sP   | j durL|  |¡\}}}|  |||¡}| j ||tj¡ | j ||tj¡ dS )z)Updates the icon for a specified node ID.N)rN   r‘   r­   rE   ZSetItemImagerD   ZTreeItemIcon_NormalZTreeItemIcon_Selected)rl   rv   r£   r–   r¤   rµ   rn   rn   ro   rù   »  s
    
z!SimpleEditor._update_icon_for_nidc                 C   s€   z|  ¡ }W n   | ¡ }Y n0 d}t|dƒr8| ¡ }|du sH| ¡ sX| j |¡\}}| ¡ rr|  |¡||f S ddd||fS )z9Unpacks an event to see whether a tree item was involved.Nrú   )ÚGetPositionZGetPointÚhasattrrú   rr   rE   ÚHitTestr‘   )rl   rŽ   Úpointrv   Úflagsrn   rn   ro   Ú_unpack_eventÃ  s    
zSimpleEditor._unpack_eventc                 C   s8   | j  |¡\}}| ¡ r*|  |¡||f S ddd||fS )z8Returns information about the node at a specified point.N)rE   rþ   rr   r‘   )rl   rÿ   rv   r   rn   rn   ro   Ú	_hit_testÖ  s    zSimpleEditor._hit_testc                 C   s6   | j }| j |j¡ |jdkr2|jdur2|jj|_dS )z!Begins an "undoable" transaction.r'   N)rB   rP   rC   ÚhistoryÚnow)rl   rB   rn   rn   ro   Ú_begin_undoÝ  s    zSimpleEditor._begin_undoc                 C   s   | j  ¡ dkrd| j_ d S r›   )rP   r�   rB   r‚   rn   rn   ro   Ú	_end_undoä  s    zSimpleEditor._end_undoc                 C   s   t |||j|j|jd�S )N)r¤   r0   r¼   ÚaddedÚremoved)r   r¼   r  r  )rl   r¤   r0   rŽ   rn   rn   ro   Ú_get_undo_itemè  s    ûzSimpleEditor._get_undo_itemc                 C   s>   z.|   ¡  |rt |¡}| ||¡ W |  ¡  n
|  ¡  0 dS )z&Performs an undoable append operation.N)r  rØ   rÙ   Zappend_childr  )rl   r–   r¤   r—   rÚ   rn   rn   ro   Ú_undoable_appendñ  s    
zSimpleEditor._undoable_appendc                 C   s@   z0|   ¡  |rt |¡}| |||¡ W |  ¡  n
|  ¡  0 dS )z&Performs an undoable insert operation.N)r  rØ   rÙ   Zinsert_childr  )rl   r–   r¤   r¼   r—   rÚ   rn   rn   ro   Ú_undoable_insertû  s    
zSimpleEditor._undoable_insertc                 C   s0   z |   ¡  | ||¡ W |  ¡  n
|  ¡  0 dS )z&Performs an undoable delete operation.N)r  Zdelete_childr  )rl   r–   r¤   r¼   rn   rn   ro   Ú_undoable_delete  s    zSimpleEditor._undoable_deletec                 C   sF   | j  t|ƒ¡}|du rdS |D ]\}}||kr |  S q |d d S )z8Gets the ID associated with a specified object (if any).Nr   r!   )rO   Úgetr1   )rl   r¤   r0   rÆ   rë   rv   rn   rn   ro   Ú_get_object_nid  s    
zSimpleEditor._get_object_nidc                 C   s4   | j }|jdur0| d¡ |j ¡  d |_|_dS )z(Clears the current editor pane (if any).N)r?   r@   rI   ru   rA   )rl   r4   rn   rn   ro   rÄ     s
    


zSimpleEditor._clear_editorc                 C   s   || j kr| jS | j |¡S )zGets the node specific data.)r¯   Ú_root_nid_datarE   ZGetItemDatarÎ   rn   rn   ro   r‘   !  s    
zSimpleEditor._get_node_datac                 C   s$   || j kr|| _n| j ||¡ dS )zSets the node specific data.N)r¯   r  rE   ZSetItemData)rl   rv   r—   rn   rn   ro   r²   (  s    
zSimpleEditor._set_node_datac                 C   s   |   |¡d S )z1Gets the object associated with a specified node.ry   )r‘   rÎ   rn   rn   ro   Ú
get_object1  s    zSimpleEditor.get_objectc                 C   s6   |   ||¡}|dur2| j |¡}| ¡ r2|  |¡S dS )zdReturns the object that is the immmediate parent of a specified
        object in the tree.
        N)r  rE   rÏ   rr   r  )rl   r¤   r0   rv   rÐ   rn   rn   ro   Ú
get_parent5  s    
zSimpleEditor.get_parentc                 C   s&   |   ||¡}|dur"|  |¡d S dS )z4Returns the node associated with a specified object.Nr!   )r  r‘   )rl   r¤   r0   rv   rn   rn   ro   Úget_nodeA  s    zSimpleEditor.get_nodec                 C   s–   | j rd| _ | ¡  dS | ¡ }| j}|  |¡\}}}| |¡rˆ|}| |¡}| ¡ sXqd| |¡ qD|}| 	|¡}| ¡ s|qˆ| |¡ qh|  
|¡ dS )zHandles a tree node expanding.FN)r�   ÚVetorú   rE   r‘   Zcan_auto_closeZGetPrevSiblingrr   ZCollapseZGetNextSiblingrœ   )rl   rŽ   rv   r/   r£   r–   r¤   Úsnidrn   rn   ro   rX   K  s&    


z$SimpleEditor._on_tree_item_expandingc                 C   s   |   |d¡ dS )z#Handles a tree node being expanded.TN©rû   r�   rn   rn   ro   rZ   j  s    z#SimpleEditor._on_tree_item_expandedc                 C   s   | j rd| _ | ¡  dS )zHandles a tree node collapsing.FN)r�   r  r�   rn   rn   ro   r\   n  s    z%SimpleEditor._on_tree_item_collapsingc                 C   s   |   |d¡ dS )z$Handles a tree node being collapsed.FNr  r�   rn   rn   ro   Ú_on_tree_item_collapsedt  s    z$SimpleEditor._on_tree_item_collapsedNc                    sÐ  ˆ j r
dS d}d}ˆ j ¡ }‡ fdd„|D ƒ}d}|D ]8}| ¡ sDq6ˆ  |¡\}}	}
|r6|	}|
}| |¡}d}q6ˆ jjdkr�dˆ _|ˆ _	dˆ _ndˆ _|ˆ _	dˆ _|du r¼ˆ j
 ˆ jj|¡ ˆ j}|du�rÌ| ¡  ˆ  ¡  |du�r�| |¡}|du �st|tƒ�r| |¡}ˆ j
jdu�s*|jdk�r<|j||dd�}n|j||d	d�}ˆ j
|_||_||_t tj¡}| |jd
tj¡ |  |¡ | !¡  | "¡ \}}| #t $||d
 ¡¡ | #t $||¡¡ | %¡  dS )z#Handles a tree node being selected.NTc                    s"   g | ]}|  ¡ rˆ  |¡d  ‘qS )ry   )rr   r‘   )rË   rv   r‚   rn   ro   rÌ   ‚  rÍ   z5SimpleEditor._on_tree_sel_changed.<locals>.<listcomp>Fr†   Zsubpanel)rm   ÚviewÚkindZpanelr!   )&rÂ   rE   ZGetSelectionsrr   r‘   Úselectr<   r‰   r   r5   rB   ÚevaluateZ	on_selectr?   ZFreezerÄ   Zget_viewrà   rá   Z
trait_viewr  r  Úedit_traitsrm   r@   rA   rD   rJ   rK   ÚAddr2   ZEXPANDrI   ZLayoutZGetSizeZSetSizerL   ZThaw)rl   rŽ   r¤   Znot_handledÚnidsr5   Úfirstrv   r£   Zsel_nodeZ
sel_objectr–   r4   r  rB   ZsizerZdxZdyrn   r‚   ro   ra   x  sf    





ÿÿ
z!SimpleEditor._on_tree_sel_changedc                 C   s†   | j  | ¡ ¡\}}|tj@ rT|  |¡\}}}| jjdurx| j 	| jj|¡ d| _
n$| jrx| jjdurx| j 	| jjd¡ | d¡ dS )z)Handles the mouse moving over a tree nodeNT)rE   rþ   rü   rD   ZTREE_HITTEST_ONITEMLABELr‘   r<   Zon_hoverrB   r  r�   ÚSkip)rl   rŽ   r1   r   r£   r–   r¤   rn   rn   ro   rk   Ó  s    
zSimpleEditor._on_hoverc                 C   s^   |   | ¡ ¡\}}}| |¡du rH| jjdurN| j | jj|¡ d| _nd| _|| _|| _dS )z$Handles a tree item being activated.TN)	r‘   rú   r6   r<   Zon_activatedrB   r  r�   r9   )rl   rŽ   r£   r–   r¤   rn   rn   ro   r_   á  s    z$SimpleEditor._on_tree_item_activatedc                 C   sh   |  ¡ }| j |¡}d}| ¡ r:|  |¡\}}}| |¡}|r\|  |¡\}}}| |¡r\dS | ¡  dS )z4Handles the user starting to edit a tree node label.TN)rú   rE   rÏ   rr   r‘   Ú
can_renameÚcan_rename_mer  )rl   rŽ   rG   rm   r   r£   r–   r¤   rn   rn   ro   re   ó  s    

z&SimpleEditor._on_tree_begin_label_editc                 C   sT   |  ¡ }t|ƒdkrH|  | ¡ ¡\}}}z| ||¡ W dS    Y n0 | ¡  dS )z4Handles the user completing tree node label editing.r   N)ZGetLabelrÀ   r‘   rú   Ú	set_labelr  )rl   rŽ   rM   r£   r–   r¤   rn   rn   ro   rg     s    z$SimpleEditor._on_tree_end_label_editc                 C   sT   t durP|  |¡\}}}}}|durPz"|| _t | j| |¡ƒ W d| _nd| _0 dS )z1Handles a drag operation starting on a tree node.N)r   r  Ú	_draggingrE   Zget_drag_object©rl   rŽ   r£   r–   r¤   rv   rÿ   rn   rn   ro   rc     s    z SimpleEditor._on_tree_begin_dragc                 C   sT   |  ¡ }| ¡ rH|  |¡}|durH|\}}}| |¡}|dkrH| |¡ | ¡  dS )z)Handles a tooltip request on a tree node.Nr-   )rú   rr   r‘   Zget_tooltipZ
SetToolTipr  )rl   rŽ   rv   Z	node_datar£   r–   r¤   Ztooltiprn   rn   ro   ri     s    



z%SimpleEditor._on_tree_item_gettooltipc                 C   sb   |   |¡\}}}}}|durV| |¡du rP| jjdurV| j | jj|¡ d| _nd| _| ¡  dS )zBHandle left mouse dclick to emit dclick event for associated node.NT)r  r9   r<   Z	on_dclickrB   r  r�   r  r$  rn   rn   ro   rV   *  s    zSimpleEditor._on_left_dclickc                 C   sZ   |   |¡\}}}}}|durN| |¡du rH| jjdurH| j | jj|¡ || _| ¡  dS )ú/Handles the user right clicking on a tree node.NT)r  r8   r<   Zon_clickrB   r  r  r$  rn   rn   ro   rR   @  s    
ÿzSimpleEditor._on_left_downc                 C   s^  |   |¡\}}}}}|du�rZ|||f| _|| || jj| jjdœ| _| j |¡}| ¡ rj|  	|¡\}}	}
nd }	}
|| _
|	| _|
| _| |¡}|du r¤|  ||¡}nXt|tƒrø| t¡}|durü| ¡  |  ||¡}t|ƒdkrü| dt|ddiŽ¡ nd}|du�r<| | j| ¡}| j ||d d |d d ¡ | ¡  d | _ | _ | _
 | _| _dS )r%  N©r¤   r4   r–   rÆ   Úhandlerr   r0   ÚNewé
   r!   )r  Ú_datarB   rÆ   r'  Ú_contextrE   rÏ   rr   r‘   Ú
_menu_nodeÚ_menu_parent_nodeÚ_menu_parent_objectZget_menuÚ_standard_menurà   r   Z
find_groupr   ÚclearÚ_new_actionsrÀ   ÚinsertZcreate_menuZ	PopupMenuZDestroy)rl   rŽ   r£   r–   r¤   rv   rÿ   rÐ   rÑ   Zparent_nodeZparent_objectÚmenuÚgroupÚactionsZwxmenurn   rn   ro   rT   R  sN    
û	



 üÿýüzSimpleEditor._on_right_downc                 C   sR   t tttƒ ttƒ tg}|  ||¡}t|ƒdkrJt|ddiŽtƒ g|dd…< t|Ž S )z,Returns the standard contextual pop-up menu.r   r0   r(  )	r   r   r   r   r   r   r1  rÀ   r   )rl   r–   r¤   r5  Úitemsrn   rn   ro   r/  �  s    ùzSimpleEditor._standard_menuc                    sØ   ˆ j d }g }| |¡}t|ƒdkr(|S |D ]¦}d}d}t|tƒrnt|ƒdkrX|\}}nt|ƒdkrn|\}}}ˆ  |¡}|du r‚q,|j}	| |¡}
|
dkrž|	}
|s¦|}‡ fdd	„}t|||d
�}| 	t
|
|d�¡ q,|S )z7Returns a list of Actions that will create new objects.r!   r   FNry   rÞ   r-   c                    s   ˆ   ||¡ d S rî   )Ú_menu_new_node)r¤   r<   Úpromptr‚   rn   ro   Úperform_add½  s    z.SimpleEditor._new_actions.<locals>.perform_add)r<   r8  )r0   Ú
on_perform)r*  Zget_addrÀ   rà   rõ   rø   Ú__name__Úget_namer   rC   r   )rl   r–   r¤   r6  Úaddr÷   r8  r<   Úadd_nodeÚ
class_namer0   r9  r:  rn   r‚   ro   r1  ¢  s4    






zSimpleEditor._new_actionsc                 C   s.   | j }t|tƒr| | j¡S |d uo,| |¡S rî   )r-  rà   r   Úcan_copyr.  )rl   r¤   rm   rn   rn   ro   Ú_is_copyableÄ  s    
zSimpleEditor._is_copyablec                 C   sV   | j }t|tƒr*| | j¡o&| | j¡}n|d uoD| |¡oD| |¡}|oT| j |¡S rî   )r-  rà   r   r@  r.  Ú
can_deleter,  Úcan_delete_me)rl   r¤   rm   Zcan_cutrn   rn   ro   Ú_is_cutableÊ  s    
ÿ
þÿýzSimpleEditor._is_cutablec                 C   s   ddl m} | j ||j¡S )Nr   ©Ú	clipboard)Úpyface.wx.clipboardrF  r,  rÕ   Zobject_type)rl   r¤   rF  rn   rn   ro   Ú_is_pasteableØ  s    zSimpleEditor._is_pasteablec                 C   s@   | j }t|tƒr| | j¡}n|d uo.| |¡}|o>| j |¡S rî   )r-  rà   r   rB  r.  r,  rC  )rl   r¤   rm   rB  rn   rn   ro   Ú_is_deletableÝ  s
    
zSimpleEditor._is_deletablec                 C   sF   | j }t|tƒr| | j¡}n|d ur2| |¡}nd}|oD| j |¡S r    )r-  rà   r   r   r.  r,  r!  )rl   r¤   rm   r   rn   rn   ro   Ú_is_renameableå  s    
zSimpleEditor._is_renameablec                 C   sì  t |tƒr.tj}|D ]}|  ||||¡}q|S |  t ||¡¡\}}}	}
}|du�ræ|tjk�r|  ||	|d¡svtjS | j	durÜ|  
||	|d¡}|durÚz6|  ¡  | j|  | j	¡Ž  |  ||	|d¡ W |  ¡  n
|  ¡  0 n(|  
||	|¡}|du�r|  ||	|d¡ |S |  |
¡\}}}|du�ræ| j	du�r¸|  
|||d¡}|du�râ|  | j	¡\}}}||u �rv||k�rv|d8 }z4|  ¡  |  |||¡ |  ||||d¡ W |  ¡  n
|  ¡  0 n*|  
|||¡}|du�râ|  ||||d¡ |S tjS )z2Handles a Python object being dropped on the tree.NFr!   )rà   ÚlistrD   ÚDragNoneÚwx_dropped_onr  ÚPointZDragMover×   r#  rÜ   r  r  rÒ   r
  r  r  )rl   ÚxÚyr—   Údrag_resultÚrcrG   r£   r–   r¤   rv   rÿ   Zto_nodeZ	to_objectZto_indexZ	from_nodeZfrom_objectZ
from_indexrn   rn   ro   rM  ñ  sh    



ÿ


ÿ

ÿÿ
ÿ

ÿzSimpleEditor.wx_dropped_onc                 C   sˆ   |   t ||¡¡\}}}}}	d}
|durF|tjkrF|  |¡\}}}d}
| jdurf|  | j||¡sftjS |dur‚|  ||||
¡r‚|S tjS )z4Handles a Python object being dragged over the tree.FNT)	r  rD   rN  ZDragCopyrÒ   r#  Ú_is_drag_okrL  r×   )rl   rO  rP  r—   rQ  r£   r–   r¤   rv   rÿ   r2  r¼   rn   rn   ro   Úwx_drag_over2  s    
ÿÿzSimpleEditor.wx_drag_overc                 C   sF   |d u s||u rdS |   |¡D ]"}|  ||  |¡d |¡s dS qdS )NFry   T)r“   rS  r‘   )rl   r  ÚsourceÚtargetr™   rn   rn   ro   rS  G  s    ÿzSimpleEditor._is_drag_okc                 C   s,   |j j}|  |j|d¡ |  |j|d¡ dS )z3Adds a menu item to the menu bar being constructed.ZenabledÚcheckedN)rG   ÚactionÚ	eval_whenZenabled_whenZchecked_when)rl   Z	menu_itemrX  rn   rn   ro   Úadd_to_menuU  s    zSimpleEditor.add_to_menuc                 C   s   |   |¡ dS )z5Adds a toolbar item to the toolbar being constructed.N)rZ  )rl   Ztoolbar_itemrn   rn   ro   Ú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 )zCReturns whether the action should be defined in the user interface.r-   FT)Zdefined_whenÚevalÚglobalsr+  Zvisible_when©rl   rX  rn   rn   ro   Úcan_add_to_menu_  s    

zSimpleEditor.can_add_to_menuc                 C   s
   |   |¡S )z\Returns whether the toolbar action should be defined in the user
        interface.
        )r_  r^  rn   rn   ro   Úcan_add_to_toolbark  s    zSimpleEditor.can_add_to_toolbarc                 C   s   | j  | j|¡ dS )z;Performs the action described by a specified Action object.N)rB   Zdo_undoableÚ_perform)rl   rX  Zaction_eventrn   rn   ro   Úperformq  s    zSimpleEditor.performc           
   	   C   sÂ   | j \}}}|j}| jj}| jj}| d¡dkrˆ| d¡dk rF|d7 }zt|tƒ || |||dœƒ W n   ddlm	} |ƒ  Y n0 d S t
||d ƒ}	|	d urª|	||ƒ d S |jd ur¾| |¡ d S )NÚ.r   ú(z()r&  )Úraise_to_debug)r*  rX  rB   rÆ   r'  Úfindr\  r]  Útraitsui.apire  Úgetattrr:  )
rl   rX  r–   r¤   rv   Úmethod_namerÆ   r'  re  Úmethodrn   rn   ro   ra  u  s8    ûý

zSimpleEditor._performc                 C   s0   |dkr,d}t |tƒ | jƒs d}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-   TFN)r\  r]  r+  Úsetattr)rl   Ú	conditionr¤   Ztraitr‹   rn   rn   ro   rY  ›  s
    zSimpleEditor.eval_whenc                 C   s"   ddl m} | jd |_d| _dS )z8Copies the current tree node object to the paste buffer.r   rE  r!   N)rG  rF  r*  r—   )rl   rF  rn   rn   ro   Ú_menu_copy_node¨  s    zSimpleEditor._menu_copy_nodec                 C   s8   ddl m} | j\}}}||_d| _| j|  |¡Ž  dS )z8Cuts the current tree node object into the paste buffer.r   rE  N)rG  rF  r*  r—   r  rÒ   ©rl   rF  r–   r¤   rv   rn   rn   ro   Ú_menu_cut_node¯  s
    zSimpleEditor._menu_cut_nodec                 C   s4   ddl m} | j\}}}d| _|  |||jd¡ dS )zWPastes the current contents of the paste buffer into the current
        node.
        r   rE  NF)rG  rF  r*  r
  Zobject_datarn  rn   rn   ro   Ú_menu_paste_node¸  s    zSimpleEditor._menu_paste_nodec                 C   sŠ   | j \}}}d| _ | |¡}|dur†|durv| jjdu rvtj| jd| |¡ dtjtj	B tj
B d�}| ¡ tjkrvdS | j|  |¡Ž  dS )z'Deletes the current node from the tree.NFTz#Are you sure you want to delete %s?zConfirm Deletionr(   )r*  Zconfirm_deleterB   r  rD   ZMessageDialogrE   r®   ÚOKZCANCELZICON_EXCLAMATIONZ	ShowModalZID_OKr  rÒ   )rl   r–   r¤   rv   rR  Zdlgrn   rn   ro   Ú_menu_delete_nodeÂ  s"    
ÿûzSimpleEditor._menu_delete_nodec                 C   sN   | j \}}}d| _ t| |¡d�}| ¡ jrJ|j ¡ }|dkrJ| ||¡ dS )zRenames the current tree node.N)rM   r-   )r*  ÚObjectLabelr®   r  rì   rM   Ústripr"  )rl   r–   r¤   rv   Zobject_labelrM   rn   rn   ro   Ú_menu_rename_node×  s    

zSimpleEditor._menu_rename_nodec                 C   sj   | j \}}}d| _ |ƒ }|du r$dS |r:|j| jdd�jrf|  |||d¡ | jjrf| j | j 	|¡¡ dS )z&Adds a new object to the current node.NZ	livemodal)rm   r  F)
r*  r  r2   rì   r
  r<   r>   rE   rz   ZGetLastChild)rl   r<   r8  r–   r¤   rv   rÛ   rn   rn   ro   r7  á  s    ÿzSimpleEditor._menu_new_nodec                 C   s    | j }|  ||¡D ]ˆ\}}}| |¡}|rr|  |¡D ]}	|  |	¡ q4|D ](}
|  |
¡\}
}|durH|  |||
¡ qH| |t|ƒdk¡ | 	|¡r| 
|¡ qdS )z9Handles the children of a node being completely replaced.Nr   )rE   rí   rÇ   r¾   rs   r¬   r¹   r¢   rÀ   r»   r�   )rl   r¤   r0   Únewr/   r£   r–   rv   Úchildrenr™   rÈ   rÉ   rn   rn   ro   rè   ô  s    

zSimpleEditor._children_replacedc                 C   s&  |dd… }|   | j|||¡ |j}|t|jƒ }| j}|  ||¡D ]Ü\}}}	t| |¡ƒ}
|râ|  |	¡||… D ]}|  	|¡ qr|
t|jƒ }d}|j
D ]F}|  |¡\}}|durš||k rÄ|| nd}|  |	|||¡ |d7 }qš| |	|
dk¡ | ¡ }| |¡rD|	|k�s| jjsD| |	¡ qDdS )z-Handles the children of a node being changed.Niúÿÿÿr   r!   )Z
log_changer	  r¼   rÀ   r  rE   rí   rÇ   r¾   rs   r  r¬   r¸   r¢   rq   r»   r<   rˆ   r�   )rl   r¤   r0   rŽ   ÚstartÚendr/   r£   r–   rv   r§   r™   Ú	remainingZchild_indexrÈ   rÉ   Zinsert_indexÚrootrn   rn   ro   ré     s4    
ÿ
ý

zSimpleEditor._children_updatedc                 C   s\   i }| j t|ƒ D ]D\}}||vrd||< |  |¡d }| j || |¡¡ |  |¡ qdS )z-Handles the label of an object being changed.Nr!   )rO   r1   r‘   rE   ZSetItemTextr®   rù   )rl   r¤   r0   rM   r  rë   rv   r–   rn   rn   ro   rê   :  s    zSimpleEditor._label_updatedc                 C   s8   | j r4t|tƒr| d¡}n|}| j ¡  | j|¡ dS )z[Restores any saved user preference information associated with the
        editor.
        Ú	structureN)rF   rà   Údictr  r2   ÚGetSizerZSetStructure)rl   Zprefsr|  rn   rn   ro   Úrestore_prefsF  s
    
zSimpleEditor.restore_prefsc                 C   s   | j rd| j ¡  ¡ iS dS )zCReturns any user preference information associated with the editor.r|  N)rF   r2   r~  ZGetStructurer‚   rn   rn   ro   Ú
save_prefsQ  s    zSimpleEditor.save_prefs)T)T)F)r-   )r-   )T)T)r-   )r-   )r-   )N)N)F)r-   N)gr;  Ú
__module__Ú__qualname__Ú__doc__Z
scrollabler	   r}   r   r5   r6   r8   r9   r:   rp   ru   r~   r€   rƒ   r=   r�   rš   rŸ   r¨   r©   r¶   r·   r¹   r¸   rs   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Ä   r‘   r²   r  r  r  rX   rZ   r\   r  ra   rk   r_   re   rg   rc   ri   rV   rR   rT   r/  r1  rA  rD  rH  rI  rJ  rM  rT  rS  rZ  r[  r_  r`  rb  ra  rY  rm  ro  rp  rr  ru  r7  rè   ré   rê   r  r€  Ú__classcell__rn   rn   rw   ro   r&   E   sÄ     	
#

 

;	




	



[="A
&	



,r&   c                   @   s*   e Zd ZdZeƒ Zedddddgd�ZdS )	rs  z An editable label for an object.rM   z
Edit LabelÚmodalrq  ZCancel)Útitler  ZbuttonsN)r;  r�  r‚  rƒ  r   rM   r   Ztraits_viewrn   rn   rn   ro   rs  \  s
   ÿrs  )9rƒ  rØ   Ú	functoolsr   r‡   rD   Zpyface.wx.drag_and_dropr   r   Zpyface.ui.wx.image_listr   Z
traits.apir   r   r   r	   r
   rg  r   r   r   r   Ztraitsui.editors.tree_editorr   r   r   r   r   r   Ztraitsui.undor   Ztraitsui.tree_noder   Ztraitsui.menur   r   r   Z
pyface.apir   Zpyface.ui_traitsr   Zpyface.dock.apir   r   r   r   r    Ú	constantsr"   r4   r#   Úhelperr$   r%   Zpaste_bufferr&   rs  rn   rn   rn   ro   Ú<module>   sH                #