
    6@b)                     b    d dl Z d dlmZ d dlmZ d dlmZmZmZ  G d dej                        Z	y)    N)category)QT6)QtCoreQtGui	QtWidgetsc                        e Zd ZdZ fdZ fdZd Z fdZ fdZ fdZ	 fdZ
 fd	Z fd
Z fdZddZd ZddZd Zd Zd Z xZS )CallTipWidgetzF Shows call tips by parsing the current text of Q[Plain]TextEdit.
    c                    t        |t        j                  t        j                  f      sJ t        |   dt        j                  j                         |j                  j                  | j                         t        j                         | _        || _        | j                  |j!                         j#                                | j%                  t&        j(                  j*                         | j-                  t&        j(                  j.                         | j1                  t        j2                  j5                                | j7                  t        j                  j8                         | j;                  d       | j=                  t        j>                  j@                         | jC                  d| jE                         jG                  t        jH                  jJ                  d|       z          | jM                  | jE                         jO                  t        jH                  jP                  d| d      dz         | jS                  d       y)zf Create a call tip manager that is attached to the specified Qt
            text edit widget.
        N   g     o@T)*
isinstancer   	QTextEditQPlainTextEditsuper__init__r   QtToolTip	destroyedconnectdeleteLaterQBasicTimer_hide_timer
_text_editsetFontdocumentdefaultFontsetForegroundRoler   QPaletteToolTipTextsetBackgroundRoleToolTipBase
setPaletteQToolTippalettesetAlignment	AlignLeft	setIndentsetFrameStyleQFrameNoFrame	setMarginstylepixelMetricQStylePM_ToolTipLabelFrameWidthsetWindowOpacity	styleHintSH_ToolTipLabel_OpacitysetWordWrap)self	text_edit	__class__s     ;/usr/lib/python3/dist-packages/qtconsole/call_tip_widget.pyr   zCallTipWidget.__init__   s    )i&9&99;S;S%TUUUvyy001##D$4$45!--/#Y'')5578u~~99:u~~99:	**2245&))--.q9++334q4::<33  ::D$H H 	Idjjl44  88$dLNST 	U    c                    || j                   k(  rE|j                         }|t        j                  j                  k(  r|j                         }|t        j                  j                  t        j                  j                  fv r| j                          n|t        j                  j                  k(  r| j                          y|t        j                  j                  k(  r| j                          ne|t        j                  j                  k(  r| j                  j                          n-|t        j                  j                  k(  r| j!                          t"        | I  ||      S )zb Reimplemented to hide on certain key presses and on text edit focus
            changes.
        T)r   typer   QEventKeyPresskeyr   	Key_Enter
Key_Returnhide
Key_EscapeFocusOutEnterr   stopLeave_leave_event_hider   eventFilter)r3   objeventetyper<   r5   s        r6   rF   zCallTipWidget.eventFilter,   s     $//!JJLE...iik699..		0D0DEEIIKFII000IIK&--000		&-----  %%'&-----&&(w"3..r7   c                     |j                         | j                  j                         k(  r+| j                  j                          | j                          yy)zE Reimplemented to hide the widget when the hide timer fires.
        N)timerIdr   rC   r?   )r3   rH   s     r6   
timerEventzCallTipWidget.timerEventF   sA     ==?d..6688!!#IIK 9r7   c                 X    t         |   |       | j                  j                          y)z1 Reimplemented to cancel the hide timer.
        N)r   
enterEventr   rC   r3   rH   r5   s     r6   rN   zCallTipWidget.enterEventQ   s$     	5!r7   c                     t         |   |       	 | j                  j                  j	                  | j
                         | j                  j                  |        y# t        $ r Y 'w xY w)zG Reimplemented to disconnect signal handlers and event filter.
        N)r   	hideEventr   cursorPositionChanged
disconnect_cursor_position_changed	TypeErrorremoveEventFilterrO   s     r6   rQ   zCallTipWidget.hideEventW   s`     	% 	OO11<<--/ 	))$/  		s   /A 	A)(A)c                 D    t         |   |       | j                          y)z0 Reimplemented to start the hide timer.
        N)r   
leaveEventrE   rO   s     r6   rX   zCallTipWidget.leaveEventc   s     	5! r7   c                    t        j                  |       }t        j                         }|j                  |        |j	                  t         j
                  j                  |       |j                          t        | %  |       y)z6 Reimplemented to paint the background panel.
        N)
r   QStylePainterQStyleOptionFrameinitFromdrawPrimitiver-   PE_PanelTipLabelendr   
paintEvent)r3   rH   painteroptionr5   s       r6   r`   zCallTipWidget.paintEventi   sa     ))$/,,.i..??H5!r7   c                 $    t         |   |       y)z> Reimplemented to allow use of this method as a slot.
        N)r   r   )r3   fontr5   s     r6   r   zCallTipWidget.setFontt   s     	r7   c                     t         |   |       | j                  j                  j	                  | j
                         | j                  j                  |        y)zD Reimplemented to connect signal handlers and event filter.
        N)r   	showEventr   rR   r   rT   installEventFilterrO   s     r6   rf   zCallTipWidget.showEventy   sE     	% --55))	+**40r7   c                 @    	 t         |          S # t        $ r Y yw xY w)zm Avoids an error when the widget has already been deleted.

            Fixes jupyter/qtconsole#507.
        N)r   r   RuntimeError)r3   r5   s    r6   r   zCallTipWidget.deleteLater   s(    
	7&(( 		s    	c                     |j                  di       }|j                  dd      }t        j                  d|z  |      }|r|d|j                          dz   }| j	                  | j                  |            S )z!Show inspection data as a tooltipdataz
text/plain z(?:[^
]*
){%i}Nz
[Documentation continues...])getrematchr_   show_tip_format_tooltip)r3   contentmaxlinesrk   textro   s         r6   show_inspect_datazCallTipWidget.show_inspect_data   sm    {{62&xxb)+h6=%(HHD}}T11$788r7   c                    | j                   }|j                         }|j                         }|j                         dz
  }| j	                  |d      \  | _        }| j
                  dk(  ry| j                  |       | j                  | j                                d}|j                  |      }t        r|j                         j                         }	n;t        j                  j                         j!                         j#                  |      }	|j%                  |j'                               }
|
j)                  |
j+                         |z          | j-                         j/                         }| j-                         j1                         }d}d}|
j+                         |z   |	j/                         |	j+                         z   kD  ra|j%                  |j3                               }|j+                         |z
  |k  r*d|
j+                         z  |	j/                         k  rd}nd	}nd	}|
j5                         |z   |	j1                         |	j5                         z   kD  ra|j%                  |j3                               }|j5                         |z
  |k  r*d|
j5                         z  |	j1                         k  rd}nd
}nd
}t7        |||      }|j%                   |             }
|
j)                  |
j+                         |z          |d	k(  r"|
j)                  |
j+                         |z
         |d
k(  r%|
j9                  |
j5                         |z
  |z
         | j;                  |
       | j=                          y)zL Attempts to show the specified tip at the current cursor location.
        r   F)forward   bottomRight   topLeftT)r   r   
textCursorposition_find_parenthesis_start_positionsetTextresizesizeHint
cursorRectr   screengeometryr   QApplicationinstancedesktopscreenGeometrymapToGlobalbottomRightsetYysizeheightwidthtopRightxgetattrsetXmoveshow)r3   tipr4   r   cursor
search_pos_paddingcursor_rectscreen_rectpoint
tip_height	tip_widthvertical
horizontalpoint_poss                    r6   rp   zCallTipWidget.show_tip   s    OO	%%'%%'__&*
"&"8"8AF #9 #Ha2% 	SDMMO$
 **62#**,557K#0099;CCETTU^_K%%k&=&=&?@

5779w&'YY['')
IIK%%'	
779z!K$6$6$8;==?$JJ**;+?+?+ABF xxzJ&0 UWWY;!3!3!55'H$H 779y ;#4#4#6#HH**;+?+?+ABF xxzI%/UWWY;!2!2!44!(J!'J#
kHj#AB%%ce,

5779w&'uJJuwwy:-.JJuwwy9,w67		%		r7   c                 h   dx}}| j                   j                         }|j                  |      }t        |      dk7  rp|dkD  rk|dk(  r|dk(  r|dz  }n-|dk(  r|r
|dk(  r	 ||fS |dz  }n|dk(  r|s
|dk(  r	 ||fS |dz  }||rdndz  }|j                  |      }t        |      dk7  r|dkD  rkd}||fS )az   If 'forward' is True (resp. False), proceed forwards
            (resp. backwards) through the line that contains 'position' until an
            unmatched closing (resp. opening) parenthesis is found. Returns a
            tuple containing the position of this parenthesis (or -1 if it is
            not found) and the number commas (at depth 0) found along the way.
        r   Cc,r   )(rx   )r   r   characterAtr   )r3   r   rw   commasdepthr   chars          r6   r   zCallTipWidget._find_parenthesis   s     ??++-##H- tn$As{uz!uz  
5A:  
W",H''1D tn$A Hr7   c                    | j                   j                         sjt        j                  j	                         j                  t        j                  j                               | k7  r| j                   j                  d|        yyy)zq Hides the tooltip after some time has passed (assuming the cursor is
            not over the tooltip).
        i,  N)
r   isActiver   r   r   
topLevelAtr   QCursorr   start)r3   s    r6   rE   zCallTipWidget._leave_event_hide   si       ))+ ""++-889J9J9LMQUU""3- V	 ,r7   c                 4    t        j                  dd|      }|S )Nz\033\[(\d|;)+?mrl   )rn   sub)r3   docs     r6   rq   zCallTipWidget._format_tooltip
  s    ff'S1
r7   c                    | j                   j                         }|j                         | j                  k  r| j	                          y| j                  | j                  dz         \  }}|dk7  r| j	                          yy)z8 Updates the tip based on user cursor movement.
        r   rx   N)r   r   r   r   r?   r   )r3   r   r   r   s       r6   rT   z&CallTipWidget._cursor_position_changed  si     ++-?? 4 44IIK#55d6J6JQ6NOHf2~		 r7   )   )T)__name__
__module____qualname____doc__r   rF   rL   rN   rQ   rX   r`   r   rf   r   ru   rp   r   rE   rq   rT   __classcell__)r5   s   @r6   r	   r	   
   sZ    4/4 
0!	"
19CR :	.	r7   r	   )
rn   unicodedatar   qtpyr   r   r   r   QLabelr	    r7   r6   <module>r      s*    	    ) )OI$$ Or7   