a
    §ˆc<?  ã                   @   sP   d Z ddlmZ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 )
z> Defines the set editors for the PyQt user interface toolkit.
é    )ÚQtCoreÚQtGui)Úenum_values_changedé   )ÚEditor)ÚInstanceÚPropertyc                       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                    s¢  t  ¡ ˆ _t  ˆ j¡ˆ _ˆ j dddd¡ ˆ j}|jdkrrˆ  |j¡\ˆ _	ˆ _
ˆ _ˆ  ¡  ˆ j	jˆ jˆ j
dd� n(‡ fdd„ˆ _ˆ  ¡  |jˆ jddd� t  ¡ }ˆ  dˆ jˆ j|j¡ˆ _d ˆ _ ˆ _ ˆ _ˆ _|jrêˆ  d	|ˆ j¡ˆ _ˆ  d
|ˆ j¡ˆ _ˆ  d|ˆ j¡ˆ _|j�r(ˆ  d|ˆ j¡ˆ _|j �rTˆ  d|ˆ j!¡ˆ _ˆ  d|ˆ j"¡ˆ _ˆ j #|ddt$j%j&j'¡ ˆ  dˆ j(ˆ j|j)¡ˆ _*ˆ j+jˆ j,ˆ j-d dd� dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        r   Ú Úui)Údispatchc                      s   ˆ j jS ©N)ÚfactoryÚvalues© ©Úselfr   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/set_editor.pyÚ<lambda>S   ó    z#SimpleEditor.init.<locals>.<lambda>r   Nz>>ú>ú<z<<zMove Upz	Move Downr   é   ú_items?).r   ZQWidgetÚcontrolZQGridLayoutÚroot_layoutZsetContentsMarginsr   ÚnameZparse_extended_nameÚ_objectÚ_nameÚ_valueÚvalues_changedÚon_trait_changeÚ_values_changedZ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   ÚQtÚAlignmentFlagZAlignCenterÚ	_on_valueZright_column_titleÚ_usedÚcontext_objectÚupdate_editorÚextended_name)r   Úparentr   Zblayoutr   r   r   ÚinitA   sX    

ÿ
ÿÿÿÿÿÿÿÿ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  | ¡ ¡}| d¡ | | ¡ d ¡ | |¡ | j 	|d|t
jjj¡ t  ¡ }| t jjj¡ | j 	|d|¡ |j |¡ |j |¡ |S )zCreates a list box.Tr   r   )r   ZQLabelZQFontÚfontZsetBoldZsetPointSizeZ	pointSizeZsetFontr   Ú	addWidgetr   r5   r6   Z	AlignLeftZQListWidgetZsetSelectionModeZQAbstractItemViewZSelectionModeZExtendedSelectionZitemClickedÚconnectZitemDoubleClicked)r   ÚcolZhandler1Zhandler2ÚtitleZtitle_widgetrD   Úlistr   r   r   r#   Š   s    


zSimpleEditor._create_listboxc                 C   s.   t  |¡}|jj|tjjjd� | |¡ |S )zCreates a button.)Útype)	r   ZQPushButtonZclickedrF   r   r5   ZConnectionTypeZQueuedConnectionrE   )r   ÚlabelZlayoutÚ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_valuer>   r@   rB   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 r6|| _| 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�rVt|
ƒdk�rV| ¡ dk�rL| d¡ n
|
 d¡ | ¡ }|D ] }||k �rb| |¡ d¡ �qb| ¡ }|
D ] }||k �r�| |¡ d¡ �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   )rN   r   rP   r   r   rR   á   r   r   T)Úinverse_mappingÚcopyÚvalueÚlenr8   Ú_get_selected_stringsr&   Úclearr   r2   ÚsortedÚ	enumerateZaddItemÚappendr   rQ   Ú_unused_itemsÚcountÚitemÚsetSelectedÚ_check_up_downÚ_check_left_right)r   r   ÚusedZused_labelsZunusedZunused_labelsZused_selectionsÚirU   rK   Zunused_selectionsZunused_itemsZunused_itemZ
used_countZunused_countr   rP   r   r:   ¸   sR    







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   r9   r:   r;   ÚsuperÚdisposer   ©Ú	__class__r   r   rf   ü   s    

ÿÿÿzSimpleEditor.disposec                 C   s   | j | jgS )z9Returns the editor's control for indicating error status.)r&   r8   r   r   r   r   Úget_error_control  s    zSimpleEditor.get_error_controlc                 C   s&   | j js| j ¡  |  ¡  |  ¡  d S r   )r   r2   r&   ÚclearSelectionra   r`   r   r   r   r   r7     s    
zSimpleEditor._on_valuec                 C   s&   | j js| j ¡  |  ¡  |  ¡  d S r   )r   r2   r8   rj   ra   r`   r   r   r   r   r$     s    
zSimpleEditor._on_unusedc                 C   s$   |   | j| j| j| j¡\| _| _d S r   )Ú_transfer_itemsr&   r8   r\   rU   r   r   r   r   r%     s    ÿzSimpleEditor._on_usec                 C   s$   |   | j| j| j| j¡\| _| _d S r   )rk   r8   r&   rU   r\   r   r   r   r   r/   "  s    ÿzSimpleEditor._on_unusec                 C   s$   |   | j| j| j| j¡\| _| _d S r   )Ú_transfer_allr&   r8   r\   rU   r   r   r   r   r-   '  s    ÿzSimpleEditor._on_use_allc                 C   s$   |   | j| j| j| j¡\| _| _d S r   )rl   r8   r&   rU   r\   r   r   r   r   r1   ,  s    ÿzSimpleEditor._on_unuse_allc                 C   s   |   d¡ d S )Néÿÿÿÿ©Ú
_move_itemr   r   r   r   r3   1  s    zSimpleEditor._on_upc                 C   s   |   d¡ d S )Nr   rn   r   r   r   r   r4   4  s    zSimpleEditor._on_downc                 C   sž   |dd… }|dd… }|  ¡  | ¡ dkrv| ¡ }| d¡ d¡ | ||  |¡¡ | d¡ | |d ¡ |d= q | d¡ d¡ |  ¡  |  	¡  ||fS )z-Transfers all items from one list to another.Nr   T)
rj   r]   r^   r_   ÚinsertItemsrW   ÚtakeItemr[   ra   r`   )r   Ú	list_fromÚlist_toÚvalues_fromÚ	values_toÚindex_tor   r   r   rl   7  s     
ÿ
zSimpleEditor._transfer_allc                 C   sH  |dd… }|dd… }t |  |¡dƒ}t |  |¡dƒ}| ¡  |  |¡}| ¡  | ||¡ | ¡ }tt|ƒd ddƒD ]}	| 	| 
||	 ¡¡ qz~|D ]`}
| | j|
 ¡}| ||| ¡ ||= | jjrš| |
tjjjtjjjB ¡}|rš|d  d¡ qš| ¡ }|dk�r0||k�r |d }| |¡ d¡ |  ¡  |  ¡  ||fS )z5Transfers the selected item from one list to another.Nr   r   rm   T)ÚmaxÚ_get_first_selectionrj   rW   Úreverserp   ÚselectedItemsÚrangerV   rq   ÚrowÚindexrQ   Úinsertr   r2   Z	findItemsr   r5   Z	MatchFlagZMatchFixedStringZMatchCaseSensitiver_   r]   r^   ra   r`   )r   rr   rs   rt   ru   Ú
index_fromrv   Zselected_listZ
items_fromrc   Z
item_labelZval_index_fromÚitemsr]   r   r   r   rk   M  s>    
þ

zSimpleEditor._transfer_itemsc           	      C   s¨   | j }|  |¡}|| }| |¡ ¡ }| ||¡ | |¡ d¡ |  ¡  | j}|dk rn|}|| || g}n|}|| || g}|d|… | ||d d…  | _dS )z0Moves an item up or down within the "used" list.Tr   Nr   )	r8   rx   rq   ÚtextZ
insertItemr^   r_   r`   rU   )	r   Ú	directionÚlistboxr   rv   rK   rU   r}   r   r   r   r   ro     s    
zSimpleEditor._move_itemc                 C   sp   | j jrl| j ¡ }| j t|ƒdko6|d | j d¡u¡ | j t|ƒdkoh|d | j | j 	¡ d ¡u¡ dS )z:Sets the proper enabled state for the up and down buttons.r   r   N)
r   r2   r8   rz   r)   Ú
setEnabledrV   r^   r*   r]   )r   Úselectedr   r   r   r`   –  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)r.   r„   r&   r]   rx   r0   r8   r   r+   r'   r(   r   r   r   r   ra   ¢  s"    þþþþzSimpleEditor._check_left_rightc                 C   s   dd„ |  ¡ D ƒS )z>Returns a list of the selected strings in the given *listbox*.c                 S   s   g | ]}t | ¡ ƒ‘qS r   )Ústrr�   )rN   Zitmr   r   r   rR   ½  r   z6SimpleEditor._get_selected_strings.<locals>.<listcomp>)rz   )r   rƒ   r   r   r   rW   »  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   rm   )rz   rV   r|   )r   rƒ   Zselect_listr   r   r   rx   Ã  s    z!SimpleEditor._get_first_selection)(Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ZQLayoutr   r   ÚnamesrQ   rS   Z
scrollabler=   r?   rA   rC   r#   r,   r    r"   r:   rf   ri   r7   r$   r%   r/   r-   r1   r3   r4   rl   rk   ro   r`   ra   rW   rx   Ú__classcell__r   r   rg   r   r	   $   s@   
=	
D2r	   N)rŠ   Z	pyface.qtr   r   Ztraitsui.helperr   Zeditorr   Z
traits.apir   r   r	   r   r   r   r   Ú<module>   s
   