3
8ßL]0T  ã               @   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 G d	d
„ d
e	ƒZdS )zB Defines the set editors for the wxPython user interface toolkit.
é    )Úabsolute_importN)ÚProperty)ÚToolkitEditorFactoryé   )ÚEditor)Úenum_values_changedÚ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             C   s°  | j }|jdkrH| j|jƒ\| _| _| _| jƒ  | jj| j| jdd� n|j| j	ddd� t
|dƒ | _}tjtjƒ}tjtjƒ}| j||| j| j|jƒ| _d | _ | _ | _| _|jrÐ| jd||d| jƒ| _| jd	||d| jƒ| _| jd
||d| jƒ| _|j�r| jd||d| jƒ| _|j �rN| jd||d| j!ƒ| _| jd||d| j"ƒ| _|j#|dtj$tj%B dƒ | j||| j&| j|j'ƒ| _(|j)|ƒ | j*j| j	| j+d dd� | j,ƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        Ú Úui)ÚdispatchÚvalues_modifiedr   Nz>>é   ú>ú<r   z<<zMove Upé   z	Move Downé   z_items?éÿÿÿÿ)-ÚfactoryÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changedÚupdate_editorr   Ú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Úextended_nameZset_tooltip)ÚselfÚparentr   ZpanelZhsizerZvsizer© r<   ú</tmp/pip-install-5sebvq2j/traitsui/traitsui/wx/set_editor.pyÚinitS   s\    











zSimpleEditor.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_names”   s    
zSimpleEditor._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_mapping    s    
zSimpleEditor._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_mapping¬   s    
z!SimpleEditor._get_inverse_mappingc             C   s¼   t jt jƒ}t j|d|ƒ}|jƒ }t j|jƒ d |jƒ |jƒ t j	ƒ}	|j
|	ƒ |j|ddƒ t j|dt jt jB d�}
|j|
dt jƒ |j|dt jƒ t j||
jƒ |ƒ t j||
jƒ |ƒ |
S )z Creates a list box.
        r   r   )Ústyler   r   )r   r   r    Z
StaticTextZGetFontZFontZGetPointSizeZ	GetFamilyZGetStyleZBOLDZSetFontr3   ZListBoxZLB_EXTENDEDZLB_NEEDED_SBÚEXPANDZ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�}|j||fƒ |j|dt jt jB dƒ t j||jƒ |ƒ |S )z Creates a button.
        r   )rE   r   r   r   )	r   ZButtonZBU_EXACTFITZ	AddSpacerr3   rF   ZBOTTOMZ
EVT_BUTTONrG   )r:   Úlabelr;   rH   Zspace_beforeÚhandlerÚbuttonr<   r<   r=   r*   Ø   s
    zSimpleEditor._create_buttonc             C   s   t | jƒ ƒ\| _| _| _dS )zO Recomputes the cached data based on the underlying enumeration model.
        N)r   r   r?   rA   rC   )r:   r<   r<   r=   r   å   s    zSimpleEditor.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SimpleEditor._values_changedc                sÎ  | j jƒ ‰ ‡ fdd„| jD ƒ}t|ƒt| jƒk r:|| _dS | j}| j|ƒ}| j}| j|ƒ}|jƒ  |jƒ  | jj	s‚t
|dd… ƒ}g }x>t|ƒD ]2\}}ˆ | }	|j|	ƒ ˆ |= |	|kr�|j|ƒ q�W g }
t
ˆ jƒ ƒ}| j‰ ‡ fdd„|D ƒ| _x0t|ƒD ]$\}}|j|ƒ ||krú|
j|ƒ qúW t|ƒdk�rbt|
ƒdk�rb|jƒ dk�rX|jdƒ n
|
jdƒ |jƒ }x"|D ]}||k �rp|j|ƒ �qpW |jƒ }x"|
D ]}||k �rœ|j|ƒ �qœW | jƒ  | jƒ  dS )z` Updates the editor when the object trait changes externally to the
            editor.
        c                s   g | ]}|ˆ kr|‘qS r<   r<   )Ú.0Úv)Úmappingr<   r=   ú
<listcomp>   s    z.SimpleEditor.update_editor.<locals>.<listcomp>Nc                s   g | ]}ˆ | ‘qS r<   r<   )rO   r   )rQ   r<   r=   rR   #  s    r   )Úinverse_mappingÚcopyÚvalueÚlenr7   Ú_get_selected_stringsr$   ZClearr   r0   ÚsortedÚ	enumerateZAppendÚappendÚvaluesrQ   Ú_unused_itemsÚGetCountÚSetSelectionÚ_check_up_downÚ_check_left_right)r:   r[   ÚusedZused_labelsZunusedZunused_labelsZused_selectionsÚirU   rL   Zunused_selectionsZunused_itemsZunused_itemZ
used_countZunused_countr<   )rQ   r=   r   ù   sT    









zSimpleEditor.update_editorc                sb   | j dk	r"| j j| j| jdd� n| jj| jddd� | jj| j| jd dd� tt	| ƒj
ƒ  dS )z0 Disposes of the contents of an editor.
        NT)Úremover   z_items?)r   r   r   r   r   r   r8   r9   Úsuperr	   Údispose)r:   )Ú	__class__r<   r=   re   B  s    



zSimpleEditor.disposec             C   s   | j | jgS )zC Returns the editor's control for indicating error status.
        )r$   r7   )r:   r<   r<   r=   Úget_error_controlW  s    zSimpleEditor.get_error_controlc             C   s(   | j js| j| jƒ | jƒ  | jƒ  d S )N)r   r0   Ú_clear_selectionr$   r`   r_   )r:   Úeventr<   r<   r=   r6   `  s    zSimpleEditor._on_valuec             C   s(   | j js| j| jƒ | jƒ  | jƒ  d S )N)r   r0   rh   r7   r`   r_   )r:   ri   r<   r<   r=   r"   f  s    zSimpleEditor._on_unusedc             C   s$   | j | j| j| j| jƒ\| _| _d S )N)Ú_transfer_itemsr$   r7   r\   rU   )r:   ri   r<   r<   r=   r#   l  s    zSimpleEditor._on_usec             C   s$   | j | j| j| j| jƒ\| _| _d S )N)rj   r7   r$   rU   r\   )r:   ri   r<   r<   r=   r-   p  s    zSimpleEditor._on_unusec             C   s$   | j | j| j| j| jƒ\| _| _d S )N)Ú_transfer_allr$   r7   r\   rU   )r:   ri   r<   r<   r=   r+   t  s    zSimpleEditor._on_use_allc             C   s$   | j | j| j| j| jƒ\| _| _d S )N)rk   r7   r$   rU   r\   )r:   ri   r<   r<   r=   r/   x  s    zSimpleEditor._on_unuse_allc             C   s   | j dƒ d S )Nr   r   )Ú
_move_item)r:   ri   r<   r<   r=   r1   |  s    zSimpleEditor._on_upc             C   s   | j dƒ d S )Nr   )rl   )r:   ri   r<   r<   r=   r2     s    zSimpleEditor._on_downc             C   s"   x|j ƒ D ]}|j|dƒ q
W dS )z2 Unselects all items in the given ListBox
        FN)ÚGetSelectionsr^   )r:   Zboxrb   r<   r<   r=   rh   Š  s    zSimpleEditor._clear_selectionc             C   s˜   |dd… }|dd… }| j |ƒ xR|jƒ dkrt|jƒ }|jdƒ |j| j|ƒ|ƒ |jdƒ |j|d ƒ |d= q$W |jdƒ | jƒ  | jƒ  ||fS )z7 Transfers all items from one list to another.
        Nr   )	rh   r]   r^   ÚInsertItemsrW   ÚDeleterZ   r`   r_   )r:   Ú	list_fromÚlist_toÚvalues_fromÚ	values_toÚindex_tor<   r<   r=   rk   ”  s    




zSimpleEditor._transfer_allc             C   s&  |dd… }|dd… }|j ƒ }t| j|ƒdƒ}t| j|ƒdƒ}| j|ƒ | j|ƒ}|jƒ  |j||ƒ x*tt|ƒd ddƒD ]}	|j	||	 ƒ q~W xJ|D ]B}
|j
| j|
 ƒ}|j||| ƒ ||= | jjrš|j|j|
ƒƒ qšW |jƒ }|dk�r||k�r|d8 }|j|ƒ | jƒ  | jƒ  ||fS )z? Transfers the selected item from one list to another.
        Nr   r   r   r   )rm   ÚmaxÚ_get_first_selectionrh   rW   Úreversern   ÚrangerV   ro   ÚindexrQ   Úinsertr   r0   r^   Z
FindStringr]   r`   r_   )r:   rp   rq   rr   rs   Zindices_fromÚ
index_fromrt   Zselected_listrb   Z
item_labelZval_index_fromÚcountr<   r<   r=   rj   ®  s2    





zSimpleEditor._transfer_itemsc       	      C   s¨   | j }| j|ƒ}|| }|j|ƒ}|j|ƒ |j||ƒ |j|ƒ | jƒ  | j}|dk rn|}|| || g}n|}|| || g}|d|… | ||d d…  | _dS )z: Moves an item up or down within the "used" list.
        r   Né   )r7   rv   Ú	GetStringro   ZInsertr^   r_   rU   )	r:   Ú	directionÚlistboxr{   rt   rL   rU   ry   r[   r<   r<   r=   rl   ß  s    



zSimpleEditor._move_itemc             C   s`   | j jr\| jjƒ }| jjt|ƒdko.|d dkƒ | jjt|ƒdkoX|d | jjƒ d k ƒ dS )zD Sets the proper enabled state for the up and down buttons.
        r   r   N)	r   r0   r7   rm   r'   ÚEnablerV   r(   r]   )r:   Zindex_selectedr<   r<   r=   r_   ü  s    
zSimpleEditor._check_up_downc             C   s¤   | j j| jjƒ dko"| j| jƒdkƒ | jj| jjƒ dkoH| j| jƒdkƒ | jjr | j	j| jjƒ dkov| j| jƒdkƒ | j
j| jjƒ dkoœ| j| jƒdkƒ dS )zG Sets the proper enabled state for the left and right buttons.
        r   N)r,   r�   r$   r]   rv   r.   r7   r   r)   r%   r&   )r:   r<   r<   r=   r`     s    zSimpleEditor._check_left_rightc             C   s*   g }x |j ƒ D ]}|j|j|ƒƒ qW |S )zH Returns a list of the selected strings in the given *listbox*.
        )rm   rZ   r~   )r:   r€   Z
stringlistZlabel_indexr<   r<   r=   rW   "  s    z"SimpleEditor._get_selected_stringsc             C   s    |j ƒ }t|ƒdkrdS |d S )zA Returns the index of the first (or only) selected item.
        r   r   r   )rm   rV   )r:   r€   Zselect_listr<   r<   r=   rv   /  s    z!SimpleEditor._get_first_selection)%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚnamesrQ   rS   Z
scrollabler>   r@   rB   rD   r!   r*   r   r   r   re   rg   r6   r"   r#   r-   r+   r/   r1   r2   rh   rk   rj   rl   r_   r`   rW   rv   Ú__classcell__r<   r<   )rf   r=   r	   4   s@   A 

I	
1r	   )r…   Ú
__future__r   r   Z
traits.apir   Ztraitsui.editors.set_editorr   Úeditorr   Úhelperr   r   r	   r<   r<   r<   r=   Ú<module>   s   	