3
8ßL]O  ã               @   sp   d Z ddlmZ ddlmZm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ZG d
d„ deƒZd	S )z> Defines the set editors for the PyQt user interface toolkit.
é    )Úabsolute_import)ÚQtCoreÚQtGui)ÚToolkitEditorFactory)Úenum_values_changedé   )ÚEditor)ÚInstanceÚPropertyNc                   s  e Zd ZdZeejƒ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&‡  Z'S )7Ú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Š  t jƒ | _t j| jƒ| _| jjddddƒ | j}|jdkrr| j|jƒ\| _	| _
| _| jƒ  | j	j| j| j
dd� n|j| jddd� t jƒ }| jd| j| j|jƒ| _d | _ | _ | _| _|jrÔ| jd|| jƒ| _| jd|| jƒ| _| jd	|| jƒ| _|j�r| jd
|| j ƒ| _|j!�r>| jd|| j"ƒ| _| jd|| j#ƒ| _| jj$|ddt%j&j'ƒ | jd| j(| j|j)ƒ| _*| j+j| j| j,d dd� dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   Ú Úui)ÚdispatchÚvalues_modifiedNz>>ú>ú<z<<zMove Upz	Move Downr   é   z_items?)-r   ZQWidgetÚcontrolZQGridLayoutÚroot_layoutZsetContentsMarginsÚfactoryÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changedÚupdate_editorZQVBoxLayoutÚ_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_downZ	addLayoutr   ÚQtZAlignCenterÚ	_on_valueZright_column_titleÚ_usedÚcontext_objectÚextended_name)ÚselfÚparentr   Zblayout© r7   ú=/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/set_editor.pyÚinitN   sD    








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)r5   r7   r7   r8   Ú
_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)r5   r7   r7   r8   Ú_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)r5   r7   r7   r8   Ú_get_inverse_mappingž   s    
z!SimpleEditor._get_inverse_mappingc             C   s–   t j|ƒ}t j|jƒ ƒ}|jdƒ |j|jƒ d ƒ |j|ƒ | jj	|d|t
jjƒ t jƒ }|jt jjƒ | jj	|d|ƒ |jj|ƒ |jj|ƒ |S )zCreates a list box.
        Tr   r   )r   ZQLabelZQFontÚfontZsetBoldZsetPointSizeZ	pointSizeZsetFontr   Ú	addWidgetr   r0   Z	AlignLeftZQListWidgetZsetSelectionModeZQAbstractItemViewZExtendedSelectionZitemClickedÚconnectZitemDoubleClicked)r5   ÚcolZhandler1Zhandler2ÚtitleZtitle_widgetr@   Úlistr7   r7   r8   r   ª   s    


zSimpleEditor._create_listboxc             C   s$   t j|ƒ}|jj|ƒ |j|ƒ |S )z Creates a button.
        )r   ZQPushButtonZclickedrB   rA   )r5   ÚlabelZlayoutÚhandlerÚbuttonr7   r7   r8   r'   Ã   s    

zSimpleEditor._create_buttonc             C   s    t | jƒ | jƒ\| _| _| _dS )zO Recomputes the cached data based on the underlying enumeration model.
        N)r   r   Zstring_valuer:   r<   r>   )r5   r7   r7   r8   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   )r5   r7   r7   r8   r   Ù   s    zSimpleEditor._values_changedc                sÖ  | j jƒ ‰ ‡ fdd„| jD ƒ}t|ƒt| jƒk r6|| _| 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�r^t|
ƒdk�r^|jƒ dk�rT|jdƒ n
|
jdƒ |jƒ }x(|D ] }||k �rl|j|ƒjdƒ �qlW |jƒ }x(|
D ] }||k �rž|j|ƒjdƒ �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 r7   r7   )Ú.0Úv)Úmappingr7   r8   ú
<listcomp>ê   s    z.SimpleEditor.update_editor.<locals>.<listcomp>Nc                s   g | ]}ˆ | ‘qS r7   r7   )rI   r   )rK   r7   r8   rL     s    r   T)Úinverse_mappingÚcopyÚvalueÚlenr2   Ú_get_selected_stringsr!   Úclearr   r-   ÚsortedÚ	enumerateZaddItemÚappendÚvaluesrK   Ú_unused_itemsÚcountÚitemÚsetSelectedÚ_check_up_downÚ_check_left_right)r5   rV   ÚusedZused_labelsZunusedZunused_labelsZused_selectionsÚirO   rF   Zunused_selectionsZunused_itemsZunused_itemZ
used_countZunused_countr7   )rK   r8   r   ã   sR    









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   r3   r4   Úsuperr   Údispose)r5   )Ú	__class__r7   r8   ra   +  s    



zSimpleEditor.disposec             C   s   | j | jgS )zC Returns the editor's control for indicating error status.
        )r!   r2   )r5   r7   r7   r8   Úget_error_control@  s    zSimpleEditor.get_error_controlc             C   s&   | j js| jjƒ  | jƒ  | jƒ  d S )N)r   r-   r!   ÚclearSelectionr\   r[   )r5   r7   r7   r8   r1   I  s    
zSimpleEditor._on_valuec             C   s&   | j js| jjƒ  | jƒ  | jƒ  d S )N)r   r-   r2   rd   r\   r[   )r5   r7   r7   r8   r   O  s    
zSimpleEditor._on_unusedc             C   s$   | j | j| j| j| jƒ\| _| _d S )N)Ú_transfer_itemsr!   r2   rW   rO   )r5   r7   r7   r8   r    U  s    zSimpleEditor._on_usec             C   s$   | j | j| j| j| jƒ\| _| _d S )N)re   r2   r!   rO   rW   )r5   r7   r7   r8   r*   Y  s    zSimpleEditor._on_unusec             C   s$   | j | j| j| j| jƒ\| _| _d S )N)Ú_transfer_allr!   r2   rW   rO   )r5   r7   r7   r8   r(   ]  s    zSimpleEditor._on_use_allc             C   s$   | j | j| j| j| jƒ\| _| _d S )N)rf   r2   r!   rO   rW   )r5   r7   r7   r8   r,   a  s    zSimpleEditor._on_unuse_allc             C   s   | j dƒ d S )Nr   éÿÿÿÿ)Ú
_move_item)r5   r7   r7   r8   r.   e  s    zSimpleEditor._on_upc             C   s   | j dƒ d S )Nr   )rh   )r5   r7   r7   r8   r/   h  s    zSimpleEditor._on_downc             C   s¢   |dd… }|dd… }|j ƒ  xX|jƒ dkrx|jƒ }|jdƒjdƒ |j|| j|ƒƒ |jdƒ |j|d ƒ |d= q"W |jdƒjdƒ | jƒ  | j	ƒ  ||fS )z7 Transfers all items from one list to another.
        Nr   T)
rd   rX   rY   rZ   ÚinsertItemsrQ   ÚtakeItemrU   r\   r[   )r5   Ú	list_fromÚlist_toÚvalues_fromÚ	values_toÚindex_tor7   r7   r8   rf   s  s    

zSimpleEditor._transfer_allc             C   sL  |dd… }|dd… }t | j|ƒdƒ}t | j|ƒdƒ}|jƒ  | j|ƒ}|jƒ  |j||ƒ |jƒ }x0tt|ƒd ddƒD ]}	|j	|j
||	 ƒƒ q|W ~xd|D ]\}
|j| j|
 ƒ}|j||| ƒ ||= | jjr |j|
tjjtjjB ƒ}|r |d jdƒ q W |jƒ }|dk�r4||k�r$|d }|j|ƒjdƒ | jƒ  | jƒ  ||fS )z? Transfers the selected item from one list to another.
        Nr   r   Trg   rg   )ÚmaxÚ_get_first_selectionrd   rQ   Úreverseri   ÚselectedItemsÚrangerP   rj   ÚrowÚindexrK   Úinsertr   r-   Z	findItemsr   r0   ZMatchFixedStringZMatchCaseSensitiverZ   rX   rY   r\   r[   )r5   rk   rl   rm   rn   Ú
index_fromro   Zselected_listZ
items_fromr^   Z
item_labelZval_index_fromÚitemsrX   r7   r7   r8   re   �  s:    



zSimpleEditor._transfer_itemsc       	      C   s¨   | j }| j|ƒ}|| }|j|ƒjƒ }|j||ƒ |j|ƒjdƒ | jƒ  | j}|dk rn|}|| || g}n|}|| || g}|d|… | ||d d…  | _dS )z: Moves an item up or down within the "used" list.
        Tr   Nr   )	r2   rq   rj   ÚtextZ
insertItemrY   rZ   r[   rO   )	r5   Ú	directionÚlistboxrx   ro   rF   rO   rv   rV   r7   r7   r8   rh   Â  s    
zSimpleEditor._move_itemc             C   sp   | j jrl| jjƒ }| jjt|ƒdko6|d | jjdƒk	ƒ | jjt|ƒdkoh|d | jj| jj	ƒ d ƒk	ƒ dS )zD Sets the proper enabled state for the up and down buttons.
        r   r   N)
r   r-   r2   rs   r$   Ú
setEnabledrP   rY   r%   rX   )r5   Úselectedr7   r7   r8   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!   rX   rq   r+   r2   r   r&   r"   r#   )r5   r7   r7   r8   r\   ì  s    zSimpleEditor._check_left_rightc             C   s   dd„ |j ƒ D ƒS )zH Returns a list of the selected strings in the given *listbox*.
        c             S   s   g | ]}t j|jƒ ƒ‘qS r7   )ÚsixÚ	text_typerz   )rI   Zitmr7   r7   r8   rL     s    z6SimpleEditor._get_selected_strings.<locals>.<listcomp>)rs   )r5   r|   r7   r7   r8   rQ      s    z"SimpleEditor._get_selected_stringsc             C   s&   |j ƒ }t|ƒdkrdS |j|d ƒS )zA Returns the index of the first (or only) selected item.
        r   r   rg   )rs   rP   ru   )r5   r|   Zselect_listr7   r7   r8   rq   	  s    z!SimpleEditor._get_first_selection)(Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   ZQLayoutr   r
   ÚnamesrK   rM   Z
scrollabler9   r;   r=   r?   r   r'   r   r   r   ra   rc   r1   r   r    r*   r(   r,   r.   r/   rf   re   rh   r[   r\   rQ   rq   Ú__classcell__r7   r7   )rb   r8   r   ,   s@   
8

H	5	r   )r„   Ú
__future__r   Z	pyface.qtr   r   Ztraitsui.editors.set_editorr   Ztraitsui.helperr   Úeditorr   Z
traits.apir	   r
   r   r   r7   r7   r7   r8   Ú<module>   s   