<s> import re <EOL> from textwrap import dedent <EOL> from unicodedata import category <EOL> from IPython . external . qt import QtCore , QtGui <EOL> class CallTipWidget ( QtGui . QLabel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text_edit ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( text_edit , ( QtGui . QTextEdit , QtGui . QPlainTextEdit ) ) <EOL> super ( CallTipWidget , self ) . __init__ ( None , QtCore . Qt . ToolTip ) <EOL> self . _hide_timer = QtCore . QBasicTimer ( ) <EOL> self . _text_edit = text_edit <EOL> self . setFont ( text_edit . document ( ) . defaultFont ( ) ) <EOL> self . setForegroundRole ( QtGui . QPalette . ToolTipText ) <EOL> self . setBackgroundRole ( QtGui . QPalette . ToolTipBase ) <EOL> self . setPalette ( QtGui . QToolTip . palette ( ) ) <EOL> self . setAlignment ( QtCore . Qt . AlignLeft ) <EOL> self . setIndent ( <NUM_LIT:1> ) <EOL> self . setFrameStyle ( QtGui . QFrame . NoFrame ) <EOL> self . setMargin ( <NUM_LIT:1> + self . style ( ) . pixelMetric ( <EOL> QtGui . QStyle . PM_ToolTipLabelFrameWidth , None , self ) ) <EOL> self . setWindowOpacity ( self . style ( ) . styleHint ( <EOL> QtGui . QStyle . SH_ToolTipLabel_Opacity , None , self , None ) / <NUM_LIT> ) <EOL> def eventFilter ( self , obj , event ) : <EOL> """<STR_LIT>""" <EOL> if obj == self . _text_edit : <EOL> etype = event . type ( ) <EOL> if etype == QtCore . QEvent . KeyPress : <EOL> key = event . key ( ) <EOL> if key in ( QtCore . Qt . Key_Enter , QtCore . Qt . Key_Return ) : <EOL> self . hide ( ) <EOL> elif key == QtCore . Qt . Key_Escape : <EOL> self . hide ( ) <EOL> return True <EOL> elif etype == QtCore . QEvent . FocusOut : <EOL> self . hide ( ) <EOL> elif etype == QtCore . QEvent . Enter : <EOL> self . _hide_timer . stop ( ) <EOL> elif etype == QtCore . QEvent . Leave : <EOL> self . _leave_event_hide ( ) <EOL> return super ( CallTipWidget , self ) . eventFilter ( obj , event ) <EOL> def timerEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . timerId ( ) == self . _hide_timer . timerId ( ) : <EOL> self . _hide_timer . stop ( ) <EOL> self . hide ( ) <EOL> def enterEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CallTipWidget , self ) . enterEvent ( event ) <EOL> self . _hide_timer . stop ( ) <EOL> def hideEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CallTipWidget , self ) . hideEvent ( event ) <EOL> self . _text_edit . cursorPositionChanged . disconnect ( <EOL> self . _cursor_position_changed ) <EOL> self . _text_edit . removeEventFilter ( self ) <EOL> def leaveEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CallTipWidget , self ) . leaveEvent ( event ) <EOL> self . _leave_event_hide ( ) <EOL> def paintEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> painter = QtGui . QStylePainter ( self ) <EOL> option = QtGui . QStyleOptionFrame ( ) <EOL> option . initFrom ( self ) <EOL> painter . drawPrimitive ( QtGui . QStyle . PE_PanelTipLabel , option ) <EOL> painter . end ( ) <EOL> super ( CallTipWidget , self ) . paintEvent ( event ) <EOL> def setFont ( self , font ) : <EOL> """<STR_LIT>""" <EOL> super ( CallTipWidget , self ) . setFont ( font ) <EOL> def showEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CallTipWidget , self ) . showEvent ( event ) <EOL> self . _text_edit . cursorPositionChanged . connect ( <EOL> self . _cursor_position_changed ) <EOL> self . _text_edit . installEventFilter ( self ) <EOL> def show_call_info ( self , call_line = None , doc = None , maxlines = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> if doc : <EOL> match = re . match ( "<STR_LIT>" % maxlines , doc ) <EOL> if match : <EOL> doc = doc [ : match . end ( ) ] + '<STR_LIT>' <EOL> else : <EOL> doc = '<STR_LIT>' <EOL> if call_line : <EOL> doc = '<STR_LIT>' . join ( [ call_line , doc ] ) <EOL> return self . show_tip ( doc ) <EOL> def show_tip ( self , tip ) : <EOL> """<STR_LIT>""" <EOL> text_edit = self . _text_edit <EOL> document = text_edit . document ( ) <EOL> cursor = text_edit . textCursor ( ) <EOL> search_pos = cursor . position ( ) - <NUM_LIT:1> <EOL> self . _start_position , _ = self . _find_parenthesis ( search_pos , <EOL> forward = False ) <EOL> if self . _start_position == - <NUM_LIT:1> : <EOL> return False <EOL> self . setText ( tip ) <EOL> self . resize ( self . sizeHint ( ) ) <EOL> padding = <NUM_LIT:3> <EOL> cursor_rect = text_edit . cursorRect ( cursor ) <EOL> screen_rect = QtGui . qApp . desktop ( ) . screenGeometry ( text_edit ) <EOL> point = text_edit . mapToGlobal ( cursor_rect . bottomRight ( ) ) <EOL> point . setY ( point . y ( ) + padding ) <EOL> tip_height = self . size ( ) . height ( ) <EOL> if point . y ( ) + tip_height > screen_rect . height ( ) : <EOL> point = text_edit . mapToGlobal ( cursor_rect . topRight ( ) ) <EOL> point . setY ( point . y ( ) - tip_height - padding ) <EOL> self . move ( point ) <EOL> self . show ( ) <EOL> return True <EOL> def _find_parenthesis ( self , position , forward = True ) : <EOL> """<STR_LIT>""" <EOL> commas = depth = <NUM_LIT:0> <EOL> document = self . _text_edit . document ( ) <EOL> char = document . characterAt ( position ) <EOL> while category ( char ) != '<STR_LIT>' and position > <NUM_LIT:0> : <EOL> if char == '<STR_LIT:U+002C>' and depth == <NUM_LIT:0> : <EOL> commas += <NUM_LIT:1> <EOL> elif char == '<STR_LIT:)>' : <EOL> if forward and depth == <NUM_LIT:0> : <EOL> break <EOL> depth += <NUM_LIT:1> <EOL> elif char == '<STR_LIT:(>' : <EOL> if not forward and depth == <NUM_LIT:0> : <EOL> break <EOL> depth -= <NUM_LIT:1> <EOL> position += <NUM_LIT:1> if forward else - <NUM_LIT:1> <EOL> char = document . characterAt ( position ) <EOL> else : <EOL> position = - <NUM_LIT:1> <EOL> return position , commas <EOL> def _leave_event_hide ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( not self . _hide_timer . isActive ( ) and <EOL> QtGui . qApp . topLevelAt ( QtGui . QCursor . pos ( ) ) != self ) : <EOL> self . _hide_timer . start ( <NUM_LIT> , self ) <EOL> def _cursor_position_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _text_edit . textCursor ( ) <EOL> if cursor . position ( ) <= self . _start_position : <EOL> self . hide ( ) <EOL> else : <EOL> position , commas = self . _find_parenthesis ( self . _start_position + <NUM_LIT:1> ) <EOL> if position != - <NUM_LIT:1> : <EOL> self . hide ( ) </s>
