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(QLineEdit *parent): QObject(parent), editor(parent) {
12 popup = new QListWidget;
13 popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
14 popup->installEventFilter(this);
15 popup->setMouseTracking(true);
17 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
18 SLOT(doneCompletion()));
20 connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
21 SLOT(currentItemChanged(QListWidgetItem *)));
24 connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
25 SLOT(currentItemChanged(QListWidgetItem *)));
27 popup->setWindowFlags(Qt::Popup);
28 popup->setFocusPolicy(Qt::NoFocus);
29 popup->setFocusProxy(parent);
31 timer = new QTimer(this);
32 timer->setSingleShot(true);
33 timer->setInterval(100);
34 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
35 connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
39 GSuggestCompletion::~GSuggestCompletion() {
43 bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) {
47 if (ev->type() == QEvent::MouseButtonPress) {
50 editor->setText(originalText);
54 if (ev->type() == QEvent::KeyPress) {
56 bool consumed = false;
58 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
59 int key = keyEvent->key();
63 if (popup->currentItem()) {
75 editor->setText(originalText);
85 case Qt::Key_PageDown:
102 void GSuggestCompletion::showCompletion(const QStringList &choices) {
104 if (choices.isEmpty())
107 popup->setUpdatesEnabled(false);
109 for (int i = 0; i < choices.count(); ++i) {
110 QListWidgetItem * item;
111 item = new QListWidgetItem(popup);
112 item->setText(choices[i]);
114 popup->setCurrentItem(0);
116 popup->setUpdatesEnabled(true);
118 int h = popup->sizeHintForRow(0) * choices.count() + 4;
119 popup->resize(popup->width(), h);
121 popup->move(editor->mapToGlobal(QPoint(0, editor->height()+4)));
126 void GSuggestCompletion::doneCompletion() {
130 QListWidgetItem *item = popup->currentItem();
132 editor->setText(item->text());
134 e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
135 QApplication::postEvent(editor, e);
136 e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
137 QApplication::postEvent(editor, e);
141 void GSuggestCompletion::preventSuggest() {
145 void GSuggestCompletion::autoSuggest() {
146 QString query = editor->text();
147 originalText = query;
148 qDebug() << "originalText" << originalText;
149 if (query.isEmpty()) return;
151 QString locale = QLocale::system().name().replace("_", "-");
152 // case for system locales such as "C"
153 if (locale.length() < 2) {
157 QString url = QString(GSUGGEST_URL).arg(locale, query);
159 QObject *reply = The::http()->get(url);
160 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
163 void GSuggestCompletion::handleNetworkData(QByteArray response) {
167 QXmlStreamReader xml(response);
168 while (!xml.atEnd()) {
170 if (xml.tokenType() == QXmlStreamReader::StartElement)
171 if (xml.name() == "suggestion") {
172 QStringRef str = xml.attributes().value("data");
173 choices << str.toString();
177 showCompletion(choices);
181 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
183 qDebug() << "current" << current->text();
184 current->setSelected(true);
185 editor->setText(current->text());
186 editor->setSelection(originalText.length(), editor->text().length());