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(buddy);
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()));
31 connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
35 AutoComplete::~AutoComplete() {
39 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
43 if (ev->type() == QEvent::FocusOut) {
49 if (ev->type() == QEvent::MouseButtonPress) {
52 editor->setText(originalText);
56 if (ev->type() == QEvent::KeyPress) {
58 bool consumed = false;
60 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
61 int key = keyEvent->key();
62 // qDebug() << keyEvent->text();
66 if (popup->currentItem()) {
78 editor->setText(originalText);
88 case Qt::Key_PageDown:
92 // qDebug() << keyEvent->text();
105 void AutoComplete::showCompletion(const QStringList &choices) {
107 if (choices.isEmpty())
110 popup->setUpdatesEnabled(false);
112 for (int i = 0; i < choices.count(); ++i) {
113 QListWidgetItem * item;
114 item = new QListWidgetItem(popup);
115 item->setText(choices[i]);
117 popup->setCurrentItem(0);
119 popup->setUpdatesEnabled(true);
121 int h = popup->sizeHintForRow(0) * choices.count() + 4;
122 popup->resize(buddy->width(), h);
124 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
130 void AutoComplete::doneCompletion() {
134 QListWidgetItem *item = popup->currentItem();
136 editor->setText(item->text());
137 emit suggestionAccepted(item->text());
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;
161 if (!buddy->hasFocus()) return;
163 QString query = editor->text();
164 originalText = query;
165 // qDebug() << "originalText" << originalText;
166 if (query.isEmpty()) {
173 suggester->suggest(query);
176 void AutoComplete::suggestionsReady(QStringList suggestions) {
177 if (!enabled) return;
178 showCompletion(suggestions);
181 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
183 // qDebug() << "current" << current->text();
184 current->setSelected(true);
185 editor->setText(current->text());
186 editor->setSelection(originalText.length(), editor->text().length());