a
    §ˆc“;  ã                   @   s¤   d 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 dd	lm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.
é    N)ÚProperty)Úenum_values_changedé   )ÚEditor)ÚOKColorÚ
ErrorColor)ÚTraitsUIPanelÚ
disconnectÚdisconnect_no_id)Úreducec                       sj   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                    sx   ˆ j }|jdkrLˆ  |j¡\ˆ _ˆ _ˆ _ˆ  ¡  ˆ jjˆ jdˆ j dd� n(‡ fdd„ˆ _ˆ  ¡  |jˆ jddd� dS )	ú\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú ú Zui)Údispatchc                      s   ˆ j jS ©N)ÚfactoryÚvalues© ©Úselfr   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/enum_editor.pyÚ<lambda>D   ó    z!BaseEditor.init.<locals>.<lambda>r   N)	r   ÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changed)r   Úparentr   r   r   r   Úinit6   s    
ÿÿÿzBaseEditor.initc                 C   s   | j S )z*Gets the current set of enumeration names.)Ú_namesr   r   r   r   Ú
_get_namesJ   s    zBaseEditor._get_namesc                 C   s   | j S )zGets the current mapping.)Ú_mappingr   r   r   r   Ú_get_mappingN   s    zBaseEditor._get_mappingc                 C   s   | j S )z!Gets the current inverse mapping.)Ú_inverse_mappingr   r   r   r   Ú_get_inverse_mappingR   s    zBaseEditor._get_inverse_mappingc                 C   s   t ‚dS )úuRebuilds the contents of the editor whenever the original factory
        object's **values** trait changes.
        N)ÚNotImplementedErrorr   r   r   r   Úrebuild_editorV   s    zBaseEditor.rebuild_editorc                 C   s    t |  ¡ | jƒ\| _| _| _dS )zsRecomputes the cached data based on the underlying enumeration model
        or the values of the factory.
        N)r   r   Ústring_valuer#   r%   r'   r   r   r   r   r   \   s    üýzBaseEditor.values_changedc                 C   s   |   ¡  |  ¡  dS )ziHandles the underlying object model's enumeration set or factory's
        values being changed.
        N)r   r+   r   r   r   r   r    f   s    zBaseEditor._values_changedc                    sH   | j dur&| j j| jd| j dd� n| jj| jddd� tƒ  ¡  dS )ú&Disposes of the contents of an editor.Nr   T)Úremover   )r   r   r    r   r   ÚsuperÚdisposer   ©Ú	__class__r   r   r0   m   s    
ÿÿzBaseEditor.dispose)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesÚmappingÚinverse_mappingr"   r$   r&   r(   r+   r   r    r0   Ú__classcell__r   r   r1   r   r   &   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 )ÚSimpleEditorz?Simple style of enumeration editor, which displays a combo box.c              	      s
  t ƒ  |¡ | j}|jdu rbt |dt dd¡t dd¡| j¡ | _	}|j
tj| j| j	 ¡ d� n–tj|ddt dd¡t dd¡| jtjd� | _	}|j
tj| j| ¡ d� |j
tj| j| ¡ d� | 
tj| j¡ |jsø|jrø|j
tj| j| ¡ d� d| _|  ¡  dS )r   Néÿÿÿÿr   ©Úidr   ©Ústyle)r/   r"   r   ÚevaluateÚwxZChoiceÚPointÚSizer7   ÚcontrolÚBindZ
EVT_CHOICEÚupdate_objectÚGetIdZ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   rE   r1   r   r   r"   ƒ   s>    
ÿ
ÿ

ù	ÿÿÿzSimpleEditor.initc                    s2   t | jtjtjtjƒ t| jtjƒ tƒ  	¡  dS ©r-   N)
r	   rE   rB   rI   rJ   rO   r
   rL   r/   r0   r   r1   r   r   r0   ¬   s
    ÿzSimpleEditor.disposec                 C   sˆ   |  j d7  _ zH| j| ¡  }|| jkrN| jjrN| jjj}| d¡ | d¡ || _W n   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   FTr   )Úraise_to_debugN)rP   r8   Ú	GetStringÚvaluer   rN   rE   ZParentZEnableEditingZtraitsui.apirS   )r   ÚeventÚ	new_valueZgridrS   r   r   r   rG   ¶   s    



zSimpleEditor.update_objectc                 C   sÂ   | j dkr¾| j ¡ }z| j| }W nP   z| j |¡}W n2 tyl } z|  |¡ W Y d}~Y dS d}~0 0 Y n0 |  j d7  _ z || _| j 	t
¡ | j ¡  W n   Y n0 |  j d8  _ dS )zAHandles the user typing text into the combo box text entry field.r   Nr   )rP   rE   ÚGetValuer8   r   rA   Ú	ExceptionÚerrorrU   ÚSetBackgroundColourr   ÚRefresh)r   rV   rU   Úexcpr   r   r   rK   Ð   s$    


 zSimpleEditor.update_text_objectc                 C   s   |   |¡ | ¡  dS )z.Handles the control losing the keyboard focus.N)rK   ZSkip©r   rV   r   r   r   rM   æ   s    
zSimpleEditor.on_kill_focusc                 C   sb   | j dkr^| jjdu r>z| j | j| j ¡ W q^   Y q^0 n z| j | j¡ W n   Y n0 dS ©ú[Updates the editor when the object trait changes externally to the
        editor.
        r   N)	rP   r   rA   rE   ZSetStringSelectionr9   rU   ÚSetValueÚ	str_valuer   r   r   r   Úupdate_editorë   s    

ÿzSimpleEditor.update_editorc                 C   s   | j  t¡ | j  ¡  dS )zDHandles an error that occurs while setting the object's trait value.N)rE   r[   r   r\   )r   r]   r   r   r   rZ   ý   s    zSimpleEditor.errorc           	         s  | j ‰ d}d}| jjdur´ˆ  ¡ }‡ fdd„t|ƒD ƒ}ˆ  ¡ }|| jv r¬d}d}t|d ddƒD ] }||| krxd}qbˆ  |¡ qb| jD ]}|sš||krŠˆ  |¡ qŠd}nˆ  	¡ }|rÌˆ  
¡  ˆ  | j¡ |du�r|  jd7  _ˆ  |¡ ˆ  |¡ |  jd8  _|  ¡  dS )r)   TNc                    s   g | ]}ˆ   |¡‘qS r   )rT   )Ú.0Úi©rE   r   r   Ú
<listcomp>  r   z/SimpleEditor.rebuild_editor.<locals>.<listcomp>Fr   r<   )rE   r   rA   ZGetCountÚrangerX   r7   ÚDeleteZAppendZGetInsertionPointÚClearÚAppendItemsrP   ra   ZSetInsertionPointrc   )	r   ÚclearÚcur_nameÚnZ	cur_namesÚincludere   r   Zpointr   rf   r   r+     s8    




zSimpleEditor.rebuild_editor)r3   r4   r5   r6   r"   r0   rG   rK   rM   rc   rZ   r+   r:   r   r   r1   r   r;   €   s   )
r;   c                       s8   e Zd ZdZ‡ f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.
    c                    s$   t ƒ  |¡ t|dƒ| _|  ¡  dS )r   r<   N)r/   r"   r   rE   r+   ©r   r!   r1   r   r   r"   6  s    zRadioEditor.initc                 C   s"   z|  ¡ j| _W n   Y n0 dS )z:Handles the user clicking one of the custom radio buttons.N)ZGetEventObjectrU   r^   r   r   r   rG   @  s    zRadioEditor.update_objectc                 C   s:   | j }| j ¡ D ]$}|j |k}| |¡ |r| ¡  qdS )r`   N)rU   rE   ZGetChildrenra   ZSetFocus)r   rU   ÚbuttonÚstater   r   r   rc   G  s    

zRadioEditor.update_editorc                 C   s¸  | j }| d¡ | ¡  | j}| j}| j}t|ƒ}| jj}|| d | }|| g| }|| }	t	|ƒD ]}
||
  |	|
k7  < qht
dd„ |dd… dƒd  |d< |dkr¾t d|dd¡}nt tj¡}tj}d}t	|ƒD ]Ì}
t	|ƒD ]¾}|dk�rv||  }}|  |tj¡}tj|d||d	�}|| |_d}| ||k¡ |jtj| j| ¡ d
� |  |¡ ||| 7 }|d8 }nt |dd¡}d|_| d¡ | |dtjd¡ qèqÜ| |¡ dS )r)   Nr   c                 S   s   | | S r   r   )ÚxÚyr   r   r   r   k  r   z,RadioEditor.rebuild_editor.<locals>.<lambda>r<   r   é   é   r?   r=   r   Fé   )rE   ZSetSizerZDestroyChildrenrb   r7   r8   Úlenr   Úcolsrh   r   rB   Z	GridSizerZBoxSizerZVERTICALZRB_GROUPr,   ÚstrÚ
capitalizeZRadioButtonrU   ra   rF   ZEVT_RADIOBUTTONrG   rH   rQ   ZShowÚAddZNORTHZSetSizerAndFit)r   Zpanelrm   r7   r8   rn   rz   ÚrowsÚincrÚremre   Zsizerr@   ÚindexÚjr   ÚlabelrE   r   r   r   r+   R  sR    
"

ý


zRadioEditor.rebuild_editor)	r3   r4   r5   r6   r"   rG   rc   r+   r:   r   r   r1   r   rp   1  s
   
rp   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 )Ú
ListEditorzSEnumeration editor, used for the "custom" style, that displays a list
    box.
    c                    sd   t ƒ  |¡ tj|dt dd¡t dd¡| jtjtjB d�| _	|j
tj| j| j	 ¡ d� |  ¡  dS )r   r<   r   r?   r=   N)r/   r"   rB   ZListBoxrC   rD   r7   Z	LB_SINGLEZLB_NEEDED_SBrE   rF   ÚEVT_LISTBOXrG   rH   rQ   rq   r1   r   r   r"   ™  s    


úÿzListEditor.initc                    s   t | jtjƒ tƒ  ¡  dS rR   )r	   rE   rB   r…   r/   r0   r   r1   r   r   r0   ­  s    zListEditor.disposec              	   C   sf   | j sb| j ¡ }z| j| }W n*   z| j |¡}W n   Y n0 Y n0 z
|| _W n   Y n0 dS )z+Handles the user selecting a list box item.N)Ú_ignore_updaterE   ZGetStringSelectionr8   r   rA   rU   )r   rV   rU   r   r   r   rG   ³  s    

zListEditor.update_objectc                 C   s@   | j }z(| | j| j ¡}|dkr,| |¡ W n   Y n0 dS r_   )rE   Z
FindStringr9   rU   ZSetSelection)r   rE   r�   r   r   r   rc   Ã  s    zListEditor.update_editorc                 C   s0   d| _ | j ¡  | j | j¡ d| _ |  ¡  dS )r)   TFN)r†   rE   rj   rk   r7   rc   r   r   r   r   r+   Ï  s
    
zListEditor.rebuild_editor)
r3   r4   r5   r6   r"   r0   rG   rc   r+   r:   r   r   r1   r   r„   ”  s   r„   )r6   rB   Z
traits.apir   Ztraitsui.helperr   Zeditorr   Ú	constantsr   r   Úhelperr   r	   r
   Ú	functoolsr   r   r;   rp   r„   r   r   r   r   Ú<module>   s   Z 2c