
    JeS                         d Z ddlmZ ddlZddlZddlmZmZ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mZmZmZmZ ddlmZ  G d d	e      Z G d
 de      Zy)a+  
Calltip widget used only to show signatures.

Adapted from IPython/frontend/qt/console/call_tip_widget.py of the
`IPython Project <https://github.com/ipython/ipython>`_.
Now located at qtconsole/call_tip_widget.py as part of the
`Jupyter QtConsole Project <https://github.com/jupyter/qtconsole>`_.
    )categoryN)QBasicTimerQCoreApplicationQEventQtQTimerSignal)QCursorQPalette)	QApplicationQFrameQLabel	QTextEditQPlainTextEditQStyleQStyleOptionFrameQStylePainterQToolTip)to_text_stringc                        e Zd ZdZ eee      Z ee      Zd fd	Z fdZ	d Z
d ZddZ fdZd Z fd	Zd
 Z fdZd Z xZS )ToolTipWidgetzF
    Shows tooltips that can be styled with the different themes.
    c                 z   t         t        |   dt        j                         d| _        d| _        t        j                         | _	        || _
        d| _        t               | _        || _        t        j                   dk(  r | j#                  t        j$                         n0| j#                  t        j                  t        j&                  z         | j                  j)                  d       | j+                  t        j,                         | j+                  t        j.                         | j1                  d       | j3                  t4        j6                         | j9                  t4        j:                         | j=                  t?        j@                                | jC                  t        jD                         | jG                  d       | jI                  tJ        jL                         | jO                         }|jQ                  tR        jT                  d|       }| jW                  d|z          | jX                  j[                  | j\                         | j                  j^                  j[                  | j`                         tc        j                         jd                  j[                  | jf                         y)zN
        Shows tooltips that can be styled with the different themes.
        N darwini  F   )4superr   __init__r   ToolTipcompletion_doc_urlr   instanceapp
as_tooltiptipr   _timer_hide
_text_editsysplatformsetWindowFlagsSplashScreenFramelessWindowHintsetIntervalsetTextInteractionFlagsTextSelectableByMouseTextBrowserInteractionsetOpenExternalLinkssetForegroundRoler   ToolTipTextsetBackgroundRoleToolTipBase
setPaletter   palettesetAlignment	AlignLeft	setIndentsetFrameStyler   NoFramestylepixelMetricr   PM_ToolTipLabelFrameWidth	setMarginlinkHoveredconnect_update_hover_html_link_styletimeouthider   applicationStateChanged_should_hide)selfparentr#   r<   delta_margin	__class__s        8/usr/lib/python3/dist-packages/spyder/widgets/calltip.pyr   zToolTipWidget.__init__,   s    	mT+D"**= #	#,,.$!8  <<8#0

R-C-C CD$$S)$$R%=%=>$$R%>%>?!!%(x334x334((*+",,'q6>>*

(()I)I)-t5q<'( 	  !C!CD  ((377??	    c                     t        |       }t               }|j                  |        |j                  t        j
                  |       |j                          t        t        | '  |       y)z,Reimplemented to paint the background panel.N)
r   r   initFromdrawPrimitiver   PE_PanelTipLabelendr   r   
paintEventrG   eventpainteroptionrJ   s       rK   rR   zToolTipWidget.paintEventW   sO    %"$f55v>mT-e4rL   c                    d}d}|| _         |r1t        j                  t        t        j
                               ||}}n||}}t        j                          | j                         }|j                  ||      }| j                  |       y)z=Update style of labels that include rich text and html links.ztext-decoration:none;ztext-decoration:underline;N)
r    r   setOverrideCursorr
   r   PointingHandCursorrestoreOverrideCursortextreplacesetText)rG   urllinklink_hoverednew_textold_textr[   s          rK   rB   z+ToolTipWidget._update_hover_html_link_stylea   st    &3	**723H3H+IJ!-thH!%|hH..0yy{<<(3XrL   c                    || _         | j                  |       | j                  | j                                |j	                         | j                         z
  }| j                  |j                         |       | j                          y)zShow basic tip.T)	r$   r]   resizesizeHintyheightmovexshow)rG   pointr$   rf   s       rK   show_basic_tipzToolTipWidget.show_basic_tipv   s]    SDMMO$GGI%		%'')Q		rL   c                 @   t        j                         j                         t        j                  k7  ry|| _        | j                  |       | j                  | j                                || _	        d}| j                  }||j                         }n|j                  |      }| j                  j                         j                  |      }|j                  |j!                         |z          | j#                         j%                         }	| j#                         j'                         }
d}d}|j!                         |	z   |j%                         |j!                         z   kD  ra|j)                  |j+                               }|j!                         |	z
  |k  r*d|j!                         z  |j%                         k  rd}nd}nd}|j-                         |
z   |j'                         |j-                         z   kD  ra|j)                  |j+                               }|j-                         |
z
  |k  r*d|j-                         z  |j'                         k  rd}nd}nd}t/        |||      }|j)                   |             }|dk(  r=t0        j2                  dk(  rd	}nd
}|j                  |j!                         |	z
  |z
         |dk(  r%|j5                  |j-                         |
z
  |z
         | j7                  |       | j9                         s| j;                          y)zT
        Attempts to show the specified tip at the current cursor location.
        Nr   bottomRight   topLeftntT)r   r!   applicationStater   ApplicationActiver$   r]   rd   re   r   r&   
cursorRectr"   desktopscreenGeometrysetYrf   sizerg   widthmapToGlobaltopRightri   getattrosnamesetXrh   	isVisiblerj   )rG   rk   r$   cursorr   padding	text_editcursor_rectscreen_rect
tip_height	tip_widthvertical
horizontalpoint_posadjusted_points                   rK   show_tipzToolTipWidget.show_tip   s   
   "3359M9MM SDMMO$,OO	>#..0K#..v6Khh&&(77	B

5779w&'YY['')
IIK%%'	
779z!K$6$6$8;==?$JJ**;+?+?+ABF xxzJ&0 uwwy=;#5#5#77'H$H 779y ;#4#4#6#HH**;+?+?+ABF xxzI%/uwwy=;#4#4#66!(J!'J#
kXz#BC"..su5uww$JJ~'')J6@AJJ~'')I5?@		%~~IIKrL   c                    t        j                          | j                  rU| j                  j                  dd      }| j                  j                  dd      }| j                  j                  ||       n%| j                  j                  | j                         t        t        | +  |       | j                          y)R
        Reimplemented to hide it when focus goes out of the main window.
        r   r   	signatureN)r   rZ   r   getsig_completion_help_requestedemitsig_help_requestedr    r   r   mousePressEvent_hide)rG   rT   r   r   rJ   s       rK   r   zToolTipWidget.mousePressEvent   s     	**,&&**626D++//R@I..33D)D##((3mT259

rL   c                 $    | j                          y)r   NrD   rG   rT   s     rK   focusOutEventzToolTipWidget.focusOutEvent        			rL   c                 L    t         t        |   |       | j                          y)z0Override Qt method to hide the tooltip on leave.N)r   r   
leaveEventrD   rG   rT   rJ   s     rK   r   zToolTipWidget.leaveEvent   s    mT-e4		rL   c                 `    t        j                          | j                  j                          y)zHide method helper.N)r   rZ   r%   startrG   s    rK   r   zToolTipWidget._hide   s     **, rL   c                 ^    t         t        |           | j                  j	                          y)z3Override Qt method to add timer and restore cursor.N)r   r   rD   r%   stop)rG   rJ   s    rK   rD   zToolTipWidget.hide   s"    mT')rL   c                 L    |t         j                  k7  r| j                          yyzR
        This widget should hide itself if the application is not active.
        Nr   rw   rD   rG   states     rK   rF   zToolTipWidget._should_hide         B(((IIK )rL   )NF)NN)__name__
__module____qualname____doc__r	   strr   r   r   rR   rB   rl   r   r   r   r   r   rD   rF   __classcell__rJ   s   @rK   r   r   $   s[     %+3$4!)V5*GR
!
 
rL   r   c                        e Zd ZdZd 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 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        t        f      sJ t        t        |   dt        j                         t        j                         | _
        || _        || _        d| _        t               | _        || _        d| _        t$        j&                  dk(  r | j)                  t        j*                         n0| j)                  t        j                  t        j,                  z         | j/                  t        j0                         | j3                  |j5                         j7                                | j9                  t:        j<                         | j?                  t:        j@                         | jC                  tE        jF                                | jI                  t        jJ                         | jM                  d       | jO                  tP        jR                         | jU                  d| jW                         jY                  tZ        j\                  d|       z          t_        j                         j`                  jc                  | jd                         y)zf Create a call tip manager that is attached to the specified Qt
            text edit widget.
        Nr   r   )3
isinstancer   r   r   r   r   r   r   r   r!   r"   r#   hide_timer_onr$   r   _hide_timerr&   _start_positionr'   r(   r)   r*   r+   r-   r.   setFontdocumentdefaultFontr1   r   r2   r3   r4   r5   r   r6   r7   r8   r9   r:   r   r;   r?   r<   r=   r   r>   r   rE   rA   rF   )rG   r   r   r#   rJ   s       rK   r   zCallTipWidget.__init__   s    )i%@AAAmT+D"**=#,,.$*&=#! <<8#0

R-C-C CD$$R%=%=>Y'')5578x334x334((*+",,'q6>>*q4::<3300$> > 	? 	77??	rL   c                    || j                   k(  r|j                         }|t        j                  k(  r|j	                         }| j                   j                         }| j                   j                  |j                         d      }|t        j                  t        j                  t        j                  t        j                  fv r| j                          n|t        j                  k(  r| j                          y|dk(  r| j                          n|t        j                  k(  r| j                          n|t        j                   k(  re| j"                  j%                         r| j&                  j)                  t+        j,                               | k(  rb| j"                  j/                          nG|t        j0                  k(  r| j3                          n#|t        j4                  k(  r| j                          t6        t8        | w  ||      S )zb Reimplemented to hide on certain key presses and on text edit focus
            changes.
        r   )offsetT))r&   typer   KeyPresskey
textCursorget_characterpositionr   	Key_Enter
Key_ReturnKey_DownKey_UprD   
Key_EscapeFocusOutEnterr   isActiver"   
topLevelAtr
   r   r   Leave_leave_event_hideWindowBlockedr   r   eventFilter)rG   objrT   etyper   r   	prev_charrJ   s          rK   r   zCallTipWidget.eventFilter#  sh    $//!JJLE'iik335 OO99&//:KAC : E	2<<;;		3 3IIKBMM)IIK#%IIK&//)		&,,&$$--/((%%gkkm4<$$))+&,,&&&(&...		]D5c5AArL   c                     |j                         | j                  j                         k(  r+| j                  j                          | j                          yy)zE Reimplemented to hide the widget when the hide timer fires.
        N)timerIdr   r   rD   r   s     rK   
timerEventzCallTipWidget.timerEventH  sA     ==?d..6688!!#IIK 9rL   c                 0   t         t        |   |       | j                  r| j	                          | j
                  j                         rL| j                  j                  t        j                               | k(  r| j
                  j                          yyy)z1 Reimplemented to cancel the hide timer.
        N)r   r   
enterEventr#   rD   r   r   r"   r   r
   r   r   r   s     rK   r   zCallTipWidget.enterEventS  so     	mT-e4??IIK%%'
((

gkkm
,
4!!# 5 (rL   c                     t         t        |   |       	 | j                  j                  j                  | j                         | j                  j                  |        y# t        t        f$ r Y yw xY w)zG Reimplemented to disconnect signal handlers and event filter.
        N)
r   r   	hideEventr&   cursorPositionChanged
disconnect_cursor_position_changedremoveEventFilter	TypeErrorRuntimeErrorr   s     rK   r   zCallTipWidget.hideEvent^  sg     	mT,U3	4OO11<<--/
 OO--d3 <( 		s   /A! !A32A3c                 L    t         t        |   |       | j                          y)z0 Reimplemented to start the hide timer.
        N)r   r   r   r   r   s     rK   r   zCallTipWidget.leaveEventk  s      	mT-e4 rL   c                 L    t         t        |   |       | j                          y )N)r   r   r   rD   r   s     rK   r   zCallTipWidget.mousePressEventq  s    mT259		rL   c                     t        |       }t               }|j                  |        |j                  t        j
                  |       |j                          t        t        | '  |       y)z6 Reimplemented to paint the background panel.
        N)
r   r   rN   rO   r   rP   rQ   r   r   rR   rS   s       rK   rR   zCallTipWidget.paintEventu  sQ      %"$f55v>mT-e4rL   c                 ,    t         t        |   |       y)z> Reimplemented to allow use of this method as a slot.
        N)r   r   r   )rG   fontrJ   s     rK   r   zCallTipWidget.setFont  s     	mT*40rL   c                     t         t        |   |       | j                  j                  j                  | j                         | j                  j                  |        y)zD Reimplemented to connect signal handlers and event filter.
        N)r   r   	showEventr&   r   rA   r   installEventFilterr   s     rK   r   zCallTipWidget.showEvent  sG     	mT,U3--55))	+**40rL   c                 $    | j                          y)zV Reimplemented to hide it when focus goes out of the main
            window.
        Nr   r   s     rK   r   zCallTipWidget.focusOutEvent  r   rL   c                    t        j                         j                         t        j                  k7  ry| j                         r | j                  |k(  ry| j                          | j                  }|j                         }|j                         dz
  }| j                  |d      \  | _        }| j                  dk(  ry| j                  r| j                  j                          t!        |      dk(  rD|d   j#                  d      d   }t!        |j#                  d	            }	|	dk(  rd
}
nS|	dk(  rd}
nKd}
nHt!        |      dk(  r8|d   j%                         }t!        |j#                  d	            }|dk(  rd}
nd}
nd}
| j                  j'                  |
|        || _        | j)                  |       | j+                  | j-                                d}|j/                  |      }| j0                  j3                         j5                  |      }|j7                  |j9                         |z          | j;                         j=                         }| j;                         j?                         }d}d}|j9                         |z   |j=                         |j9                         z   kD  ra|jA                  |jC                               }|j9                         |z
  |k  r*d|j9                         z  |j=                         k  rd}nd}nd}|jE                         |z   |j?                         |jE                         z   kD  ra|jA                  |jC                               }|jE                         |z
  |k  r*d|jE                         z  |j?                         k  rd}nd}nd}tG        |||      }|jA                   |             }|dk(  r=tH        jJ                  dk(  rd}nd}|j7                  |j9                         |z
  |z
         |dk(  r%|jM                  |jE                         |z
  |z
         | jO                  |       | jQ                          y)zL Attempts to show the specified tip at the current cursor location.
        NTr   Fforwardr   r   (,ix  rp   i@  i  i	  i
  i  rn   ro   rq   rr   rs   rt   ru   ))r   r!   rv   r   rw   r   r$   rD   r&   r   r   _find_parenthesisr   r   r   r   lensplitstripr   r]   rd   re   rx   r"   ry   rz   r{   rf   r|   rg   r}   r~   r   ri   r   r   r   r   rh   rj   )rG   rk   r$   wrapped_tiplinesr   r   
search_pos_argsnargs	hide_timeargs1nargs1r   r   r   r   r   r   r   r   r   r   s                          rK   r   zCallTipWidget.show_tip  s      "3359M9MM >>xx3		 OO	%%'__&*
"&"8"8AF #9 #Ha2%!!# #$)'*005a8DJJsO,A: $IaZ $I $I%&!+(+113U[[-.Q; $I $I 	""9d3 SDMMO$
 **62hh&&(77	B

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"..su5uww$JJ~'')J6@AJJ~'')I5?@		%		rL   c                    dx}}| j                   j                         }t        |j                  |            }t	        |      dk7  ry|dkD  rt|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  }t        |j                  |            }t	        |      dk7  r|dkD  rtd}||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   Ccr   r   r   r   r   )r&   r   r   characterAtr   )rG   r   r   commasdepthr   chars          rK   r   zCallTipWidget._find_parenthesis  s     ??++-h228<= tn$As{uz!uz  
5A:  
W",H!("6"6x"@AD tn$A HrL   c                     | j                   ri| j                  j                         sN| j                  j	                  t        j                               | k7  r| j                  j                  d|        yyyy)zq Hides the tooltip after some time has passed (assuming the cursor is
            not over the tooltip).
        i   N)r   r   r   r"   r   r
   r   r   r   s    rK   r   zCallTipWidget._leave_event_hide"  sa     t'7'7'@'@'B HH.$6""3- 7	 (CrL   c                 p   | j                   j                         }|j                         }| j                   j                         }t	        |j                  |dz
              }|| j                  k  r| j                          y|dk(  r0| j                  |dz
  d      \  }}|dk(  r| j                          yyy)z8 Updates the tip based on user cursor movement.
        r   r   Fr   r   N)	r&   r   r   r   r   r  r   rD   r   )rG   r   r   r   r  r   r   s          rK   r   z&CallTipWidget._cursor_position_changed/  s     ++-??$??++-h228a<@At+++IIKS[++HqL%+HFCby		  rL   c                 L    |t         j                  k7  r| j                          yyr   r   r   s     rK   rF   zCallTipWidget._should_hide=  r   rL   )FF)T)r   r   r   r   r   r   r   r   r   r   r   rR   r   r   r   r   r   r   r   rF   r   r   s   @rK   r   r      s]    $L#BJ	$4!	51
1h\ :	.rL   r   )r   unicodedatar   r   r'   qtpy.QtCorer   r   r   r   r   r	   
qtpy.QtGuir
   r   qtpy.QtWidgetsr   r   r   r   r   r   r   r   r   spyder.py3compatr   r   r    rL   rK   <module>r     sS    ! 	 
! ! (5 5 5
 ,MF MbMF MrL   