a
    §ˆc(*  ã                   @   sÊ   d Z ddlmZmZmZmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ eeƒZG d
d„ deƒZG dd„ deƒZG dd„ dejƒZ dS )z; Defines file editors for the PyQt user interface toolkit.
é    )ÚabspathÚsplitextÚisfileÚexists)ÚQtCoreÚQtGuiÚis_qt4)Ú
FileDialogÚOK)ÚAnyÚCallableÚListÚEventÚFileÚStrÚ
TraitErrorÚTupleé   )ÚEditor)ÚSimpleEditor)Ú
IconButtonc                   @   s^   e Zd ZdZeeeƒ 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dS )r   z¾Simple style of file editor, consisting of a text field and a **Browse**
    button that opens a file-selection dialog box. The user can also drag
    and drop a file onto this control.
    c                 C   sÖ   t  ¡ | _t  | j¡}| dddd¡ t  ¡  | _}| |¡ | jj	rj|j
 | j¡ | j |j
| jf¡ n"|j | j¡ | j |j| jf¡ tt jjj| jƒ}| |¡ |  |¡ | jj| _| j| jjdddd� dS )ú\Finishes initializing the editor by creating the underlying toolkit
        widget.
        r   ÚfilterÚfromT©Zis_listN)r   ZQWidgetÚcontrolZQHBoxLayoutZsetContentsMarginsZ	QLineEditÚ
_file_nameZ	addWidgetÚfactoryZauto_setZ
textEditedÚconnectÚupdate_objectÚ_connections_to_removeÚappendZeditingFinishedr   ZQStyleZStandardPixmapZ
SP_DirIconÚshow_file_dialogÚset_tooltipr   Ú
sync_valueÚfilter_name)ÚselfÚparentZlayoutr   Úbutton© r)   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/file_editor.pyÚinit4   s&    


ÿ
ÿ


zSimpleEditor.initc                 C   s.   | j r | j  ¡ \}}| |¡ q t | ¡ dS ©z&Disposes of the contents of an editor.N)r    ÚpopÚ
disconnectr   Údispose)r&   ÚsignalÚhandlerr)   r)   r*   r/   T   s    zSimpleEditor.disposec              
   C   sn   | j durjt| j ¡ ƒ}z| jjr.t|ƒd }|| _W n2 tyh } z| j 	| j¡ W Y d}~n
d}~0 0 dS ©z;Handles the user changing the contents of the edit control.Nr   )
r   Ústrr   Útextr   Útruncate_extr   Úvaluer   ÚsetText)r&   Ú	file_nameZexcpr)   r)   r*   r   `   s    

zSimpleEditor.update_objectc                 C   s   | j  | j¡ dS ©z[Updates the editor when the object trait changes externally to the
        editor.
        N)r   r7   Ú	str_value©r&   r)   r)   r*   Úupdate_editorl   s    zSimpleEditor.update_editorc                 C   sH   |   ¡ }| ¡  |jtkrD| jjr4t|jƒd | _n|j| _|  	¡  dS )z Displays the pop-up file dialog.r   N)
Ú_create_file_dialogÚopenZreturn_coder
   r   r5   r   Úpathr6   r<   )r&   Údlgr)   r)   r*   r"   r   s    
zSimpleEditor.show_file_dialogc                 C   s   | j S )z9Returns the editor's control for indicating error status.)r   r;   r)   r)   r*   Úget_error_control�   s    zSimpleEditor.get_error_controlc                 C   s<   d  | jj¡}t|  ¡ | j ¡ | jjdkr.dnd|d�}|S )z(Creates the correct type of file dialog.ú zsave asÚsaver>   )r'   Zdefault_pathÚactionÚwildcard)Újoinr   r   r	   Zget_control_widgetr   r4   Zdialog_style)r&   rE   r@   r)   r)   r*   r=   ‡   s    üz SimpleEditor._create_file_dialogN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    Úfilter_traitr   r+   r/   r   r<   r"   rA   r=   r)   r)   r)   r*   r   &   s    r   c                   @   sj   e Zd ZdZdZe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dd„ ZdS )ÚCustomEditorzCCustom style of file editor, consisting of a file system tree view.Tc           	      C   s”  t | ƒ}|j | j¡ t ¡  | _}t| jƒ}| 	t
jjjt
jjjB t
jjjB t
jjjB ¡ | j d¡ | t
j ¡ ¡ | |¡ | | t
j ¡ ¡¡ || _|rÀ| |¡}| |¡ | |¡ | j ¡  ¡  td| ¡ ƒD ]}| j |¡ qÜ| j}|j | _ |j!| _!| j"|j#dddd� |  "|j$dd¡ |  "|j%dd¡ |  "|j&d	d
¡ |  '¡  | j ¡ }t(�rt| )dtj*j+j,¡ n| -dtj*j+j,¡ | .d¡ dS )r   Fr   r   r   Tr   Ú	root_pathÚreloadÚdclickÚtor   N)/Ú	_TreeViewÚdoubleClickedr   Ú
_on_dclickr   ZQFileSystemModelÚ_modelr   r:   Z	setFilterr   ÚQDirÚFilterZAllDirsZFilesZDrivesZNoDotAndDotDotZsetNameFilterDisablesÚsetRootPathÚhomePathZsetModelÚsetRootIndexÚindexr   ÚexpandÚsetCurrentIndexÚheaderÚhideÚrangeZcolumnCountZ
hideColumnr   r   rM   r$   r%   Zroot_path_nameZreload_nameZdclick_namer#   r   ZsetResizeModeZQHeaderViewZ
ResizeModeZResizeToContentsZsetSectionResizeModeZsetStretchLastSection)	r&   r'   r   ÚmodelÚcurrent_pathrZ   Úcolumnr   r]   r)   r)   r*   r+   ¥   sL    
ÿþýÿ




zCustomEditor.initc                 C   s<   | j  ¡  | j  ¡  | jdur.| jj | j¡ t | ¡ dS r,   )	rT   ZbeginResetModelZendResetModelr   rR   r.   rS   r   r/   r;   r)   r)   r*   r/   à   s
    


zCustomEditor.disposec                 C   sH   | j durDt| j |¡ƒ}| jjs*t|ƒrD| jjr>t|ƒd }|| _	dS r2   )
r   r3   rT   ÚfilePathr   Z	allow_dirr   r5   r   r6   )r&   Úidxr?   r)   r)   r*   r   ë   s    
zCustomEditor.update_objectc                 C   s8   t | jƒr4| j t| jƒ¡}| j |¡ | j |¡ dS r9   )r   r:   rT   rZ   r   r   r[   r\   )r&   rZ   r)   r)   r*   r<   ö   s    
zCustomEditor.update_editorc                 C   s   t | j |¡ƒ| _dS )z0Handles the user double-clicking on a file name.N)r3   rT   rc   rO   )r&   rd   r)   r)   r*   rS     s    zCustomEditor._on_dclickc                 C   s   | j  | j¡ dS )z)Handles the 'filter' trait being changed.N)rT   ZsetNameFiltersr   r;   r)   r)   r*   Ú_filter_changed  s    zCustomEditor._filter_changedc                 C   s8   | j }|stj ¡ }| j |¡ | j | j |¡¡ dS )z,Handles the 'root_path' trait being changed.N)	rM   r   rU   rX   rT   rW   r   rY   rZ   )r&   r?   r)   r)   r*   Ú_root_path_changed  s
    
zCustomEditor._root_path_changedc                 C   s   | j  ¡  dS )z)Handles the 'reload' trait being changed.N)rT   Zrefreshr;   r)   r)   r*   Ú_reload_changed  s    zCustomEditor._reload_changedN)rG   rH   rI   rJ   Z
scrollablerK   r   r   rM   r   rN   rO   r+   r/   r   r<   rS   re   rf   rg   r)   r)   r)   r*   rL   “   s   ;rL   c                       s@   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Z‡ fdd	„Z‡  ZS )
rQ   z€This is an internal class needed because QAbstractItemView doesn't
    provide a signal for when the current index changes.
    c                    s   t ƒ  ¡  || _d S )N)ÚsuperÚ__init__Ú_editor)r&   Úeditor©Ú	__class__r)   r*   ri     s    
z_TreeView.__init__c                    sh   |  ¡ tjjjkr\|  | ¡ ¡}|rF| ¡ rFtj	 
| ¡ | ¡ | ¡ ntj	 ¡  | ¡  dS tƒ  |¡S )NT)Útyper   ZQEventÚTypeZToolTipZindexAtÚposZisValidr   ZQToolTipZshowTextZ	globalPosÚdataZhideTextÚignorerh   Úevent)r&   rs   rZ   rl   r)   r*   rs   !  s    
z_TreeView.eventc                 C   sR   |  ¡ }|tjjjks$|tjjjkr@| j |  ¡ d ¡ | 	¡  t
j | |¡ d S )Nr   )Úkeyr   ZQtZKeyZ
Key_ReturnZ	Key_Enterrj   rS   ZselectedIndexesÚacceptr   Ú	QTreeViewÚkeyPressEvent)r&   Zkeyeventrt   r)   r)   r*   rw   .  s
    z_TreeView.keyPressEventc                    s   t ƒ  ||¡ | j |¡ dS )zDReimplemented to tell the editor when the current index has changed.N)rh   ÚcurrentChangedrj   r   )r&   ÚcurrentÚpreviousrl   r)   r*   rx   5  s    z_TreeView.currentChanged)	rG   rH   rI   rJ   ri   rs   rw   rx   Ú__classcell__r)   r)   rl   r*   rQ     s
   rQ   N)!rJ   Úos.pathr   r   r   r   Z	pyface.qtr   r   r   Z
pyface.apir	   r
   Z
traits.apir   r   r   r   r   r   r   r   rk   r   Ztext_editorr   ZSimpleTextEditorÚhelperr   rK   rL   rv   rQ   r)   r)   r)   r*   Ú<module>   s   (m 