-
Notifications
You must be signed in to change notification settings - Fork 1
/
QtCompleterWithAdvancedCompletion.cpp
244 lines (218 loc) · 7.28 KB
/
QtCompleterWithAdvancedCompletion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "QtCompleterWithAdvancedCompletion.h"
#include <QEvent>
#include <QKeyEvent>
#include <QDebug>
/*
* There are some existing bugs to support this feature in QCompleter
* And people are actively working on it.
* https://bugreports.qt-project.org/browse/QTBUG-7830
* https://bugreports.qt-project.org/browse/QTBUG-3414
* https://codereview.qt-project.org/#change,43718
*
* Hopefully Qt5 will have this feature soon, and we shall remove this file
*/
QtCompleterWithAdvancedCompletion::QtCompleterWithAdvancedCompletion(QLineEdit *le) :
QObject(le),
popuplist(new QListView),
w(le),
model(new QStringListModel),
maxVisibleItems(10),
noItemsShown(0),
filterMode(QtCompleterWithAdvancedCompletion::ContainsWord)
{
init();
connect(popuplist, SIGNAL(clicked(QModelIndex)),
this, SLOT(slot_completerComplete(QModelIndex)));
connect(le, SIGNAL(textEdited(QString)), this, SLOT(completionSearchString(QString)));
connect(this, SIGNAL(activated(QString)), le, SLOT(setText(QString)));
}
QtCompleterWithAdvancedCompletion::QtCompleterWithAdvancedCompletion(QComboBox *cb) :
QObject(cb),
popuplist(new QListView),
w(cb),
model(new QStringListModel),
maxVisibleItems(10),
noItemsShown(0),
filterMode(QtCompleterWithAdvancedCompletion::ContainsWord)
{
init();
cb->setEditable(true);
cb->setAutoCompletion(false);
cb->installEventFilter(this);
connect(popuplist, SIGNAL(clicked(QModelIndex)),
this, SLOT(slot_completerComplete(QModelIndex)));
connect(cb, SIGNAL(editTextChanged(QString)), this, SLOT(completionSearchString(QString)));
connect(this, SIGNAL(activated(QString)), cb, SLOT(setEditText(QString)));
}
QtCompleterWithAdvancedCompletion::~QtCompleterWithAdvancedCompletion()
{
popuplist->deleteLater();
}
void QtCompleterWithAdvancedCompletion::init()
{
popuplist->setEditTriggers(QAbstractItemView::NoEditTriggers);
popuplist->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
popuplist->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
popuplist->setSelectionBehavior(QAbstractItemView::SelectRows);
popuplist->setSelectionMode(QAbstractItemView::SingleSelection);
popuplist->setParent(NULL, Qt::Popup);
popuplist->setFocusPolicy(Qt::NoFocus);
popuplist->setFocusProxy(w);
popuplist->installEventFilter(this);
popuplist->setModel(model);
}
void QtCompleterWithAdvancedCompletion::setModel(QStringList &completions)
{
this->completions = completions;
QComboBox *c = qobject_cast<QComboBox*>(w);
if (c)
c->addItems(completions);
}
bool QtCompleterWithAdvancedCompletion::eventFilter(QObject *o, QEvent *e)
{
if (o == w) {
switch (e->type()) {
case QEvent::MouseButtonPress:
is_keypress = false;
break;
}
return w->eventFilter(o, e);
}
if (o != popuplist)
return QObject::eventFilter(o, e);
switch (e->type()) {
case QEvent::KeyPress: {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
const int key = ke->key();
QModelIndex curIndex = popuplist->currentIndex();
// Handle popup navigation keys. These are hardcoded because up/down might make the
// widget do something else (lineedit cursor moves to home/end on mac, for instance)
switch (key) {
case Qt::Key_End:
case Qt::Key_Home:
if (ke->modifiers() & Qt::ControlModifier)
return false;
break;
case Qt::Key_Up:
if (!curIndex.isValid()) {
int rowCount = popuplist->model()->rowCount();
QModelIndex lastIndex = popuplist->model()->index(rowCount - 1, 0);
popuplist->setCurrentIndex(lastIndex);
return true;
} else if (curIndex.row() == 0) {
popuplist->setCurrentIndex(QModelIndex());
return true;
}
return false;
case Qt::Key_Down:
if (!curIndex.isValid()) {
QModelIndex firstIndex = popuplist->model()->index(0, 0);
popuplist->setCurrentIndex(firstIndex);
return true;
} else if (curIndex.row() == popuplist->model()->rowCount() - 1) {
popuplist->setCurrentIndex(QModelIndex());
return true;
}
return false;
case Qt::Key_PageUp:
case Qt::Key_PageDown:
return false;
}
(static_cast<QObject *>(w))->event(ke);
if (e->isAccepted()) {
if (!w->hasFocus())
popuplist->hide();
return true;
}
// default implementation for keys not handled by the widget when popup is open
switch (key) {
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Tab:
popuplist->hide();
if (curIndex.isValid())
slot_completerComplete(curIndex);
break;
case Qt::Key_F4:
if (ke->modifiers() & Qt::AltModifier)
popuplist->hide();
break;
case Qt::Key_Backtab:
case Qt::Key_Escape:
popuplist->hide();
break;
}
return true;
}
case QEvent::MouseButtonPress: {
if (!popuplist->underMouse()) {
popuplist->hide();
return true;
}
return false;
}
}
return false;
}
void QtCompleterWithAdvancedCompletion::slot_completerComplete(QModelIndex index)
{
is_keypress = false;
if (index.isValid()) {
emit activated(index.data().toString());
}
popuplist->hide();
}
void QtCompleterWithAdvancedCompletion::completionSearchString(QString str)
{
if (!w || str.isEmpty()) {
popuplist->hide();
return;
}
if (!is_keypress) {
is_keypress = true;
popuplist->hide();
return;
}
QStringList tmp;
int noitems = maxVisibleItems;
for(int i=0; i<completions.size() && noitems; i++) {
switch (filterMode) {
case QtCompleterWithAdvancedCompletion::StartsWith:
if (completions[i].startsWith(str)) {
tmp << completions[i];
noitems--;
}
break;
case QtCompleterWithAdvancedCompletion::Contains:
if (completions[i].contains(str)) {
tmp << completions[i];
noitems--;
}
break;
case QtCompleterWithAdvancedCompletion::ContainsWord:
if (completions[i].contains(str)) {
tmp << completions[i];
noitems--;
}
break;
}
}
model->setStringList(tmp);
int itemsToShow = qMin(tmp.length(), maxVisibleItems);
if (popuplist->isHidden() || noItemsShown != itemsToShow) {
QPoint pos = w->mapToGlobal(QPoint(0, w->height()));
popuplist->setGeometry(pos.x(), pos.y(), w->width(), itemsToShow*w->height());
popuplist->show();
noItemsShown = itemsToShow;
}
}
void QtCompleterWithAdvancedCompletion::setText(QString str)
{
is_keypress = false;
QComboBox *c = qobject_cast<QComboBox*>(w);
if (c)
c->setEditText(str);
QLineEdit *le = qobject_cast<QLineEdit*>(w);
if (le)
le->setText(str);
}