From: Flavio Tordini Date: Wed, 29 Jul 2009 08:51:32 +0000 (+0200) Subject: autocompleter refinements X-Git-Tag: 0.5~9 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=050b7316c051fbca7408b3c64647136e83fcc641;p=minitube autocompleter refinements --- diff --git a/src/SearchView.h b/src/SearchView.h index 0db47b7..4690a44 100644 --- a/src/SearchView.h +++ b/src/SearchView.h @@ -17,6 +17,7 @@ public: updateRecentKeywords(); queryEdit->clear(); queryEdit->setFocus(Qt::OtherFocusReason); + queryEdit->enableSuggest(); } void disappear() {} diff --git a/src/googlesuggest.cpp b/src/googlesuggest.cpp index 71e545c..234fe61 100644 --- a/src/googlesuggest.cpp +++ b/src/googlesuggest.cpp @@ -7,7 +7,10 @@ namespace The { NetworkAccess* http(); } -GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) { +GSuggestCompletion::GSuggestCompletion(QWidget *parent, QLineEdit *editor): + QObject(parent), buddy(parent), editor(editor) { + + enabled = true; popup = new QListWidget; popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -17,12 +20,12 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit connect(popup, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion())); - connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), - SLOT(currentItemChanged(QListWidgetItem *))); + // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), + // SLOT(currentItemChanged(QListWidgetItem *))); // mouse hover - connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), - SLOT(currentItemChanged(QListWidgetItem *))); + // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), + // SLOT(currentItemChanged(QListWidgetItem *))); popup->setWindowFlags(Qt::Popup); popup->setFocusPolicy(Qt::NoFocus); @@ -30,7 +33,7 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit timer = new QTimer(this); timer->setSingleShot(true); - timer->setInterval(100); + timer->setInterval(300); connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start())); @@ -116,9 +119,10 @@ void GSuggestCompletion::showCompletion(const QStringList &choices) { popup->setUpdatesEnabled(true); int h = popup->sizeHintForRow(0) * choices.count() + 4; - popup->resize(popup->width(), h); + popup->resize(buddy->width(), h); + + popup->move(buddy->mapToGlobal(QPoint(0, buddy->height()))); - popup->move(editor->mapToGlobal(QPoint(0, editor->height()+4))); popup->setFocus(); popup->show(); } @@ -140,9 +144,17 @@ void GSuggestCompletion::doneCompletion() { void GSuggestCompletion::preventSuggest() { timer->stop(); + enabled = false; + popup->hide(); +} + +void GSuggestCompletion::enableSuggest() { + enabled = true; } void GSuggestCompletion::autoSuggest() { + if (!enabled) return; + QString query = editor->text(); originalText = query; qDebug() << "originalText" << originalText; @@ -161,6 +173,7 @@ void GSuggestCompletion::autoSuggest() { } void GSuggestCompletion::handleNetworkData(QByteArray response) { + if (!enabled) return; QStringList choices; diff --git a/src/googlesuggest.h b/src/googlesuggest.h index 8ca766b..9773380 100644 --- a/src/googlesuggest.h +++ b/src/googlesuggest.h @@ -7,7 +7,7 @@ class GSuggestCompletion : public QObject { Q_OBJECT public: - GSuggestCompletion(QLineEdit *parent); + GSuggestCompletion(QWidget *parent, QLineEdit *editor); ~GSuggestCompletion(); bool eventFilter(QObject *obj, QEvent *ev); void showCompletion(const QStringList &choices); @@ -15,15 +15,18 @@ public: public slots: void doneCompletion(); void preventSuggest(); + void enableSuggest(); void autoSuggest(); void handleNetworkData(QByteArray response); void currentItemChanged(QListWidgetItem *current); private: + QWidget *buddy; QLineEdit *editor; QString originalText; QListWidget *popup; QTimer *timer; + bool enabled; };