a
    §ˆc²5  ã                   @   sÂ   d Z ddlmZmZmZmZmZ ddlZddlm	Z	m
Z
 ddlmZmZmZmZmZmZ ddlmZ ddlmZmZ eeƒZG d	d
„ d
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 wxPython user interface toolkit.
é    )ÚabspathÚsplitÚsplitextÚisfileÚexistsN)Ú
FileDialogÚOK)ÚListÚStrÚEventÚAnyÚobserveÚ
TraitErroré   )ÚSimpleEditor)ÚTraitsUIPanelÚPopupControlc                       s¸   e Zd ZdZeƒ Zeƒ ZeZdd„ Z	‡ fdd„Z
edƒdd„ ƒZd	d
„ Zdd„ Zdd„ Zdd„ Zedƒdd„ ƒZedƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Z‡  Z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 |dƒ | _}t tj¡}| j}|jdkrvddlm} ||j|j	d�| _
| j
 |¡}d}tj|ddt dd¡d	�}n–|jrªtj|dd
tjd�}|jtj| j| ¡ d� nt |dd
¡}| tj| j¡ |j	ræ|jtj| j| ¡ d� tjjtjdd	�}	tj|d|	d�}d}|| _| |dtj¡ | |dtjtjB |¡ |jtj | j!| ¡ d� | "t#| ƒ¡ | $|¡ || _%|  &|¡ |j'| _'| j(|j)dddd� dS )ú\Finishes initializing the editor by creating the underlying toolkit
        widget.
        éÿÿÿÿr   r   )ÚHistoryControl)ÚentriesÚauto_seté   z...é   )ÚsizeÚ ©Ústyle©Úid)é   r    )Zbitmapé   ÚfilterÚfromT©Zis_listN)*r   ÚcontrolÚwxZBoxSizerZ
HORIZONTALÚfactoryr   Zhistory_controlr   r   ÚhistoryÚcreate_controlZButtonÚSizeZ	enter_setZTextCtrlZTE_PROCESS_ENTERÚBindÚEVT_TEXT_ENTERÚupdate_objectÚGetIdÚEVT_KILL_FOCUSÚEVT_TEXTZArtProviderZ	GetBitmapZART_FOLDER_OPENZBitmapButtonÚ
_file_nameÚAddZEXPANDÚLEFTZALIGN_CENTERÚ
EVT_BUTTONÚshow_file_dialogZSetDropTargetÚFileDropTargetZSetSizerAndFitÚ_buttonÚset_tooltipr"   Ú
sync_valueÚfilter_name)
ÚselfÚparentÚpanelZsizerr'   r   r%   ÚpadÚbuttonZbmp© r@   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/file_editor.pyÚinit4   sB    
ÿÿ

zSimpleEditor.initc                    sŽ   | j }|jtj| j ¡ d� d| _| jdur>| j ¡  d| _nB| jd }| _| tj	¡ |jtj
| ¡ d� |jtj| ¡ d� tƒ  ¡  dS )ú&Disposes of the contents of an editor.r   N)r%   ÚUnbindr&   r4   r7   r.   r(   Údisposer1   r/   r,   r0   Úsuper)r;   r=   r%   ©Ú	__class__r@   rA   rE   e   s    

zSimpleEditor.disposezhistory:valuec                 C   s   |j }| js|  |¡ dS )z0Handles the history 'value' trait being changed.N)ÚnewÚ
_no_updateÚ_update)r;   ÚeventÚvaluer@   r@   rA   Ú_history_value_changedv   s    z#SimpleEditor._history_value_changedc                 C   s(   t |tjƒr| ¡  |  | j ¡ ¡ dS )ú;Handles the user changing the contents of the edit control.N)Ú
isinstancer&   Z
FocusEventZSkiprK   r1   ÚGetValue©r;   rL   r@   r@   rA   r-   }   s    zSimpleEditor.update_objectc                 C   s4   | j dur"d| _| j| j _d| _n| j | j¡ dS )ú[Updates the editor when the object trait changes externally to the
        editor.
        NTF)r(   rJ   Ú	str_valuerM   r1   ZSetValue©r;   r@   r@   rA   Úupdate_editorƒ   s
    

zSimpleEditor.update_editorc                 C   s^   | j dur|  ¡ | _nD|  ¡ }| ¡  |jtkrZ| jjrJt	|j
ƒd | _n|j
| _|  ¡  dS )z Displays the pop-up file dialog.Nr   )r(   Ú_create_file_popupÚpopupÚ_create_file_dialogÚopenZreturn_coder   r'   Útruncate_extr   ÚpathrM   rV   )r;   rL   Údlgr@   r@   rA   r5   Ž   s    

zSimpleEditor.show_file_dialogc                 C   s   | j S ©z9Returns the editor's control for indicating error status.)r1   rU   r@   r@   rA   Úget_error_control�   s    zSimpleEditor.get_error_controlzpopup:valuec                 C   s>   |j }| jjrt|ƒd }|| _d| _| j | j¡ d| _dS )z&Handles the popup value being changed.r   TFN)	rI   r'   r[   r   rM   rJ   r(   Ú	set_valuerT   )r;   rL   Ú	file_namer@   r@   rA   Ú_popup_value_changed£   s    z!SimpleEditor._popup_value_changedzpopup:closedc                 C   s
   d| _ dS )z'Handles the popup control being closed.N)rX   rR   r@   r@   rA   Ú_popup_closed_changed¯   s    z"SimpleEditor._popup_closed_changedc                 C   s*   | j dur&| dg ¡d| jj… | j _ dS )z[Restores any saved user preference information associated with the
        editor.
        Nr(   )r(   Úgetr'   r   )r;   Zprefsr@   r@   rA   Úrestore_prefs¶   s    


ÿzSimpleEditor.restore_prefsc                 C   s"   | j durd| j j dd… iS dS )zCReturns any user preference information associated with the editor.Nr(   )r(   rU   r@   r@   rA   Ú
save_prefs¿   s    
zSimpleEditor.save_prefsc                 C   sR   t | jjƒdkr d | jj¡}nd}t|  ¡ | j ¡ | jjdkrDdnd|d�}|S )z(Creates the correct type of file dialog.r   ú|zAll Files (*.*)|*.*zsave asÚsaverZ   )r<   Zdefault_pathÚactionÚwildcard)	Úlenr'   r"   Újoinr   Zget_control_widgetr1   ÚtextZdialog_style)r;   rj   r]   r@   r@   rA   rY   È   s    üz SimpleEditor._create_file_dialogc                 C   s   t | j| j| jjdd�S )z'Creates the correct type of file popup.i,  )r%   ra   r"   Úheight)Ú	PopupFiler%   rT   r'   r"   rU   r@   r@   rA   rW   ×   s    üzSimpleEditor._create_file_popupc              
   C   sH   z| j jrt|ƒd }|| _W n$ tyB } zW Y d}~n
d}~0 0 dS )z4Updates the editor value with a specified file name.r   N)r'   r[   r   rM   r   )r;   ra   Zexcpr@   r@   rA   rK   à   s    
zSimpleEditor._updatec                 C   s   | j dur| j jS | j ¡ S )z4Returns the current file name from the edit control.N)r(   rM   r1   rQ   rU   r@   r@   rA   Ú
_get_valueê   s    
zSimpleEditor._get_value)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   rX   Úfilter_traitr"   rB   rE   r   rN   r-   rV   r5   r_   rb   rc   re   rf   rY   rW   rK   rp   Ú__classcell__r@   r@   rG   rA   r   %   s,   1


			
r   c                       s|   e Zd ZdZdZeZeƒ Zeƒ 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‡  ZS )ÚCustomEditorzCCustom style of file editor, consisting of a file system tree view.Tc                 C   sÜ   |   ¡ }| j}t|jƒdks&|jdkr0|tjO }tj||d�| _| j 	¡  | _
}| ¡ }|jtj| j|d� |jtj| j|d� |jtj| j|d� |j| _| j|jdddd� |  |jd	d¡ |  |jd
d¡ |  ¡  dS )r   r   r   r   r   r"   r#   Tr$   ÚreloadÚdclickÚtoN)Ú	get_styler'   rk   r"   r:   r&   ÚDIRCTRL_SHOW_FILTERSÚGenericDirCtrlr%   ÚGetTreeCtrlÚ_treer.   r+   ÚEVT_TREE_SEL_CHANGEDr-   ÚEVT_TREE_ITEM_ACTIVATEDÚ
_on_dclickZEVT_TREE_ITEM_GETTOOLTIPÚ_on_tooltipr9   Zreload_nameZdclick_namer8   )r;   r<   r   r'   Útreer   r@   r@   rA   rB     s    
zCustomEditor.initc                    s4   | j d }| _ | tj¡ | tj¡ tƒ  ¡  dS )rC   N)r   rD   r&   r€   r�   rF   rE   )r;   r„   rG   r@   rA   rE     s    zCustomEditor.disposec                 C   sB   | j dur>| j  ¡ }| jjs$t|ƒr>| jjr8t|ƒd }|| _dS )rO   Nr   )r%   ÚGetPathr'   Z	allow_dirr   r[   r   rM   ©r;   rL   r\   r@   r@   rA   r-   !  s    

zCustomEditor.update_objectc                 C   s   t | jƒr| j | j¡ dS )rS   N)r   rT   r%   ÚSetPathrU   r@   r@   rA   rV   +  s    
zCustomEditor.update_editorc                 C   s   t jS )z/Returns the basic style to use for the control.©r&   ZDIRCTRL_EDIT_LABELSrU   r@   r@   rA   r{   2  s    zCustomEditor.get_stylec                 C   s   | j S r^   )r   rU   r@   r@   rA   r_   6  s    zCustomEditor.get_error_controlc                 C   s    | j  d | jdd… ¡¡ dS )z)Handles the 'filter' trait being changed.rg   N)r%   Z	SetFilterrl   r"   rU   r@   r@   rA   Ú_filter_changed:  s    zCustomEditor._filter_changedc                 C   s   | j  ¡ | _dS )z0Handles the user double-clicking on a file name.N)r%   r…   ry   rR   r@   r@   rA   r‚   >  s    zCustomEditor._on_dclickc                 C   s   | j  | ¡ ¡}| |¡ dS )z7Handles the user hovering on a file name for a tooltip.N)r   ZGetItemTextZGetItemZ
SetToolTip)r;   rL   rm   r@   r@   rA   rƒ   B  s    zCustomEditor._on_tooltipc                 C   s   | j  ¡  dS )z)Handles the 'reload' trait being changed.N)r%   ZReCreateTreerU   r@   r@   rA   Ú_reload_changedG  s    zCustomEditor._reload_changed)rq   rr   rs   rt   Z
scrollableru   r"   r   rx   ry   rB   rE   r-   rV   r{   r_   r‰   r‚   rƒ   rŠ   rv   r@   r@   rG   rA   rw   ò   s   	
rw   c                   @   sB   e Zd Zeƒ ZeZdZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
d„ ZdS )ro   Tc                 C   st   |   ¡ }t| jƒdkr |tjO }tj||d | j¡d� | _}| | j	¡ | 
¡  | _}|jtj| j| ¡ d� dS )z3Creates the file control and gets it ready for use.r   rg   )r   r"   r   N)r{   rk   r"   r&   r|   r}   rl   Ú_filesr‡   ra   r~   r   r+   r€   Ú_select_filer.   )r;   r<   r   Úfilesr„   r@   r@   rA   r)   Y  s    
ÿzPopupFile.create_controlc                 C   s   | j  tj¡ d  | _ | _d S ©N)r   rD   r&   r€   r‹   rU   r@   r@   rA   rE   f  s    zPopupFile.disposec                 C   s   t jS )z.Returns the base style for this type of popup.rˆ   rU   r@   r@   rA   r{   j  s    zPopupFile.get_stylec                 C   s   t |ƒS )z)Returns whether or not the path is valid.)r   )r;   r\   r@   r@   rA   Úis_validn  s    zPopupFile.is_validc                 C   s<   | j  ¡ }|t| jƒkr8|dd… dvr8|  |¡r8|| _dS )z2Handles a file being selected in the file control.r   Nz/\)r‹   r…   r   ra   r�   rM   r†   r@   r@   rA   rŒ   t  s    
ÿþýzPopupFile._select_fileN)rq   rr   rs   r
   ra   ru   r"   Z	resizabler)   rE   r{   r�   rŒ   r@   r@   r@   rA   ro   L  s   ro   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r6   z=A target for a drag and drop operation, which accepts a file.c                 C   s   t j | ¡ || _d S rŽ   )r&   r6   Ú__init__Úeditor)r;   r‘   r@   r@   rA   r�   �  s    zFileDropTarget.__init__c                 C   s   |d | j _| j  ¡  dS )Nr   T)r‘   rM   rV   )r;   ÚxÚyZ
file_namesr@   r@   rA   ÚOnDropFiles‘  s    
zFileDropTarget.OnDropFilesN)rq   rr   rs   rt   r�   r”   r@   r@   r@   rA   r6   Š  s   r6   )rt   Úos.pathr   r   r   r   r   r&   Z
pyface.apir   r   Z
traits.apir	   r
   r   r   r   r   Ztext_editorr   ZSimpleTextEditorÚhelperr   r   ru   rw   ro   r6   r@   r@   r@   rA   Ú<module>   s     NZ>