a
    §ˆcæv  ã                   @   sü   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddl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"m#Z# ddl$m%Z%m&Z& dZ'G dd„ deƒZ(G dd„ de)ƒZ*dS )zS Defines the UI class used to represent an active traits-based user
    interface.
é    N)Úwarn)ÚImage)ÚAnyÚBoolÚCallableÚDictÚEventÚHasPrivateTraitsÚInstanceÚIntÚListÚPropertyÚStrÚ
TraitErrorÚobserve)Útraits_homeÚis_stré   )ÚEditor)ÚViewElements)ÚHandlerÚViewHandler)Útoolkit)ÚUIInfo)ÚItem)ÚGroupÚShadowGroup)ZpanelZsubpanelc                   @   s˜  e Zd ZdZeeƒZeee	ƒZ
eeƒZedƒZe	ƒ Zed ƒZe	ƒ ZeeƒZedƒZe	ƒ Zeddgd�Zeƒ ZedƒZeeƒZeƒ ZeZedƒZe ƒ Z!e"ƒ Z#edƒZ$eee	ƒZ%eee	ƒZ&e'ƒ Z(e'ƒ Z)e'ƒ Z*e'ƒ Z+e'ƒ Z,e'ƒ Z-e'ƒ Z.e'ƒ Z/e ƒ Z0eƒ Z1e	ƒ Z2e dƒZ3e"ƒ Z4e'ƒ Z5e	ƒ Z6edƒZ7edƒZ8g d	¢Z9g d
¢Z:dd„ Z;dd„ Z<dadd„Z=dd„ Z>dd„ Z?dbdd„Z@dd„ ZAdd„ ZBdd„ ZCdd „ ZDd!d"„ ZEd#d$„ ZFd%d&„ ZGd'd(„ ZHd)d*„ ZIdcd+d,„ZJddd-d.„ZKded0d1„ZLd2d3„ ZMd4d5„ ZNd6d7„ ZOd8d9„ ZPd:d;„ ZQd<d=„ ZRd>d?„ ZSd@dA„ ZTdfdBdC„ZUdDdE„ ZVdgdFdG„ZWdHdI„ ZXdJdK„ ZYdhdLdM„ZZdidNdO„Z[dPdQ„ Z\dRdS„ Z]dTdU„ Z^dVdW„ Z_dXdY„ Z`dZd[„ Zad\d]„ Zbecd^ƒd_d`„ ƒZddS )jÚUIz0Information about the user interface for a View.ztraitsui.view.ViewFzview._key_bindingsÚcontext)Z
depends_onéÿÿÿÿz!traitsui.key_bindings.KeyBindings)Ú_contextÚ_revertÚ_definedÚ_visibleÚ_enabledÚ_checkedÚ_searchÚ_dispatchersÚ_editorsÚ_namesÚ_active_groupÚ	_undoableÚ_rebuildÚ_groups_cacheÚ_key_bindingsÚ_focus_control)	Úview_elementsÚinfoÚhandlerr   ÚviewÚhistoryÚkey_bindingsÚiconÚrebuildc                 C   s   t | d�| _| j | j¡ dS )zInitializes the traits object.)ÚuiN)r   r1   r2   Z	init_info©Úself© r;   úN/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/ui.pyÚtraits_initÝ   s    zUI.traits_initc                 C   sJ   |du r|t v rd}| jj| jddd� ttƒ d| ƒ| _|  | |¡ dS )zBCreates a user interface from the associated View template object.NZliveÚupdatedr8   ©ÚdispatchZui_)Úkind_must_have_parentr3   Úon_trait_changeÚ_updated_changedÚgetattrr   r7   )r:   ÚparentÚkindr;   r;   r<   r8   â   s    ÿzUI.uiNc                 C   sL   | j dur| j  j| j8  _|dur*|| _| jdurH|s@|  ¡  |  ¡  dS )z-Disposes of the contents of a user interface.N)rE   ÚerrorsÚresultÚcontrolÚ
save_prefsÚfinish)r:   rH   Úabortr;   r;   r<   Údisposeì   s    

z
UI.disposec                 C   s$   | j dd� d| j_|  | j¡ dS )z3Recycles the user interface prior to rebuilding it.F©ÚdestroyN)ÚresetrI   Ú_objectÚreset_traitsÚrecyclable_traitsr9   r;   r;   r<   Úrecycleý   s    z
UI.recyclec                 C   s¢   t ƒ  | j¡ | jdd� | j ¡ D ]}|j| jdd� q$| j 	| j
| j¡ d| j
_d| j_d| _| jdurv| j ¡  | j ¡  |  | j¡ |  | j¡ d| _dS )z'Finishes disposing of a user interface.FrN   T©ÚremoveN)r   Zdestroy_controlrI   rP   r   ÚvaluesrB   Ú_evaluate_whenr2   Úclosedr1   rH   r8   rQ   r.   rM   ÚclearrR   rS   Údisposable_traitsÚ	destroyed)r:   Úobjectr;   r;   r<   rK     s    


z	UI.finishTc                 C   sˆ   | j D ]&}|jdur| j|j_| ¡  d|_q| jD ]\}}}|j||ddd� q4| jdd…= |rptƒ  | j¡ | j	D ]}| 
¡  qvdS )z(Resets the contents of a user interface.NTr8   )rV   r@   )r(   Z_uirH   rM   rI   Ú
_statusbarr   r   Zdestroy_childrenr'   rV   )r:   rO   Úeditorr]   r2   ÚnameÚ
dispatcherr;   r;   r<   rP   -  s    



zUI.resetc                 C   sÎ   | j }d}t|ƒdkr(t| ¡ ƒd }n
| d¡}| j}|du rP|durP| ¡ }|durh| |j| j	¡}|du rÊ| d¡}|duržt
||jdƒ}t|ƒrž|ƒ }|du rÊ|durÊt
||jdƒ}t|ƒrÊ|ƒ }|S )zuFinds the definition of the specified Include object in the current
        user interface building context.
        Nr   r   r]   r2   )r   ÚlenÚlistrW   Úgetr0   Ztrait_view_elementsÚfindÚidr&   rD   Úcallable)r:   Úincluder   rH   r]   Úver2   Úmethodr;   r;   r<   re   F  s*    

zUI.findc                 C   s
   t | jƒS )z'Returns the current search stack level.)rb   r&   r9   r;   r;   r<   Ú
push_levelo  s    zUI.push_levelc                 C   s   | j dt| j ƒ| …= dS )z0Restores a previously pushed search stack level.N)r&   rb   )r:   Úlevelr;   r;   r<   Ú	pop_levels  s    zUI.pop_levelc                 C   s¦  | j jdd�}| jD ]}||ƒ q| jdd…= |  ¡  tƒ  | | jd| j¡ | j}t	|t
ƒrltƒ  | | j¡ | |¡}|du rˆtdƒ‚nt	|tƒs¤td|› d�tƒ | j}|  |¡D ]–}|dd… d	kr´|dd… }| d
d¡}|dkr´| |d|… ¡}	|	dur´t||ƒ}||d d… }
| j t|||	|
ƒ¡ |	 |
¡jdkr´||ƒ q´t| jƒt| jƒ t| jƒ dk�rœ| ¡ D ]}	|	j| j dd� �qx| j!dd� d|_"dS )zYPerforms all processing that occurs after the user interface is
        created.
        F)ÚinitializedNÚkeyszUser interface creation abortedz?Handler.init() must return True or False, but instead returned z1.  This will become an error in a future release.iøÿÿÿZ_changedÚ_r   r   Úeventr8   r?   T©Úat_init)#r1   Z	trait_setr"   Ú	sync_viewr   Zhook_eventsrI   Úkey_handlerr2   Ú
isinstancer   Úinitr   Úboolr   ÚDeprecationWarningr   Z_each_trait_methodre   rd   rD   r'   ÚappendÚ
DispatcherZ
base_traitÚtyperb   r#   r$   r%   rW   rB   rX   Ú_do_evaluate_whenrn   )r:   r1   rj   r2   Ústartedr   r`   ÚprefixÚcolr]   Ú
trait_namer;   r;   r<   Ú
prepare_uiw  sN    





ÿü
ÿ
	$zUI.prepare_uic                 C   sH   | j  ¡ D ]8\}}|  ||dd¡ |  ||dd¡ |  ||dd¡ q
dS )z:Synchronize context object traits with view editor traits.Zsync_to_viewÚfromZsync_from_viewÚtoZsync_with_viewZbothN)r   ÚitemsÚ
_sync_view)r:   r`   r]   r;   r;   r<   rt   À  s    zUI.sync_viewc              
   C   s¾   | j }|jf i |ti¤Ž ¡ D ]š\}}t||ƒ d¡D ]€}zdd„ | d¡D ƒ\}	}
W n   td|||f ƒ‚Y n0 t||	d ƒ}|d ur¢| d||f |
|¡ q6td|	|||f ƒ‚q6qd S )Nú,c                 S   s   g | ]}|  ¡ ‘qS r;   )Ústrip)Ú.0Úitemr;   r;   r<   Ú
<listcomp>Ì  s   z!UI._sync_view.<locals>.<listcomp>Ú.ztThe '%s' metadata for the '%s' trait in the '%s' context object should be of the form: 'id1.trait1[,...,idn.traitn].z%s.%szgNo editor with id = '%s' was found for the '%s' metadata for the '%s' trait in the '%s' context object.)r1   Ztraitsr   r…   rD   Úsplitr   Z
sync_value)r:   r`   r]   ÚmetadataÚ	directionr1   r�   ÚtraitÚsyncZ	editor_idZeditor_namer_   r;   r;   r<   r†   Ç  s.     ÿýÿ
ÿ
ýÿzUI._sync_viewc                 C   sN   |  d¡}t|ƒdkr,| j|d  }|d= n
| jd }|D ]}t||ƒ}q:|S )z:Gets the current value of a specified extended trait name.rŒ   r   r   r]   )r�   rb   r   rD   )r:   r`   ÚnamesÚvaluer;   r;   r<   Úget_extended_valueä  s    

zUI.get_extended_valuec                 C   sN   | j }|dkrJ|  ¡ }|durJz| |¡}| ¡  |  |¡W S    Y n0 dS )zeRetrieves and restores any saved user preference information
        associated with the UI.
        Ú N)rf   Ú	get_ui_dbrd   ÚcloseÚ	set_prefs)r:   rf   ÚdbÚui_prefsr;   r;   r<   Úrestore_prefsò  s    
zUI.restore_prefsc                 C   sŽ   t |tƒrŠ| j}| jD ]@}t||dƒ}t |tƒr|j| u r| |¡}|dur| |¡ q| j	dur€| d¡}|dur€| j	 
|¡ | d¡S dS )z/Sets the values of user preferences for the UI.Nú$r•   )rv   Údictr1   r)   rD   r   r8   rd   r›   r5   Úmerge)r:   Úprefsr1   r`   r_   Zeditor_prefsr5   r;   r;   r<   r˜     s    





zUI.set_prefsc                 C   sT   |du rt ƒ  | ¡ dS | j}|dkrP| jdd�}|durP|  |¡||< | ¡  dS )z=Saves any user preference information associated with the UI.Nr•   Úc)Úmode)r   Zsave_windowrf   r–   Ú	get_prefsr—   )r:   rŸ   rf   r™   r;   r;   r<   rJ     s    zUI.save_prefsc                 C   sv   i }|dur||d< | j dur(| j |d< | j}| jD ]<}t||dƒ}t|tƒr4|j| u r4| ¡ }|dur4|||< q4|S )z8Gets the preferences to be saved for the user interface.Nr•   rœ   )r5   r1   r)   rD   rv   r   r8   rJ   )r:   rŸ   rš   r1   r`   r_   r;   r;   r<   r¢   $  s    



zUI.get_prefsÚrc                 C   s2   zt jtj tƒ d¡|dd�W S    Y dS 0 dS )z9Returns a reference to the Traits UI preference database.Z	traits_uir   )ÚflagÚprotocolN)ÚshelveÚopenÚosÚpathÚjoinr   )r:   r¡   r;   r;   r<   r–   7  s    ýzUI.get_ui_dbc                    s   ‡ fdd„| j D ƒS )z3Returns a list of editors for the given trait name.c                    s   g | ]}|j ˆ kr|‘qS r;   ©r`   )r‰   r_   r«   r;   r<   r‹   D  ó    z"UI.get_editors.<locals>.<listcomp>)r(   )r:   r`   r;   r«   r<   Úget_editorsB  s    zUI.get_editorsc                 C   s<   g }| j D ],}| ¡ }t|tƒr,| |¡ q
| |¡ q
|S )z[Returns the list of editor error controls contained by the user
        interface.
        )r(   Zget_error_controlrv   rc   Úextendrz   )r:   Úcontrolsr_   rI   r;   r;   r<   Úget_error_controlsF  s    

zUI.get_error_controlsc                 C   s   | j  |¡ dS )zxAdds a Handler method to the list of methods to be called once the
        user interface has been constructed.
        N)r"   rz   )r:   rj   r;   r;   r<   Úadd_definedT  s    zUI.add_definedc                 C   s.   z| j  t|ddƒ|f¡ W n   Y n0 dS )zlAdds a conditionally enabled Editor object to the list of monitored
        'visible_when' objects.
        ú<string>ÚevalN)r#   rz   Úcompile)r:   Zvisible_whenr_   r;   r;   r<   Úadd_visibleZ  s    ÿzUI.add_visiblec                 C   s.   z| j  t|ddƒ|f¡ W n   Y n0 dS )zlAdds a conditionally enabled Editor object to the list of monitored
        'enabled_when' objects.
        r²   r³   N)r$   rz   r´   )r:   Zenabled_whenr_   r;   r;   r<   Úadd_enabledf  s    ÿzUI.add_enabledc                 C   s.   z| j  t|ddƒ|f¡ W n   Y n0 dS )zsAdds a conditionally enabled (menu) Editor object to the list of
        monitored 'checked_when' objects.
        r²   r³   N)r%   rz   r´   )r:   Zchecked_whenr_   r;   r;   r<   Úadd_checkedr  s    ÿzUI.add_checkedc                 O   sX   | j }z<|dkr$| jdur$| jj| _ ||i |¤Ž W |dkrTd| _ n|dkrRd| _ 0 dS )z&Performs an action that can be undone.r   N)r+   r4   Únow)r:   ÚactionÚargsÚkwZundoabler;   r;   r<   Údo_undoable~  s    
ÿzUI.do_undoablec                 C   s   t ƒ  | |¡ dS )z6Routes a "hooked" event to the correct handler method.N)r   Úroute_event)r:   rq   r;   r;   r<   r½   Š  s    zUI.route_eventc                 C   s\   | j }|duo&|j|g | j| jdu d�}|sD| jdurD| j |d¡}|sX|rXtƒ  |¡ |S )zHandles key events.N)Ú	recursiveF)r5   Zdor1   rE   ru   r   Z
skip_event)r:   rq   Úskipr5   Zhandledr;   r;   r<   ru   Ž  s    ÿzUI.key_handlerc                 O   sV   |du rdS t |ƒr"||i |¤ŽS | j ¡ }| |d< | j|d< t|tƒ |ƒ|i |¤ŽS )z7Evaluates a specified function in the UI's **context**.Nr8   r2   )rg   r   Úcopyr2   r³   Úglobals)r:   Úfunctionrº   Zkw_argsr   r;   r;   r<   Úevaluate�  s    

zUI.evaluatec                 C   sH   |   | j¡}zt|tƒ |ƒ}W n   ddlm} |ƒ  Y n0 |d= |S )zXEvaluates an expression in the UI's **context** and returns the
        result.
        r   ©Úraise_to_debugr8   )Ú_get_contextr   r³   rÁ   Útraitsui.apirÅ   )r:   ÚwhenrH   r   rÅ   r;   r;   r<   Ú	eval_whenª  s    zUI.eval_whenc                 C   sŽ   d}t |ƒ}|dkr<d|v r<| ¡ D ]\}}|dkr$ qTq$n|dkrTt| ¡ ƒd }| |¡}|durz| ¡ }| |¡ n| ¡ }| |d< |S )z5Gets the context to use for evaluating an expression.r]   é   r2   r   r   Nr8   )rb   r…   rc   ro   rd   Z	trait_getÚupdaterÀ   )r:   r   r`   Únr“   Zcontext2r;   r;   r<   rÆ   º  s    
zUI._get_contextc                 C   s   | j dd� dS )z¨Set the 'visible', 'enabled', and 'checked' states for all Editors
        controlled by a 'visible_when', 'enabled_when' or 'checked_when'
        expression.
        Frr   N)r}   r9   r;   r;   r<   rX   Ð  s    zUI._evaluate_whenc                 C   s4   |   | jd|¡ |   | jd|¡ |   | jd|¡ dS )a  Set the 'visible', 'enabled', and 'checked' states for all Editors.

        This function does the job of _evaluate_when. We define it here to
        work around the traits dispatching mechanism that automatically
        determines the number of arguments of a notification method.

        :attr:`at_init` is set to true when this function is called the first
        time at initialization. In that case, we want to force the state of
        the items to be set (normally it is set only if it changes).
        ZvisibleZenabledÚcheckedN)Ú_evaluate_conditionr#   r$   r%   )r:   rs   r;   r;   r<   r}   ×  s    zUI._do_evaluate_whenc              	   C   sÀ   |   | j¡}g }g }|D ]v\}}zHt|tƒ |ƒ}	t||ƒ}
|	rP|sF|
sP| |¡ |	sf|s\|
rf| |¡ W q tyŒ   ddlm} |ƒ  Y q0 q|D ]}t	||dƒ q”|D ]}t	||dƒ qªdS )a—  Evaluates a list of (eval, editor) pairs and sets a specified trait
        on each editor to reflect the Boolean value of the expression.

        1) All conditions are evaluated
        2) The elements whose condition evaluates to False are updated
        3) The elements whose condition evaluates to True are updated

        E.g., we first make invisible all elements for which 'visible_when'
        evaluates to False, and then we make visible the ones
        for which 'visible_when' is True. This avoids mutually exclusive
        elements to be visible at the same time, and thus making a dialog
        unnecessarily large.

        The state of an editor is updated only when it changes, unless
        at_init is set to True.

        Parameters
        ----------
        conditions : list of (str, Editor) tuple
            A list of tuples, each formed by 1) a string that contains a
            condition that evaluates to either True or False, and
            2) the editor whose state depends on the condition

        trait : str
            The trait that is set by the condition.
            Either 'visible, 'enabled', or 'checked'.

        at_init : bool
            If False, the state of an editor is set only when it changes
            (e.g., a visible element would not be updated to visible=True
            again). If True, the state is always updated (used at
            initialization).
        r   rÄ   FTN)
rÆ   r   r³   rÁ   rD   rz   Ú	ExceptionrÇ   rÅ   Úsetattr)r:   Z
conditionsr�   rs   r   ÚactivateZ
deactivaterÈ   r_   Z
cond_valueZeditor_staterÅ   r;   r;   r<   rÎ   æ  s$    #

zUI._evaluate_conditionc                 C   s\   | j du rV| jj | ¡}| ¡ | _ | j D ],}t|tƒr(tt| j Ž | j dd�g| _  qVq(| j S )z|Returns the top-level Groups for the view (after resolving
        Includes. (Implements the **_groups** property.)
        Nr   )ZshadowÚcontentÚgroups)	r-   r3   rÒ   Z
get_shadowÚget_contentrv   r   r   r   )r:   Zshadow_grouprŠ   r;   r;   r<   Ú_get__groups*  s    



ýÿzUI._get__groupsc                 C   st   | j d u rn| j| j }}|d u s(|d u r,d S t| ¡ ƒ}|j}|d u r`ddlm} ||d�| _ n|j|d�| _ | j S )Nr   )ÚKeyBindings)Zcontrollers)r.   r3   r   rc   rW   r5   rÖ   Úclone)r:   r3   r   rW   r5   rÖ   r;   r;   r<   Ú_get_key_bindings?  s    
zUI._get_key_bindingsc                 C   s   | j r| j j| | | j _d S ©N)rE   rG   ©r:   r`   ÚoldÚnewr;   r;   r<   Ú_errors_changedU  s    zUI._errors_changedc                 C   s4   |d ur| j | j 8  _ |d ur0| j | j 7  _ d S rÙ   )rG   rÚ   r;   r;   r<   Ú_parent_changedY  s    zUI._parent_changedc                 C   s   | j d urtƒ  | ¡ d S rÙ   )r7   r   Z
rebuild_uir9   r;   r;   r<   rC   _  s    
zUI._updated_changedc                 C   s   | j d urtƒ  | ¡ d S rÙ   )rI   r   Ú	set_titler9   r;   r;   r<   Ú_title_changedc  s    
zUI._title_changedc                 C   s   | j d urtƒ  | ¡ d S rÙ   )rI   r   Zset_iconr9   r;   r;   r<   Ú_icon_changedg  s    
zUI._icon_changedzparent, view, contextc                 C   sH   | j }|d urD| jd urD| jd u r*|j| _|jd urD|jj | j¡ d S rÙ   )rE   r5   r4   Úchildrenrz   )r:   rq   rE   r;   r;   r<   Ú_pvc_changedk  s    

zUI._pvc_changed)NF)T)N)N)r£   )T)T)F)F)eÚ__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r0   r   r   r   r   r   r2   r3   rI   rE   Úownerr   r1   r   rH   r4   r   r5   rf   Úmodifiedr   r>   Útitler   r6   Z
scrollabler   rG   r   r7   r\   r    r!   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   Z_groupsr-   r+   r,   r^   r/   Z_scrollabler.   rS   r[   r=   r8   rM   rT   rK   rP   re   rk   rm   r‚   rt   r†   r”   r›   r˜   rJ   r¢   r–   r­   r°   r±   rµ   r¶   r·   r¼   r½   ru   rÃ   rÉ   rÆ   rX   r}   rÎ   rÕ   rØ   rÝ   rÞ   rC   rà   rá   r   rã   r;   r;   r;   r<   r   9   s¨   




%
)I






Dr   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )r{   c                 C   s.   || _ || _|| _|| _|j| j|dd� dS )zInitializes the object.r8   r?   N)rj   r1   r]   Úmethod_namerB   r@   )r:   rj   r1   r]   rë   r;   r;   r<   Ú__init__z  s
    zDispatcher.__init__c                 C   s   |   | j¡ dS )zDispatches the method.N)rj   r1   r9   r;   r;   r<   r@   ‚  s    zDispatcher.dispatchc                 C   s   | j j| j| jdd� dS )zRemoves the dispatcher.TrU   N)r]   rB   r@   rë   r9   r;   r;   r<   rV   †  s    
ÿzDispatcher.removeN)rä   rå   ræ   rì   r@   rV   r;   r;   r;   r<   r{   y  s   r{   )+rç   r¦   r¨   Úwarningsr   Zpyface.ui_traitsr   Z
traits.apir   r   r   r   r   r	   r
   r   r   r   r   r   r   Ztraits.trait_baser   r   r_   r   r0   r   r2   r   r   r   Zui_infor   rŠ   r   Úgroupr   r   rA   r   r]   r{   r;   r;   r;   r<   Ú<module>   s,   <      F