<s> from IPython . external . qt import QtCore , QtGui <EOL> class CompletionWidget ( QtGui . QListWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text_edit ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( text_edit , ( QtGui . QTextEdit , QtGui . QPlainTextEdit ) ) <EOL> super ( CompletionWidget , self ) . __init__ ( ) <EOL> self . _text_edit = text_edit <EOL> self . setAttribute ( QtCore . Qt . WA_StaticContents ) <EOL> self . setWindowFlags ( QtCore . Qt . ToolTip | QtCore . Qt . WindowStaysOnTopHint ) <EOL> self . setFocusProxy ( self . _text_edit ) <EOL> self . setFrameShadow ( QtGui . QFrame . Plain ) <EOL> self . setFrameShape ( QtGui . QFrame . StyledPanel ) <EOL> self . itemActivated . connect ( self . _complete_current ) <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 , text = event . key ( ) , event . text ( ) <EOL> if key in ( QtCore . Qt . Key_Return , QtCore . Qt . Key_Enter , <EOL> QtCore . Qt . Key_Tab ) : <EOL> self . _complete_current ( ) <EOL> return True <EOL> elif key == QtCore . Qt . Key_Escape : <EOL> self . hide ( ) <EOL> return True <EOL> elif key in ( QtCore . Qt . Key_Up , QtCore . Qt . Key_Down , <EOL> QtCore . Qt . Key_PageUp , QtCore . Qt . Key_PageDown , <EOL> QtCore . Qt . Key_Home , QtCore . Qt . Key_End ) : <EOL> self . keyPressEvent ( event ) <EOL> return True <EOL> elif etype == QtCore . QEvent . FocusOut : <EOL> self . hide ( ) <EOL> return super ( CompletionWidget , self ) . eventFilter ( obj , event ) <EOL> def hideEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CompletionWidget , self ) . hideEvent ( event ) <EOL> self . _text_edit . cursorPositionChanged . disconnect ( self . _update_current ) <EOL> self . _text_edit . removeEventFilter ( self ) <EOL> def showEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> super ( CompletionWidget , self ) . showEvent ( event ) <EOL> self . _text_edit . cursorPositionChanged . connect ( self . _update_current ) <EOL> self . _text_edit . installEventFilter ( self ) <EOL> def show_items ( self , cursor , items ) : <EOL> """<STR_LIT>""" <EOL> text_edit = self . _text_edit <EOL> point = text_edit . cursorRect ( cursor ) . bottomRight ( ) <EOL> point = text_edit . mapToGlobal ( point ) <EOL> screen_rect = QtGui . QApplication . desktop ( ) . availableGeometry ( self ) <EOL> if screen_rect . size ( ) . height ( ) - point . y ( ) - self . height ( ) < <NUM_LIT:0> : <EOL> point = text_edit . mapToGlobal ( text_edit . cursorRect ( ) . topRight ( ) ) <EOL> point . setY ( point . y ( ) - self . height ( ) ) <EOL> self . move ( point ) <EOL> self . _start_position = cursor . position ( ) <EOL> self . clear ( ) <EOL> self . addItems ( items ) <EOL> self . setCurrentRow ( <NUM_LIT:0> ) <EOL> self . show ( ) <EOL> def _complete_current ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _current_text_cursor ( ) . insertText ( self . currentItem ( ) . text ( ) ) <EOL> self . hide ( ) <EOL> def _current_text_cursor ( self ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _text_edit . textCursor ( ) <EOL> if cursor . position ( ) >= self . _start_position : <EOL> cursor . setPosition ( self . _start_position , <EOL> QtGui . QTextCursor . KeepAnchor ) <EOL> return cursor <EOL> def _update_current ( self ) : <EOL> """<STR_LIT>""" <EOL> prefix = self . _current_text_cursor ( ) . selection ( ) . toPlainText ( ) <EOL> if prefix : <EOL> items = self . findItems ( prefix , ( QtCore . Qt . MatchStartsWith | <EOL> QtCore . Qt . MatchCaseSensitive ) ) <EOL> if items : <EOL> self . setCurrentItem ( items [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . hide ( ) <EOL> else : <EOL> self . hide ( ) </s>
