a
    §ˆcu9  ã                   @   s|   d Z ddlmZ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.
é    )ÚQtCoreÚ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„ 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   z1Base class for PyQt editors for Traits-based UIs.c                 C   s2   | j  ¡ }| d¡}|du rq.| ¡  d¡ q
dS )z*Delete the contents of a control's layout.r   N)ÚcontrolÚlayoutZtakeAtÚwidgetÚ	setParent)Úselfr   Úitm© r   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/editor.pyÚclear_layout'   s
    

zEditor.clear_layoutc                 C   s   |dur| |_ dS )z(Handles the **control** trait being set.N)Z_editor)r   r   r   r   r   Ú_control_changed1   s    zEditor._control_changedc                 C   s   | j dur| j  ¡  dS )z8Assigns focus to the editor's underlying toolkit widget.N)r   ZsetFocus©r   r   r   r   Ú	set_focus7   s    
zEditor.set_focusc                 C   s$   | j }| j ¡ |kr | 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_editor<   s    zEditor.update_editorc                 C   s"   t | jtjƒr| j ¡ S | jS dS )zÈGet the concrete widget for the control.

        Some editors may use a QLayout  instead of a Qwidget, which may not be
        suitable for certain usages (eg. for parenting other widgets).
        N)Ú
isinstancer   r   ZQLayoutZparentWidgetr   r   r   r   Úget_control_widgetD   s    
zEditor.get_control_widgetc                 C   s   |  |¡ dS )z)Sets the tooltip for a specified control.N)Z
setToolTip)r   r   r   r   r   r   Úset_tooltip_textO   s    zEditor.set_tooltip_textc                 C   s2   | j dur|  | j |¡ | jdur.| j |¡ dS )z:Handles the **enabled** state of the editor being changed.N)r   Ú_enabled_changed_helperÚlabel_controlÚ
setEnabled)r   Úenabledr   r   r   Ú_enabled_changedS   s    

zEditor._enabled_changedc                 C   sP   t |tjƒr| |¡ n4t| ¡ ƒD ]&}| |¡}|  | ¡ pD| 	¡ |¡ q$dS ©ziA 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   Z   s    
ÿzEditor._enabled_changed_helperc                 C   sP  | j dur| j  |¡ | jdu r$dS |  | j|¡ | j ¡ }|du s|| ¡ du s|| ¡  ¡ du s|| ¡ du s|| ¡  ¡ dkr€dS | ¡ }| ¡ }| d¡}|du s²t|t	j
ƒr²|s¶dS |�std| ¡ ƒD ]>}| |¡|krÊ|| _| |¡| _| d¡ | |¡  �qLqÊn@t| ddƒdu�rLt| ddƒdu�rL| d¡ | | j|| j¡ dS )	z:Handles 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_widgetZnotebookZis_tabbed_groupr)   r   r   r   Ú_visible_changedg   sT    


ÿ
þý
üû
ÿ
þý


ÿþ
zEditor._visible_changedc                 C   sP   t |tjƒr| |¡ n4t| ¡ ƒD ]&}| |¡}|  | ¡ pD| 	¡ |¡ q$dS r$   )
r   r   r%   r,   r&   r'   r(   r-   r   r   )r   r   r1   r)   r   r   r   r   r-   ž   s    
ÿzEditor._visible_changed_helperc                 C   s   | j S )z9Returns 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 )z7Returns whether or not the editor is in an error state.Fr   r   r   r   r   Úin_error_state¯   s    zEditor.in_error_stateNc                 C   sº   |du r| j }|p|  ¡ }|du r*|  ¡ }t|tƒs:|g}|D ]v}|du rLq>t | ¡ ¡}|rŒt}t	|ddƒdu r˜t 
| tjjj¡¡|_nt	|dtƒ}| tjjj|¡ | |¡ q>dS )z&Sets the editor's current error state.NÚ	_ok_color)Úinvalidr5   r4   r   Úlistr   ZQPaletteZpaletter   r0   ZQColorÚcolorZ	ColorRoleZBaser6   r
   ZsetColorZ
setPalette)r   Ústater   ÚitemZpalr9   r   r   r   Úset_error_state³   s(    
ÿzEditor.set_error_statec                 C   s   |   ¡  dS )z,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|¡ dS )z;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< | d¡dkr”| d¡dk r\|d	7 }zt|tƒ | jƒ W n   dd
lm} |ƒ  Y n0 d S t||d ƒ}|d ur¶|||ƒ d S |jd urÎ| |¡ d S | 	|¡ d S )NÚinfoÚhandlerÚobjectÚ	selectionr@   Ú.r   ú(z()©Úraise_to_debug)
r@   Ú_menu_contextÚfindÚevalÚglobalsÚtraitsui.apirI   r0   Z
on_performrA   )	r   r@   Úmethod_namerB   rC   rD   rE   rI   Úmethodr   r   r   r?   Ù   s.    







zEditor._performc                 C   sT   |dkrPd}zt |tƒ | jƒs"d}W n   ddlm} |ƒ  Y n0 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.
        Ú TFr   rH   N)rL   rM   rJ   rN   rI   Úsetattr)r   Ú	conditionrD   ZtraitÚvaluerI   r   r   r   Ú	eval_when÷   s    zEditor.eval_whenc                 C   s,   |j j}|  |j|d¡ |  |j|d¡ dS )z3Adds a menu item to the menu bar being constructed.r"   ÚcheckedN)r;   r@   rU   Zenabled_whenZchecked_when)r   Z	menu_itemr@   r   r   r   Úadd_to_menu  s    zEditor.add_to_menuc                 C   s�   |j dkrFzt|j tƒ | jƒs$W dS W n   ddlm} |ƒ  Y n0 |jdkrŒzt|jtƒ | jƒsjW dS W n   ddlm} |ƒ  Y n0 dS )zCReturns whether the action should be defined in the user interface.rQ   Fr   rH   T)Zdefined_whenrL   rM   rJ   rN   rI   Zvisible_when)r   r@   rI   r   r   r   Úcan_add_to_menu  s&    
ÿ

ÿ
zEditor.can_add_to_menuc                 C   s¢   | j  ¡ }|tjjjkrV|r6| |¡ | tjj	j
¡ |r’| |¡ | tjj	j
¡ n<|rt| |¡ | tjj	j
¡ |r’| |¡ | tjj	j
¡ | j  |¡ dS )aq  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 editor. Either
            QtGui.QBoxLayout.Direction.LeftToRight or QtGui.QBoxLayout.Direction.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	DirectionZLeftToRightZsetHorizontalStretchZsetHorizontalPolicyZQSizePolicyÚPolicyZ	ExpandingZsetVerticalStretchZsetVerticalPolicyZsetSizePolicy)r   Ú	directionZ	resizableZspringyZstretchÚpolicyr   r   r   Úset_size_policy*  s    




zEditor.set_size_policy)NN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r#   r   r3   r-   r4   r5   r<   r=   rA   r?   rU   rW   rX   r\   r   r   r   r   r   $   s*   
7

r   c                       sL   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 )
ÚEditorWithListz*Editor for an object that contains a list.c                    s„   | j ‰ ˆ j}|dkr,|  |¡\| _| _| _nˆ d | _| _‡ fdd„| _| jj| j| jdd� | jj| j| jd dd� |  ¡  dS )	úInitializes the object.rQ   Úvaluesc                      s   ˆ j S )N)rc   r   ©Úfactoryr   r   Ú<lambda>u  ó    z%EditorWithList.init.<locals>.<lambda>r>   )ÚdispatchÚ_itemsN)re   ÚnameZparse_extended_nameÚlist_objectÚ	list_nameÚ
list_valueÚon_trait_changeÚ_list_updated)r   r.   rj   r   rd   r   Úiniti  s$    üý
ÿÿzEditorWithList.initc                    s>   | j j| j| jdd� | j j| j| jd dd� tƒ  ¡  dS )z4Disconnects the listeners set up by the constructor.T)Úremoveri   N)rk   rn   ro   rl   ÚsuperÚdisposer   ©Ú	__class__r   r   rs   €  s    
ÿÿzEditorWithList.disposec                 C   s   |   |  ¡ ¡ dS )z*Handles the monitored trait being updated.N)Úlist_updatedrm   r   r   r   r   ro   ‹  s    zEditorWithList._list_updatedc                 C   s   t ‚dS )z)Handles the monitored list being updated.N©ÚNotImplementedError)r   rc   r   r   r   rv   �  s    zEditorWithList.list_updated)r]   r^   r_   r`   r   r   rk   r   rl   r   rm   rp   rs   ro   rv   Ú__classcell__r   r   rt   r   ra   Y  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du r$| jj|_|j| _dS )rb   N)Úinit_uiÚ_uiÚhistoryr>   r   )r   r.   r>   r   r   r   rp   —  s    

zEditorFromView.initc                 C   s   t ‚dS )znCreates and returns the traits UI defined by this editor.
        (Must be overridden by a subclass).
        Nrw   )r   r.   r   r   r   r{   Ÿ  s    zEditorFromView.init_uic                 C   s   dS r   r   r   r   r   r   r   ¥  s    zEditorFromView.update_editorc                    s   | j  ¡  tƒ  ¡  dS )zDisposes of the editor.N)r|   rs   rr   r   rt   r   r   rs   ­  s    
zEditorFromView.dispose)	r]   r^   r_   r`   rp   r{   r   rs   ry   r   r   rt   r   rz   ”  s
   rz   N)r`   Z	pyface.qtr   r   Z
traits.apir   r   r   r   rN   r   ZUIEditorÚ	constantsr
   r   ra   rz   r   r   r   r   Ú<module>   s     7;