+/* $BEGIN_LICENSE
+
+This file is part of Minitube.
+Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
+
+Minitube is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+Minitube is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Minitube. If not, see <http://www.gnu.org/licenses/>.
+
+$END_LICENSE */
+
#include "autocomplete.h"
#include "suggester.h"
+#ifdef APP_MAC
+#include "searchlineedit_mac.h"
+#else
+#include "searchlineedit.h"
+#endif
-AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
- QObject(parent), buddy(parent), editor(editor), suggester(0) {
+AutoComplete::AutoComplete(SearchLineEdit *parent, QLineEdit *editor):
+ QObject(parent), editor(editor), suggester(0) {
+ buddy = parent;
enabled = true;
popup = new QListWidget;
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- popup->installEventFilter(this);
popup->setMouseTracking(true);
popup->setWindowOpacity(.9);
+ popup->installEventFilter(this);
+ popup->setWindowFlags(Qt::Popup);
+ popup->setFocusPolicy(Qt::NoFocus);
+ popup->setFocusProxy(buddy);
- connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
- SLOT(doneCompletion()));
+ connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
// connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
// SLOT(currentItemChanged(QListWidgetItem *)));
// connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
// SLOT(currentItemChanged(QListWidgetItem *)));
- popup->setWindowFlags(Qt::Popup);
- popup->setFocusPolicy(Qt::NoFocus);
- popup->setFocusProxy(parent);
-
timer = new QTimer(this);
timer->setSingleShot(true);
- timer->setInterval(300);
+ timer->setInterval(600);
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
- connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
+ connect(buddy, SIGNAL(textChanged(QString)), timer, SLOT(start()));
}
if (obj != popup)
return false;
+ if (ev->type() == QEvent::FocusOut) {
+ popup->hide();
+ buddy->setFocus();
+ return true;
+ }
+
if (ev->type() == QEvent::MouseButtonPress) {
popup->hide();
- editor->setFocus();
- editor->setText(originalText);
+ buddy->setFocus();
+ buddy->setText(originalText);
return true;
}
doneCompletion();
consumed = true;
} else {
- editor->setFocus();
+ buddy->setFocus();
editor->event(ev);
popup->hide();
}
break;
case Qt::Key_Escape:
- editor->setFocus();
+ buddy->setFocus();
editor->setText(originalText);
popup->hide();
consumed = true;
default:
// qDebug() << keyEvent->text();
- editor->setFocus();
+ buddy->setFocus();
editor->event(ev);
popup->hide();
break;
void AutoComplete::doneCompletion() {
timer->stop();
popup->hide();
- editor->setFocus();
+ buddy->setFocus();
QListWidgetItem *item = popup->currentItem();
if (item) {
- editor->setText(item->text());
- QKeyEvent *e;
- e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
- QApplication::postEvent(editor, e);
- e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
- QApplication::postEvent(editor, e);
+ buddy->setText(item->text());
+ emit suggestionAccepted(item->text());
}
}
void AutoComplete::autoSuggest() {
if (!enabled) return;
+ if (!buddy->hasFocus()) return;
QString query = editor->text();
originalText = query;
// qDebug() << "originalText" << originalText;
- if (query.isEmpty()) return;
+ if (query.isEmpty()) {
+ popup->hide();
+ buddy->setFocus();
+ return;
+ }
if (suggester)
suggester->suggest(query);
if (current) {
// qDebug() << "current" << current->text();
current->setSelected(true);
- editor->setText(current->text());
+ buddy->setText(current->text());
editor->setSelection(originalText.length(), editor->text().length());
}
}