1
#include "autocomplete.h"
4
AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
5
QObject(parent), buddy(parent), editor(editor), suggester(0) {
9
popup = new QListWidget;
10
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
11
popup->installEventFilter(this);
12
popup->setMouseTracking(true);
13
popup->setWindowOpacity(.9);
15
connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
16
SLOT(doneCompletion()));
18
// connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
19
// SLOT(currentItemChanged(QListWidgetItem *)));
22
// connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
23
// SLOT(currentItemChanged(QListWidgetItem *)));
25
popup->setWindowFlags(Qt::Popup);
26
popup->setFocusPolicy(Qt::NoFocus);
27
popup->setFocusProxy(parent);
29
timer = new QTimer(this);
30
timer->setSingleShot(true);
31
timer->setInterval(300);
32
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
33
connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
37
AutoComplete::~AutoComplete() {
41
bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
45
if (ev->type() == QEvent::MouseButtonPress) {
48
editor->setText(originalText);
52
if (ev->type() == QEvent::KeyPress) {
54
bool consumed = false;
56
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
57
int key = keyEvent->key();
58
// qDebug() << keyEvent->text();
62
if (popup->currentItem()) {
74
editor->setText(originalText);
84
case Qt::Key_PageDown:
88
// qDebug() << keyEvent->text();
101
void AutoComplete::showCompletion(const QStringList &choices) {
103
if (choices.isEmpty())
106
popup->setUpdatesEnabled(false);
108
for (int i = 0; i < choices.count(); ++i) {
109
QListWidgetItem * item;
110
item = new QListWidgetItem(popup);
111
item->setText(choices[i]);
113
popup->setCurrentItem(0);
115
popup->setUpdatesEnabled(true);
117
int h = popup->sizeHintForRow(0) * choices.count() + 4;
118
popup->resize(buddy->width(), h);
120
popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
126
void AutoComplete::doneCompletion() {
130
QListWidgetItem *item = popup->currentItem();
132
editor->setText(item->text());
134
e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
135
QApplication::postEvent(editor, e);
136
e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
137
QApplication::postEvent(editor, e);
141
void AutoComplete::preventSuggest() {
142
// qDebug() << "preventSuggest";
148
void AutoComplete::enableSuggest() {
149
// qDebug() << "enableSuggest";
153
void AutoComplete::setSuggester(Suggester* suggester) {
154
if (this->suggester) this->suggester->disconnect();
155
this->suggester = suggester;
156
connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
159
void AutoComplete::autoSuggest() {
160
if (!enabled) return;
162
QString query = editor->text();
163
originalText = query;
164
// qDebug() << "originalText" << originalText;
165
if (query.isEmpty()) return;
168
suggester->suggest(query);
171
void AutoComplete::suggestionsReady(QStringList suggestions) {
172
if (!enabled) return;
173
showCompletion(suggestions);
176
void AutoComplete::currentItemChanged(QListWidgetItem *current) {
178
// qDebug() << "current" << current->text();
179
current->setSelected(true);
180
editor->setText(current->text());
181
editor->setSelection(originalText.length(), editor->text().length());