author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
QObject::connect(c, SIGNAL(activated(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
this, SLOT(insertCompletion(QString))); |
0 | 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 |