1 #include "googlesuggest.h"
2 #include "networkaccess.h"
4 #define GSUGGEST_URL "http://suggestqueries.google.com/complete/search?ds=yt&output=toolbar&hl=%1&q=%2"
10 GSuggestCompletion::GSuggestCompletion(QWidget *parent, QLineEdit *editor):
11 QObject(parent), buddy(parent), editor(editor) {
15 popup = new QListWidget;
16 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 popup->installEventFilter(this);
18 popup->setMouseTracking(true);
20 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
21 SLOT(doneCompletion()));
23 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
24 // SLOT(currentItemChanged(QListWidgetItem *)));
27 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
28 // SLOT(currentItemChanged(QListWidgetItem *)));
30 popup->setWindowFlags(Qt::Popup);
31 popup->setFocusPolicy(Qt::NoFocus);
32 popup->setFocusProxy(parent);
34 timer = new QTimer(this);
35 timer->setSingleShot(true);
36 timer->setInterval(300);
37 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
38 connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
42 GSuggestCompletion::~GSuggestCompletion() {
46 bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) {
50 if (ev->type() == QEvent::MouseButtonPress) {
53 editor->setText(originalText);
57 if (ev->type() == QEvent::KeyPress) {
59 bool consumed = false;
61 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
62 int key = keyEvent->key();
63 // qDebug() << keyEvent->text();
67 if (popup->currentItem()) {
79 editor->setText(originalText);
89 case Qt::Key_PageDown:
93 qDebug() << keyEvent->text();
106 void GSuggestCompletion::showCompletion(const QStringList &choices) {
108 if (choices.isEmpty())
111 popup->setUpdatesEnabled(false);
113 for (int i = 0; i < choices.count(); ++i) {
114 QListWidgetItem * item;
115 item = new QListWidgetItem(popup);
116 item->setText(choices[i]);
118 popup->setCurrentItem(0);
120 popup->setUpdatesEnabled(true);
122 int h = popup->sizeHintForRow(0) * choices.count() + 4;
123 popup->resize(buddy->width(), h);
125 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
131 void GSuggestCompletion::doneCompletion() {
135 QListWidgetItem *item = popup->currentItem();
137 editor->setText(item->text());
139 e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
140 QApplication::postEvent(editor, e);
141 e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
142 QApplication::postEvent(editor, e);
146 void GSuggestCompletion::preventSuggest() {
152 void GSuggestCompletion::enableSuggest() {
156 void GSuggestCompletion::autoSuggest() {
157 if (!enabled) return;
159 QString query = editor->text();
160 originalText = query;
161 // qDebug() << "originalText" << originalText;
162 if (query.isEmpty()) return;
164 QString locale = QLocale::system().name().replace("_", "-");
165 // case for system locales such as "C"
166 if (locale.length() < 2) {
170 QString url = QString(GSUGGEST_URL).arg(locale, query);
172 QObject *reply = The::http()->get(url);
173 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
176 void GSuggestCompletion::handleNetworkData(QByteArray response) {
177 if (!enabled) return;
181 QXmlStreamReader xml(response);
182 while (!xml.atEnd()) {
184 if (xml.tokenType() == QXmlStreamReader::StartElement)
185 if (xml.name() == "suggestion") {
186 QStringRef str = xml.attributes().value("data");
187 choices << str.toString();
191 showCompletion(choices);
195 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
197 // qDebug() << "current" << current->text();
198 current->setSelected(true);
199 editor->setText(current->text());
200 editor->setSelection(originalText.length(), editor->text().length());