a
    §ˆc=B  ã                   @   sP   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	 G dd	„ d	eƒZ
dS )
zB Defines the set editors for the wxPython user interface toolkit.
é    N)ÚProperty)Úenum_values_changedé   )ÚEditor)ÚTraitsUIPanelc                       s  e Zd ZdZeƒ Zeƒ Zeƒ Z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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d#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Z d3d4„ Z!d5d6„ Z"d7d8„ Z#‡  Z$S )9ÚSimpleEditora’  Simple style of editor for sets.

    The editor displays two list boxes, with buttons for moving the selected
    items from left to right, or vice versa. If **can_move_all** on the
    factory is True, then buttons are displayed for moving all the items to
    one box or the other. If the set is ordered, buttons are displayed for
    moving the selected item up or down in right-side list box.
    Tc                    sÆ  ˆ j }|jdkrHˆ  |j¡\ˆ _ˆ _ˆ _ˆ  ¡  ˆ jjˆ jˆ jdd� n(‡ fdd„ˆ _ˆ  ¡  |jˆ jddd� t	|dƒ ˆ _
}t tj¡}t tj¡}ˆ  ||ˆ jˆ j|j¡ˆ _d ˆ _ ˆ _ ˆ _ˆ _|jræˆ  d	||d
ˆ j¡ˆ _ˆ  d||d
ˆ j¡ˆ _ˆ  d||dˆ j¡ˆ _|j�r0ˆ  d||d
ˆ j¡ˆ _|j�rdˆ  d||dˆ j ¡ˆ _ˆ  d||dˆ j!¡ˆ _| "|dtj#tj$B d¡ ˆ  ||ˆ j%ˆ j|j&¡ˆ _'| (|¡ ˆ j)jˆ j*ˆ j+d dd� ˆ  ,¡  dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú Úui)Údispatchc                      s   ˆ j jS ©N)ÚfactoryÚvalues© ©Úselfr   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/set_editor.pyÚ<lambda>I   ó    z#SimpleEditor.init.<locals>.<lambda>r   éÿÿÿÿNz>>é   ú>ú<r   z<<zMove Upé   z	Move Downé   ú_items?)-r   ÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changedr   ÚcontrolÚwxÚBoxSizerZ
HORIZONTALÚVERTICALÚ_create_listboxÚ
_on_unusedÚ_on_useZleft_column_titleÚ_unusedÚ_use_allÚ
_unuse_allÚ_upÚ_downÚcan_move_allÚ_create_buttonÚ_on_use_allÚ_useÚ	_on_unuseÚ_unuseÚ_on_unuse_allÚorderedÚ_on_upÚ_on_downÚAddÚLEFTÚRIGHTÚ	_on_valueZright_column_titleÚ_usedZSetSizerÚcontext_objectÚupdate_editorÚextended_nameZset_tooltip)r   Úparentr   ZpanelZhsizerZvsizerr   r   r   Úinit;   sr    
ÿ
ÿÿûÿÿÿÿÿû
ÿzSimpleEditor.initc                 C   s   | j S )z*Gets the current set of enumeration names.)Ú_namesr   r   r   r   Ú
_get_names‚   s    zSimpleEditor._get_namesc                 C   s   | j S )zGets the current mapping.)Ú_mappingr   r   r   r   Ú_get_mapping†   s    zSimpleEditor._get_mappingc                 C   s   | j S )z!Gets the current inverse mapping.)Ú_inverse_mappingr   r   r   r   Ú_get_inverse_mappingŠ   s    z!SimpleEditor._get_inverse_mappingc                 C   sÄ   t  t j¡}t  |d|¡}| ¡ }t  | ¡ d | ¡ | ¡ t j	¡}	| 
|	¡ | |dd¡ t j|dt jt jB d�}
| |
dt j¡ | |dt j¡ |jt j||
 ¡ d� |jt j||
 ¡ d� |
S )zCreates a list box.r   r   r   ©Ústyle©Úid)r#   r$   r%   Z
StaticTextZGetFontZFontZGetPointSizeZ	GetFamilyZGetStyleZBOLDZSetFontr8   ZListBoxZLB_EXTENDEDZLB_NEEDED_SBÚEXPANDÚBindZEVT_LISTBOXÚGetIdZEVT_LISTBOX_DCLICK)r   r@   ÚsizerZhandler1Zhandler2ÚtitleZcolumn_sizerZtitle_widgetÚfontZemphasis_fontÚlistr   r   r   r&   Ž   s    ÿ
zSimpleEditor._create_listboxc                 C   sP   t j|d|t jd�}| |¡ | |dt jt jB d¡ |jt j|| 	¡ d� |S )zCreates a button.r   rH   r   r   rJ   )
r#   ZButtonZBU_EXACTFITZ	AddSpacerr8   rL   ZBOTTOMrM   Z
EVT_BUTTONrN   )r   Úlabelr@   rO   Zspace_beforeÚhandlerÚbuttonr   r   r   r/   ¨   s
    
zSimpleEditor._create_buttonc                 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   Zstring_valuerB   rD   rF   r   r   r   r   r   °   s    üýzSimpleEditor.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!   º   s    zSimpleEditor._values_changedc                    s¾  | j  ¡ ‰ ‡ fdd„| jD ƒ}t|ƒt| jƒk r:|| _dS | j}|  |¡}| j}|  |¡}| ¡  | ¡  | jj	s‚t
|dd… ƒ}g }t|ƒD ]2\}}ˆ | }	| |	¡ ˆ |= |	|v rŽ| |¡ qŽg }
t
ˆ  ¡ ƒ}| j‰ ‡ fdd„|D ƒ| _t|ƒD ]$\}}| |¡ ||v rô|
 |¡ qôt|ƒdk�rZt|
ƒdk�rZ| ¡ dk�rP| d¡ n
|
 d¡ | ¡ }|D ]}||k �rf| |¡ �qf| ¡ }|
D ]}||k �rŽ| |¡ �qŽ|  ¡  |  ¡  dS )z[Updates the editor when the object trait changes externally to the
        editor.
        c                    s   g | ]}|ˆ v r|‘qS r   r   )Ú.0Úv©Úmappingr   r   Ú
<listcomp>È   r   z.SimpleEditor.update_editor.<locals>.<listcomp>Nc                    s   g | ]}ˆ | ‘qS r   r   )rV   r	   rX   r   r   rZ   ë   r   r   )Úinverse_mappingÚcopyÚvalueÚlenr<   Ú_get_selected_stringsr)   ZClearr   r5   ÚsortedÚ	enumerateZAppendÚappendr   rY   Ú_unused_itemsÚGetCountÚSetSelectionÚ_check_up_downÚ_check_left_right)r   r   ÚusedZused_labelsZunusedZunused_labelsZused_selectionsÚir]   rS   Zunused_selectionsZunused_itemsZunused_itemZ
used_countZunused_countr   rX   r   r>   Á   sT    







zSimpleEditor.update_editorc                    s^   | j dur"| j j| j| jdd� n| jj| jddd� | jj| j| jd dd� tƒ  	¡  dS )z&Disposes of the contents of an editor.NT)Úremover   r   )
r   r    r!   r   r   r=   r>   r?   ÚsuperÚdisposer   ©Ú	__class__r   r   rl     s    

ÿÿÿzSimpleEditor.disposec                 C   s   | j | jgS )z9Returns the editor's control for indicating error status.)r)   r<   r   r   r   r   Úget_error_control  s    zSimpleEditor.get_error_controlc                 C   s(   | j js|  | j¡ |  ¡  |  ¡  d S r   )r   r5   Ú_clear_selectionr)   rg   rf   ©r   Úeventr   r   r   r;     s    zSimpleEditor._on_valuec                 C   s(   | j js|  | j¡ |  ¡  |  ¡  d S r   )r   r5   rp   r<   rg   rf   rq   r   r   r   r'   %  s    zSimpleEditor._on_unusedc                 C   s$   |   | j| j| j| j¡\| _| _d S r   )Ú_transfer_itemsr)   r<   rc   r]   rq   r   r   r   r(   +  s    ÿzSimpleEditor._on_usec                 C   s$   |   | j| j| j| j¡\| _| _d S r   )rs   r<   r)   r]   rc   rq   r   r   r   r2   0  s    ÿzSimpleEditor._on_unusec                 C   s$   |   | j| j| j| j¡\| _| _d S r   )Ú_transfer_allr)   r<   rc   r]   rq   r   r   r   r0   5  s    ÿzSimpleEditor._on_use_allc                 C   s$   |   | j| j| j| j¡\| _| _d S r   )rt   r<   r)   r]   rc   rq   r   r   r   r4   :  s    ÿzSimpleEditor._on_unuse_allc                 C   s   |   d¡ d S )Nr   ©Ú
_move_itemrq   r   r   r   r6   ?  s    zSimpleEditor._on_upc                 C   s   |   d¡ d S )Nr   ru   rq   r   r   r   r7   B  s    zSimpleEditor._on_downc                 C   s   |  ¡ D ]}| |¡ qdS )z(Unselects all items in the given ListBoxN)ÚGetSelectionsÚDeselect)r   Zboxri   r   r   r   rp   M  s    zSimpleEditor._clear_selectionc                 C   s”   |dd… }|dd… }|   |¡ | ¡ dkrr| ¡ }| d¡ | |  |¡|¡ | d¡ | |d ¡ |d= q"| d¡ |  ¡  |  ¡  ||fS )z-Transfers all items from one list to another.Nr   )	rp   rd   re   ÚInsertItemsr_   ÚDeleterb   rg   rf   )r   Ú	list_fromÚlist_toÚvalues_fromÚ	values_toÚindex_tor   r   r   rt   R  s     


ÿ

zSimpleEditor._transfer_allc                 C   s  |dd… }|dd… }|  ¡ }t|  |¡dƒ}t|  |¡dƒ}|  |¡ |  |¡}| ¡  | ||¡ tt|ƒd ddƒD ]}	| 	||	 ¡ q||D ]B}
| 
| j|
 ¡}| ||| ¡ ||= | jjr”| | |
¡¡ q”| ¡ }|dk�r||krú|d8 }| |¡ |  ¡  |  ¡  ||fS )z5Transfers the selected item from one list to another.Nr   r   r   )rw   ÚmaxÚ_get_first_selectionrp   r_   Úreversery   Úranger^   rz   ÚindexrY   Úinsertr   r5   re   Z
FindStringrd   rg   rf   )r   r{   r|   r}   r~   Zindices_fromÚ
index_fromr   Zselected_listri   Z
item_labelZval_index_fromÚcountr   r   r   rs   h  s2    



zSimpleEditor._transfer_itemsc           	      C   s²   | j }|  |¡}|| }| |¡}| |¡ | |¡ | ||¡ | |¡ |  ¡  | j}|dk rx|}|| || g}n|}|| || g}|d|… | ||d d…  | _dS )z0Moves an item up or down within the "used" list.r   Né   )	r<   r�   Ú	GetStringrx   rz   ZInsertre   rf   r]   )	r   Ú	directionÚlistboxr†   r   rS   r]   r„   r   r   r   r   rv   ”  s     




zSimpleEditor._move_itemc                 C   s`   | j jr\| j ¡ }| j t|ƒdko.|d dk¡ | j t|ƒdkoX|d | j ¡ d k ¡ dS )z:Sets the proper enabled state for the up and down buttons.r   r   N)	r   r5   r<   rw   r,   ÚEnabler^   r-   rd   )r   Zindex_selectedr   r   r   rf   ­  s    
ÿþzSimpleEditor._check_up_downc                 C   s¤   | j  | j ¡ dko"|  | j¡dk¡ | j | j ¡ dkoH|  | j¡dk¡ | jjr | j	 | j ¡ dkov|  | j¡dk¡ | j
 | j ¡ dkoœ|  | j¡dk¡ dS )z=Sets the proper enabled state for the left and right buttons.r   N)r1   rŒ   r)   rd   r�   r3   r<   r   r.   r*   r+   r   r   r   r   rg   ¹  s"    þþþþzSimpleEditor._check_left_rightc                 C   s&   g }|  ¡ D ]}| | |¡¡ q|S )z>Returns a list of the selected strings in the given *listbox*.)rw   rb   r‰   )r   r‹   Z
stringlistZlabel_indexr   r   r   r_   Ò  s    z"SimpleEditor._get_selected_stringsc                 C   s    |  ¡ }t|ƒdkrdS |d S )z7Returns the index of the first (or only) selected item.r   r   )rw   r^   )r   r‹   Zselect_listr   r   r   r�   Þ  s    z!SimpleEditor._get_first_selection)%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesrY   r[   Z
scrollablerA   rC   rE   rG   r&   r/   r   r!   r>   rl   ro   r;   r'   r(   r2   r0   r4   r6   r7   rp   rt   rs   rv   rf   rg   r_   r�   Ú__classcell__r   r   rm   r   r   !   s@   G
E,r   )r�   r#   Z
traits.apir   Ztraitsui.helperr   Zeditorr   Úhelperr   r   r   r   r   r   Ú<module>   s   