a
    §ˆcáG  ã                   @   sÄ   d Z ddl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 ddlmZ d	d
lmZ d	dlmZ d	dlmZ d	dlmZmZ dejejdœZG dd„ deƒZG dd„ deƒZdS )zO Defines the various instance editors for the wxPython user interface
toolkit.
é    N)ÚPythonDropTarget)Ú	HasTraitsÚInstanceÚProperty)ÚAView)Úuser_name_for)ÚHandler)ÚInstanceChoiceItemé   )Útoolkit)ÚEditor)Ú	DropColor)ÚTraitsUIPanelÚposition_window)ÚdefaultZ
horizontalZverticalc                       s¼   e Zd ZdZeZejZdZ	e
ƒ Zd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„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Z‡  Z S )%ÚCustomEditora   Custom style of editor for instances. If selection among instances is
    allowed, the editor displays a combo box listing instances that can be
    selected. If the current instance is editable, the editor displays a panel
    containing trait editors for all the instance's traits.
    r   c           
   	   C   sü  | j }|jdkr(|  |j¡\| _| _| _|jrDt|dƒ | _ | _	}|j
}|j}| j}|D ]}|| ¡ O }|| ¡ O }qZ|�rpi | _|  | j¡}|dur¨| j| jt|ƒ< t |dt dd¡t dd¡g ¡ | _}|jtj| j| ¡ d� |rú| j | j¡ |  | j¡ |jdk�rB| jj| j | jdd� | jj| j | jd dd� |j| j d	dd� |j| j d
dd� |   ¡  n6|�r¦tj!|ddtj"d�| _| j | j¡ |  | j¡ |�r¼| j #t$| ƒ¡ t%|j& }|du �rÖ| j&}|�sâ|�rX|j�rXt '|¡}	|	 (| j| j)tj*¡ |tj+k�r@|	 (tj,|dtj-d�dtj*tj.B tj/B d¡ |  0||	¡ | 1|	¡ n‚| jdu �rº| jdu �r°t |dt dd¡t dd¡g ¡ | _}|jtj| j| ¡ d� | j| _n t '|¡}	|  0||	¡ | 1|	¡ | j2|j3d� |  4|j5dd¡ dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú éÿÿÿÿNr   ©ÚidÚui)ÚdispatchÚ_itemsÚvaluesÚvalues_items)Ústyleé   )Úviewr   Úfrom)6ÚfactoryÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚeditabler   ÚcontrolÚ_panelÚ
selectableÚ	droppableÚitemsÚis_droppableÚis_selectableÚ_object_cacheÚitem_forÚvaluer   ÚwxÚChoiceZPointÚSizeÚ_choiceÚBindÚ
EVT_CHOICEÚupdate_objectÚGetIdZSetBackgroundColourÚok_colorZset_tooltipÚon_trait_changeÚrebuild_itemsZTextCtrlZTE_READONLYZSetDropTargetr   ÚOrientationMapÚorientationÚBoxSizerÚAddÚextraÚEXPANDÚVERTICALZ
StaticLineZLI_HORIZONTALZTOPZBOTTOMÚcreate_editorÚSetSizerZ
trait_setqr   Z
sync_valueZ	view_name)
ÚselfÚparentr   r'   r(   r)   ÚitemÚchoicer;   Úsizer© rH   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/instance_editor.pyÚinitI   s”    
ÿÿ

ÿÿÿÿ



üÿ
ÿ


zCustomEditor.initc                    sº   d| _ | jdur| j ¡  | j}|dur¬t|tjƒrJ|jtjd| 	¡ d� | j
dur„| j
j| j| jdd� | j
j| j| jd dd� | jj| jddd� | jj| jddd� tƒ  ¡  dS )ú&Disposes of the contents of an editor.Nr   T)Úremover   r   r   )r,   Ú_uiÚdisposer2   Ú
isinstancer/   r0   r3   r4   r6   r!   r8   r9   r"   r   Úsuper)rC   rF   ©Ú	__class__rH   rI   rN   «   s*    



ÿÿÿÿzCustomEditor.disposec                 C   s"   t |dƒ| _| | jdtj¡ dS )zCreates the editor control.r   r
   N)r   r&   r=   r/   r?   )rC   rD   rG   rH   rH   rI   rA   É   s    zCustomEditor.create_editorc                 C   sr   | j dur| j S | j}| jdur0|  ¡ |j }n|j}g }|j}|D ]"}t|tƒs\||d�}| |¡ qD|| _ |S )z2Gets the current list of InstanceChoiceItem items.N)Úobject)r   r   r#   r   ÚadapterrO   r	   Úappend)rC   r   r   r)   rT   r.   rH   rH   rI   Ú
_get_itemsÎ   s    



zCustomEditor._get_itemsc                 C   s‚   d| _ d}| j}| j}| ¡  | jD ],}| ¡ r$| ¡ }| |¡ | |¡r$|}q$|durf| 	|¡ nz
d| _W n   Y n0 dS )z"Rebuilds the object selector list.N)
r   r.   r2   ZClearr)   r+   Úget_nameÚAppendÚis_compatibleÚSetStringSelection)rC   r    r.   rF   rE   Z	item_namerH   rH   rI   r9   ä   s"    



zCustomEditor.rebuild_itemsc                 C   s"   | j D ]}| |¡r|  S qdS )z6Returns the InstanceChoiceItem for a specified object.N)r)   rY   )rC   rS   rE   rH   rH   rI   r-   ÿ   s    


zCustomEditor.item_forc                 C   s@   d}|dur|  ¡ }|dkr"| j}| jj | jj||| j| j¡S )z/Returns the view to use for a specified object.r   N)Zget_viewr   r   ÚhandlerÚtrait_view_forÚinfoÚobject_namer    )rC   rS   rE   r   rH   rH   rI   Úview_for  s    ÿzCustomEditor.view_forc                 C   sœ   |  ¡ }| jD ]ˆ}|| ¡ krt|ƒ}| j |¡}|du r„| ¡ }| jjsr|j	rr|  
||  |¡¡}| || jd¡ | jjr„|| j|< || _|  ¡   q˜qdS )z:Handles the user selecting a new value from the combo box.NÚmodal)Z	GetStringr)   rW   r   r,   ÚgetZ
get_objectr   r$   Z
is_factoryr_   r-   r   r%   Zcachabler.   Úresynch_editor)rC   Úeventr    rE   Zid_itemrS   r   rH   rH   rI   r5     s    

zCustomEditor.update_objectc                 C   s    | j  ¡ }| j|_t|ddƒ}|dur.| ¡  |  ¡  | j}|  | j¡}|durœ|durœ| 	| j¡}| j
dur’| |¡dk r†| |¡ | |¡ n
| |¡ dS )z[Updates the editor when the object trait changes externally to the
        editor.
        Z_dock_controlNr   )r%   Ú	GetParentr.   r!   ÚgetattrZ	reset_tabrb   r2   r-   rW   r,   Z
FindStringrX   rZ   ZSetValue)rC   rD   Zdock_controlrF   rE   r    rH   rH   rI   Úupdate_editor(  s    



zCustomEditor.update_editorc                 C   s*  | j }|du�r&| ¡ \}}d }}| jdur8| j\}}t t||ƒt||ƒ¡ | _}| d¡ | jdur|| j ¡  d| _n| 	¡ D ]}t
 |¡ q„t tj¡}d}	| j}
t|
tƒsÖd}|
durÆ| j}t |d|¡}n |  |
|  |
¡¡}|
 ¡ }d}t|
tƒ�r|
}| d| j¡ | d| jj¡ | ||d|
 ¡ || jj¡ | _}|j}|j| _ | j|_!|j"�sr|j �sr|j�rvd}	| #¡  |	�r¤|d	k�r¤| $|¡ | $|¡ n| $| ¡ ¡ | %||	tj&¡ | |¡ | '¡  | j (¡  | j )¡ }| (¡  |du�rt|tj*ƒ�s| )¡ }�qð|du�r&| +¡  dS )
úrResynchronizes the contents of the editor when the object trait
        changes externally to the editor.
        Nr   r   r   ÚcontextZcontext_handlerZsubpanelr
   )é   ri   ),r&   ZGetSizeZ_panel_sizer/   r1   ÚmaxrB   rM   rN   ZGetChildrenr   Zdestroy_controlr<   r@   r.   rO   r   Ú	str_valueZ
StaticTextr_   r-   Ztrait_contextr   Ú
setdefaultrS   r   r[   Ztrait_view_elementsr   r   r%   Z_scrollableZ
scrollablerD   Z	resizableZFreezeZSetSizer=   r?   ZThawZLayoutrd   ZScrolledWindowZSendSizeEvent)rC   ZpanelZdxZdyZmdxZmdyÚsizeÚchildrG   Zstretchr.   rk   r%   r   rh   r[   r   rD   rH   rH   rI   rb   G  st    






ú






ÿ
zCustomEditor.resynch_editorc                 C   s   dS )zDHandles an error that occurs while setting the object's trait value.NrH   )rC   ZexcprH   rH   rI   Úerror˜  s    zCustomEditor.errorc                 C   s   | j p
| jS )z9Returns the editor's control for indicating error status.)r2   r%   )rC   rH   rH   rI   Úget_error_controlœ  s    zCustomEditor.get_error_controlc                 C   s2   | j }|dur.| d¡|jkr.| | d¡¡ dS )z[Restores any saved user preference information associated with the
        editor.
        Nr   Úprefs)rM   ra   r   Z	set_prefs)rC   rq   r   rH   rH   rI   Úrestore_prefs¢  s    zCustomEditor.restore_prefsc                 C   s,   | j }|dur(|jdkr(|j| ¡ dœS dS )zCReturns any user preference information associated with the editor.Nr   )r   rq   )rM   r   Z	get_prefs)rC   r   rH   rH   rI   Ú
save_prefsª  s    zCustomEditor.save_prefsc                 C   sD   | j D ]6}| ¡ r| |¡r| jdur.|  ¡  || _|  S qtjS )z2Handles a Python object being dropped on the tree.N)r)   r*   rY   r,   r9   r.   r/   ÚDragNone©rC   ÚxÚyÚdataZdrag_resultrE   rH   rH   rI   Úwx_dropped_on´  s    


zCustomEditor.wx_dropped_onc                 C   s,   | j D ]}| ¡ r| |¡r|  S qtjS )z4Handles a Python object being dragged over the tree.)r)   r*   rY   r/   rt   ru   rH   rH   rI   Úwx_drag_over¿  s    

zCustomEditor.wx_drag_overc                 C   s   |   ¡  d S )N)rb   )rC   r   rH   rH   rI   Ú_view_changedÉ  s    zCustomEditor._view_changed)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r7   r/   r@   r;   r>   r   r)   Zborder_sizer   r   rJ   rN   rA   rV   r9   r-   r_   r5   rf   rb   ro   rp   rr   rs   ry   rz   r{   Ú__classcell__rH   rH   rQ   rI   r   +   s0   bQ

r   c                       sJ   e Zd ZdZejZdZedƒZ	dd„ Z
‡ fdd„Zdd	„ Zd
d„ Z‡  ZS )ÚSimpleEditorz–Simple style of editor for instances, which displays a button. Clicking
    the button displays a dialog box in which the instance can be edited.
    é   ztraitsui.ui.UIc                 C   sH   t  |dd¡ | _}| |dt jt jB d¡ |jt j| j| 	¡ d� dS )z&Creates the editor control (a button).r   r   r
   r   r   N)
r/   ZButtonÚ_buttonr=   r?   ÚLEFTr3   Ú
EVT_BUTTONÚedit_instancer6   )rC   rD   rG   ÚbuttonrH   rH   rI   rA   Ù  s    zSimpleEditor.create_editorc                    sL   | j }|dur$|jtjd| ¡ d� | jdur>| j ¡  d| _tƒ  ¡  dS )rK   Nr   )rƒ   r3   r/   r…   r6   Ú
_dialog_uirN   rP   )rC   r‡   rQ   rH   rI   rN   ß  s    

zSimpleEditor.disposec                 C   st   | j }| jj | jj|j| j| j| j¡}| jj	|| j
|j|jd�}|j
durjt|j
ƒ |jdu rj| jj|_|| _dS )zWEdit the contents of the object trait when the user clicks the
        button.
        r   N)r   r   r[   r\   r]   r   r.   r^   r    Zedit_traitsr%   Úkindr   r   Úhistoryrˆ   )rC   rc   r   r   r   rH   rH   rI   r†   ë  s    ÿÿ



zSimpleEditor.edit_instancec                 C   sH   | j }|durD| jj}|dkr(t| jƒ}| |¡ | t| jt	ƒ¡ dS )rg   Nr   )
rƒ   r   Úlabelr   r    ZSetLabelZEnablerO   r.   r   )rC   r‡   r‹   rH   rH   rI   rb     s    

zSimpleEditor.resynch_editor)r|   r}   r~   r   r/   Ú
HORIZONTALr;   r>   r   rˆ   rA   rN   r†   rb   r€   rH   rH   rQ   rI   r�   Í  s   r�   )r   r/   Zpyface.wx.drag_and_dropr   Z
traits.apir   r   r   Ztraitsui.ui_traitsr   Ztraitsui.helperr   Ztraitsui.handlerr   Ztraitsui.instance_choicer	   r   r   Zeditorr   Ú	constantsr   Úhelperr   r   rŒ   r@   r:   r   r�   rH   rH   rH   rI   Ú<module>   s(   ý   %