# Code adapted from:
#
# http://rowinggolfer.blogspot.de/2010/08/qtextedit-with-autocompletion-using.html
#
# and based on:
#
# http://qt-project.org/doc/qt-4.8/tools-customcompleter.html

from ...external.qt import QtGui, QtCore


__all__ = ["CompletionTextEdit"]


class CompletionTextEdit(QtGui.QTextEdit):

    def __init__(self, parent=None):

        super(CompletionTextEdit, self).__init__(parent)

        self.setMinimumWidth(400)
        self.completer = None
        self.word_list = None

        self.moveCursor(QtGui.QTextCursor.End)

    def set_word_list(self, word_list):
        self.word_list = word_list
        self.set_completer(QtGui.QCompleter(word_list))

    def set_completer(self, completer):

        if self.completer:
            self.disconnect(self.completer, 0, self, 0)
        if not completer:
            return

        completer.setWidget(self)
        completer.setCompletionMode(QtGui.QCompleter.PopupCompletion)
        completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.completer = completer
        self.connect(self.completer, QtCore.SIGNAL("activated(const QString&)"), self.insert_completion)

    def insert_completion(self, completion):

        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        tc.deleteChar()

        completion = completion + " "

        self.setTextCursor(tc)

        self.insertPlainText(completion)


    def text_under_cursor(self):
        tc = self.textCursor()
        tc.select(QtGui.QTextCursor.WordUnderCursor)
        return tc.selectedText()

    # The following methods override methods in QTextEdit and should not be
    # renamed.

    def focusInEvent(self, event):
        if self.completer:
            self.completer.setWidget(self);
        QtGui.QTextEdit.focusInEvent(self, event)

    def keyPressEvent(self, event):

        if self.completer and self.completer.popup().isVisible():
            if event.key() in (
            QtCore.Qt.Key_Enter,
            QtCore.Qt.Key_Return,
            QtCore.Qt.Key_Escape,
            QtCore.Qt.Key_Tab,
            QtCore.Qt.Key_Backtab):
                event.ignore()
                return

        # Check if TAB has been pressed
        isShortcut = event.key() == QtCore.Qt.Key_Tab

        if (not self.completer or not isShortcut):
            QtGui.QTextEdit.keyPressEvent(self, event)
            return

        eow = "~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="

        completionPrefix = self.text_under_cursor()

        if not isShortcut and (len(event.text()) == 0 or event.text()[-1:] in eow):
            self.completer.popup().hide()
            return

        if (completionPrefix != self.completer.completionPrefix()):
            self.completer.setCompletionPrefix(completionPrefix)
            popup = self.completer.popup()
            popup.setCurrentIndex(
                self.completer.completionModel().index(0,0))

        cr = self.cursorRect()
        cr.setWidth(self.completer.popup().sizeHintForColumn(0)
            + self.completer.popup().verticalScrollBar().sizeHint().width())
        self.completer.complete(cr)

@@ -845,32 +845,73 @@
 return%0A%0A        
+self.completer = completer%0A%0A        self.
 completer.setWid
@@ -920,32 +920,37 @@
 t(self)%0A        
+self.
 completer.setCom
@@ -999,24 +999,29 @@
 on)%0A        
+self.
 completer.se
@@ -1090,73 +1090,9 @@
 eter
- = completer%0A        self.connect(self.completer, QtCore.SIGNAL(%22
+.
 acti
@@ -1100,28 +1100,17 @@
 ated
-(const QString&)%22), 
+.connect(
 self

Simplify event connection as suggested by @ChrisBeaumont