a
    §ˆc±?  ã                   @   sÐ   d Z ddl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 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ejjjejjjdœZG dd„ deƒZG dd„ deƒZ dS )zp Defines the various instance editors and the instance editor factory for
    the PyQt user interface toolkit..
é    )ÚQtCoreÚQtGui)Ú	HasTraitsÚInstanceÚProperty)Útrait)ÚAView)Úuser_name_for)ÚHandler)ÚInstanceChoiceItemé   )ÚEditor)Ú_DropEventFilter)Ú	DropColor)Úposition_windowN)ÚdefaultZ
horizontalZverticalc                       s®   e Zd ZdZeZejjj	Z
dZeƒ Ze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‡ f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rBt ¡  | _	 | _
}|j}|j}| j}|D ]}|| ¡ O }|| ¡ O }qX|�ri | _|  | j¡}|dur¦| j| jt|ƒ< t ¡ | _| jj | j¡ |  | j¡ |jdkrð| jj| j| jd dd� |j| jddd� |  ¡  n(|�r4t ¡ | _| j d¡ |  | j¡ t|j }|du �rN| j}|�sZ|�rÚ|j�rÚt  ||¡}| !dddd¡ | "| j¡ |tj j#j$k�rÌt %¡ }	|	 &tj%j'j(¡ |	 )tj%j*j+¡ | "|	¡ |  ,||¡ nh| j	du �r| jdu �rt ¡ | _| jjt-  | j¡ | j| _	n(t  ||¡}| !dddd¡ |  ,||¡ |�r^t.| j	ƒ}
| j	 /|
¡ | j0|j1d	� |  2|j3d
d¡ dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú Nú.itemsÚui©Údispatchúvalues.itemsTr   )Úviewr   Úfrom)4ÚfactoryÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚeditabler   ÚQWidgetÚcontrolÚ_panelÚ
selectableÚ	droppableÚitemsZis_droppableÚis_selectableÚ_object_cacheÚitem_forÚvalueÚidÚ	QComboBoxÚ_choiceZ	activatedÚconnectÚupdate_objectZset_tooltipÚobserveÚrebuild_itemsZ	QLineEditZsetReadOnlyÚOrientationMapÚorientationÚ
QBoxLayoutÚsetContentsMarginsÚ	addWidgetÚ	DirectionÚTopToBottomZQFrameZsetFrameShapeZShapeZHLineZsetFrameShadowZShadowZSunkenÚcreate_editorÚintr   ZinstallEventFilterZ
trait_setqr   Z
sync_valueZ	view_name)ÚselfÚparentr   r$   r%   r&   Úitemr3   ÚlayoutZhlineZdrop_event_filter© r?   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/instance_editor.pyÚinitL   sr    
ÿ

ÿ







zCustomEditor.initc                 C   s   t  ¡ | _| | j¡ dS )zCreates the editor control.N)r   r!   r#   r6   ©r;   r<   r>   r?   r?   r@   r9   ¢   s    
zCustomEditor.create_editorc                 C   s�   | j dur| j S | j}| jdur0|  ¡ |j }n|j}g }|j}|D ]@}t|tƒs\||d�}|jj| j	t
|jdd�dd� | |¡ qD|| _ |S )z2Gets the current list of InstanceChoiceItem items.N)ÚobjectT)Úoptionalr   r   )Ú_itemsr   r   ÚvaluesÚadapterÚ
isinstancer   rC   r0   r1   r   Z
name_traitÚappend)r;   r   rF   r&   rG   r*   r?   r?   r@   Ú
_get_items§   s&    



ýzCustomEditor._get_itemsNc                 C   s�   d| _ d}| j}| j}| ¡  t| jƒD ],\}}| ¡ r(| | ¡ ¡ | 	|¡r(|}q(|dkrj| 
|¡ n"zd| _| 
d¡ W n   Y n0 dS )z"Rebuilds the object selector list.Néÿÿÿÿr   )rE   r*   r-   ÚclearÚ	enumerater&   r'   ÚaddItemÚget_nameÚis_compatibleÚsetCurrentIndex)r;   Úeventr   r*   ÚchoiceÚir=   r?   r?   r@   r1   Ä   s"    
zCustomEditor.rebuild_itemsc                 C   s"   | j D ]}| |¡r|  S qdS )z6Returns the InstanceChoiceItem for a specified object.N)r&   rP   )r;   rC   r=   r?   r?   r@   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   )r;   rC   r=   r   r?   r?   r@   Úview_forç   s    ÿzCustomEditor.view_forc                 C   s‚   | j | }t|ƒ}| j |¡}|du rp| ¡ }| jjs^|jr^|  ||  	|¡¡}| 
|| jd¡ | jjrp|| j|< || _|  ¡  dS )z:Handles the user selecting a new value from the combo box.NÚmodal)r&   r+   r(   ÚgetZ
get_objectr   r    Z
is_factoryrY   r)   r   r"   Zcachabler*   Úresynch_editor)r;   Úindexr=   Zid_itemrC   r   r?   r?   r@   r/   ô   s    

zCustomEditor.update_objectc                 C   s–   |   ¡  | j}|  | j¡}|dur’|dur|| | j¡}| jdurp| |¡}|dk rd| ¡ }| |¡ | 	|¡ q’| 
|¡ nt|tjƒr’| 	d¡ dS )z[Updates the editor when the object trait changes externally to the
        editor.
        Nr   rK   )r\   r-   r)   r*   rO   r(   ZfindTextÚcountrN   rQ   ÚsetTextrH   r   r,   )r;   rS   r=   r   Úidxr?   r?   r@   Úupdate_editor  s    


zCustomEditor.update_editorc                 C   sV  | j }|du�rR| ¡ }|du r<t |¡}| dddd¡ n<| jdurX| j ¡  d| _n | d¡}|durv| d¡}qb~d}| j}t	|t
ƒsªd}|durž| j}t |¡}nž|  ||  |¡¡}| ¡ }	d}
t	|tƒrÖ|}
|	 d| j¡ |	 d| jj¡ | |	|d| ¡ |
| jj¡ | _}|j}|j| _| j|_|j�sD|j�sD|j�rHd}| |¡ dS )úrResynchronizes the contents of the editor when the object trait
        changes externally to the editor.
        Nr   r   ÚcontextZcontext_handlerZsubpanelr   )r#   r>   r   ZQVBoxLayoutr5   Ú_uiÚdisposeZtakeAtr*   rH   r   Ú	str_valueZQLabelrY   r)   Ztrait_contextr
   Ú
setdefaultrC   r   rU   Ztrait_view_elementsr   r+   r"   Z_scrollableZ
scrollabler<   Z	resizabler6   )r;   Zpanelr>   ÚchildZstretchr*   rf   r"   r   rc   rU   r   r?   r?   r@   r\   !  sR    






ú
zCustomEditor.resynch_editorc                    s|   d| _ | jdur| j ¡  | jdurnt| jtjƒrn| jdurX| jj| j	| j
d ddd� | jj| j	dddd� tƒ  ¡  dS )z&Disposes of the contents of an editor.Nr   Tr   )Úremover   r   )r(   rd   re   r-   rH   r   r,   r   r0   r1   r   r   Úsuper©r;   ©Ú	__class__r?   r@   re   X  s&    



üüzCustomEditor.disposec                 C   s   dS )zDHandles an error that occurs while setting the object's trait value.Nr?   )r;   Zexcpr?   r?   r@   Úerroru  s    zCustomEditor.errorc                 C   s   | j p
| jS )z9Returns the editor's control for indicating error status.)r-   r"   rk   r?   r?   r@   Úget_error_controly  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)rd   r[   r+   Z	set_prefs)r;   rp   r   r?   r?   r@   Ú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+   rp   )rd   r+   Z	get_prefs)r;   r   r?   r?   r@   Ú
save_prefs‡  s    zCustomEditor.save_prefsc                 C   s   |   ¡  d S ©N)r\   )r;   r   r?   r?   r@   Ú_view_changed‘  s    zCustomEditor._view_changed)N) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zok_colorr   r4   r7   r8   r3   Úextrar   r&   r   r   rA   r9   rJ   r1   r)   rY   r/   ra   r\   re   rn   ro   rq   rr   rt   Ú__classcell__r?   r?   rl   r@   r   2   s*   
V
7
r   c                   @   sF   e Zd ZdZedƒZejjj	Z
dZdd„ Zdd„ Zdd	„ Zd
d„ Zd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.UIé   c                 C   sF   t  ¡ | _| j d¡ | | j¡ | jj | j¡ | jj | j	¡ dS )z&Creates the editor control (a button).FN)
r   ZQPushButtonÚ_buttonZsetAutoDefaultr6   Zclickedr.   Úedit_instanceZ	destroyedÚ_parent_closedrB   r?   r?   r@   r9   ¡  s
    
zSimpleEditor.create_editorc                 C   s|   | j }| jj | jj|j| j| j| j¡}| jj	||j
|jd�| _| jjdurrt| jjƒ | jjdu rx| jj| j_nd| _dS )zWEdit the contents of the object trait when the user clicks the
        button.
        )Úkindr+   N)r   r   rU   rV   rW   r   r*   rX   r   Zedit_traitsr€   r+   Ú
_dialog_uir"   r   Úhistory)r;   r   r   r?   r?   r@   r~   ª  s    ÿ
ÿzSimpleEditor.edit_instancec                 C   sH   | j }|durD| jj}|dkr(t| jƒ}| |¡ | t| jt	ƒ¡ dS )rb   Nr   )
r}   r   Úlabelr	   r   r_   Z
setEnabledrH   r*   r   )r;   Úbuttonrƒ   r?   r?   r@   r\   Å  s    

zSimpleEditor.resynch_editorc                 C   s6   | j d ur2| j jd ur"| j j ¡  | j  ¡  d | _ d S rs   )r�   r"   Úclosere   rk   r?   r?   r@   r   Ò  s
    

zSimpleEditor._parent_closedN)ru   rv   rw   rx   r   r�   r   r4   r7   ÚLeftToRightr3   ry   r9   r~   r\   r   r?   r?   r?   r@   r{   •  s   
	r{   )!rx   Z	pyface.qtr   r   Z
traits.apir   r   r   Ztraits.observation.apir   Ztraitsui.ui_traitsr   Ztraitsui.helperr	   Ztraitsui.handlerr
   Ztraitsui.instance_choicer   Zeditorr   Zdrop_editorr   Ú	constantsr   Úhelperr   r4   r7   r†   r8   r2   r   r{   r?   r?   r?   r@   Ú<module>   s&   ý  e