#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);
}
bool AutoComplete::eventFilter(QObject *obj, QEvent *ev) {
+
if (obj != popup) {
switch (ev->type()) {
case QEvent::Move:
return false;
}
+ // qDebug() << ev;
+
if (ev->type() == QEvent::Leave) {
popup->setCurrentItem(0);
popup->clearSelection();
return false;
}
-void AutoComplete::showCompletion(const QList<Suggestion *> &suggestions) {
+void AutoComplete::showSuggestions(const QList<Suggestion *> &suggestions) {
if (suggestions.isEmpty()) {
hideSuggestions();
return;
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();
}
}
this->suggestions = suggestions;
if (!enabled) return;
if (!buddy->hasFocus()) return;
- showCompletion(suggestions);
+ showSuggestions(suggestions);
}
void AutoComplete::adjustPosition() {
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());
}
public:
AutoComplete(SearchLineEdit *buddy, QLineEdit *lineEdit);
~AutoComplete();
- bool eventFilter(QObject *obj, QEvent *ev);
- void showCompletion(const QList<Suggestion*> &suggestions);
void setSuggester(Suggester* suggester);
QListWidget* getPopup() { return popup; }
void preventSuggest();
void suggestionAccepted(Suggestion *suggestion);
void suggestionAccepted(const QString &value);
+protected:
+ bool eventFilter(QObject *obj, QEvent *ev);
+
private slots:
void acceptSuggestion();
void suggest();
void adjustPosition();
private:
+ void showSuggestions(const QList<Suggestion*> &suggestions);
void hideSuggestions();
SearchLineEdit *buddy;
+++ /dev/null
-/* $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 "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<QSize> 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) +
- ")");
-}
+++ /dev/null
-/* $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 */
-
-#ifndef UTILS_H
-#define UTILS_H
-
-#include <QtGui>
-#if QT_VERSION >= 0x050000
-#include <QtWidgets>
-#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<QSize> sizes = QList<QSize>());
- 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