B
    nôb\ß/  ã               @   s  d Z ddlZddlZddl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mZmZmZmZ ddlmZ ddlmZ ddlmZ G d	d
„ d
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ƒZG dd„ deƒZdd„ Z G dd„ deƒZ!dS )zCustomized combobox widgets.é    N)ÚQEventÚQtÚQTimerÚQUrlÚSignal)ÚQFont)Ú	QComboBoxÚ
QCompleterÚ	QLineEditÚQSizePolicyÚQToolTip)Ú_)Úto_text_string)ÚIconLineEditc               @   s‚   e Zd ZdZeeeƒZeeƒZe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d„ Zdd„ Zdd„ ZdS )ÚBaseComboBoxzEditable combo box base classc             C   s8   t  | |¡ |  d¡ |  t| ƒ¡ d| _|  ¡ | _d S )NTr   )r   Ú__init__ZsetEditableÚsetCompleterr	   ÚnumpressÚcurrentTextÚselected_text)ÚselfÚparent© r   ú8lib/python3.7/site-packages/spyder/widgets/comboboxes.pyr   &   s
    
zBaseComboBox.__init__c             C   s`   |  ¡ tjkrT| ¡ tjkrT| j d¡ |  jd7  _| jdkrPt	 
d| j¡| _dS t | |¡S )zKQt Override.

        Filter tab keys and process double tab keys.
        Té   i  )Útyper   ZKeyPressÚkeyr   ZKey_TabÚsig_tab_pressedÚemitr   r   Ú
singleShotÚhandle_keypressZ
presstimerr   Úevent)r   r!   r   r   r   r!   .   s    
zBaseComboBox.eventc             C   sj   |  ¡ tjks|  ¡ tjkr6|  ¡ rf|  ¡  |  ¡  n0|  ¡ tjkrZ|  | j	¡ |  ¡  nt
 | |¡ dS )z7Qt Override.

        Handle key press events.
        N)r   r   Z
Key_ReturnZ	Key_EnterÚadd_current_text_if_validÚselectedÚhide_completerZ
Key_EscapeÚset_current_textr   r   ÚkeyPressEvent)r   r!   r   r   r   r&   ;   s    

zBaseComboBox.keyPressEventc             C   s    | j dkr| j d¡ d| _ dS )z4When hitting tab, it handles if single or double tabé   Tr   N)r   Úsig_double_tab_pressedr   )r   r   r   r   r    K   s    
zBaseComboBox.handle_keypressc             C   s   dS )z`
        Return True if string is valid
        Return None if validation can't be done
        Nr   )r   Úqstrr   r   r   Úis_validQ   s    zBaseComboBox.is_validc             C   s   | j  dd¡ dS )z9Action to be executed when a valid item has been selectedTN)Úvalidr   )r   r   r   r   r#   X   s    zBaseComboBox.selectedc             C   sŽ   |   |¡}x |dkr*|  |¡ |   |¡}qW |  d|¡ |   d¡}|dkr€|  |¡ |  dd¡ |dkrt|  d¡ qŠ|  d¡ n
|  d¡ dS )zVAdd text to combo box: add a new item if text is not found in
        combo box items.éÿÿÿÿr   Ú r   N)ZfindTextZ
removeItemZ
insertItemZsetCurrentIndex)r   ÚtextÚindexr   r   r   Úadd_text\   s    




zBaseComboBox.add_textc             C   s   |   ¡  t|ƒ¡ dS )z0Sets the text of the QLineEdit of the QComboBox.N)ÚlineEditÚsetTextr   )r   r.   r   r   r   r%   o   s    zBaseComboBox.set_current_textc             C   s   |   ¡ }|  |¡ dS )z9Add current text to combo box history (convenient method)N)r   r0   )r   r.   r   r   r   Úadd_current_texts   s    zBaseComboBox.add_current_textc             C   s6   |   |  ¡ ¡}|s|dkr&|  ¡  dS |  | j¡ dS )z.Add current text to combo box history if validNT)r*   r   r3   r%   r   )r   r+   r   r   r   r"   x   s
    z&BaseComboBox.add_current_text_if_validc             C   s   |   tg | ƒ¡ dS )zHides the completion widget.N)r   r	   )r   r   r   r   r$      s    zBaseComboBox.hide_completerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úboolr+   r   r(   r   r!   r&   r    r*   r#   r0   r%   r3   r"   r$   r   r   r   r   r       s   
	r   c               @   s   e Zd ZdZddd„ZdS )ÚPatternComboBoxzSearch pattern combo boxNTc             C   sp   t  | |¡ t|  ¡ dƒr(|  ¡  d¡ |r8|  tj¡ |  t	j
t	j¡ |d k	rZ|  |¡ |d k	rl|  |¡ d S )NÚsetClearButtonEnabledT)r   r   Úhasattrr1   r:   ÚsetSizeAdjustPolicyr   ÚAdjustToMinimumContentsLengthÚsetSizePolicyr   Ú	ExpandingÚFixedZaddItemsZ
setToolTip)r   r   ÚitemsÚtipZadjust_to_minimumr   r   r   r   ˆ   s    
zPatternComboBox.__init__)NNT)r4   r5   r6   r7   r   r   r   r   r   r9   †   s    r9   c               @   s4   e Zd ZdZdd„ Zddd„Zdd„ Zdd
d„ZdS )ÚEditableComboBoxz'
    Editable combo box + Validate
    c             C   sP   t  | |¡ tƒ | _|  ¡ | _|  tj¡ | j	 
| j¡ tdƒtdƒdœ| _d S )Nz"Press enter to validate this entryzThis entry is incorrect)TF)r   r   r   Zfontr   r   r<   r   r=   ÚeditTextChangedÚconnectÚvalidater   Útips)r   r   r   r   r   r   ›   s    
zEditableComboBox.__init__r-   c             C   s   t  |  |  ¡ ¡|| ¡ dS )zShow tipN)r   ZshowTextZmapToGlobalÚpos)r   rB   r   r   r   Úshow_tip¨   s    zEditableComboBox.show_tipc             C   s   t  | ¡ |  ¡ | _dS )z9Action to be executed when a valid item has been selectedN)r   r#   r   r   )r   r   r   r   r#   ¬   s    
zEditableComboBox.selectedTc             C   sX   | j |kr$|dkr$| j dd¡ dS |  |¡}|rT|rF| j dd¡ n| j dd¡ dS )zValidate entered pathr-   TNF)r   r+   r   r*   )r   r)   Zeditingr+   r   r   r   rF   ±   s    
zEditableComboBox.validateN)r-   )T)r4   r5   r6   r7   r   rI   r#   rF   r   r   r   r   rC   –   s
   
rC   c               @   sl   e Zd ZdZeeƒZddd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ ZdS )ÚPathComboBoxz+
    QComboBox handling path locations
    Fc             C   s˜   t  | |¡ t| ƒ}|r&|  tj¡ n|  tj¡ |  tj	tj
¡ tdƒddœ| _|  |¡ | j | j¡ | j | j¡ | j | j¡ | j |j¡ d S )Nz!Press enter to validate this pathr-   )TF)rC   r   r   r<   r   ÚAdjustToContentsr=   r>   r   r?   r@   r   rG   ÚsetLineEditZhighlightedrE   Úadd_tooltip_to_highlighted_itemr   Útab_completer(   Údouble_tab_completer+   Zupdate_status)r   r   Úadjust_to_contentsÚlineeditr   r   r   r   Å   s    

zPathComboBox.__init__c             C   s*   t |  ¡ ddƒ}|r|ƒ  t | |¡ dS )z;Handle focus in event restoring to display the status icon.Zshow_status_iconN)Úgetattrr1   r   ÚfocusInEvent)r   r!   Zshow_statusr   r   r   rS   Ü   s    zPathComboBox.focusInEventc                sP   ˆ  ¡ s&ˆ ¡ ‰ t d‡ ‡fdd„¡ tˆ ¡ ddƒ}|r@|ƒ  t ˆ|¡ dS )z>Handle focus out event restoring the last valid selected path.é2   c                  s   ˆ   ˆj¡S )N)r2   r   r   )rQ   r   r   r   Ú<lambda>é   s    z,PathComboBox.focusOutEvent.<locals>.<lambda>Zhide_status_iconN)r*   r1   r   r   rR   r   ÚfocusOutEvent)r   r!   Zhide_statusr   )rQ   r   r   rV   ã   s    zPathComboBox.focusOutEventc             C   s@   t |  ¡ ƒ}t |d ¡}tdd„ |D ƒƒ}|  t|| ƒ¡ |S )z"Find available completion options.Ú*c             S   s   g | ]}t  |¡r|‘qS r   )ÚospÚisdir)Ú.0Zoptr   r   r   ú
<listcomp>õ   s    z2PathComboBox._complete_options.<locals>.<listcomp>)r   r   ÚglobÚsortedr   r	   )r   r.   Úoptsr   r   r   Ú_complete_optionsñ   s
    zPathComboBox._complete_optionsc             C   s$   |   ¡ }t|ƒdkr |  ¡  ¡  dS )z;If several options available a double tab displays options.r   N)r_   ÚlenZ	completerZcomplete)r   r^   r   r   r   rO   ù   s    z PathComboBox.double_tab_completec             C   s4   |   ¡ }t|ƒdkr0|  |d tj ¡ |  ¡  dS )zU
        If there is a single option available one tab completes the option.
        r   r   N)r_   r`   r%   ÚosÚsepr$   )r   r^   r   r   r   rN   ÿ   s    zPathComboBox.tab_completeNc             C   s   |dkr|   ¡ }t t|ƒ¡S )zReturn True if string is validN)r   rX   rY   r   )r   r)   r   r   r   r*     s    zPathComboBox.is_validc             C   s*   |   ¡ | _| j dd¡ | j | j¡ dS )z9Action to be executed when a valid item has been selectedTN)r   r   r+   r   Úopen_dir)r   r   r   r   r#     s    
zPathComboBox.selectedc             C   s>   |   ¡ }t |¡r0|r0|d tjkr0|dd… }|  |¡ dS )z“
        Add current text to combo box history (convenient method).
        If path ends in os separator ("" windows, "/" unix) remove it.
        r,   N)r   rX   rY   ra   rb   r0   )r   r.   r   r   r   r3     s
    zPathComboBox.add_current_textc             C   s   |   ||  |¡tj¡ dS )zt
        Add a tooltip showing the full path of the currently highlighted item
        of the PathComboBox.
        N)ZsetItemDataZitemTextr   ZToolTipRole)r   r/   r   r   r   rM     s    z,PathComboBox.add_tooltip_to_highlighted_item)F)N)r4   r5   r6   r7   r   Ústrrc   r   rS   rV   r_   rO   rN   r*   r#   r3   rM   r   r   r   r   rJ   ¿   s   
	
rJ   c               @   s$   e Zd ZdZddd„Zd	dd„ZdS )
ÚUrlComboBoxz!
    QComboBox handling urls
    Fc             C   s    t  | ||¡ | j | j¡ d S )N)rJ   r   rD   Z
disconnectrF   )r   r   rP   r   r   r   r   +  s    zUrlComboBox.__init__Nc             C   s   |dkr|   ¡ }t|ƒ ¡ S )zReturn True if string is validN)r   r   ZisValid)r   r)   r   r   r   r*   /  s    zUrlComboBox.is_valid)F)N)r4   r5   r6   r7   r   r*   r   r   r   r   re   '  s   
re   c               @   s$   e Zd ZdZddd„Zd	dd„ZdS )
ÚFileComboBoxz'
    QComboBox handling File paths
    NFc             C   sV   t  | ||¡ |r$t| ƒ}|  |¡ |r6|  tj¡ n|  tj¡ |  t	j
t	j¡ d S )N)rJ   r   r
   rL   r<   r   rK   ZAdjustToContentsOnFirstShowr>   r   r?   r@   )r   r   rP   Zdefault_line_editZ	line_editr   r   r   r   :  s    
zFileComboBox.__init__c             C   s   |dkr|   ¡ }t t|ƒ¡S )zReturn True if string is validN)r   rX   Úisfiler   )r   r)   r   r   r   r*   I  s    zFileComboBox.is_valid)NFF)N)r4   r5   r6   r7   r   r*   r   r   r   r   rf   6  s    
rf   c             C   s@   t  | ¡ot  | ¡d dk}t  | ¡o6t  t  | d¡¡}|p>|S )z.Return True if path is a Python module/packager   )z.pyz.pywz__init__.py)rX   rg   ÚsplitextrY   Újoin)ÚpathZ	is_moduleÚ
is_packager   r   r   Úis_module_or_packageP  s    rl   c               @   s,   e Zd ZdZd
dd„Zddd„Zdd	„ ZdS )ÚPythonModulesComboBoxz€
    QComboBox handling Python modules or packages path
    (i.e. .py, .pyw files *and* directories containing __init__.py)
    Fc             C   s   t  | ||¡ d S )N)rJ   r   )r   r   rP   r   r   r   r   \  s    zPythonModulesComboBox.__init__Nc             C   s   |dkr|   ¡ }tt|ƒƒS )zReturn True if string is validN)r   rl   r   )r   r)   r   r   r   r*   _  s    zPythonModulesComboBox.is_validc             C   s   t  | ¡ | j |  ¡ ¡ dS )z9Action to be executed when a valid item has been selectedN)rC   r#   rc   r   r   )r   r   r   r   r#   e  s    
zPythonModulesComboBox.selected)F)N)r4   r5   r6   r7   r   r*   r#   r   r   r   r   rm   W  s   

rm   )"r7   r\   ra   Úos.pathrj   rX   Zqtpy.QtCorer   r   r   r   r   Z
qtpy.QtGuir   Zqtpy.QtWidgetsr   r	   r
   r   r   Zspyder.config.baser   Zspyder.py3compatr   Zspyder.widgets.helperwidgetsr   r   r9   rC   rJ   re   rf   rl   rm   r   r   r   r   Ú<module>   s"   	f)h