1 #include "autocomplete.h"
4 #include "searchlineedit_mac.h"
6 #include "searchlineedit.h"
9 AutoComplete::AutoComplete(SearchLineEdit *parent, QLineEdit *editor):
10 QObject(parent), editor(editor), suggester(0) {
15 popup = new QListWidget;
16 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 popup->setMouseTracking(true);
18 popup->setWindowOpacity(.9);
19 popup->installEventFilter(this);
20 popup->setWindowFlags(Qt::Popup);
21 popup->setFocusPolicy(Qt::NoFocus);
22 popup->setFocusProxy(buddy);
24 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
26 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
27 // SLOT(currentItemChanged(QListWidgetItem *)));
30 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
31 // SLOT(currentItemChanged(QListWidgetItem *)));
33 timer = new QTimer(this);
34 timer->setSingleShot(true);
35 timer->setInterval(600);
36 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
37 connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
41 AutoComplete::~AutoComplete() {
45 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
49 if (ev->type() == QEvent::FocusOut) {
55 if (ev->type() == QEvent::MouseButtonPress) {
58 buddy->setText(originalText);
62 if (ev->type() == QEvent::KeyPress) {
64 bool consumed = false;
66 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
67 int key = keyEvent->key();
68 // qDebug() << keyEvent->text();
72 if (popup->currentItem()) {
84 editor->setText(originalText);
94 case Qt::Key_PageDown:
98 // qDebug() << keyEvent->text();
111 void AutoComplete::showCompletion(const QStringList &choices) {
113 if (choices.isEmpty())
116 popup->setUpdatesEnabled(false);
118 for (int i = 0; i < choices.count(); ++i) {
119 QListWidgetItem * item;
120 item = new QListWidgetItem(popup);
121 item->setText(choices[i]);
123 popup->setCurrentItem(0);
125 popup->setUpdatesEnabled(true);
127 int h = popup->sizeHintForRow(0) * choices.count() + 4;
128 popup->resize(buddy->width(), h);
130 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
136 void AutoComplete::doneCompletion() {
140 QListWidgetItem *item = popup->currentItem();
142 buddy->setText(item->text());
143 emit suggestionAccepted(item->text());
147 void AutoComplete::preventSuggest() {
148 // qDebug() << "preventSuggest";
154 void AutoComplete::enableSuggest() {
155 // qDebug() << "enableSuggest";
159 void AutoComplete::setSuggester(Suggester* suggester) {
160 if (this->suggester) this->suggester->disconnect();
161 this->suggester = suggester;
162 connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
165 void AutoComplete::autoSuggest() {
166 if (!enabled) return;
167 if (!buddy->hasFocus()) return;
169 QString query = editor->text();
170 originalText = query;
171 // qDebug() << "originalText" << originalText;
172 if (query.isEmpty()) {
179 suggester->suggest(query);
182 void AutoComplete::suggestionsReady(QStringList suggestions) {
183 if (!enabled) return;
184 showCompletion(suggestions);
187 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
189 // qDebug() << "current" << current->text();
190 current->setSelected(true);
191 buddy->setText(current->text());
192 editor->setSelection(originalText.length(), editor->text().length());