| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "textedit.h" | 
|---|
| 43 | #include <QCompleter> | 
|---|
| 44 | #include <QKeyEvent> | 
|---|
| 45 | #include <QAbstractItemView> | 
|---|
| 46 | #include <QtDebug> | 
|---|
| 47 | #include <QApplication> | 
|---|
| 48 | #include <QModelIndex> | 
|---|
| 49 | #include <QAbstractItemModel> | 
|---|
| 50 | #include <QScrollBar> | 
|---|
| 51 |  | 
|---|
| 52 | //! [0] | 
|---|
| 53 | TextEdit::TextEdit(QWidget *parent) | 
|---|
| 54 | : QTextEdit(parent), c(0) | 
|---|
| 55 | { | 
|---|
| 56 | setPlainText(tr("This TextEdit provides autocompletions for words that have more than" | 
|---|
| 57 | " 3 characters. You can trigger autocompletion using ") + | 
|---|
| 58 | QKeySequence("Ctrl+E").toString(QKeySequence::NativeText)); | 
|---|
| 59 | } | 
|---|
| 60 | //! [0] | 
|---|
| 61 |  | 
|---|
| 62 | //! [1] | 
|---|
| 63 | TextEdit::~TextEdit() | 
|---|
| 64 | { | 
|---|
| 65 | } | 
|---|
| 66 | //! [1] | 
|---|
| 67 |  | 
|---|
| 68 | //! [2] | 
|---|
| 69 | void TextEdit::setCompleter(QCompleter *completer) | 
|---|
| 70 | { | 
|---|
| 71 | if (c) | 
|---|
| 72 | QObject::disconnect(c, 0, this, 0); | 
|---|
| 73 |  | 
|---|
| 74 | c = completer; | 
|---|
| 75 |  | 
|---|
| 76 | if (!c) | 
|---|
| 77 | return; | 
|---|
| 78 |  | 
|---|
| 79 | c->setWidget(this); | 
|---|
| 80 | c->setCompletionMode(QCompleter::PopupCompletion); | 
|---|
| 81 | c->setCaseSensitivity(Qt::CaseInsensitive); | 
|---|
| 82 | QObject::connect(c, SIGNAL(activated(const QString&)), | 
|---|
| 83 | this, SLOT(insertCompletion(const QString&))); | 
|---|
| 84 | } | 
|---|
| 85 | //! [2] | 
|---|
| 86 |  | 
|---|
| 87 | //! [3] | 
|---|
| 88 | QCompleter *TextEdit::completer() const | 
|---|
| 89 | { | 
|---|
| 90 | return c; | 
|---|
| 91 | } | 
|---|
| 92 | //! [3] | 
|---|
| 93 |  | 
|---|
| 94 | //! [4] | 
|---|
| 95 | void TextEdit::insertCompletion(const QString& completion) | 
|---|
| 96 | { | 
|---|
| 97 | if (c->widget() != this) | 
|---|
| 98 | return; | 
|---|
| 99 | QTextCursor tc = textCursor(); | 
|---|
| 100 | int extra = completion.length() - c->completionPrefix().length(); | 
|---|
| 101 | tc.movePosition(QTextCursor::Left); | 
|---|
| 102 | tc.movePosition(QTextCursor::EndOfWord); | 
|---|
| 103 | tc.insertText(completion.right(extra)); | 
|---|
| 104 | setTextCursor(tc); | 
|---|
| 105 | } | 
|---|
| 106 | //! [4] | 
|---|
| 107 |  | 
|---|
| 108 | //! [5] | 
|---|
| 109 | QString TextEdit::textUnderCursor() const | 
|---|
| 110 | { | 
|---|
| 111 | QTextCursor tc = textCursor(); | 
|---|
| 112 | tc.select(QTextCursor::WordUnderCursor); | 
|---|
| 113 | return tc.selectedText(); | 
|---|
| 114 | } | 
|---|
| 115 | //! [5] | 
|---|
| 116 |  | 
|---|
| 117 | //! [6] | 
|---|
| 118 | void TextEdit::focusInEvent(QFocusEvent *e) | 
|---|
| 119 | { | 
|---|
| 120 | if (c) | 
|---|
| 121 | c->setWidget(this); | 
|---|
| 122 | QTextEdit::focusInEvent(e); | 
|---|
| 123 | } | 
|---|
| 124 | //! [6] | 
|---|
| 125 |  | 
|---|
| 126 | //! [7] | 
|---|
| 127 | void TextEdit::keyPressEvent(QKeyEvent *e) | 
|---|
| 128 | { | 
|---|
| 129 | if (c && c->popup()->isVisible()) { | 
|---|
| 130 | // The following keys are forwarded by the completer to the widget | 
|---|
| 131 | switch (e->key()) { | 
|---|
| 132 | case Qt::Key_Enter: | 
|---|
| 133 | case Qt::Key_Return: | 
|---|
| 134 | case Qt::Key_Escape: | 
|---|
| 135 | case Qt::Key_Tab: | 
|---|
| 136 | case Qt::Key_Backtab: | 
|---|
| 137 | e->ignore(); | 
|---|
| 138 | return; // let the completer do default behavior | 
|---|
| 139 | default: | 
|---|
| 140 | break; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E | 
|---|
| 145 | if (!c || !isShortcut) // dont process the shortcut when we have a completer | 
|---|
| 146 | QTextEdit::keyPressEvent(e); | 
|---|
| 147 | //! [7] | 
|---|
| 148 |  | 
|---|
| 149 | //! [8] | 
|---|
| 150 | const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); | 
|---|
| 151 | if (!c || (ctrlOrShift && e->text().isEmpty())) | 
|---|
| 152 | return; | 
|---|
| 153 |  | 
|---|
| 154 | static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word | 
|---|
| 155 | bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift; | 
|---|
| 156 | QString completionPrefix = textUnderCursor(); | 
|---|
| 157 |  | 
|---|
| 158 | if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 | 
|---|
| 159 | || eow.contains(e->text().right(1)))) { | 
|---|
| 160 | c->popup()->hide(); | 
|---|
| 161 | return; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | if (completionPrefix != c->completionPrefix()) { | 
|---|
| 165 | c->setCompletionPrefix(completionPrefix); | 
|---|
| 166 | c->popup()->setCurrentIndex(c->completionModel()->index(0, 0)); | 
|---|
| 167 | } | 
|---|
| 168 | QRect cr = cursorRect(); | 
|---|
| 169 | cr.setWidth(c->popup()->sizeHintForColumn(0) | 
|---|
| 170 | + c->popup()->verticalScrollBar()->sizeHint().width()); | 
|---|
| 171 | c->complete(cr); // popup it up! | 
|---|
| 172 | } | 
|---|
| 173 | //! [8] | 
|---|
| 174 |  | 
|---|