|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# textedit.cpp Example File
tools/customcompleter/textedit.cpp

    /****************************************************************************
    **
    ** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
    **
    ** This file is part of the example classes of the Qt Toolkit.
    **
    ** Licensees holding valid Qt Preview licenses may use this file in
    ** accordance with the Qt Preview License Agreement provided with the
    ** Software.
    **
    ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
    ** information about Qt Commercial License Agreements.
    **
    ** Contact info@trolltech.com if any conditions of this licensing are
    ** not clear to you.
    **
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    **
    ****************************************************************************/

    #include "textedit.h"
    #include <QCompleter>
    #include <QKeyEvent>
    #include <QAbstractItemView>
    #include <QtDebug>
    #include <QApplication>
    #include <QModelIndex>
    #include <QAbstractItemModel>

    TextEdit::TextEdit(QWidget *parent)
    : QTextEdit(parent), c(0)
    {
        setPlainText(tr("This TextEdit provides autocompletions for words that have more than"
                        " 3 characters. You can trigger autocompletion using Ctrl+E."));
    }

    TextEdit::~TextEdit()
    {
    }

    void TextEdit::setCompleter(QCompleter *completer)
    {
        if (c)
            QObject::disconnect(c, 0, this, 0);

        c = completer;

        if (!c)
            return;

        c->setWidget(this);
        c->setCompletionMode(QCompleter::PopupCompletion);
        QObject::connect(completer, SIGNAL(activated(const QString&)),
                         this, SLOT(insertCompletion(const QString&)));
    }

    QCompleter *TextEdit::completer() const
    {
        return c;
    }

    void TextEdit::insertCompletion(const QString& completion)
    {
        QTextCursor tc = textCursor();
        int extra = completion.length() - c->completionPrefix().length();
        tc.insertText(completion.right(extra));
        tc.movePosition(QTextCursor::EndOfWord);
        setTextCursor(tc);
    }

    QString TextEdit::textUnderCursor() const
    {
        QTextCursor tc = textCursor();
        tc.select(QTextCursor::WordUnderCursor);
        return tc.selectedText();
    }

    void TextEdit::keyPressEvent(QKeyEvent *e)
    {
        if (c && c->popup()->isVisible()) {
            // The following keys are forwarded by the completer to the widget
           switch (e->key()) {
           case Qt::Key_Enter:
           case Qt::Key_Return:
           case Qt::Key_Escape:
           case Qt::Key_Tab:
           case Qt::Key_Backtab:
           case Qt::Key_PageUp:
           case Qt::Key_PageDown:
           case Qt::Key_F4:
                e->ignore();
                return; // let the completer do default behavior
           case Qt::Key_Up:
           case Qt::Key_Down: {
                QModelIndex curIndex = c->popup()->currentIndex();
                int row = curIndex.row() + (e->key() == Qt::Key_Up ? -1 : 1);
                int col = curIndex.column();
                QModelIndex nextIndex = c->completionModel()->index(row, col);
                if (nextIndex.isValid()) {
                    c->popup()->selectionModel()->clear();
                    c->popup()->selectionModel()->setCurrentIndex(nextIndex,
                         QItemSelectionModel::Select | QItemSelectionModel::Rows);
                }
                return;
           }
           default:
               break;
           }
        }

        QTextEdit::keyPressEvent(e);

        if (!c)
            return;

        static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
        bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
        bool hasModifier = e->modifiers() != Qt::NoModifier;
        QString completionPrefix = textUnderCursor();

        if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3
                          || eow.contains(e->text().right(1)))) {
            c->popup()->hide();
            return;
        }

        if (completionPrefix != c->completionPrefix()) {
            c->setCompletionPrefix(completionPrefix);
            c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
        }
        c->complete(cursorRect()); // popup it up!
    }