-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathqcompletelineedit.cpp
98 lines (83 loc) · 2.65 KB
/
qcompletelineedit.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
#include "qcompletelineedit.h"
#include <QKeyEvent>
#include <QListView>
#include <QStringListModel>
#include <QDebug>
void QCompleteLineEdit::setList(QStringList list)
{
words = list;
listView = new QListView(this);
model = new QStringListModel(this);
listView->setWindowFlags(Qt::ToolTip);
connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(setCompleter(const QString &)));
connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(completeText(const QModelIndex &)));
}
void QCompleteLineEdit::keyPressEvent(QKeyEvent *e) {
if (!words.empty() && !listView->isHidden()) {
int key = e->key();
int count = listView->model()->rowCount();
QModelIndex currentIndex = listView->currentIndex();
if (Qt::Key_Down == key) {
int row = currentIndex.row() + 1;
if (row >= count) {
row = 0;
}
QModelIndex index = listView->model()->index(row, 0);
listView->setCurrentIndex(index);
} else if (Qt::Key_Up == key) {
int row = currentIndex.row() - 1;
if (row < 0) {
row = count - 1;
}
QModelIndex index = listView->model()->index(row, 0);
listView->setCurrentIndex(index);
} else if (Qt::Key_Escape == key) {
listView->hide();
} else if (Qt::Key_Enter == key || Qt::Key_Return == key) {
if (currentIndex.isValid()) {
QString text = listView->currentIndex().data().toString();
setText(text);
}
listView->hide();
} else {
listView->hide();
QLineEdit::keyPressEvent(e);
}
} else {
QLineEdit::keyPressEvent(e);
}
}
void QCompleteLineEdit::setCompleter(const QString &text) {
if (text.isEmpty()) {
listView->hide();
return;
}
if ((text.length() > 1) && (!listView->isHidden())) {
return;
}
QStringList sl;
foreach(QString word, words) {
if (word.toLower().contains(text.toLower())) {
sl << word;
}
}
model->setStringList(sl);
listView->setModel(model);
if (model->rowCount() == 0) {
return;
}
// Position the text edit
listView->setMinimumWidth(width());
listView->setMaximumWidth(width());
QPoint p(0, height());
int x = mapToGlobal(p).x();
int y = mapToGlobal(p).y() + 1;
listView->move(x, y);
listView->show();
}
void QCompleteLineEdit::completeText(const QModelIndex &index) {
QString text = index.data().toString();
setText(text);
listView->hide();
emit selected();
}