3
8Ad]
–  ã               @   sô   d Z ddlmZ ddlZddl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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.
é    )Úabsolute_importN)ÚAnyÚBoolÚCallableÚ
DictStrAnyÚEventÚHasPrivateTraitsÚInstanceÚIntÚListÚPropertyÚStrÚ
TraitErrorÚon_trait_changeÚproperty_depends_on)Útraits_homeÚis_stré   )ÚEditor)ÚViewElements)ÚHandlerÚViewHandler)Útoolkit)ÚUIInfo)ÚItem)ÚGroupÚShadowGroupÚpanelÚsubpanelc               @   sp  e Zd ZdZeeƒZ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Z$eZ%e&Z'e&Z(e&Z)e&Z*e&Z+e&Z,e&Z-e&Z.eZ/eZ0eZ1edrƒZ2e!Z3e&Z4edƒZ5edƒZ6d	d
dddddddddddddgZ7dddddddddg	Z8d d!„ Z9d"d#„ Z:dsd%d&„Z;d'd(„ Z<d)d*„ Z=dtd,d-„Z>d.d/„ Z?d0d1„ Z@d2d3„ ZAd4d5„ ZBd6d7„ ZCd8d9„ ZDd:d;„ ZEd<d=„ ZFd>d?„ ZGdud@dA„ZHdvdBdC„ZIdwdEdF„ZJdGdH„ ZKdIdJ„ ZLdKdL„ ZMdMdN„ ZNdOdP„ ZOdQdR„ ZPdSdT„ ZQdUdV„ ZRdxdWdX„ZSdYdZ„ ZTdyd[d\„ZUd]d^„ ZVd_d`„ ZWdzdadb„ZXd{dcdd„ZYdedf„ ZZdgdh„ Z[didj„ Z\dkdl„ Z]dmdn„ Z^e_doƒdpdq„ ƒZ`d$S )|ÚUIz6 Information about the user interface for a View.
    ztraitsui.view.ViewFzview._key_bindingsÚcontext)Z
depends_onr   z!traitsui.key_bindings.KeyBindingsÚ_contextÚ_revertÚ_definedÚ_visibleÚ_enabledÚ_checkedÚ_searchÚ_dispatchersÚ_editorsÚ_namesÚ_active_groupÚ	_undoableÚ_rebuildÚ_groups_cacheÚ_key_bindingsÚview_elementsÚinfoÚhandlerÚviewÚhistoryÚkey_bindingsÚiconÚrebuildc             C   s   t | d�| _| jj| jƒ dS )z( Initializes the traits object.
        )ÚuiN)r   r1   r2   Z	init_info)Úself© r:   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/ui.pyÚtraits_initÛ   s    zUI.traits_initc             C   sJ   |dkr|t krd}| jj| jddd� ttƒ d| ƒ| _| j| |ƒ dS )zL Creates a user interface from the associated View template object.
        NZliveÚupdatedr8   )ÚdispatchZui_)Úkind_must_have_parentr3   r   Ú_updated_changedÚgetattrr   r7   )r9   ÚparentÚkindr:   r:   r;   r8   å   s    zUI.uiNc             C   s0   |dk	r|| _ | jdk	r,|s$| jƒ  | jƒ  dS )z7 Disposes of the contents of a user interface.
        N)ÚresultÚcontrolÚ
save_prefsÚfinish)r9   rD   Úabortr:   r:   r;   Údisposeó   s    
z
UI.disposec             C   s$   | j dd� d| j_| j| jƒ dS )z= Recycles the user interface prior to rebuilding it.
        F)ÚdestroyN)ÚresetrE   Ú_objectÚreset_traitsÚrecyclable_traits)r9   r:   r:   r;   Úrecycle  s    z
UI.recyclec             C   s¦   | j dd� x"| jjƒ D ]}|j| jdd� qW | jj| j| jƒ d| j_	d| j
_tƒ j| j
ƒ d| _
| jdk	rz| jjƒ  | jjƒ  | j| jƒ | j| jƒ d| _dS )z1 Finishes disposing of a user interface.
        F)rJ   T)ÚremoveN)rK   r    Úvaluesr   Ú_evaluate_whenr2   Úclosedr1   rD   r8   rE   rL   r   Zdestroy_controlr/   rI   ÚclearrM   rN   Údisposable_traitsÚ	destroyed)r9   Úobjectr:   r:   r;   rG     s    


z	UI.finishTc             C   s’   x0| j D ]&}|jdk	r | j|j_|jƒ  d|_qW x$| jD ]\}}}|j||dd� q:W | jdd…= |rvtƒ j| jƒ x| j	D ]}|j
ƒ  q~W dS )z2 Resets the contents of a user interface.
        NT)rP   )r)   Z_uirD   rI   rE   Ú
_statusbarr   r   Zdestroy_childrenr(   rP   )r9   rJ   ÚeditorrW   r2   ÚnameÚ
dispatcherr:   r:   r;   rK   >  s    


zUI.resetc             C   sÎ   | j }d}t|ƒdkr(t|jƒ ƒd }n
|jdƒ}| j}|dkrP|dk	rP|jƒ }|dk	rh|j|j| j	ƒ}|dkrÊ|jdƒ}|dk	ržt
||jdƒ}t|ƒrž|ƒ }|dkrÊ|dk	rÊt
||jdƒ}t|ƒrÊ|ƒ }|S )zz Finds the definition of the specified Include object in the current
            user interface building context.
        Nr   r   rW   r2   )r    ÚlenÚlistrQ   Úgetr0   Ztrait_view_elementsÚfindÚidr'   rA   Úcallable)r9   Úincluder    rD   rW   Úver2   Úmethodr:   r:   r;   r_   ]  s*    

zUI.findc             C   s
   t | jƒS )z1 Returns the current search stack level.
        )r\   r'   )r9   r:   r:   r;   Ú
push_levelŠ  s    zUI.push_levelc             C   s   | j dt| j ƒ| …= dS )z: Restores a previously pushed search stack level.
        N)r'   r\   )r9   Úlevelr:   r:   r;   Ú	pop_level“  s    zUI.pop_levelc       
      C   s�  | j jdd�}x| jD ]}||ƒ qW | jdd…= | jƒ  tƒ j| | jd| jƒ | j}t	|t
ƒrptƒ j| | jƒ |j|ƒdkr†tdƒ‚| j}x¤| j|ƒD ]–}|dd… dkr˜|dd… }|jdd	ƒ}|d
kr˜|j|d|… ƒ}|dk	r˜t||ƒ}||d	 d… }	| jjt||||	ƒƒ |j|	ƒjdkr˜||ƒ q˜W t| jƒt| jƒ t| jƒ d
k�r†x"|jƒ D ]}|j| jdd� �q`W | jdd� d|_dS )z^ Performs all processing that occurs after the user interface is
            created.
        F)ÚinitializedNÚkeyszUser interface creation abortedé   Z_changedÚ_r   r   Úeventr8   )r>   T)Úat_initiøÿÿÿiøÿÿÿ) r1   Z	trait_setr#   Ú	sync_viewr   Zhook_eventsrE   Úkey_handlerr2   Ú
isinstancer   Úinitr   r    Z_each_trait_methodr_   r^   rA   r(   ÚappendÚ
DispatcherZ
base_traitÚtyper\   r$   r%   r&   rQ   r   rR   Ú_do_evaluate_whenrh   )
r9   r1   rd   r2   r    rZ   ÚprefixÚcolrW   Ú
trait_namer:   r:   r;   Ú
prepare_uiœ  s>    


zUI.prepare_uic             C   sL   xF| j jƒ D ]8\}}| j||ddƒ | j||ddƒ | j||ddƒ qW dS )zD 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)r9   rZ   rW   r:   r:   r;   rn   â  s    zUI.sync_viewc             C   sÂ   | j }x¶|jf |tiŽjƒ D ]ž\}}x”t||ƒjdƒD ]€}ydd„ |jdƒD ƒ\}	}
W n   td|||f ƒ‚Y nX t||	d ƒ}|d k	r¢|jd||f |
|ƒ q6td|	|||f ƒ‚q6W qW d S )Nú,c             S   s   g | ]}|j ƒ ‘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   Útraitsr   r|   rA   Úsplitr   Z
sync_value)r9   rZ   rW   ÚmetadataÚ	directionr1   rx   ÚtraitÚsyncZ	editor_idZeditor_namerY   r:   r:   r;   r}   ê  s"    
zUI._sync_viewc             C   sR   |j dƒ}t|ƒdkr,| j|d  }|d= n
| jd }x|D ]}t||ƒ}q<W |S )zD Gets the current value of a specified extended trait name.
        rƒ   r   r   rW   )r…   r\   r    rA   )r9   rZ   ÚnamesÚvaluer:   r:   r;   Úget_extended_value  s    


zUI.get_extended_valuec          	   C   sL   | j }|dkrH| jƒ }|dk	rHy|j|ƒ}|jƒ  | j|ƒS    Y nX dS )zf Retrieves and restores any saved user preference information
        associated with the UI.
        Ú N)r`   Ú	get_ui_dbr^   ÚcloseÚ	set_prefs)r9   r`   ÚdbÚui_prefsr:   r:   r;   Úrestore_prefs  s    

zUI.restore_prefsc             C   s’   t |tƒrŽ| j}xJ| jD ]@}t||dƒ}t |tƒr|j| kr|j|ƒ}|dk	r|j|ƒ qW | j	dk	r„|jdƒ}|dk	r„| j	j
|ƒ |jdƒS dS )z9 Sets the values of user preferences for the UI.
        Nú$r�   )rp   Údictr1   r*   rA   r   r8   r^   r“   r5   Úmerge)r9   Úprefsr1   rZ   rY   Zeditor_prefsr5   r:   r:   r;   r�   /  s    




zUI.set_prefsc             C   sT   |dkrt ƒ j| ƒ dS | j}|dkrP| jdd�}|dk	rP| j|ƒ||< |jƒ  dS )zG Saves any user preference information associated with the UI.
        Nr�   Úc)Úmode)r   Zsave_windowr`   rŽ   Ú	get_prefsr�   )r9   r—   r`   r‘   r:   r:   r;   rF   H  s    zUI.save_prefsc             C   sz   i }|dk	r||d< | j dk	r(| j |d< | j}xF| jD ]<}t||dƒ}t|tƒr6|j| kr6|jƒ }|dk	r6|||< q6W |S )zB Gets the preferences to be saved for the user interface.
        Nr�   r”   )r5   r1   r*   rA   rp   r   r8   rF   )r9   r—   r’   r1   rZ   rY   r:   r:   r;   rš   Z  s    

zUI.get_prefsÚrc             C   s,   yt jtjjtƒ dƒ|dd�S    dS dS )zC Returns a reference to the Traits UI preference database.
        Z	traits_uir   )ÚflagÚprotocolNéÿÿÿÿ)ÚshelveÚopenÚosÚpathÚjoinr   )r9   r™   r:   r:   r;   rŽ   r  s
    
zUI.get_ui_dbc                s   ‡ fdd„| j D ƒS )z= Returns a list of editors for the given trait name.
        c                s   g | ]}|j ˆ kr|‘qS r:   )rZ   )r€   rY   )rZ   r:   r;   r‚   ‚  s    z"UI.get_editors.<locals>.<listcomp>)r)   )r9   rZ   r:   )rZ   r;   Úget_editors  s    zUI.get_editorsc             C   s@   g }x6| j D ],}|jƒ }t|tƒr.|j|ƒ q|j|ƒ qW |S )z` Returns the list of editor error controls contained by the user
            interface.
        )r)   Zget_error_controlrp   r]   Úextendrr   )r9   ZcontrolsrY   rE   r:   r:   r;   Úget_error_controls‰  s    
zUI.get_error_controlsc             C   s   | j j|ƒ dS )z} Adds a Handler method to the list of methods to be called once the
            user interface has been constructed.
        N)r#   rr   )r9   rd   r:   r:   r;   Úadd_definedœ  s    zUI.add_definedc             C   s.   y| j jt|ddƒ|fƒ W n   Y nX dS )zq Adds a conditionally enabled Editor object to the list of monitored
            'visible_when' objects.
        z<string>ÚevalN)r$   rr   Úcompile)r9   Zvisible_whenrY   r:   r:   r;   Úadd_visible§  s
    zUI.add_visiblec             C   s.   y| j jt|ddƒ|fƒ W n   Y nX dS )zq Adds a conditionally enabled Editor object to the list of monitored
            'enabled_when' objects.
        z<string>r¨   N)r%   rr   r©   )r9   Zenabled_whenrY   r:   r:   r;   Úadd_enabled·  s
    zUI.add_enabledc             C   s.   y| j jt|ddƒ|fƒ W n   Y nX dS )zx Adds a conditionally enabled (menu) Editor object to the list of
            monitored 'checked_when' objects.
        z<string>r¨   N)r&   rr   r©   )r9   Zchecked_whenrY   r:   r:   r;   Úadd_checkedÇ  s
    zUI.add_checkedc             O   sF   | j }z*|dkr$| jdk	r$| jj| _ |||Ž W d|dkr@d| _ X dS )z0 Performs an action that can be undone.
        r   Nrž   rž   rž   )r,   r4   Únow)r9   ÚactionÚargsÚkwZundoabler:   r:   r;   Údo_undoableÖ  s    
zUI.do_undoablec             C   s   t ƒ j| |ƒ dS )z@ Routes a "hooked" event to the correct handler method.
        N)r   Úroute_event)r9   rl   r:   r:   r;   r²   ç  s    zUI.route_eventc             C   s`   | j }|dk	o&|j|g | j| jdkd�}| rF| jdk	rF| jj|dƒ}| r\|r\tƒ j|ƒ |S )z Handles key events.
        N)Ú	recursiveF)r5   Zdor1   rB   ro   r   Z
skip_event)r9   rl   Úskipr5   Zhandledr:   r:   r;   ro   ð  s    
zUI.key_handlerc             O   sN   |dkrdS t |ƒr|||ŽS | jjƒ }| |d< | j|d< t|tƒ |ƒ||ŽS )zA Evaluates a specified function in the UI's **context**.
        Nr8   r2   )ra   r    Úcopyr2   r¨   Úglobals)r9   Úfunctionr¯   Zkw_argsr    r:   r:   r;   Úevaluate  s    


zUI.evaluatec          
   C   sH   | j | jƒ}yt|tƒ |ƒ}W n   ddlm} |ƒ  Y nX |d= |S )z] Evaluates an expression in the UI's **context** and returns the
            result.
        r   )Úraise_to_debugr8   )Ú_get_contextr    r¨   r¶   Útraitsui.apir¹   )r9   ÚwhenrD   r    r¹   r:   r:   r;   Ú	eval_when  s    zUI.eval_whenc             C   s�   d}t |ƒ}|dkr>d|kr>x8|jƒ D ]\}}|dkr&P q&W n|dkrVt|jƒ ƒd }|j|ƒ}|dk	r||jƒ }|j|ƒ n|jƒ }| |d< |S )z? Gets the context to use for evaluating an expression.
        rW   é   r2   r   r   Nr8   )r\   r|   r]   ri   r^   Z	trait_getÚupdaterµ   )r9   r    rZ   Ú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.
        F)rm   N)ru   )r9   r:   r:   r;   rR   F  s    zUI._evaluate_whenc             C   s4   | j | jd|ƒ | j | jd|ƒ | j | 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visibleÚenabledÚcheckedN)Ú_evaluate_conditionr$   r%   r&   )r9   rm   r:   r:   r;   ru   M  s    zUI._do_evaluate_whenc             C   sÒ   | j | jƒ}g }g }x„|D ]|\}}yLt|tƒ |ƒ}	t||ƒ}
|	rT|sJ|
 rT|j|ƒ |	 rl|sb|
rl|j|ƒ W q tk
r”   ddlm} |ƒ  Y qX qW x|D ]}t	||dƒ q W x|D ]}t	||dƒ qºW 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¶   rA   rr   Ú	Exceptionr»   r¹   Úsetattr)r9   Z
conditionsrˆ   rm   r    ÚactivateZ
deactivater¼   rY   Z
cond_valueZeditor_stater¹   r:   r:   r;   rÃ   a  s$    #



zUI._evaluate_conditionc             C   s^   | j dkrX| jjj| ƒ}|jƒ | _ x4| j D ]*}t|tƒr*tt| j Ž | j dd�g| _ P q*W | 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_contentrp   r   r   r   )r9   Zshadow_groupr�   r:   r:   r;   Ú_get__groups©  s    



zUI._get__groupsc             C   st   | j d krn| j| j }}|d ks(|d kr,d S t|jƒ ƒ}|j}|d kr`ddlm} ||d�| _ n|j|d�| _ | j S )Nr   )ÚKeyBindings)Zcontrollers)r/   r3   r    r]   rQ   r5   rË   Úclone)r9   r3   r    rQ   r5   rË   r:   r:   r;   Ú_get_key_bindings¼  s    
zUI._get_key_bindingsc             C   s   | j d k	rtƒ j| ƒ d S )N)r7   r   Z
rebuild_ui)r9   r:   r:   r;   r@   Ñ  s    
zUI._updated_changedc             C   s   | j d k	rtƒ j| ƒ d S )N)rE   r   Z	set_title)r9   r:   r:   r;   Ú_title_changedÕ  s    
zUI._title_changedc             C   s   | j d k	rtƒ j| ƒ d S )N)rE   r   Zset_icon)r9   r:   r:   r;   Ú_icon_changedÙ  s    
zUI._icon_changedzparent, view, contextc             C   sH   | j }|d k	rD| jd k	rD| jd kr*|j| _|jd k	rD|jjj| jƒ d S )N)rB   r5   r4   Úchildrenrr   )r9   rB   r:   r:   r;   Ú_pvc_changedÝ  s    

zUI._pvc_changedrž   )NF)T)N)N)r›   )T)T)F)F)aÚ__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r0   r   r    r   r2   r3   r   rE   rB   Úownerr   r1   r   rD   r4   r   r5   r   r`   Úmodifiedr   r=   Útitler6   Z
scrollabler
   Úerrorsr   r7   rV   r!   r"   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   Z_groupsr.   r,   r-   rX   Z_scrollabler/   rN   rU   r<   r8   rI   rO   rG   rK   r_   re   rg   ry   rn   r}   rŒ   r“   r�   rF   rš   rŽ   r¤   r¦   r§   rª   r«   r¬   r±   r²   ro   r¸   r½   rº   rR   ru   rÃ   rÊ   rÍ   r@   rÎ   rÏ   r   rÑ   r:   r:   r:   r;   r   K   s¦   



(
-		F



	



Hr   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )rs   c             C   s.   || _ || _|| _|| _|j| j|dd� dS )z! Initializes the object.
        r8   )r>   N)rd   r1   rW   Úmethod_namer   r>   )r9   rd   r1   rW   rÚ   r:   r:   r;   Ú__init__õ  s
    zDispatcher.__init__c             C   s   | j | jƒ dS )z  Dispatches the method.
        N)rd   r1   )r9   r:   r:   r;   r>     s    zDispatcher.dispatchc             C   s   | j j| j| jdd� dS )z! Removes the dispatcher.
        T)rP   N)rW   r   r>   rÚ   )r9   r:   r:   r;   rP     s    zDispatcher.removeN)rÒ   rÓ   rÔ   rÛ   r>   rP   r:   r:   r:   r;   rs   ï  s   	rs   )r   r   )*rÕ   Ú
__future__r   rŸ   r¡   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   r   r   r   r   Ztraits.trait_baser   r   rY   r   r0   r   r2   r   r   r   Zui_infor   r�   r   Úgroupr   r   r?   r   rW   rs   r:   r:   r:   r;   Ú<module>   s,   @       +