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 AutoComplete::AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit):
30 QObject(buddy), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0) {
32 popup = new QListWidget();
33 popup->setMouseTracking(true);
34 popup->setWindowFlags(Qt::Popup);
35 popup->setAttribute(Qt::WA_ShowWithoutActivating);
36 popup->setFocusPolicy(Qt::NoFocus);
37 popup->setFocusProxy(buddy);
38 popup->installEventFilter(this);
39 buddy->window()->installEventFilter(this);
42 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 popup->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 popup->setWindowOpacity(.9);
45 popup->setProperty("suggest", true);
47 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(acceptSuggestion()));
48 connect(popup, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
49 SLOT(currentItemChanged(QListWidgetItem*)));
50 connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), SLOT(itemEntered(QListWidgetItem *)));
52 timer = new QTimer(this);
53 timer->setSingleShot(true);
54 timer->setInterval(500);
55 connect(timer, SIGNAL(timeout()), SLOT(suggest()));
56 connect(buddy, SIGNAL(textEdited(QString)), timer, SLOT(start()));
59 AutoComplete::~AutoComplete() {
63 bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
76 if (ev->type() == QEvent::Leave) {
77 popup->setCurrentItem(0);
78 popup->clearSelection();
79 if (!originalText.isEmpty()) buddy->setText(originalText);
83 if (ev->type() == QEvent::FocusOut || ev->type() == QEvent::MouseButtonPress) {
88 if (ev->type() == QEvent::KeyPress) {
89 bool consumed = false;
90 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
91 // qWarning() << keyEvent->text();
92 switch (keyEvent->key()) {
95 if (popup->currentItem()) {
110 if (popup->currentRow() == 0) {
111 popup->setCurrentItem(0);
112 popup->clearSelection();
113 buddy->setText(originalText);
123 case Qt::Key_PageDown:
128 // qDebug() << keyEvent->text();
140 void AutoComplete::showCompletion(const QList<Suggestion *> &suggestions) {
141 if (suggestions.isEmpty()) {
145 popup->setUpdatesEnabled(false);
147 for (int i = 0; i < suggestions.count(); ++i) {
148 QListWidgetItem *item = new QListWidgetItem(popup);
149 Suggestion *s = suggestions[i];
150 item->setText(s->value);
151 if (!s->type.isEmpty())
152 item->setIcon(QIcon(":/images/" + s->type + ".png"));
154 popup->setCurrentItem(0);
156 for (int i = 0; i < suggestions.count(); ++i)
157 h += popup->sizeHintForRow(i);
158 popup->resize(buddy->width(), h);
160 popup->setUpdatesEnabled(true);
162 if (popup->isHidden()) {
168 void AutoComplete::acceptSuggestion() {
169 int index = popup->currentIndex().row();
170 if (index >= 0 && index < suggestions.size()) {
171 Suggestion* suggestion = suggestions.at(index);
172 buddy->setText(suggestion->value);
173 emit suggestionAccepted(suggestion);
174 emit suggestionAccepted(suggestion->value);
175 originalText.clear();
177 } else qWarning() << "No suggestion for index" << index;
180 void AutoComplete::preventSuggest() {
186 void AutoComplete::enableSuggest() {
190 void AutoComplete::setSuggester(Suggester* suggester) {
191 if (this->suggester) this->suggester->disconnect();
192 this->suggester = suggester;
193 connect(suggester, SIGNAL(ready(QList<Suggestion*>)), SLOT(suggestionsReady(QList<Suggestion*>)));
196 void AutoComplete::suggest() {
197 if (!enabled) return;
199 popup->setCurrentItem(0);
200 popup->clearSelection();
202 originalText = buddy->text();
203 if (originalText.isEmpty()) {
208 if (suggester) suggester->suggest(originalText);
211 void AutoComplete::suggestionsReady(const QList<Suggestion *> &suggestions) {
212 qDeleteAll(this->suggestions);
213 this->suggestions = suggestions;
214 if (!enabled) return;
215 if (!buddy->hasFocus()) return;
216 showCompletion(suggestions);
219 void AutoComplete::adjustPosition() {
220 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
223 void AutoComplete::hideSuggestions() {
225 mac::fadeOutWindow(popup);
230 if (!originalText.isEmpty()) {
231 buddy->setText(originalText);
232 originalText.clear();
238 void AutoComplete::itemEntered(QListWidgetItem *item) {
240 item->setSelected(true);
241 popup->setCurrentItem(item);
244 void AutoComplete::currentItemChanged(QListWidgetItem *item) {
246 buddy->setText(item->text());
247 // lineEdit->setSelection(originalText.length(), editor->text().length());