From ef3f0ff514b7101dab47012fd72e1541f3564b36 Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Thu, 16 Oct 2014 09:26:58 +0200 Subject: [PATCH] Autocomplete fixes --- src/autocomplete.cpp | 36 +++++++++++++---- src/autocomplete.h | 6 ++- src/utils.cpp | 96 -------------------------------------------- src/utils.h | 46 --------------------- 4 files changed, 32 insertions(+), 152 deletions(-) delete mode 100644 src/utils.cpp delete mode 100644 src/utils.h diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index cb0773a..e20f1ba 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -26,17 +26,34 @@ $END_LICENSE */ #include "searchlineedit.h" #endif +#ifndef QT_NO_DEBUG_OUTPUT +/// Gives human-readable event type information. +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(); + } else { + str << (void*)ev; + } + return str.maybeSpace(); +} +#endif + AutoComplete::AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit): QObject(buddy), buddy(buddy), lineEdit(lineEdit), enabled(true), suggester(0) { popup = new QListWidget(); - popup->setMouseTracking(true); popup->setWindowFlags(Qt::Popup); - popup->setAttribute(Qt::WA_ShowWithoutActivating); - popup->setFocusPolicy(Qt::NoFocus); popup->setFocusProxy(buddy); popup->installEventFilter(this); buddy->window()->installEventFilter(this); +#if defined(APP_MAC) || defined(APP_WIN) + // #FIXME on Linux, mouse tracking triggers an "Enter" event when popup is shown + // this triggers item entering and replaces text while writing + popup->setMouseTracking(true); +#endif // style popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -61,6 +78,7 @@ AutoComplete::~AutoComplete() { } bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) { + if (obj != popup) { switch (ev->type()) { case QEvent::Move: @@ -73,6 +91,8 @@ bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) { return false; } + // qDebug() << ev; + if (ev->type() == QEvent::Leave) { popup->setCurrentItem(0); popup->clearSelection(); @@ -137,7 +157,7 @@ bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) { return false; } -void AutoComplete::showCompletion(const QList &suggestions) { +void AutoComplete::showSuggestions(const QList &suggestions) { if (suggestions.isEmpty()) { hideSuggestions(); return; @@ -152,16 +172,16 @@ void AutoComplete::showCompletion(const QList &suggestions) { item->setIcon(QIcon(":/images/" + s->type + ".png")); } popup->setCurrentItem(0); - int h = 0; + int h = popup->frameWidth() * 2; for (int i = 0; i < suggestions.count(); ++i) h += popup->sizeHintForRow(i); + popup->resize(buddy->width(), h); adjustPosition(); popup->setUpdatesEnabled(true); if (popup->isHidden()) { popup->show(); - popup->setFocus(); } } @@ -213,7 +233,7 @@ void AutoComplete::suggestionsReady(const QList &suggestions) { this->suggestions = suggestions; if (!enabled) return; if (!buddy->hasFocus()) return; - showCompletion(suggestions); + showSuggestions(suggestions); } void AutoComplete::adjustPosition() { @@ -244,5 +264,5 @@ void AutoComplete::itemEntered(QListWidgetItem *item) { void AutoComplete::currentItemChanged(QListWidgetItem *item) { if (!item) return; buddy->setText(item->text()); - // lineEdit->setSelection(originalText.length(), editor->text().length()); + // lineEdit->setSelection(originalText.length(), lineEdit->text().length()); } diff --git a/src/autocomplete.h b/src/autocomplete.h index 1699ebe..f2e9e13 100644 --- a/src/autocomplete.h +++ b/src/autocomplete.h @@ -33,8 +33,6 @@ class AutoComplete : public QObject { public: AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit); ~AutoComplete(); - bool eventFilter(QObject *obj, QEvent *ev); - void showCompletion(const QList &suggestions); void setSuggester(Suggester* suggester); QListWidget* getPopup() { return popup; } void preventSuggest(); @@ -44,6 +42,9 @@ signals: void suggestionAccepted(Suggestion *suggestion); void suggestionAccepted(const QString &value); +protected: + bool eventFilter(QObject *obj, QEvent *ev); + private slots: void acceptSuggestion(); void suggest(); @@ -53,6 +54,7 @@ private slots: void adjustPosition(); private: + void showSuggestions(const QList &suggestions); void hideSuggestions(); SearchLineEdit *buddy; diff --git a/src/utils.cpp b/src/utils.cpp deleted file mode 100644 index 5c5aa1e..0000000 --- a/src/utils.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* $BEGIN_LICENSE - -This file is part of Minitube. -Copyright 2009, Flavio Tordini - -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 . - -$END_LICENSE */ - -#include "utils.h" -#ifdef APP_EXTRA -#include "extra.h" -#endif - -QIcon Utils::themeIcon(const QString &name) { - if (QIcon::hasThemeIcon(name)) - return QIcon::fromTheme(name); - else - return QIcon(QString(":/images/%1.png").arg(name)); -} - -QIcon Utils::icon(const QString &name) { -#ifdef APP_EXTRA - return Extra::getIcon(name); -#else - return themeIcon(name); -#endif -} - -QIcon Utils::icon(const QStringList &names) { - QIcon icon; - foreach (QString name, names) { - icon = Utils::themeIcon(name); - if (!icon.availableSizes().isEmpty()) break; - } - return icon; -} - -QIcon Utils::tintedIcon(const QString &name, const QColor &color, QList sizes) { - QIcon i = icon(name); - QIcon t; - if (sizes.isEmpty()) sizes = i.availableSizes(); - foreach (QSize size, sizes) { - QPixmap pixmap = i.pixmap(size); - QImage tintedImage = tinted(pixmap.toImage(), color); - t.addPixmap(QPixmap::fromImage(tintedImage)); - } - return t; -} - -QImage Utils::grayscaled(const QImage &image) { - QImage img = image; - int pixels = img.width() * img.height(); - unsigned int *data = (unsigned int *)img.bits(); - for (int i = 0; i < pixels; ++i) { - int val = qGray(data[i]); - data[i] = qRgba(val, val, val, qAlpha(data[i])); - } - return img; -} - -QImage Utils::tinted(const QImage &image, const QColor &color, - QPainter::CompositionMode mode) { - QImage img(image.size(), QImage::Format_ARGB32_Premultiplied); - QPainter painter(&img); - painter.drawImage(0, 0, grayscaled(image)); - painter.setCompositionMode(mode); - painter.fillRect(img.rect(), color); - painter.end(); - img.setAlphaChannel(image.alphaChannel()); - return img; -} - -void Utils::setupAction(QAction *action) { - // never autorepeat. - // unexperienced users tend to keep keys pressed for a "long" time - action->setAutoRepeat(false); - - // show keyboard shortcuts in the status bar - if (!action->shortcut().isEmpty()) - action->setStatusTip(action->statusTip() + - " (" + - action->shortcut().toString(QKeySequence::NativeText) + - ")"); -} diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index de0d9fb..0000000 --- a/src/utils.h +++ /dev/null @@ -1,46 +0,0 @@ -/* $BEGIN_LICENSE - -This file is part of Minitube. -Copyright 2009, Flavio Tordini - -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 . - -$END_LICENSE */ - -#ifndef UTILS_H -#define UTILS_H - -#include -#if QT_VERSION >= 0x050000 -#include -#endif - -class Utils { - -public: - static QIcon themeIcon(const QString &name); - static QIcon icon(const QString &name); - static QIcon icon(const QStringList &names); - static QIcon tintedIcon(const QString &name, const QColor &color, - QList sizes = QList()); - static void setupAction(QAction *action); - -private: - Utils() { } - static QImage grayscaled(const QImage &image); - static QImage tinted(const QImage &image, const QColor &color, - QPainter::CompositionMode mode = QPainter::CompositionMode_Screen); -}; - -#endif // UTILS_H -- 2.39.5