3 This file is part of Minitube.
4 Copyright 2013, 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/>.
20 #include "autocomplete.h"
21 #include "suggester.h"
23 #include "searchlineedit_mac.h"
26 #include "searchlineedit.h"
29 #ifndef QT_NO_DEBUG_OUTPUT
30 /// Gives human-readable event type information.
31 QDebug operator<<(QDebug str, const QEvent * ev) {
32 static int eventEnumIndex = QEvent::staticMetaObject.indexOfEnumerator("Type");
35 QString name = QEvent::staticMetaObject.enumerator(eventEnumIndex).valueToKey(ev->type());
36 if (!name.isEmpty()) str << name; else str << ev->type();
40 return str.maybeSpace();
44 AutoComplete::AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit):
45 QObject(buddy), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0), itemHovering(false) {
47 popup = new QListWidget();
48 popup->setWindowFlags(Qt::Popup);
49 popup->setFocusProxy(buddy);
50 popup->installEventFilter(this);
51 buddy->window()->installEventFilter(this);
52 popup->setMouseTracking(true);
55 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56 popup->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57 popup->setWindowOpacity(.9);
58 popup->setProperty("suggest", true);
60 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(acceptSuggestion()));
61 connect(popup, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
62 SLOT(currentItemChanged(QListWidgetItem*)));
63 connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), SLOT(itemEntered(QListWidgetItem *)));
65 timer = new QTimer(this);
66 timer->setSingleShot(true);
67 timer->setInterval(500);
68 connect(timer, SIGNAL(timeout()), SLOT(suggest()));
69 connect(buddy, SIGNAL(textEdited(QString)), timer, SLOT(start()));
72 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
88 if (ev->type() == QEvent::Leave) {
89 popup->setCurrentItem(0);
90 popup->clearSelection();
91 if (!originalText.isEmpty()) buddy->setText(originalText);
95 if (ev->type() == QEvent::FocusOut || ev->type() == QEvent::MouseButtonPress) {
100 if (ev->type() == QEvent::KeyPress) {
101 bool consumed = false;
102 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
103 // qWarning() << keyEvent->text();
104 switch (keyEvent->key()) {
107 if (popup->currentItem()) {
122 if (popup->currentRow() == 0) {
123 popup->setCurrentItem(0);
124 popup->clearSelection();
125 buddy->setText(originalText);
135 case Qt::Key_PageDown:
140 // qDebug() << keyEvent->text();
152 void AutoComplete::showSuggestions(const QList<Suggestion *> &suggestions) {
153 if (suggestions.isEmpty()) {
157 popup->setUpdatesEnabled(false);
159 for (int i = 0; i < suggestions.count(); ++i) {
160 QListWidgetItem *item = new QListWidgetItem(popup);
161 Suggestion *s = suggestions[i];
162 item->setText(s->value);
163 if (!s->type.isEmpty())
164 item->setIcon(QIcon(":/images/" + s->type + ".png"));
166 popup->setCurrentItem(0);
167 int h = popup->frameWidth() * 2;
168 for (int i = 0; i < suggestions.count(); ++i)
169 h += popup->sizeHintForRow(i);
171 popup->resize(buddy->width(), h);
173 popup->setUpdatesEnabled(true);
175 if (popup->isHidden()) {
176 itemHovering = false;
178 QTimer::singleShot(100, this, SLOT(enableItemHovering()));
182 void AutoComplete::acceptSuggestion() {
183 int index = popup->currentIndex().row();
184 if (index >= 0 && index < suggestions.size()) {
185 Suggestion* suggestion = suggestions.at(index);
186 buddy->setText(suggestion->value);
187 emit suggestionAccepted(suggestion);
188 emit suggestionAccepted(suggestion->value);
189 originalText.clear();
191 } else qWarning() << "No suggestion for index" << index;
194 void AutoComplete::preventSuggest() {
200 void AutoComplete::enableSuggest() {
204 void AutoComplete::setSuggester(Suggester* suggester) {
205 if (this->suggester) this->suggester->disconnect();
206 this->suggester = suggester;
207 connect(suggester, SIGNAL(ready(QList<Suggestion*>)), SLOT(suggestionsReady(QList<Suggestion*>)));
210 void AutoComplete::suggest() {
211 if (!enabled) return;
213 popup->setCurrentItem(0);
214 popup->clearSelection();
216 originalText = buddy->text();
217 if (originalText.isEmpty()) {
222 if (suggester) suggester->suggest(originalText);
225 void AutoComplete::suggestionsReady(const QList<Suggestion *> &suggestions) {
226 qDeleteAll(this->suggestions);
227 this->suggestions = suggestions;
228 if (!enabled) return;
229 if (!buddy->hasFocus()) return;
230 showSuggestions(suggestions);
233 void AutoComplete::adjustPosition() {
234 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
237 void AutoComplete::enableItemHovering() {
241 void AutoComplete::hideSuggestions() {
242 itemHovering = false;
244 mac::fadeOutWindow(popup);
249 if (!originalText.isEmpty()) {
250 buddy->setText(originalText);
251 originalText.clear();
257 void AutoComplete::itemEntered(QListWidgetItem *item) {
258 if (!itemHovering) return;
260 item->setSelected(true);
261 popup->setCurrentItem(item);
264 void AutoComplete::currentItemChanged(QListWidgetItem *item) {
266 buddy->setText(item->text());
267 // lineEdit->setSelection(originalText.length(), lineEdit->text().length());