3
8ßL]WT  ã               @   s¼   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	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„ 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 )zi Defines the various editors for single-selection enumerations, for the
wxPython user interface toolkit.
é    )Úabsolute_importN)ÚProperty)ÚToolkitEditorFactoryé   )ÚEditor)ÚOKColorÚ
ErrorColor)Úenum_values_changedÚTraitsUIPanelÚ
disconnectÚdisconnect_no_id)Úreducec             C   s   | j ƒ S )N)Ú
capitalize)Ús© r   ú=/tmp/pip-install-5sebvq2j/traitsui/traitsui/wx/enum_editor.pyÚ<lambda>2   s    r   c                   sd   e Zd ZdZe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‡ fdd„Z‡  ZS )Ú
BaseEditorz) Base class for enumeration editors.
    c             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Ú_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changedÚrebuild_editor)ÚselfÚparentr   r   r   r   ÚinitO   s    


zBaseEditor.initc             C   s   | j dkr| jjS | jS )z4 Gets the current set of enumeration names.
        N)r   r   Ú_names)r"   r   r   r   Ú
_get_namesb   s    
zBaseEditor._get_namesc             C   s   | j dkr| jjS | jS )z# Gets the current mapping.
        N)r   r   Ú_mapping)r"   r   r   r   Ú_get_mappingn   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   Ú_inverse_mapping)r"   r   r   r   Ú_get_inverse_mappingz   s    
zBaseEditor._get_inverse_mappingc             C   s   t ‚dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        N)ÚNotImplementedError)r"   r   r   r   r!   ‡   s    zBaseEditor.rebuild_editorc             C   s   t | jƒ ƒ\| _| _| _dS )zO Recomputes the cached data based on the underlying enumeration model.
        N)r	   r   r%   r'   r)   )r"   r   r   r   r   ‘   s    zBaseEditor.values_changedc             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_changedc                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.dispose)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesÚmappingÚinverse_mappingr$   r&   r(   r*   r!   r   r    r.   Ú__classcell__r   r   )r/   r   r   9   s   


r   c                   s\   e Zd ZdZ‡ f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 )ÚSimpleEditorzE Simple style of enumeration editor, which displays a combo box.
    c          	      sþ   t t| ƒj|ƒ | j}|jdkrbtj|dtjddƒtjddƒ| j	ƒ | _
}tj|| j
jƒ | jƒ nŠtj|d	dtjddƒtjd
dƒ| j	tjd� | _
}tj||jƒ | jƒ tj||jƒ | jƒ tj|| jƒ |j rì|jrìtj||jƒ | jƒ d| _| jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        Nr   r   r   )Ústyleéÿÿÿÿr:   r:   r:   r:   r:   )r-   r8   r$   r   ÚevaluateÚwxZChoiceÚPointÚSizer4   ÚcontrolZ
EVT_CHOICEÚGetIdÚupdate_objectZComboBoxZCB_DROPDOWNÚEVT_COMBOBOXÚEVT_TEXT_ENTERÚupdate_text_objectÚEVT_KILL_FOCUSÚon_kill_focusÚis_grid_cellZauto_setÚEVT_TEXTÚ_no_enum_updateÚset_tooltip)r"   r#   r   r?   )r/   r   r   r$   ¿   s"    

zSimpleEditor.initc                s6   t | jtjtjtjƒ t| jtjƒ tt	| ƒj
ƒ  dS )z0 Disposes of the contents of an editor.
        N)r   r?   r<   rB   rC   rH   r   rE   r-   r8   r.   )r"   )r/   r   r   r.   Ú   s    zSimpleEditor.disposec          	   C   sˆ   |  j d7  _ yH| j|jƒ  }|| jkrN| jjrN| jjj}|jdƒ |jdƒ || _W n   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   FTr   )Úraise_to_debugN)rI   r5   Ú	GetStringÚvaluer   rG   r?   ZParentZEnableEditingZtraitsui.apirK   )r"   ÚeventZ	new_valueZgridrK   r   r   r   rA   è   s    



zSimpleEditor.update_objectc             C   sº   | j dkr¶| jjƒ }y| j| }W nH   y| jj|ƒ}W n* tk
rd } z| j|ƒ dS d}~X nX Y nX |  j d7  _ y || _| jj	t
ƒ | jjƒ  W n   Y nX |  j d8  _ dS )zK Handles the user typing text into the combo box text entry field.
        r   Nr   )rI   r?   ÚGetValuer5   r   r;   Ú	ExceptionÚerrorrM   ÚSetBackgroundColourr   ÚRefresh)r"   rN   rM   Úexcpr   r   r   rD     s$    


zSimpleEditor.update_text_objectc             C   s   | j |ƒ |jƒ  dS )z8 Handles the control losing the keyboard focus.
        N)rD   ZSkip)r"   rN   r   r   r   rF   !  s    
zSimpleEditor.on_kill_focusc          	   C   sb   | j dkr^| jjdkr>y| jj| j| j ƒ W q^   Y q^X n y| jj| jƒ W n   Y nX dS )z` Updates the editor when the object trait changes externally to the
            editor.
        r   N)	rI   r   r;   r?   ZSetStringSelectionr6   rM   ÚSetValueÚ	str_value)r"   r   r   r   Úupdate_editor+  s    
zSimpleEditor.update_editorc             C   s   | j jtƒ | j jƒ  dS )zN Handles an error that occurs while setting the object's trait value.
        N)r?   rR   r   rS   )r"   rT   r   r   r   rQ   @  s    zSimpleEditor.errorc       	         s  | j ‰ d}d}| jjdk	r¼ˆ jƒ }‡ fdd„t|ƒD ƒ}ˆ jƒ }|| jkr´d}d}x4t|d ddƒD ] }||| krzd}qdˆ j|ƒ qdW x$| jD ]}|s ||kr�ˆ j|ƒ q�W d}nˆ j	ƒ }|rÔˆ j
ƒ  ˆ j| jƒ |dk	�r|  jd7  _ˆ j|ƒ ˆ j|ƒ |  jd8  _| jƒ  dS )	zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        TNc                s   g | ]}ˆ j |ƒ‘qS r   )rL   )Ú.0Úi)r?   r   r   ú
<listcomp>\  s    z/SimpleEditor.rebuild_editor.<locals>.<listcomp>Fr   r:   r:   )r?   r   r;   ZGetCountÚrangerO   r4   ÚDeleteZAppendZGetInsertionPointÚClearÚAppendItemsrI   rU   ZSetInsertionPointrW   )	r"   ÚclearÚcur_nameÚnZ	cur_namesÚincluderY   r   Zpointr   )r?   r   r!   K  s8    



zSimpleEditor.rebuild_editor)r0   r1   r2   r3   r$   r.   rA   rD   rF   rW   rQ   r!   r7   r   r   )r/   r   r8   ¶   s   
r8   c                   s8   e Zd ZdZ‡ f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.
    c                s(   t t| ƒj|ƒ t|dƒ| _| jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   Nr:   )r-   rc   r$   r
   r?   r!   )r"   r#   )r/   r   r   r$   ˆ  s    zRadioEditor.initc          	   C   s"   y|j ƒ j| _W n   Y nX dS )zD Handles the user clicking one of the custom radio buttons.
        N)ZGetEventObjectrM   )r"   rN   r   r   r   rA   –  s    zRadioEditor.update_objectc             C   s>   | j }x2| jjƒ D ]$}|j |k}|j|ƒ |r|jƒ  qW dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)rM   r?   ZGetChildrenrU   ZSetFocus)r"   rM   ÚbuttonÚstater   r   r   rW   ¢  s    

zRadioEditor.update_editorc             C   s¾  | j }|jdƒ |jƒ  | j}| j}| j}t|ƒ}| jj}|| d | }|| g| }|| }	x$t	|ƒD ]}
||
  |	|
k7  < qjW t
dd„ |dd… dƒd  |d< |dkrÂtjd|ddƒ}ntjtjƒ}tj}d}xÖt	|ƒD ]Ê}
xÄt	|ƒD ]¸}|dk�rx||  }}| j|tƒ}tj|d||d�}|| |_d}|j||kƒ tj||jƒ | jƒ | j|ƒ ||| 7 }|d8 }ntj|dd	ƒ}d	|_|jd
ƒ |j|dtjdƒ qðW qâW |j|ƒ dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        Nr   c             S   s   | | S )Nr   )ÚxÚyr   r   r   r   È  s    z,RadioEditor.rebuild_editor.<locals>.<lambda>r   é   é   )r9   r   Fé   r:   r:   r:   r:   )r?   ZSetSizerZDestroyChildrenrV   r4   r5   Úlenr   Úcolsr[   r   r<   Z	GridSizerZBoxSizerZVERTICALZRB_GROUPZstring_valuer   ZRadioButtonrM   rU   ZEVT_RADIOBUTTONr@   rA   rJ   ZShowÚAddZNORTHZSetSizerAndFit)r"   Zpanelr`   r4   r5   ra   rl   ZrowsÚincrÚremrY   Zsizerr9   ÚindexÚjr   Úlabelr?   r   r   r   r!   ²  sL    
"




zRadioEditor.rebuild_editor)	r0   r1   r2   r3   r$   rA   rW   r!   r7   r   r   )r/   r   rc   ~  s
   rc   c                   sD   e Zd ZdZ‡ fd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                sd   t t| ƒj|ƒ tj|dtjddƒtjddƒ| jtjtj	B d�| _
tj|| j
jƒ | jƒ | jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   r   )r9   Nr:   r:   r:   )r-   rs   r$   r<   ZListBoxr=   r>   r4   Z	LB_SINGLEZLB_NEEDED_SBr?   ÚEVT_LISTBOXr@   rA   rJ   )r"   r#   )r/   r   r   r$   ÷  s    zListEditor.initc                s    t | jtjƒ tt| ƒjƒ  dS )z0 Disposes of the contents of an editor.
        N)r   r?   r<   rt   r-   rs   r.   )r"   )r/   r   r   r.     s    zListEditor.disposec             C   sf   | j sb| jjƒ }y| j| }W n*   y| jj|ƒ}W n   Y nX Y nX y
|| _W n   Y nX dS )z5 Handles the user selecting a list box item.
        N)Ú_ignore_updater?   ZGetStringSelectionr5   r   r;   rM   )r"   rN   rM   r   r   r   rA     s    

zListEditor.update_objectc          	   C   s@   | j }y(|j| j| j ƒ}|dkr,|j|ƒ W n   Y nX dS )z` Updates the editor when the object trait changes externally to the
            editor.
        r   N)r?   Z
FindStringr6   rM   ZSetSelection)r"   r?   rp   r   r   r   rW   $  s    zListEditor.update_editorc             C   s0   d| _ | jjƒ  | jj| jƒ d| _ | jƒ  dS )zz Rebuilds the contents of the editor whenever the original factory
            object's **values** trait changes.
        TFN)ru   r?   r]   r^   r4   rW   )r"   r   r   r   r!   5  s
    
zListEditor.rebuild_editor)
r0   r1   r2   r3   r$   r.   rA   rW   r!   r7   r   r   )r/   r   rs   í  s   rs   )r3   Ú
__future__r   r<   Z
traits.apir   Ztraitsui.editors.enum_editorr   Úeditorr   Ú	constantsr   r   Úhelperr	   r
   r   r   Ú	functoolsr   r   r   r8   rc   rs   r   r   r   r   Ú<module>   s   } Io