3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "autocomplete.h"
22 #include "suggester.h"
24 #include "searchlineedit_mac.h"
26 #include "searchlineedit.h"
29 AutoComplete::AutoComplete(SearchLineEdit *parent, QLineEdit *editor):
30 QObject(parent), editor(editor), suggester(0) {
35 popup = new QListWidget;
36 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
37 popup->setMouseTracking(true);
38 popup->setWindowOpacity(.9);
39 popup->installEventFilter(this);
40 popup->setWindowFlags(Qt::Popup);
41 popup->setFocusPolicy(Qt::NoFocus);
42 popup->setFocusProxy(buddy);
44 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
46 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
47 // SLOT(currentItemChanged(QListWidgetItem *)));
50 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
51 // SLOT(currentItemChanged(QListWidgetItem *)));
53 timer = new QTimer(this);
54 timer->setSingleShot(true);
55 timer->setInterval(600);
56 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
57 connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
61 AutoComplete::~AutoComplete() {
65 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
69 if (ev->type() == QEvent::FocusOut) {
75 if (ev->type() == QEvent::MouseButtonPress) {
78 buddy->setText(originalText);
82 if (ev->type() == QEvent::KeyPress) {
84 bool consumed = false;
86 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
87 int key = keyEvent->key();
88 // qDebug() << keyEvent->text();
92 if (popup->currentItem()) {
104 editor->setText(originalText);
114 case Qt::Key_PageDown:
118 // qDebug() << keyEvent->text();
131 void AutoComplete::showCompletion(const QStringList &choices) {
133 if (choices.isEmpty())
136 popup->setUpdatesEnabled(false);
138 for (int i = 0; i < choices.count(); ++i) {
139 QListWidgetItem * item;
140 item = new QListWidgetItem(popup);
141 item->setText(choices[i]);
143 popup->setCurrentItem(0);
145 popup->setUpdatesEnabled(true);
147 int h = popup->sizeHintForRow(0) * choices.count() + 4;
148 popup->resize(buddy->width(), h);
150 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
156 void AutoComplete::doneCompletion() {
160 QListWidgetItem *item = popup->currentItem();
162 buddy->setText(item->text());
163 emit suggestionAccepted(item->text());
167 void AutoComplete::preventSuggest() {
168 // qDebug() << "preventSuggest";
174 void AutoComplete::enableSuggest() {
175 // qDebug() << "enableSuggest";
179 void AutoComplete::setSuggester(Suggester* suggester) {
180 if (this->suggester) this->suggester->disconnect();
181 this->suggester = suggester;
182 connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
185 void AutoComplete::autoSuggest() {
186 if (!enabled) return;
187 if (!buddy->hasFocus()) return;
189 QString query = editor->text();
190 originalText = query;
191 // qDebug() << "originalText" << originalText;
192 if (query.isEmpty()) {
199 suggester->suggest(query);
202 void AutoComplete::suggestionsReady(QStringList suggestions) {
203 if (!enabled) return;
204 showCompletion(suggestions);
207 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
209 // qDebug() << "current" << current->text();
210 current->setSelected(true);
211 buddy->setText(current->text());
212 editor->setSelection(originalText.length(), editor->text().length());