3
8ßL]!%  ã               @   sÌ   d Z ddlmZ ddlmZ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 ddlmZ ddlmZ dd	lmZ dd
lZe	jd dk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.
é    )Úabsolute_import)ÚabspathÚsplitextÚisfileÚexists)ÚQtCoreÚQtGui)ÚListÚEventÚFileÚUnicodeÚ
TraitError)ÚToolkitEditorFactoryé   )ÚSimpleEditor)Ú
IconButtonNé   c               @   s@   e Zd Z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 jƒ | _t j| jƒ}|jddddƒ t jƒ  | _}|j|ƒ | jj	rV|j
j| jƒ n|jj| jƒ tt jj| jƒ}|j|ƒ | j|ƒ dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   N)r   ZQWidgetÚcontrolZQHBoxLayoutZsetContentsMarginsZ	QLineEditÚ
_file_nameZ	addWidgetÚfactoryZauto_setZ
textEditedÚconnectÚupdate_objectZeditingFinishedr   ZQStyleZ
SP_DirIconÚshow_file_dialogÚset_tooltip)ÚselfÚparentZlayoutr   Úbutton© r   ú>/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/file_editor.pyÚinit)   s    


zSimpleEditor.initc             C   sn   | j dk	rjtj| jjƒ ƒ}y| jjr0t|ƒd }|| _W n0 t	k
rh } z| jj
| jƒ W Y dd}~X nX dS )zE Handles the user changing the contents of the edit control.
        Nr   )r   ÚsixÚ	text_typer   Útextr   Útruncate_extr   Úvaluer   ÚsetText)r   Ú	file_nameZexcpr   r   r   r   @   s    

zSimpleEditor.update_objectc             C   s   | j j| jƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)r   r%   Ú	str_value)r   r   r   r   Úupdate_editorM   s    zSimpleEditor.update_editorc             C   s`   | j ƒ }|jƒ tjjkr\|jƒ }t|ƒdkr\tj|d ƒ}| j	j
rNt|ƒd }|| _| jƒ  dS )z* Displays the pop-up file dialog.
        r   N)Ú_create_file_dialogÚexec_r   ZQDialogÚAcceptedZselectedFilesÚlenr    r!   r   r#   r   r$   r(   )r   ÚdlgÚfilesr&   r   r   r   r   S   s    zSimpleEditor.show_file_dialogc             C   s   | j S )zC Returns the editor's control for indicating error status.
        )r   )r   r   r   r   Úget_error_controlh   s    zSimpleEditor.get_error_controlc             C   sp   t j| jƒ}|j| jjƒ ƒ | jjdkr6|j|j	ƒ n| jjdkrN|j|j
ƒ t| jjƒdkrl|j| jjƒ |S )z2 Creates the correct type of file dialog.
        ÚopenÚsaver   )r   ZQFileDialogr   Z
selectFiler   r"   r   Zdialog_styleZsetAcceptModeZ
AcceptOpenZ
AcceptSaver,   ÚfilterÚsetNameFilters)r   r-   r   r   r   r)   o   s    z SimpleEditor._create_file_dialogN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r   r/   r)   r   r   r   r   r   #   s   r   c               @   s\   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S )ÚCustomEditorzI Custom style of file editor, consisting of a file system tree view.
    Tc       	      C   s~  t | ƒ}tjƒ  | _}t| jƒ}|jtjj	tjj
B tjjB tjjB ƒ | jjdƒ |jtjjƒ ƒ |j|ƒ |j|jtjjƒ ƒƒ || _|rª|j|ƒ}|j|ƒ |j|ƒ | jjƒ jƒ  x"td|jƒ ƒD ]}| jj|ƒ qÈW | j}|j| _|j| _| j|jdddd� | j|j ddƒ | j|j!ddƒ | j|j"d	d
ƒ | j#ƒ  | jjƒ }t$�r`|j%dtj&j'ƒ n|j(dtj&j'ƒ |j)dƒ dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        Fr   r2   ÚfromT)Zis_listÚ	root_pathÚreloadÚdclickZtor   N)*Ú	_TreeViewr   ZQFileSystemModelÚ_modelr   r'   Z	setFilterr   ÚQDirZAllDirsZFilesZDrivesZNoDotAndDotDotZsetNameFilterDisablesÚsetRootPathÚhomePathZsetModelÚsetRootIndexÚindexr   ÚexpandÚsetCurrentIndexÚheaderZhideÚrangeZcolumnCountZ
hideColumnr   r2   r:   Z
sync_valueZfilter_nameZroot_path_nameZreload_nameZdclick_namer   Úis_qt5ZsetSectionResizeModeZQHeaderViewZResizeToContentsZsetResizeModeZsetStretchLastSection)	r   r   r   ÚmodelÚcurrent_pathrC   Úcolumnr   rF   r   r   r   r   “   s<    
"




zCustomEditor.initc             C   sJ   | j dk	rFtj| jj|ƒƒ}| jjs,t|ƒrF| jjr@t	|ƒd }|| _
dS )zE Handles the user changing the contents of the edit control.
        Nr   )r   r    r!   r>   ÚfilePathr   Z	allow_dirr   r#   r   r$   )r   ÚidxÚpathr   r   r   r   È   s    
zCustomEditor.update_objectc             C   s8   t | jƒr4| jjt| jƒƒ}| jj|ƒ | jj|ƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)r   r'   r>   rC   r   r   rD   rE   )r   rC   r   r   r   r(   Ô   s    
zCustomEditor.update_editorc             C   s   t j| jj|ƒƒ| _dS )z: Handles the user double-clicking on a file name.
        N)r    r!   r>   rL   r<   )r   rM   r   r   r   Ú
_on_dclickß   s    zCustomEditor._on_dclickc             C   s   | j j| jƒ dS )z3 Handles the 'filter' trait being changed.
        N)r>   r3   r2   )r   r   r   r   Ú_filter_changedæ   s    zCustomEditor._filter_changedc             C   s8   | j }|stjjƒ }| jj|ƒ | jj| jj|ƒƒ dS )z6 Handles the 'root_path' trait being changed.
        N)	r:   r   r?   rA   r>   r@   r   rB   rC   )r   rN   r   r   r   Ú_root_path_changedë   s
    
zCustomEditor._root_path_changedc             C   s   | j jƒ  dS )z3 Handles the 'reload' trait being changed.
        N)r>   Zrefresh)r   r   r   r   Ú_reload_changedô   s    zCustomEditor._reload_changedN)r4   r5   r6   r7   Z
scrollableÚfilter_traitr2   r   r:   r
   r;   r<   r   r   r(   rO   rP   rQ   rR   r   r   r   r   r8   €   s   5	r8   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 )
r=   z… This is an internal class needed because QAbstractItemView doesn't
        provide a signal for when the current index changes.
    c                s&   t t| ƒjƒ  | jj|jƒ || _d S )N)Úsuperr=   Ú__init__ZdoubleClickedr   rO   Ú_editor)r   Úeditor)Ú	__class__r   r   rU   ÿ   s    z_TreeView.__init__c                sj   |j ƒ tjjkrZ| j|jƒ ƒ}|rD|jƒ rDtjj	|j
ƒ |jƒ | ƒ ntjjƒ  |jƒ  dS tt| ƒj|ƒS )NT)Útyper   ZQEventZToolTipZindexAtÚposZisValidr   ZQToolTipZshowTextZ	globalPosÚdataZhideTextÚignorerT   r=   Úevent)r   r]   rC   )rX   r   r   r]     s    
z_TreeView.eventc             C   sN   |j ƒ }|tjjks |tjjkr<| jj| jƒ d ƒ |jƒ  t	j
j| |ƒ d S )Nr   )Úkeyr   ZQtZ
Key_ReturnZ	Key_EnterrV   rO   ZselectedIndexesÚacceptr   Ú	QTreeViewÚkeyPressEvent)r   Zkeyeventr^   r   r   r   ra     s
    z_TreeView.keyPressEventc                s"   t t| ƒj||ƒ | jj|ƒ dS )zN Reimplemented to tell the editor when the current index has changed.
        N)rT   r=   ÚcurrentChangedrV   r   )r   ÚcurrentÚprevious)rX   r   r   rb     s    z_TreeView.currentChanged)	r4   r5   r6   r7   rU   r]   ra   rb   Ú__classcell__r   r   )rX   r   r=   ú   s
   r=   )r7   Ú
__future__r   Úos.pathr   r   r   r   Z	pyface.qtr   r   Z
traits.apir	   r
   r   r   r   Ztraitsui.editors.file_editorr   Ztext_editorr   ZSimpleTextEditorÚhelperr   r    Ú__version_info__rH   rS   r8   r`   r=   r   r   r   r   Ú<module>   s   ]z