B
    tËV  ã               @   s*   d Z ddlmZmZ G dd„ dejƒZdS )z.A dropdown completer widget for the qtconsole.é    )ÚQtCoreÚQtGuic                   sx   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd	„Z‡ fd
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚCompletionWidgetz& A widget for GUI tab completion.
    c                sÞ   |j }t|tjtjfƒst‚tt| ƒ ¡  || _	|  
tjj¡ |  tjj¡ |  tjj¡ |  tjj¡ |  tjj¡ |  tjj¡ | ¡ }|  tjj¡ | |¡ |  | j	¡ |  tjj¡ |   tjj!¡ | j" #| j$¡ dS )zg Create a completion widget that is attached to the specified Qt
            text edit widget.
        N)%Z_controlÚ
isinstancer   Z	QTextEditZQPlainTextEditÚAssertionErrorÚsuperr   Ú__init__Ú
_text_editZsetEditTriggersZQAbstractItemViewZNoEditTriggersZsetHorizontalScrollBarPolicyr   ÚQtZScrollBarAlwaysOffZsetSelectionBehaviorZ
SelectRowsZsetSelectionModeZSingleSelectionZsetWindowFlagsZPopupZsetAttributeZWA_StaticContentsZfocusPolicyZsetFocusPolicyZNoFocusZsetFocusProxyZsetFrameShadowZQFrameZPlainZsetFrameShapeZStyledPanelZitemActivatedÚconnectÚ_complete_current)ÚselfZconsole_widgetÚ	text_editZoriginal_policy)Ú	__class__© ú:lib/python3.7/site-packages/qtconsole/completion_widget.pyr      s"    
zCompletionWidget.__init__c                sf   || krT|  ¡ tjjkrT|  | ¡ ¡}tj |¡}|r@|  	|¡sH|| krLdS |  
¡  tt| ƒ ||¡S )zj Reimplemented to handle mouse input and to auto-hide when the
            text edit loses focus.
        F)Útyper   ZQEventZMouseButtonPressÚmapToGlobalÚposr   ÚQApplicationZwidgetAtZisAncestorOfÚcancel_completionr   r   ÚeventFilter)r   ÚobjÚeventr   Útarget)r   r   r   r   .   s    zCompletionWidget.eventFilterc                s’   |  ¡ }|tjjtjjtjjfkr,|  ¡  nb|tjjkrB|  ¡  nL|tjj	tjj
tjjtjjtjjtjjfkr~tt| ƒ |¡S tj | j|¡ d S )N)Úkeyr   r
   Z
Key_ReturnZ	Key_EnterZKey_Tabr   Z
Key_EscapeÚhideZKey_UpZKey_DownZ
Key_PageUpZKey_PageDownZKey_HomeZKey_Endr   r   ÚkeyPressEventr   r   Z	sendEventr	   )r   r   r   )r   r   r   r   =   s    

zCompletionWidget.keyPressEventc                s.   t t| ƒ |¡ | jj | j¡ |  | ¡ dS )zG Reimplemented to disconnect signal handlers and event filter.
        N)r   r   Ú	hideEventr	   ÚcursorPositionChangedZ
disconnectÚ_update_currentZremoveEventFilter)r   r   )r   r   r   r   O   s    zCompletionWidget.hideEventc                s.   t t| ƒ |¡ | jj | j¡ |  | ¡ dS )zD Reimplemented to connect signal handlers and event filter.
        N)r   r   Ú	showEventr	   r   r   r    ZinstallEventFilter)r   r   )r   r   r   r!   V   s    zCompletionWidget.showEventc             C   sî   | j }| |¡ ¡ }| |¡}|  ¡  |  |¡ |  ¡  ¡ }tj	 
¡  | ¡}| ¡  ¡ | ¡  | ¡  | dk r”| | ¡  ¡ ¡}| | ¡ | ¡ |  d¡|  ¡  ¡  ¡  }|  | ¡ | ¡ ||¡ | ¡ | _|  d¡ |  ¡  |  ¡  dS )ze Shows the completion widget with 'items' at the position specified
            by 'cursor'.
        r   N)r	   Z
cursorRectZbottomRightr   ÚclearZaddItemsZsizeHintÚheightr   r   ZdesktopZavailableGeometryÚsizeÚyZtopRightZsetYZsizeHintForColumnZverticalScrollBarÚwidthZsetGeometryÚxÚpositionÚ_start_positionZsetCurrentRowZraise_Zshow)r   ÚcursorÚitemsr   Zpointr#   Zscreen_rectÚwr   r   r   Ú
show_itemsa   s"    

$

zCompletionWidget.show_itemsc             C   s"   |   ¡  |  ¡  ¡ ¡ |  ¡  dS )zB Perform the completion with the currently selected item.
        N)Ú_current_text_cursorZ
insertTextZcurrentItemÚtextr   )r   r   r   r   r   |   s    z"CompletionWidget._complete_currentc             C   s.   | j  ¡ }| ¡ | jkr*| | jtjj¡ |S )zn Returns a cursor with text between the start position and the
            current position selected.
        )r	   Z
textCursorr(   r)   ZsetPositionr   ZQTextCursorZ
KeepAnchor)r   r*   r   r   r   r.   ‚   s
    

z%CompletionWidget._current_text_cursorc             C   sV   |   ¡  ¡  ¡ }|rJ|  |tjjtjjB ¡}|r@|  |d ¡ qR|  	¡  n|  	¡  dS )z= Updates the current item based on the current text.
        r   N)
r.   Z	selectionZtoPlainTextZ	findItemsr   r
   ZMatchStartsWithZMatchCaseSensitiveZsetCurrentItemr   )r   Úprefixr+   r   r   r   r    Œ   s    
z CompletionWidget._update_currentc             C   s   |   ¡  d S )N)r   )r   r   r   r   r   š   s    z"CompletionWidget.cancel_completion)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r!   r-   r   r.   r    r   Ú__classcell__r   r   )r   r   r      s    
r   N)r4   Zqtconsole.qtr   r   ZQListWidgetr   r   r   r   r   Ú<module>   s   