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();
66 if (popup->currentItem()) {
78 editor->setText(originalText);
88 case Qt::Key_PageDown:
105 void GSuggestCompletion::showCompletion(const QStringList &choices) {
107 if (choices.isEmpty())
110 popup->setUpdatesEnabled(false);
112 for (int i = 0; i < choices.count(); ++i) {
113 QListWidgetItem * item;
114 item = new QListWidgetItem(popup);
115 item->setText(choices[i]);
117 popup->setCurrentItem(0);
119 popup->setUpdatesEnabled(true);
121 int h = popup->sizeHintForRow(0) * choices.count() + 4;
122 popup->resize(buddy->width(), h);
124 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
130 void GSuggestCompletion::doneCompletion() {
134 QListWidgetItem *item = popup->currentItem();
136 editor->setText(item->text());
138 e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
139 QApplication::postEvent(editor, e);
140 e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
141 QApplication::postEvent(editor, e);
145 void GSuggestCompletion::preventSuggest() {
151 void GSuggestCompletion::enableSuggest() {
155 void GSuggestCompletion::autoSuggest() {
156 if (!enabled) return;
158 QString query = editor->text();
159 originalText = query;
160 qDebug() << "originalText" << originalText;
161 if (query.isEmpty()) return;
163 QString locale = QLocale::system().name().replace("_", "-");
164 // case for system locales such as "C"
165 if (locale.length() < 2) {
169 QString url = QString(GSUGGEST_URL).arg(locale, query);
171 QObject *reply = The::http()->get(url);
172 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
175 void GSuggestCompletion::handleNetworkData(QByteArray response) {
176 if (!enabled) return;
180 QXmlStreamReader xml(response);
181 while (!xml.atEnd()) {
183 if (xml.tokenType() == QXmlStreamReader::StartElement)
184 if (xml.name() == "suggestion") {
185 QStringRef str = xml.attributes().value("data");
186 choices << str.toString();
190 showCompletion(choices);
194 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
196 qDebug() << "current" << current->text();
197 current->setSelected(true);
198 editor->setText(current->text());
199 editor->setSelection(originalText.length(), editor->text().length());