a
    §ˆc A  ã                   @   s¶   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ ejjjej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.
é    )Úreduce)ÚQtCoreÚQtGuiÚ	is_pyside)ÚBoolÚProperty)Úenum_values_changedé   )ÚOKColorÚ
ErrorColor)ÚEditor)ZpopupÚinlinec                       sj   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ƒ\| _| _| _dS )zsRecomputes the cached data based on the underlying enumeration model
        or the values of the factory.
        N)r   Ú_valueÚstring_valueÚ_namesÚ_mappingÚ_inverse_mapping©Úself© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/enum_editor.pyÚ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)ÚNotImplementedErrorr   r   r   r   Úrebuild_editorF   s    zBaseEditor.rebuild_editorc                    sx   ˆ j }|jdkrLˆ  |j¡\ˆ _ˆ _ˆ _ˆ  ¡  ˆ jjˆ jˆ jd dd� n(‡ fdd„ˆ _ˆ  ¡  |jˆ jddd� dS )	ú\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú ú.itemsÚui)Údispatchc                      s   ˆ j jS ©N)ÚfactoryÚvaluesr   r   r   r   Ú<lambda>b   ó    z!BaseEditor.init.<locals>.<lambda>r"   N)	r!   ÚnameZparse_extended_nameÚ_objectÚ_namer   r   ÚobserveÚ!_update_values_and_rebuild_editor)r   Úparentr!   r   r   r   ÚinitR   s     
ÿýÿzBaseEditor.initc                    sL   | j dur(| j j| j| jd ddd� n| jj| jdddd� tƒ  ¡  dS )z&Disposes of the contents of an editor.Nr   Tr   )Úremover   r"   )r&   r(   r)   r'   r!   ÚsuperÚdisposer   ©Ú	__class__r   r   r.   h   s    
üüzBaseEditor.disposec                 C   s   | j S )z*Gets the current set of enumeration names.)r   r   r   r   r   Ú
_get_names�   s    zBaseEditor._get_namesc                 C   s   | j S )zGets the current mapping.)r   r   r   r   r   Ú_get_mapping…   s    zBaseEditor._get_mappingc                 C   s   | j S )z!Gets the current inverse mapping.)r   r   r   r   r   Ú_get_inverse_mapping‰   s    zBaseEditor._get_inverse_mappingc                 C   s   |   ¡  |  ¡  dS )ziHandles the underlying object model's enumeration set or factory's
        values being changed.
        N)r   r   )r   Úeventr   r   r   r)   �   s    z,BaseEditor._update_values_and_rebuild_editor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesÚmappingÚinverse_mappingr   r   r+   r.   r1   r2   r3   r)   Ú__classcell__r   r   r/   r   r   ,   s   
r   c                       st   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dd„ Z‡  ZS )ÚSimpleEditorz?Simple style of enumeration editor, which displays a combo box.c                    s¨   t ƒ  |¡ |  ¡  | _}|  ¡  |j | j¡ | jj	dur–| 
d¡ | ¡  t| jj ¡ | jjrt|j | j¡ n| ¡ j | j¡ | tjjj¡ d| _|  ¡  dS )r   NTr   )r-   r+   Úcreate_combo_boxÚcontrolÚ_add_items_to_combo_boxZcurrentIndexChangedÚ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InsertPolicyZ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u rjz$| j | j| j ¡}| j |¡ W qœ t	yf   | j d¡ Y qœ0 n2z| j 
| j¡ W n t	yš   | j 
d¡ Y n0 |  j d8  _ dS )ú[Updates the editor when the object trait changes externally to the
        editor.
        r   r	   Néÿÿÿÿr   )rI   r!   rC   r9   Úindexr;   Úvaluer?   ZsetCurrentIndexÚ	ExceptionZsetEditTextÚ	str_value©r   rM   r   r   r   Úupdate_editor¹   s    
zSimpleEditor.update_editorc                 C   s   |   t¡ dS )zDHandles an error that occurs while setting the object's trait value.N)Ú_set_backgroundr   )r   Úexcpr   r   r   ÚerrorÌ   s    zSimpleEditor.errorc              	   C   sJ   | j  d¡ z"| j  ¡  |  ¡  W | j  d¡ n| j  d¡ 0 |  ¡  dS ©úuRebuilds the contents of the editor whenever the original factory
        object's **values** trait changes.
        TFN)r?   ÚblockSignalsÚclearr@   rR   r   r   r   r   r   Ð   s    

zSimpleEditor.rebuild_editorc                    sL   t ƒ  ||||¡ |tjjjkr$|s6|tjjjkrH|rH| j tjj	j
¡ d S r    )r-   Úset_size_policyr   Z
QBoxLayoutZ	DirectionZLeftToRightr?   ÚsetSizeAdjustPolicyrH   ÚSizeAdjustPolicyZAdjustToContentsOnFirstShow)r   Ú	directionZ	resizableZspringyZstretchr/   r   r   rZ   Ý   s    ÿÿÿzSimpleEditor.set_size_policyc                 C   s4   t  ¡ }| t jjj¡ | t jjjt jjj	¡ |S )z2Returns the QComboBox used for the editor control.)
r   rH   r[   r\   ZAdjustToContentsZsetSizePolicyZQSizePolicyÚPolicyZMaximumZFixed)r   r?   r   r   r   r>   ë   s    ÿzSimpleEditor.create_combo_boxc                 C   sD   | j D ]8}| jjr2|| jjkr2| j | j ¡ ¡ q| j |¡ qd S r    )r9   r!   Zuse_separatorÚ	separatorr?   ZinsertSeparatorÚcountÚaddItem©r   r%   r   r   r   r@   ô   s    
z$SimpleEditor._add_items_to_combo_boxc                 C   s8   | j  ¡ }t | ¡ ¡}| tjjj|¡ | |¡ d S r    )	r?   rF   r   ZQPaletteZpaletteZsetColorZ	ColorRoleZBaseZ
setPalette)r   ÚcolÚleZpalr   r   r   rS   û   s    
zSimpleEditor._set_backgroundc                 C   sj   | j dkrf|  j d7  _ z| j| }| j| | _W n$ tyV   ddlm} |ƒ  Y n0 |  j d8  _ dS )z:Handles the user selecting a new value from the combo box.r   r	   )Úraise_to_debugN)rI   r9   r:   rN   rO   Ztraitsui.apire   )r   rM   Útextre   r   r   r   rB     s    

zSimpleEditor.update_objectc                 C   sî   | j dk� rêt|ƒ}z| j| }W nV tyx   z| j |¡}W n2 tyr } z|  |¡ W Y d}~Y dS d}~0 0 Y n0 |  j d7  _ z
|| _W n> tyÐ } z&|  j d8  _ |  |¡ W Y d}~dS d}~0 0 |  t	¡ |  j d8  _ dS )zAHandles the user typing text into the combo box text entry field.r   Nr	   )
rI   Ústrr:   rO   r!   rC   rU   rN   rS   r
   )r   rf   rN   rT   r   r   r   rE     s&    
 


zSimpleEditor.update_text_objectc                 C   s&   | j d ur"| j  ¡  ¡ }|  |¡S d S r    )r?   rF   rf   rE   )r   rf   r   r   r   rG   (  s    
z'SimpleEditor.update_autoset_text_object)r5   r6   r7   r8   r+   rR   rU   r   rZ   r>   r@   rS   rB   rE   rG   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 )ÚRadioEditorzVEnumeration editor, used for the "custom" style, that displays radio
    buttons.
    Fc                    sx   t ƒ  |¡ t ¡ | _t | j¡}| dddd¡ t ¡ | _	t
r\tjdkr\| j	j | j¡ n| j	j | j¡ |  ¡  dS )r   r   )é   é   N)r-   r+   r   ZQWidgetr?   ZQGridLayoutZsetContentsMarginsr   ZQSignalMapperÚ_mapperr   Ú__version_info__Z	mappedIntrA   rB   Zmappedr   )r   r*   Úlayoutr/   r   r   r+   ;  s    

zRadioEditor.initc                 C   sD   | j  ¡ }| j}t| ¡ ƒD ]"}| |¡ ¡ }| |j|k¡ qdS ©rK   N)r?   rm   rN   Úranger`   ZitemAtZwidgetÚ
setChecked)r   rm   rN   ÚiÚrbr   r   r   rR   M  s
    
zRadioEditor.update_editorc                 C   sb  |   ¡  | j}| j}| j}t|ƒ}| jj}|| d | }| jrLdg| }nZ|| g| }|| }t|ƒD ]}	||	  ||	k7  < qjt	dd„ |dd… dƒd  |d< | j
 ¡ }
d}t|ƒD ] }	t|ƒD ]’}|dkrÈ|| }|  |¡}|| |_| ||k¡ |jj| jjtjjjd� | j ||¡ |  |¡ |
 ||	|¡ |tt|| ƒƒ7 }|d8 }qÈq¼dS )rW   r	   c                 S   s   | | S r    r   )ÚxÚyr   r   r   r#   q  r$   z,RadioEditor.rebuild_editor.<locals>.<lambda>NrL   r   )Útype)Zclear_layoutrP   r9   r:   Úlenr!   ÚcolsÚ	row_majorro   r   r?   rm   Úcreate_buttonrN   rp   ZclickedrA   rk   Úmapr   ZQtZConnectionTypeZQueuedConnectionZ
setMappingrJ   Z	addWidgetÚintÚround)r   Zcur_namer9   r:   Únrw   ÚrowsÚincrÚremrq   rm   rM   Újr%   rr   r   r   r   r   W  s>    "


ÿ
zRadioEditor.rebuild_editorc                 C   s   |   |tj¡}t |¡S )z6Returns the QAbstractButton used for the radio button.)r   rg   Ú
capitalizer   ZQRadioButton)r   r%   Úlabelr   r   r   ry   •  s    zRadioEditor.create_buttonc                 C   s.   z| j | j|  | _W n ty(   Y n0 dS )z:Handles the user clicking one of the custom radio buttons.N)r:   r9   rN   rO   rQ   r   r   r   rB   œ  s    zRadioEditor.update_object)r5   r6   r7   r8   r   rx   r+   rR   r   ry   rB   r<   r   r   r/   r   rh   /  s   
>rh   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
Ú
ListEditorzSEnumeration editor, used for the "custom" style, that displays a list
    box.
    c                    s:   t ƒ  |¡ t ¡ | _| jj | j¡ |  ¡  |  	¡  dS )r   N)
r-   r+   r   ZQListWidgetr?   ZcurrentTextChangedrA   rB   r   rJ   )r   r*   r/   r   r   r+   ­  s
    
zListEditor.initc                 C   sn   | j }zP| j| j }t| ¡ ƒD ]2}| |¡}| ¡ |kr | |¡ | |¡  qTq W n t	yh   Y n0 dS rn   )
r?   r;   rN   ro   r`   Úitemrf   ZsetCurrentItemZscrollToItemrO   )r   r?   rN   ÚrowZitmr   r   r   rR   ¹  s    



zListEditor.update_editorc                 C   sF   | j  d¡ | j  ¡  | jD ]}| j  |¡ q| j  d¡ |  ¡  dS rV   )r?   rX   rY   r9   ra   rR   rb   r   r   r   r   Ë  s    

zListEditor.rebuild_editorc                 C   sp   t |ƒ}z| j| }W n6 tyL   z| j |¡}W n tyF   Y n0 Y n0 z
|| _W n tyj   Y n0 dS )z+Handles the user selecting a list box item.N)rg   r:   rO   r!   rC   rN   )r   rf   rN   r   r   r   rB   Þ  s    
zListEditor.update_object)	r5   r6   r7   r8   r+   rR   r   rB   r<   r   r   r/   r   r„   ¤  s
   r„   N)r8   Ú	functoolsr   Z	pyface.qtr   r   r   Z
traits.apir   r   Ztraitsui.helperr   Ú	constantsr
   r   Zeditorr   Z
QCompleterZCompletionModeZPopupCompletionZInlineCompletionrD   r   r=   rh   r„   r   r   r   r   Ú<module>   s   þk u