o
    º¼tf×  ã                   @   sJ   d Z ddlZddlZddlmZ ddlmZmZmZ G dd„ dejƒZ	dS )z.A dropdown completer widget for the qtconsole.é    N)ÚQT6)ÚQtCoreÚQtGuiÚ	QtWidgetsc                       s„   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡ fdd„Z‡ fd	d
„Z‡ fdd„Zd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.
    r   c                    sê   |j }t|tjtjfƒsJ ‚tƒ j|d || _|dkr|n|  ¡  	¡ | _
|  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.
        )Úparentr   N)$Ú_controlÚ
isinstancer   Z	QTextEditZQPlainTextEditÚsuperÚ__init__Ú
_text_editÚsizeHintÚheightÚ_height_maxZsetEditTriggersZQAbstractItemViewZNoEditTriggersZsetSelectionBehaviorZ
SelectRowsZsetSelectionModeZSingleSelectionZsetWindowFlagsr   ÚQtZPopupZsetAttributeZWA_StaticContentsZfocusPolicyZsetFocusPolicyZNoFocusZsetFocusProxyZsetFrameShadowZQFrameZPlainZsetFrameShapeZStyledPanelZitemActivatedÚconnectÚ_complete_current)ÚselfZconsole_widgetr   Z	text_editZoriginal_policy©Ú	__class__© úd/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/qtconsole/completion_widget.pyr      s"   
zCompletionWidget.__init__c                    sb   || u r*|  ¡ tjjkr*|  | ¡ ¡}tj |¡}|r |  	|¡s$|| u r&dS |  
¡  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
   ÚeventFilter)r   ÚobjÚeventr   Útargetr   r   r   r   2   s   zCompletionWidget.eventFilterc                    s’   |  ¡ }|tjjtjjtjjfv r|  ¡  d S |tjjkr#|  ¡  d S |tjj	tjj
tjjtjjtjjtjjfv r?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
   ÚkeyPressEventr   r   Z	sendEventr   )r   r   r"   r   r   r   r$   A   s   ÿþzCompletionWidget.keyPressEventc                    sB   t ƒ  |¡ z
| jj | j¡ W n	 ty   Y nw |  | ¡ dS )zG Reimplemented to disconnect signal handlers and event filter.
        N)r
   Ú	hideEventr   ÚcursorPositionChangedZ
disconnectÚ_update_currentÚ	TypeErrorZremoveEventFilter©r   r   r   r   r   r%   S   s   ÿzCompletionWidget.hideEventc                    s*   t ƒ  |¡ | jj | j¡ |  | ¡ dS )zD Reimplemented to connect signal handlers and event filter.
        N)r
   Ú	showEventr   r&   r   r'   ZinstallEventFilterr)   r   r   r   r*   ]   s   zCompletionWidget.showEventc              	   C   s  |   |¡}|  ¡  g }|D ]C}tj tj | dd¡¡¡s+tj tj | dd¡¡¡r5| | dd¡¡ qt	 
¡ }| tjj|¡ | | d¡d ¡ |  |¡ qtj tj |¡¡}|D ]$}	t	 
¡ }| tjj|	¡ |ru|	 |¡d }
n|	}
| |
¡ |  |¡ q]tr‹|  ¡  ¡ }nt	j ¡  | ¡}| ¡ }tt| j|d ƒƒ}|| ¡  | dk rÈ| j | j  ¡  !¡ ¡}| ¡ }| "t|t||d ƒ ƒ¡ |  #d¡|  $¡  %¡  &¡  d|  '¡   }|  (| )¡ | ¡ ||¡ |j*t+j,j-|d	 | .¡ | _/|  0d¡ |  1¡  |  2¡  d
S )ze Shows the completion widget with 'items' at the position specified
            by 'cursor'.
        ú"Ú Ú.éÿÿÿÿé2   r   é
   é   )ÚnN)3Ú_get_top_left_positionÚclearÚosÚpathÚisfileÚabspathÚreplaceÚisdirÚappendr   ZQListWidgetItemZsetDatar   r   ÚUserRoleZsetTextÚsplitZaddItemÚdirnameÚcommonprefixr   ÚscreenZavailableGeometryr   Zdesktopr   ÚintÚminr   Úyr   r   Ú
cursorRectZtopRightÚsetYZsizeHintForColumnZverticalScrollBarr   ÚwidthZ
frameWidthZsetGeometryÚxZmovePositionr   ÚQTextCursorZLeftÚpositionÚ_start_positionZsetCurrentRowZraise_Úshow)r   ÚcursorÚitemsZprefix_lengthÚpointZ
path_itemsÚitemZ	list_itemÚcommon_prefixÚ	path_itemÚtextZscreen_rectZscreen_heightr   ÚpyÚwr   r   r   Ú
show_itemsh   sR   
ÿ
ÿ
þ

zCompletionWidget.show_itemsc                 C   s   | j  | j  ¡  ¡ ¡S )z0 Get top left position for this widget.
        )r   r   rD   ZbottomRight©r   rL   r   r   r   r3   ¢   s   z'CompletionWidget._get_top_left_positionc                 C   s,   |   ¡  tjj¡}|  ¡  |¡ |  ¡  dS )zB Perform the completion with the currently selected item.
        N)ZcurrentItemÚdatar   r   r<   Ú_current_text_cursorZ
insertTextr#   )r   rR   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   Ú
textCursorrI   rJ   ZsetPositionr   rH   Z
KeepAnchorrV   r   r   r   rX   ®   s   
ÿz%CompletionWidget._current_text_cursorc                 C   s†   | j  ¡ }|  |¡}| |  ¡ ¡ |  |¡ |  ¡  ¡  ¡ }|r=|  	|t
jjt
jjB ¡}|r7|  |d ¡ dS |  ¡  dS |  ¡  dS )zh Updates the current item based on the current text and the
            position of the widget.
        r   N)r   rY   r3   rE   rC   ÚmoverX   Z	selectionZtoPlainTextZ	findItemsr   r   ZMatchStartsWithZMatchCaseSensitiveZsetCurrentItemr#   )r   rL   rN   ÚprefixrM   r   r   r   r'   ¸   s   


ÿz CompletionWidget._update_currentc                 C   s   |   ¡  d S r!   )r#   )r   r   r   r   r   Î   s   z"CompletionWidget.cancel_completion)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r%   r*   rU   r3   r   rX   r'   r   Ú__classcell__r   r   r   r   r   
   s     

:
r   )
r_   r5   ÚsysZqtpyr   r   r   r   ZQListWidgetr   r   r   r   r   Ú<module>   s    