3
8ßL]Ú<  ã               @   sâ   d Z ddlmZ ddlmZ ddlZddlmZ ddl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„ Ze
jje
jjdœZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z€ Defines the various editors and the editor factory for single-selection
    enumerations, for the PyQt user interface toolkit.
é    )Úabsolute_import)ÚreduceN)Úrange)ÚQtCoreÚQtGui)ÚBoolÚProperty)ÚToolkitEditorFactory)Úenum_values_changedé   )ÚOKColorÚ
ErrorColor)ÚEditorc             C   s   | j ƒ S )N)Ú
capitalize)Ús© r   ú>/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/enum_editor.pyÚ<lambda>$   s    r   )ZpopupÚinlinec                   sd   e Zd ZdZeZeZe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‡  ZS )Ú
BaseEditorz) Base class for enumeration editors.
    c             C   s    t | jƒ | jƒ\| _| _| _dS )zO Recomputes the cached data based on the underlying enumeration model.
        N)r
   Ú_valueÚstring_valueÚ_namesÚ_mappingÚ_inverse_mapping)Úselfr   r   r   Úvalues_changed>   s    zBaseEditor.values_changedc             C   s   t ‚dS )z¯ Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.

            This is not needed for the Qt backends.
        N)ÚNotImplementedError)r   r   r   r   Úrebuild_editorD   s    zBaseEditor.rebuild_editorc             C   sb   | j }|jdkrL| j|jƒ\| _| _| _| jƒ  | jj| jd| j dd� n|j| j	ddd� dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        Ú ú Úui)ÚdispatchÚvalues_modifiedN)
ÚfactoryÚnameZparse_extended_nameÚ_objectÚ_namer   r   Úon_trait_changeÚ_values_changedr   )r   Úparentr$   r   r   r   ÚinitP   s    


zBaseEditor.initc                sL   | j dk	r&| j j| jd| j dd� n| jj| jddd� tt| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        Nr    T)Úremover#   )	r&   r(   r)   r'   r$   r   Úsuperr   Údispose)r   )Ú	__class__r   r   r.   _   s    



zBaseEditor.disposec             C   s   | j dkr| jjS | jS )z4 Gets the current set of enumeration names.
        N)r&   r$   r   )r   r   r   r   Ú
_get_namesq   s    
zBaseEditor._get_namesc             C   s   | j dkr| jjS | jS )z# Gets the current mapping.
        N)r&   r$   r   )r   r   r   r   Ú_get_mappingy   s    
zBaseEditor._get_mappingc             C   s   | j dkr| jjS | jS )z+ Gets the current inverse mapping.
        N)r&   r$   r   )r   r   r   r   Ú_get_inverse_mapping�   s    
zBaseEditor._get_inverse_mappingc             C   s   | j ƒ  | jƒ  dS )zN Handles the underlying object model's enumeration set being changed.
        N)r   r   )r   r   r   r   r)   ‹   s    zBaseEditor._values_changed)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesÚmappingÚinverse_mappingr   r   r+   r.   r0   r1   r2   r)   Ú__classcell__r   r   )r/   r   r   -   s   
r   c                   sl   e Zd ZdZ‡ f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 )ÚSimpleEditorzE Simple style of enumeration editor, which displays a combo box.
    c                s²   t t| ƒj|ƒ | jƒ  | _}|j| jƒ |jt j	| j
ƒ | jjdk	r |jdƒ |jƒ jt| jj ƒ | jjr€|jj	| jƒ n|jƒ jj	| jƒ |jtjjƒ d| _| jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        NTr   )r-   r;   r+   Úcreate_combo_boxÚcontrolÚaddItemsr7   ZcurrentIndexChangedÚstrÚconnectÚupdate_objectr$   ÚevaluateZsetEditableZ	completerZsetCompletionModeÚcompletion_mode_mapZcompletion_modeZauto_setZeditTextChangedÚupdate_text_objectÚlineEditZeditingFinishedÚupdate_autoset_text_objectZsetInsertPolicyr   Ú	QComboBoxZNoInsertÚ_no_enum_updateÚset_tooltip)r   r*   r=   )r/   r   r   r+   š   s    

zSimpleEditor.initc             C   s²   | j dkr®|  j d7  _ | jjdkrly$| jj| j| j ƒ}| jj|ƒ W q  t	k
rh   | jjdƒ Y q X n4y| jj
| jƒ W n  t	k
rž   | jj
dƒ Y nX |  j d8  _ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        r   r   Nr   éÿÿÿÿ)rH   r$   rB   r7   Úindexr9   Úvaluer=   ZsetCurrentIndexÚ	ExceptionZsetEditTextÚ	str_value)r   rK   r   r   r   Úupdate_editor³   s    
zSimpleEditor.update_editorc             C   s   | j tƒ dS )zN Handles an error that occurs while setting the object's trait value.
        N)Ú_set_backgroundr   )r   Úexcpr   r   r   ÚerrorÆ   s    zSimpleEditor.errorc             C   sD   | j jdƒ z| j jƒ  | j j| jƒ W d| j jdƒ X | jƒ  dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        TNF)r=   ÚblockSignalsÚclearr>   r7   rO   )r   r   r   r   r   Ë   s    
zSimpleEditor.rebuild_editorc                sJ   t t| ƒj||||ƒ |tjjkr&|s6|tjjkrF|rF| jjtjj	ƒ d S )N)
r-   r;   Úset_size_policyr   Z
QBoxLayoutZLeftToRightr=   ÚsetSizeAdjustPolicyrG   ZAdjustToContentsOnFirstShow)r   Ú	directionZ	resizableZspringyZstretch)r/   r   r   rU   Ø   s    
zSimpleEditor.set_size_policyc             C   s.   t jƒ }|jt jjƒ |jt jjt jjƒ |S )z< Returns the QComboBox used for the editor control.
        )r   rG   rV   ZAdjustToContentsZsetSizePolicyZQSizePolicyZMaximumZFixed)r   r=   r   r   r   r<   å   s
    

zSimpleEditor.create_combo_boxc             C   s6   | j jƒ }tj|jƒ ƒ}|jtjj|ƒ |j|ƒ d S )N)r=   rE   r   ZQPaletteZpaletteZsetColorZBaseZ
setPalette)r   ÚcolÚleZpalr   r   r   rP   î   s    
zSimpleEditor._set_backgroundc             C   sh   | j dkrd|  j d7  _ y| jtj|ƒ | _W n& tk
rT   ddlm} |ƒ  Y nX |  j d8  _ dS )zD Handles the user selecting a new value from the combo box.
        r   r   )Úraise_to_debugN)rH   r8   ÚsixÚ	text_typerL   rM   Ztraitsui.apirZ   )r   ÚtextrZ   r   r   r   rA   ö   s    
zSimpleEditor.update_objectc          %   C   sâ   | j dkrÞtj|ƒ}y| j| }W nP tk
rr   y| jj|ƒ}W n* tk
rl } z| j|ƒ dS d}~X nX Y nX |  j d7  _ y
|| _W n8 tk
rÄ } z|  j d8  _ | j|ƒ dS d}~X nX | j	t
ƒ |  j d8  _ dS )zK Handles the user typing text into the combo box text entry field.
        r   Nr   )rH   r[   r\   r8   rM   r$   rB   rR   rL   rP   r   )r   r]   rL   rQ   r   r   r   rD     s&    





zSimpleEditor.update_text_objectc             C   s&   | j d k	r"| j jƒ jƒ }| j|ƒS d S )N)r=   rE   r]   rD   )r   r]   r   r   r   rF     s    
z'SimpleEditor.update_autoset_text_object)r3   r4   r5   r6   r+   rO   rR   r   rU   r<   rP   rA   rD   rF   r:   r   r   )r/   r   r;   ’   s   	r;   c                   sH   e Zd ZdZedƒZ‡ fdd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
‡  ZS )ÚRadioEditorz[ Enumeration editor, used for the "custom" style, that displays radio
        buttons.
    Fc                s\   t t| ƒj|ƒ tjƒ | _tj| jƒ}|jddddƒ tj	ƒ | _
| j
jj| jƒ | jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   N)r-   r^   r+   r   ZQWidgetr=   ZQGridLayoutZsetContentsMarginsr   ZQSignalMapperÚ_mapperZmappedr@   rA   r   )r   r*   Úlayout)r/   r   r   r+   .  s    

zRadioEditor.initc             C   sH   | j jƒ }| j}x2t|jƒ ƒD ]"}|j|ƒjƒ }|j|j|kƒ qW dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)r=   r`   rL   r   ÚcountZitemAtZwidgetÚ
setChecked)r   r`   rL   ÚiÚrbr   r   r   rO   =  s
    
zRadioEditor.update_editorc             C   sd  | j ƒ  | j}| j}| j}t|ƒ}| jj}|| d | }| jrLdg| }n^|| g| }|| }x$t|ƒD ]}	||	  ||	k7  < qlW t	dd„ |dd… dƒd  |d< | j
jƒ }
d}x¦t|ƒD ]š}	x”t|ƒD ]ˆ}|dkrÐ|| }| j|ƒ}|| |_|j||kƒ |jj| jjƒ | jj||ƒ | j|ƒ |
j||	|ƒ |tt|| ƒƒ7 }|d8 }qÐW qÂW dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        r   c             S   s   | | S )Nr   )ÚxÚyr   r   r   r   ^  s    z,RadioEditor.rebuild_editor.<locals>.<lambda>Nr   rJ   rJ   )Zclear_layoutrN   r7   r8   Úlenr$   ÚcolsÚ	row_majorr   r   r=   r`   Úcreate_buttonrL   rb   Zclickedr@   r_   ÚmapZ
setMappingrI   Z	addWidgetÚintÚround)r   Zcur_namer7   r8   Únrh   ZrowsÚincrÚremrc   r`   rK   Újr%   rd   r   r   r   r   G  s:    "



zRadioEditor.rebuild_editorc             C   s   | j |tƒ}tj|ƒS )z@ Returns the QAbstractButton used for the radio button.
        )r   r   r   ZQRadioButton)r   r%   Úlabelr   r   r   rj   y  s    zRadioEditor.create_buttonc             C   s0   y| j | j|  | _W n tk
r*   Y nX dS )zD Handles the user clicking one of the custom radio buttons.
        N)r8   r7   rL   rM   )r   rK   r   r   r   rA   �  s    zRadioEditor.update_object)r3   r4   r5   r6   r   ri   r+   rO   r   rj   rA   r:   r   r   )r/   r   r^   "  s   
2r^   c                   s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
Ú
ListEditorzX Enumeration editor, used for the "custom" style, that displays a list
        box.
    c                s>   t t| ƒj|ƒ tjƒ | _| jjj| jƒ | j	ƒ  | j
ƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        N)r-   rs   r+   r   ZQListWidgetr=   ZcurrentTextChangedr@   rA   r   rI   )r   r*   )r/   r   r   r+   “  s
    
zListEditor.initc             C   sr   | j }yR| j| j }x@t|jƒ ƒD ]0}|j|ƒ}|jƒ |kr"|j|ƒ |j|ƒ P q"W W n t	k
rl   Y nX dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)
r=   r9   rL   r   ra   Úitemr]   ZsetCurrentItemZscrollToItemrM   )r   r=   rL   ÚrowZitmr   r   r   rO   Ÿ  s    



zListEditor.update_editorc             C   sJ   | j jdƒ | j jƒ  x| jD ]}| j j|ƒ qW | j jdƒ | jƒ  dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        TFN)r=   rS   rT   r7   ZaddItemrO   )r   r%   r   r   r   r   ±  s    
zListEditor.rebuild_editorc             C   sx   t j|ƒ}y| j| }W n: tk
rR   y| jj|ƒ}W n tk
rL   Y nX Y nX y
|| _W n tk
rr   Y nX dS )z5 Handles the user selecting a list box item.
        N)r[   r\   r8   rM   r$   rB   rL   )r   r]   rL   r   r   r   rA   Ä  s    

zListEditor.update_object)	r3   r4   r5   r6   r+   rO   r   rA   r:   r   r   )r/   r   rs   Š  s
   rs   ) r6   Ú
__future__r   Ú	functoolsr   r[   Z	six.movesr   Z	pyface.qtr   r   Z
traits.apir   r   Ztraitsui.editors.enum_editorr	   Ztraitsui.helperr
   Ú	constantsr   r   Úeditorr   r   Z
QCompleterZPopupCompletionZInlineCompletionrC   r   r;   r^   rs   r   r   r   r   Ú<module>   s$   e h