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->setMouseTracking(true);
12 popup->setWindowOpacity(.9);
13 popup->installEventFilter(this);
14 popup->setWindowFlags(Qt::Popup);
15 popup->setFocusPolicy(Qt::NoFocus);
16 popup->setFocusProxy(parent);
18 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
20 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
21 // SLOT(currentItemChanged(QListWidgetItem *)));
24 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
25 // SLOT(currentItemChanged(QListWidgetItem *)));
27 timer = new QTimer(this);
28 timer->setSingleShot(true);
29 timer->setInterval(600);
30 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
32 connect(parent, SIGNAL(textChanged(QString)), timer, SLOT(start()));
34 connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
39 AutoComplete::~AutoComplete() {
43 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
47 if (ev->type() == QEvent::MouseButtonPress) {
50 editor->setText(originalText);
54 if (ev->type() == QEvent::KeyPress) {
56 bool consumed = false;
58 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
59 int key = keyEvent->key();
60 // qDebug() << keyEvent->text();
64 if (popup->currentItem()) {
76 editor->setText(originalText);
86 case Qt::Key_PageDown:
90 // qDebug() << keyEvent->text();
103 void AutoComplete::showCompletion(const QStringList &choices) {
105 if (choices.isEmpty())
108 popup->setUpdatesEnabled(false);
110 for (int i = 0; i < choices.count(); ++i) {
111 QListWidgetItem * item;
112 item = new QListWidgetItem(popup);
113 item->setText(choices[i]);
115 popup->setCurrentItem(0);
117 popup->setUpdatesEnabled(true);
119 int h = popup->sizeHintForRow(0) * choices.count() + 4;
120 popup->resize(buddy->width(), h);
122 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
124 // popup->setFocus();
128 void AutoComplete::doneCompletion() {
132 QListWidgetItem *item = popup->currentItem();
134 editor->setText(item->text());
135 emit suggestionAccepted(item->text());
139 void AutoComplete::preventSuggest() {
140 // qDebug() << "preventSuggest";
146 void AutoComplete::enableSuggest() {
147 // qDebug() << "enableSuggest";
151 void AutoComplete::setSuggester(Suggester* suggester) {
152 if (this->suggester) this->suggester->disconnect();
153 this->suggester = suggester;
154 connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
157 void AutoComplete::autoSuggest() {
158 if (!enabled) return;
160 QString query = editor->text();
161 originalText = query;
162 // qDebug() << "originalText" << originalText;
163 if (query.isEmpty()) {
169 suggester->suggest(query);
172 void AutoComplete::suggestionsReady(QStringList suggestions) {
173 if (!enabled) return;
174 showCompletion(suggestions);
177 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
179 // qDebug() << "current" << current->text();
180 current->setSelected(true);
181 editor->setText(current->text());
182 editor->setSelection(originalText.length(), editor->text().length());