X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fgooglesuggest.cpp;h=234fe61c0c42d2613ba3abac559d2a1e8ce810d5;hb=2492f8daafa9723a3fe2d926a012efe26d28a3df;hp=0ad193255368087b6008b4dfe354d9c93ff511ca;hpb=baf4d5b6882803c96fbbdc528f7cb71bd849288d;p=minitube diff --git a/src/googlesuggest.cpp b/src/googlesuggest.cpp index 0ad1932..234fe61 100644 --- a/src/googlesuggest.cpp +++ b/src/googlesuggest.cpp @@ -1,28 +1,31 @@ #include "googlesuggest.h" #include "networkaccess.h" -#define GSUGGEST_URL "http://suggestqueries.google.com/complete/search?output=toolbar&hl=%1&q=%2" +#define GSUGGEST_URL "http://suggestqueries.google.com/complete/search?ds=yt&output=toolbar&hl=%1&q=%2" 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); - popup->installEventFilter(this); popup->setMouseTracking(true); 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 *))); - connect(popup, SIGNAL(itemEntered(QListWidgetItem*)), - SLOT(currentItemChanged(QListWidgetItem *))); + // mouse hover + // 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(250); + timer->setInterval(300); connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start())); @@ -54,7 +57,9 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) { if (ev->type() == QEvent::KeyPress) { bool consumed = false; - int key = static_cast(ev)->key(); + + QKeyEvent *keyEvent = static_cast(ev); + int key = keyEvent->key(); switch (key) { case Qt::Key_Enter: case Qt::Key_Return: @@ -84,6 +89,7 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) { break; default: + editor->setFocus(); editor->event(ev); popup->hide(); @@ -112,7 +118,11 @@ void GSuggestCompletion::showCompletion(const QStringList &choices) { popup->adjustSize(); popup->setUpdatesEnabled(true); - popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); + int h = popup->sizeHintForRow(0) * choices.count() + 4; + popup->resize(buddy->width(), h); + + popup->move(buddy->mapToGlobal(QPoint(0, buddy->height()))); + popup->setFocus(); popup->show(); } @@ -134,21 +144,36 @@ void GSuggestCompletion::doneCompletion() { void GSuggestCompletion::preventSuggest() { timer->stop(); + enabled = false; + popup->hide(); +} + +void GSuggestCompletion::enableSuggest() { + enabled = true; } void GSuggestCompletion::autoSuggest() { - QString str = editor->text(); - originalText = str; + if (!enabled) return; + + QString query = editor->text(); + originalText = query; qDebug() << "originalText" << originalText; - if (str.isEmpty()) return; + if (query.isEmpty()) return; + + QString locale = QLocale::system().name().replace("_", "-"); + // case for system locales such as "C" + if (locale.length() < 2) { + locale = "en-US"; + } - QString url = QString(GSUGGEST_URL).arg(QLocale::system().name().replace("_", "-"), str); + QString url = QString(GSUGGEST_URL).arg(locale, query); QObject *reply = The::http()->get(url); connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray))); } void GSuggestCompletion::handleNetworkData(QByteArray response) { + if (!enabled) return; QStringList choices; @@ -172,7 +197,5 @@ void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) { current->setSelected(true); editor->setText(current->text()); editor->setSelection(originalText.length(), editor->text().length()); - } else { - popup->clearSelection(); } }