3
8Ad]äM  ã               @   s„   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZmZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdS )z* Defines the base class for PyQt editors.
é    )Úabsolute_import)ÚQtGui)Ú	HasTraitsÚInstanceÚStrÚCallable)ÚEditoré   )ÚOKColorÚ
ErrorColorc               @   s¶   e Zd Z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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S ).r   z7 Base class for PyQt editors for Traits-based UIs.
    c             C   s6   | j jƒ }x&|jdƒ}|dkr P |jƒ jdƒ qW dS )z4 Delete the contents of a control's layout.
        r   N)ÚcontrolÚlayoutZtakeAtÚwidgetZ	setParent)Úselfr   Úitm© r   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/qt4/editor.pyÚclear_layout)   s    

zEditor.clear_layoutc             C   s   |dk	r| |_ dS )z2 Handles the **control** trait being set.
        N)Z_editor)r   r   r   r   r   Ú_control_changed8   s    zEditor._control_changedc             C   s   | j dk	r| j jƒ  dS )zB Assigns focus to the editor's underlying toolkit widget.
        N)r   ZsetFocus)r   r   r   r   Ú	set_focusC   s    
zEditor.set_focusc             C   s$   | j }| jjƒ |kr | jj|ƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)Z	str_valuer   ÚtextZsetText)r   Ú	new_valuer   r   r   Úupdate_editorM   s    zEditor.update_editorc             C   s>   t | jtjƒr| jjƒ }n| j}tjj|| jd t|ƒƒ dS )zN Handles an error that occurs while setting the object's trait value.
        z value errorN)	Ú
isinstancer   r   ZQLayoutZparentWidgetZQMessageBoxZinformationÚdescriptionÚstr)r   Zexcpr   r   r   r   ÚerrorY   s
    zEditor.errorNc             C   sf   | j }|dkrJ| jj| jƒj}|dkrJ| jj| jƒj}|dkrBdS d| }|dkrX| j}|j|ƒ dS )z3 Sets the tooltip for a specified control.
        Ú NFz
Specifies T)r   ÚobjectZ
base_traitÚnameZtooltipÚdescr   Z
setToolTip)r   r   r    r   r   r   Úset_tooltipi   s    
zEditor.set_tooltipc             C   s2   | j dk	r| j| j |ƒ | jdk	r.| jj|ƒ dS )zCHandles the **enabled** state of the editor being changed.
        N)r   Ú_enabled_changed_helperÚlabel_controlÚ
setEnabled)r   Úenabledr   r   r   Ú_enabled_changed�   s    

zEditor._enabled_changedc             C   sT   t |tjƒr|j|ƒ n8x6t|jƒ ƒD ]&}|j|ƒ}| j|jƒ pF|j	ƒ |ƒ q&W dS )zlA helper that allows the control to be a layout and recursively
           manages all its widgets.
        N)
r   r   ÚQWidgetr$   ÚrangeÚcountÚitemAtr"   r   r   )r   r   r%   Úir   r   r   r   r"   ‰   s    
zEditor._enabled_changed_helperc             C   sT  | j dk	r| j j|ƒ | jdkr$dS | j| j|ƒ | jjƒ }|dks||jƒ dks||jƒ jƒ dks||jƒ dks||jƒ jƒ dkr€dS |jƒ }|jƒ }|jdƒ}|dks¶t|t	j
ƒ s¶| rºdS |�sxŽtd|jƒ ƒD ]:}|j|ƒ|krÐ|| _|j|ƒ| _|jdƒ |j|ƒ P qÐW n@t| ddƒdk	�rPt| ddƒdk	�rP|jdƒ |j| j|| jƒ dS )	zCHandles the **visible** state of the editor being changed.
        Nr	   Ztraits_tabbed_groupr   FÚ
_tab_indexÚ	_tab_textT)r#   Ú
setVisibler   Ú_visible_changed_helperÚparentr   r)   Úpropertyr   r   Z
QTabWidgetr(   r   r,   ZtabTextr-   Z	removeTabÚgetattrZ	insertTab)r   ÚvisibleÚpageZstack_widgetÚnotebookZis_tabbed_groupr+   r   r   r   Ú_visible_changed™   s8    


"




zEditor._visible_changedc             C   sT   t |tjƒr|j|ƒ n8x6t|jƒ ƒD ]&}|j|ƒ}| j|jƒ pF|j	ƒ |ƒ q&W dS )zlA helper that allows the control to be a layout and recursively
           manages all its widgets.
        N)
r   r   r'   r.   r(   r)   r*   r/   r   r   )r   r   r3   r+   r   r   r   r   r/   Ç   s    
zEditor._visible_changed_helperc             C   s   | j S )zC Returns the editor's control for indicating error status.
        )r   )r   r   r   r   Úget_error_control×   s    zEditor.get_error_controlc             C   s   dS )zA Returns whether or not the editor is in an error state.
        Fr   )r   r   r   r   Úin_error_stateà   s    zEditor.in_error_statec             C   sº   |dkr| j }|p| jƒ }|dkr*| jƒ }t|tƒs:|g}xz|D ]r}|dkrNq@tj|jƒ ƒ}|rŒt}t	|ddƒdkr˜tj
|jtjjƒƒ|_nt	|dtƒ}|jtjj|ƒ |j|ƒ q@W dS )z0 Sets the editor's current error state.
        NÚ	_ok_color)Úinvalidr8   r7   r   Úlistr   ZQPaletteZpaletter   r2   ZQColorÚcolorÚBaser9   r
   ZsetColorZ
setPalette)r   Ústater   ÚitemZpalr<   r   r   r   Úset_error_stateé   s&    

zEditor.set_error_statec             C   s   | j ƒ  dS )z6 Handles the editor's invalid state changing.
        N)r@   )r   r>   r   r   r   Ú_invalid_changed  s    zEditor._invalid_changedc             C   s   | j j| j|ƒ dS )zE Performs the action described by a specified Action object.
        N)ÚuiZdo_undoableÚ_perform)r   ÚactionZaction_eventr   r   r   Úperform  s    zEditor.performc       	   
   C   sØ   |j }| jd }| jd }| jd }| jd }|| jd< |jdƒdkr”|jdƒdk r\|d	7 }yt|tƒ | jƒ W n   dd
lm} |ƒ  Y nX d S t||d ƒ}|d k	r¶|||ƒ d S |jd k	rÊ|j|ƒ |j	|ƒ d S )NÚinfoÚhandlerr   Ú	selectionrD   Ú.r   ú(z())Úraise_to_debug)
rD   Ú_menu_contextÚfindÚevalÚglobalsÚtraitsui.apirK   r2   Z
on_performrE   )	r   rD   Úmethod_namerF   rG   r   rH   rK   Úmethodr   r   r   rC     s,    







zEditor._performc          
   C   sT   |dkrPd}yt |tƒ | jƒs"d}W n   ddlm} |ƒ  Y nX 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   TFr   )rK   N)rN   rO   rL   rP   rK   Úsetattr)r   Ú	conditionr   ZtraitÚvaluerK   r   r   r   Ú	eval_when5  s    zEditor.eval_whenc             C   s,   |j j}| j|j|dƒ | j|j|dƒ dS )z= Adds a menu item to the menu bar being constructed.
        r%   ÚcheckedN)r?   rD   rV   Zenabled_whenZchecked_when)r   Z	menu_itemrD   r   r   r   Úadd_to_menuD  s    zEditor.add_to_menuc             C   sŒ   |j dkrDyt|j tƒ | jƒs"dS W n   ddlm} |ƒ  Y nX |jdkrˆyt|jtƒ | jƒsfdS W n   ddlm} |ƒ  Y nX dS )zM Returns whether the action should be defined in the user interface.
        r   Fr   )rK   T)Zdefined_whenrN   rO   rL   rP   rK   Zvisible_when)r   rD   rK   r   r   r   Úcan_add_to_menuK  s*    

zEditor.can_add_to_menuc             C   s˜   | j jƒ }|tjjkrP|r2|j|ƒ |jtjjƒ |rˆ|j	|ƒ |j
tjjƒ n8|rl|j	|ƒ |j
tjjƒ |rˆ|j|ƒ |jtjjƒ | j j|ƒ dS )a]   Set the size policy of the editor's controller.

        Based on the "direction" of the group that contains this editor
        (VGroup or HGroup), set the stretch factor and the resizing
        policy of the control.

        Parameters
        ----------
        direction : QtGui.QBoxLayout.Direction
            Directionality of the group that contains this edito. Either
            QtGui.QBoxLayout.LeftToRight or QtGui.QBoxLayout.TopToBottom

        resizable : bool
            True if control should be resizable in the orientation opposite
            to the group directionality

        springy : bool
            True if control should be resizable in the orientation equal
            to the group directionality

        stretch : int
            Stretch factor used by Qt to distribute the total size to
            each component.
        N)r   Z
sizePolicyr   Z
QBoxLayoutZLeftToRightZsetHorizontalStretchZsetHorizontalPolicyZQSizePolicyZ	ExpandingZsetVerticalStretchZsetVerticalPolicyZsetSizePolicy)r   Ú	directionZ	resizableZspringyZstretchÚpolicyr   r   r   Úset_size_policyi  s    




zEditor.set_size_policy)N)NN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r!   r&   r"   r6   r/   r7   r8   r@   rA   rE   rC   rV   rX   rY   r\   r   r   r   r   r   %   s*   

.		
"	
r   c                   sH   e Zd ZdZeeƒZeZe	Z
dd„ Z‡ fdd„Zdd„ Zdd	„ Z‡  ZS )
ÚEditorWithListz0 Editor for an object that contains a list.
    c                s„   | j ‰ ˆ j}|dkr,| j|ƒ\| _| _| _nˆ d | _| _‡ fdd„| _| jj| j| jdd� | jj| j| jd dd� | jƒ  dS )	z! Initializes the object.
        r   Úvaluesc                  s   ˆ j S )N)rb   r   )Úfactoryr   r   Ú<lambda>º  s    z%EditorWithList.init.<locals>.<lambda>rB   )ÚdispatchÚ_itemsN)rc   r   Zparse_extended_nameÚlist_objectÚ	list_nameÚ
list_valueÚon_trait_changeÚ_list_updated)r   r0   r   r   )rc   r   Úinit°  s    
zEditorWithList.initc                sB   | j j| j| jdd� | j j| j| jd dd� tt| ƒjƒ  dS )z> Disconnects the listeners set up by the constructor.
        T)Úremoverf   N)rg   rj   rk   rh   Úsuperra   Údispose)r   )Ú	__class__r   r   ro   É  s    
zEditorWithList.disposec             C   s   | j | jƒ ƒ dS )z4 Handles the monitored trait being updated.
        N)Úlist_updatedri   )r   r   r   r   rk   Ù  s    zEditorWithList._list_updatedc             C   s   t ‚dS )z3 Handles the monitored list being updated.
        N)ÚNotImplementedError)r   rb   r   r   r   rq   â  s    zEditorWithList.list_updated)r]   r^   r_   r`   r   r   rg   r   rh   r   ri   rl   ro   rk   rq   Ú__classcell__r   r   )rp   r   ra   œ  s   	ra   c                   s8   e Zd ZdZdd„ Zdd„ Zdd„ Z‡ fdd	„Z‡  ZS )
ÚEditorFromViewz- An editor generated from a View object.
    c             C   s0   | j |ƒ | _}|jdkr$| jj|_|j| _dS )z! Initializes the object.
        N)Úinit_uiÚ_uiÚhistoryrB   r   )r   r0   rB   r   r   r   rl   ô  s    

zEditorFromView.initc             C   s   t ‚dS )zs Creates and returns the traits UI defined by this editor.
            (Must be overridden by a subclass).
        N)rr   )r   r0   r   r   r   ru     s    zEditorFromView.init_uic             C   s   dS )z` Updates the editor when the object trait changes externally to the
            editor.
        Nr   )r   r   r   r   r     s    zEditorFromView.update_editorc                s   | j jƒ  tt| ƒjƒ  dS )z! Disposes of the editor.
        N)rv   ro   rn   rt   )r   )rp   r   r   ro     s    
zEditorFromView.dispose)	r]   r^   r_   r`   rl   ru   r   ro   rs   r   r   )rp   r   rt   ì  s
   
rt   N)r`   Ú
__future__r   Z	pyface.qtr   Z
traits.apir   r   r   r   rP   r   ZUIEditorÚ	constantsr
   r   ra   rt   r   r   r   r   Ú<module>   s     yP