$END_LICENSE */
#include "autocomplete.h"
#include "suggester.h"
-#ifdef APP_MAC
+#ifdef APP_MAC_SEARCHFIELD
#include "searchlineedit_mac.h"
-#include "macutils.h"
#else
#include "searchlineedit.h"
#endif
+#ifdef APP_MAC
+#include "macutils.h"
+#endif
+
+#include <QListWidget>
+
#ifndef QT_NO_DEBUG_OUTPUT
/// Gives human-readable event type information.
-QDebug operator<<(QDebug str, const QEvent * ev) {
+QDebug operator<<(QDebug str, const QEvent *ev) {
static int eventEnumIndex = QEvent::staticMetaObject.indexOfEnumerator("Type");
str << "QEvent";
if (ev) {
QString name = QEvent::staticMetaObject.enumerator(eventEnumIndex).valueToKey(ev->type());
- if (!name.isEmpty()) str << name; else str << ev->type();
+ if (!name.isEmpty())
+ str << name;
+ else
+ str << ev->type();
} else {
- str << (void*)ev;
+ str << (void *)ev;
}
return str.maybeSpace();
}
#endif
-AutoComplete::AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit):
- QObject(buddy), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0), itemHovering(false) {
-
+AutoComplete::AutoComplete(SearchWidget *buddy, QLineEdit *lineEdit)
+ : QObject(lineEdit), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0),
+ itemHovering(false) {
popup = new QListWidget();
popup->setWindowFlags(Qt::Popup);
- popup->setFocusProxy(buddy);
+ popup->setFocusProxy(buddy->toWidget());
popup->installEventFilter(this);
- buddy->window()->installEventFilter(this);
+ buddy->toWidget()->window()->installEventFilter(this);
popup->setMouseTracking(true);
// style
popup->setWindowOpacity(.9);
popup->setProperty("suggest", true);
- connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(acceptSuggestion()));
- connect(popup, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
- SLOT(currentItemChanged(QListWidgetItem*)));
- connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), SLOT(itemEntered(QListWidgetItem *)));
+ connect(popup, SIGNAL(itemClicked(QListWidgetItem *)), SLOT(acceptSuggestion()));
+ connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
+ SLOT(currentItemChanged(QListWidgetItem *)));
+ connect(popup, SIGNAL(itemEntered(QListWidgetItem *)), SLOT(itemEntered(QListWidgetItem *)));
timer = new QTimer(this);
timer->setSingleShot(true);
timer->setInterval(500);
connect(timer, SIGNAL(timeout()), SLOT(suggest()));
- connect(buddy, SIGNAL(textEdited(QString)), timer, SLOT(start()));
+ connect(buddy->toWidget(), SIGNAL(textEdited(QString)), timer, SLOT(start()));
}
bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
-
if (obj != popup) {
switch (ev->type()) {
case QEvent::Move:
if (ev->type() == QEvent::KeyPress) {
bool consumed = false;
- QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
+ QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
// qWarning() << keyEvent->text();
switch (keyEvent->key()) {
case Qt::Key_Enter:
popup->setCurrentItem(0);
popup->clearSelection();
buddy->setText(originalText);
- buddy->setFocus();
+ buddy->toWidget()->setFocus();
consumed = true;
}
break;
return false;
}
-void AutoComplete::showSuggestions(const QList<Suggestion *> &suggestions) {
+void AutoComplete::showSuggestions(const QVector<Suggestion *> &suggestions) {
if (suggestions.isEmpty()) {
hideSuggestions();
return;
QListWidgetItem *item = new QListWidgetItem(popup);
Suggestion *s = suggestions[i];
item->setText(s->value);
- if (!s->type.isEmpty())
- item->setIcon(QIcon(":/images/" + s->type + ".png"));
+ if (!s->type.isEmpty()) item->setIcon(QIcon(":/images/" + s->type + ".png"));
}
- popup->setCurrentItem(0);
+ popup->setCurrentItem(nullptr);
int h = popup->frameWidth() * 2;
for (int i = 0; i < suggestions.count(); ++i)
h += popup->sizeHintForRow(i);
- popup->resize(buddy->width(), h);
+ popup->resize(buddy->toWidget()->width(), h);
adjustPosition();
popup->setUpdatesEnabled(true);
if (popup->isHidden()) {
itemHovering = false;
- popup->show();
+ popup->showNormal();
QTimer::singleShot(100, this, SLOT(enableItemHovering()));
}
}
void AutoComplete::acceptSuggestion() {
int index = popup->currentIndex().row();
if (index >= 0 && index < suggestions.size()) {
- Suggestion* suggestion = suggestions.at(index);
+ Suggestion *suggestion = suggestions.at(index);
buddy->setText(suggestion->value);
emit suggestionAccepted(suggestion);
emit suggestionAccepted(suggestion->value);
originalText.clear();
hideSuggestions();
- } else qWarning() << "No suggestion for index" << index;
+ } else
+ qWarning() << "No suggestion for index" << index;
}
void AutoComplete::preventSuggest() {
enabled = true;
}
-void AutoComplete::setSuggester(Suggester* suggester) {
+void AutoComplete::setSuggester(Suggester *suggester) {
if (this->suggester) this->suggester->disconnect();
this->suggester = suggester;
- connect(suggester, SIGNAL(ready(QList<Suggestion*>)), SLOT(suggestionsReady(QList<Suggestion*>)));
+ connect(suggester, SIGNAL(ready(QVector<Suggestion *>)),
+ SLOT(suggestionsReady(QVector<Suggestion *>)));
}
void AutoComplete::suggest() {
if (!enabled) return;
- popup->setCurrentItem(0);
+ popup->setCurrentItem(nullptr);
popup->clearSelection();
originalText = buddy->text();
if (suggester) suggester->suggest(originalText);
}
-void AutoComplete::suggestionsReady(const QList<Suggestion *> &suggestions) {
+void AutoComplete::suggestionsReady(const QVector<Suggestion *> &suggestions) {
qDeleteAll(this->suggestions);
this->suggestions = suggestions;
if (!enabled) return;
- if (!buddy->hasFocus()) return;
+ if (!buddy->toWidget()->hasFocus() && buddy->toWidget()->isVisible()) return;
showSuggestions(suggestions);
}
void AutoComplete::adjustPosition() {
- popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
+ popup->move(buddy->toWidget()->mapToGlobal(QPoint(0, buddy->toWidget()->height())));
}
void AutoComplete::enableItemHovering() {
void AutoComplete::hideSuggestions() {
itemHovering = false;
-#ifdef APP_MAC
+#ifdef APP_MAC_NO
mac::fadeOutWindow(popup);
#else
popup->hide();
buddy->setText(originalText);
originalText.clear();
}
- buddy->setFocus();
+ buddy->toWidget()->setFocus();
timer->stop();
}