3
8ßL]rO  ã               @   sØ   d Z ddlm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e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..
é    )Úabsolute_import)ÚQtCoreÚQtGui)Ú	HasTraitsÚInstanceÚProperty)ÚToolkitEditorFactory)Ú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Z	dZ
eZe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‡ 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ƒ\| _| _| _|jrBtjƒ  | _	 | _
}|j}|j}| j}x$|D ]}||jƒ O }||jƒ O }qZW |�r:i | _| j| jƒ}|dk	rª| j| jt|ƒ< tjƒ | _| jjj| jƒ | j| jƒ |jdk�r| jj| j| jdd� | jj| j| jd dd� |j| jddd� |j| jddd� | jƒ  n(|�rbtjƒ | _| jjdƒ | j| jƒ |�r~t| j	ƒ}| j	j|ƒ t |j! }|dk�r˜| j!}|�s¤|�r|j�rtj"||ƒ}	|	j#d	d	d	d	ƒ |	j$| jƒ |tj"j%k�rtj&ƒ }
|
j'tj&j(ƒ |
j)tj&j*ƒ |	j$|
ƒ | j+||	ƒ nh| j	dk�r^| jdk�rTtjƒ | _| jjt, j| jƒ | j| _	n(tj"||ƒ}	|	j#d	d	d	d	ƒ | j+||	ƒ | j-|j.d
� | j/|j0ddƒ dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        Ú NÚui)ÚdispatchÚ_itemsÚvaluesÚvalues_itemsTr   )Úviewr   Úfrom)1ÚfactoryÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚeditabler   ÚQWidgetÚcontrolÚ_panelÚ
selectableÚ	droppableÚitemsZis_droppableÚis_selectableÚ_object_cacheÚitem_forÚvalueÚidZ	QComboBoxÚ_choiceZ	activatedÚconnectÚupdate_objectZset_tooltipÚon_trait_changeÚrebuild_itemsZ	QLineEditZsetReadOnlyr   ZinstallEventFilterÚOrientationMapÚorientationÚ
QBoxLayoutÚsetContentsMarginsÚ	addWidgetÚTopToBottomZQFrameZsetFrameShapeZHLineZsetFrameShadowZSunkenÚcreate_editorÚintZ
trait_setqr   Z
sync_valueZ	view_name)ÚselfÚparentr   r%   r&   r'   ÚitemZdrop_event_filterr3   ÚlayoutZhline© r>   úB/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/instance_editor.pyÚinitU   sv    













zCustomEditor.initc             C   s   t jƒ | _|j| jƒ dS )z% Creates the editor control.
        N)r   r"   r$   r6   )r:   r;   r=   r>   r>   r?   r8   ²   s    
zCustomEditor.create_editorc             C   sv   | j dk	r| j S | j}| jdk	r0| jƒ |j }n|j}g }|j}x*|D ]"}t|tƒs^||d�}|j|ƒ qFW || _ |S )z< Gets the current list of InstanceChoiceItem items.
        N)Úobject)r   r   r    r   ÚadapterÚ
isinstancer   Úappend)r:   r   r   r'   rB   r+   r>   r>   r?   Ú
_get_items¼   s    




zCustomEditor._get_itemsc          	   C   sŠ   d| _ d}| j}| j}|jƒ  x:t| jƒD ],\}}|jƒ r*|j|jƒ ƒ |j	|ƒr*|}q*W |dkrn|j
|ƒ ny
d| _W n   Y nX dS )z, Rebuilds the object selector list.
        Nr   r   éÿÿÿÿ)r   r+   r-   ÚclearÚ	enumerater'   r(   ÚaddItemÚget_nameÚis_compatibleÚsetCurrentIndex)r:   r   r+   ÚchoiceÚir<   r>   r>   r?   r1   ×   s     

zCustomEditor.rebuild_itemsc             C   s"   x| j D ]}|j|ƒr|S qW dS )z@ Returns the InstanceChoiceItem for a specified object.
        N)r'   rK   )r:   rA   r<   r>   r>   r?   r*   ö   s    
zCustomEditor.item_forc             C   s@   d}|dk	r|j ƒ }|dkr"| j}| jjj| jj||| j| jƒS )z9 Returns the view to use for a specified object.
        r   N)Zget_viewr   r   ÚhandlerÚtrait_view_forÚinfoÚobject_namer   )r:   rA   r<   r   r>   r>   r?   Úview_for  s    zCustomEditor.view_forc             C   s„   | j | }t|ƒ}| jj|ƒ}|dkrr|jƒ }| jj r`|jr`| j|| j	|ƒƒ}|j
|| jdƒ | jjrr|| j|< || _| jƒ  dS )zD Handles the user selecting a new value from the combo box.
        NÚmodal)r'   r,   r)   ÚgetZ
get_objectr   r!   Z
is_factoryrS   r*   r   r#   Zcachabler+   Úresynch_editor)r:   Úindexr<   Zid_itemrA   r   r>   r>   r?   r/     s    

zCustomEditor.update_objectc             C   s~   | j ƒ  | j}| j| jƒ}|dk	rz|dk	rz|j| jƒ}| jdk	rp|j|ƒ}|dk rd|jƒ }|j|ƒ |j	|ƒ n
|j
|ƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        Nr   )rV   r-   r*   r+   rJ   r)   ZfindTextÚcountrI   rL   ÚsetText)r:   rM   r<   r   Úidxr>   r>   r?   Úupdate_editor*  s    


zCustomEditor.update_editorc             C   sZ  | j }|dk	�rV|jƒ }|dkr<tj|ƒ}|jddddƒ n@| jdk	rX| jjƒ  d| _n$|jdƒ}x|dk	rx|jdƒ}qdW ~d}| j}t	|t
ƒs®d}|dk	r¢| j}tj|ƒ}nž| j|| j|ƒƒ}|jƒ }	d}
t	|tƒrÚ|}
|	jd| jƒ |	jd| jjƒ |j|	|d|jƒ |
| jjƒ | _}|j}|j| _| j|_|j�sH|j�sH|j�rLd}|j|ƒ dS )zs Resynchronizes 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+   rC   r   Ú	str_valueZQLabelrS   r*   Ztrait_contextr   Ú
setdefaultrA   r   rO   Ztrait_view_elementsr   r,   r#   Z_scrollableZ
scrollabler;   Z	resizabler6   )r:   Zpanelr=   ÚchildZstretchr+   r_   r#   r   r\   rO   r   r>   r>   r?   rV   E  sH    








zCustomEditor.resynch_editorc                s˜   d| _ | jdk	r| jjƒ  | jdk	r†| jdk	r^| jj| j| jdd� | jj| j| jd dd� | jj| jddd� | jj| jddd� t	t
| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        NT)Úremover   r   r   )r)   r]   r^   r-   r   r0   r1   r   r   Úsuperr   )r:   )Ú	__class__r>   r?   r^   {  s    






zCustomEditor.disposec             C   s   dS )zN Handles an error that occurs while setting the object's trait value.
        Nr>   )r:   Zexcpr>   r>   r?   Úerror–  s    zCustomEditor.errorc             C   s   | j p
| jS )zC Returns the editor's control for indicating error status.
        )r-   r#   )r:   r>   r>   r?   Úget_error_controlŸ  s    zCustomEditor.get_error_controlc             C   s2   | j }|dk	r.|jdƒ|jkr.|j|jdƒƒ dS )z` Restores any saved user preference information associated with the
            editor.
        Nr,   Úprefs)r]   rU   r,   Z	set_prefs)r:   rg   r   r>   r>   r?   Úrestore_prefs«  s    zCustomEditor.restore_prefsc             C   s,   | j }|dk	r(|jdkr(|j|jƒ dœS dS )zM Returns any user preference information associated with the editor.
        Nr   )r,   rg   )r]   r,   Z	get_prefs)r:   r   r>   r>   r?   Ú
save_prefs·  s
    zCustomEditor.save_prefsc             C   s   | j ƒ  d S )N)rV   )r:   r   r>   r>   r?   Ú_view_changedÃ  s    zCustomEditor._view_changed)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zok_colorr   r4   r7   r3   Úextrar   r'   r	   r   r@   r8   rE   r1   r*   rS   r/   r[   rV   r^   re   rf   rh   ri   rj   Ú__classcell__r>   r>   )rd   r?   r   6   s*   ]
6	r   c               @   sD   e Zd ZdZedƒZe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   s:   t jƒ | _|j| jƒ | jjj| jƒ | jjj| jƒ dS )z0 Creates the editor control (a button).
        N)	r   ZQPushButtonÚ_buttonr6   Zclickedr.   Úedit_instanceZ	destroyedÚ_parent_closed)r:   r;   r=   r>   r>   r?   r8   Û  s    
zSimpleEditor.create_editorc             C   s|   | j }| jjj| jj|j| j| j| jƒ}| jj	||j
|jd�| _| jjdk	rrt| jjƒ | jjdkrx| jj| j_nd| _dS )z\ Edit the contents of the object trait when the user clicks the
            button.
        )Úkindr,   N)r   r   rO   rP   rQ   r   r+   rR   r   Zedit_traitsrv   r,   Ú
_dialog_uir#   r   Úhistory)r:   r   r   r>   r>   r?   rt   è  s    zSimpleEditor.edit_instancec             C   sH   | j }|dk	rD| jj}|dkr(t| jƒ}|j|ƒ |jt| jt	ƒƒ dS )zw Resynchronizes the contents of the editor when the object trait
            changes externally to the editor.
        Nr   )
rs   r   Úlabelr
   r   rY   Z
setEnabledrC   r+   r   )r:   Úbuttonry   r>   r>   r?   rV     s    

zSimpleEditor.resynch_editorc             C   s6   | j d k	r2| j jd k	r"| j jjƒ  | j jƒ  d | _ d S )N)rw   r#   Úcloser^   )r:   r>   r>   r?   ru     s
    

zSimpleEditor._parent_closedN)rk   rl   rm   rn   r   rw   r   r4   ÚLeftToRightr3   ro   r8   rt   rV   ru   r>   r>   r>   r?   rq   Ë  s   rq   )"rn   Ú
__future__r   Z	pyface.qtr   r   Z
traits.apir   r   r   Z traitsui.editors.instance_editorr   Ztraitsui.ui_traitsr	   Ztraitsui.helperr
   Ztraitsui.handlerr   Ztraitsui.instance_choicer   Úeditorr   Zdrop_editorr   Ú	constantsr   Úhelperr   r4   r|   r7   r2   r   rq   r>   r>   r>   r?   Ú<module>   s(      