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 popup->setFrameShape(QFrame::NoFrame);
45 popup->setAttribute(Qt::WA_TranslucentBackground);
46 popup->viewport()->setStyleSheet("border:0; border-radius:5px; background:palette(base)");
48 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
50 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
51 // SLOT(currentItemChanged(QListWidgetItem *)));
54 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
55 // SLOT(currentItemChanged(QListWidgetItem *)));
57 timer = new QTimer(this);
58 timer->setSingleShot(true);
59 timer->setInterval(600);
60 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
61 connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
65 AutoComplete::~AutoComplete() {
69 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
73 if (ev->type() == QEvent::FocusOut) {
79 if (ev->type() == QEvent::MouseButtonPress) {
82 buddy->setText(originalText);
86 if (ev->type() == QEvent::KeyPress) {
88 bool consumed = false;
90 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
91 int key = keyEvent->key();
92 // qDebug() << keyEvent->text();
96 if (popup->currentItem()) {
108 editor->setText(originalText);
118 case Qt::Key_PageDown:
122 // qDebug() << keyEvent->text();
135 void AutoComplete::showCompletion(const QStringList &choices) {
137 if (choices.isEmpty())
140 popup->setUpdatesEnabled(false);
142 for (int i = 0; i < choices.count(); ++i) {
143 QListWidgetItem * item;
144 item = new QListWidgetItem(popup);
145 item->setText(choices[i]);
147 popup->setCurrentItem(0);
149 popup->setUpdatesEnabled(true);
151 int h = popup->sizeHintForRow(0) * choices.count() + 4;
152 popup->resize(buddy->width(), h);
154 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
156 popup->setFrameShape(QFrame::NoFrame);
162 void AutoComplete::doneCompletion() {
166 QListWidgetItem *item = popup->currentItem();
168 buddy->setText(item->text());
169 emit suggestionAccepted(item->text());
173 void AutoComplete::preventSuggest() {
174 // qDebug() << "preventSuggest";
178 popup->setFrameShape(QFrame::NoFrame);
181 void AutoComplete::enableSuggest() {
182 // qDebug() << "enableSuggest";
186 void AutoComplete::setSuggester(Suggester* suggester) {
187 if (this->suggester) this->suggester->disconnect();
188 this->suggester = suggester;
189 connect(suggester, SIGNAL(ready(QStringList)), SLOT(suggestionsReady(QStringList)));
192 void AutoComplete::autoSuggest() {
193 if (!enabled) return;
194 if (!buddy->hasFocus()) return;
196 QString query = editor->text();
197 originalText = query;
198 // qDebug() << "originalText" << originalText;
199 if (query.isEmpty()) {
206 suggester->suggest(query);
209 void AutoComplete::suggestionsReady(QStringList suggestions) {
210 if (!enabled) return;
211 showCompletion(suggestions);
214 void AutoComplete::currentItemChanged(QListWidgetItem *current) {
216 // qDebug() << "current" << current->text();
217 current->setSelected(true);
218 buddy->setText(current->text());
219 editor->setSelection(originalText.length(), editor->text().length());