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);
19 popup->setWindowOpacity(.9);
21 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
22 SLOT(doneCompletion()));
24 // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
25 // SLOT(currentItemChanged(QListWidgetItem *)));
28 // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
29 // SLOT(currentItemChanged(QListWidgetItem *)));
31 popup->setWindowFlags(Qt::Popup);
32 popup->setFocusPolicy(Qt::NoFocus);
33 popup->setFocusProxy(parent);
35 timer = new QTimer(this);
36 timer->setSingleShot(true);
37 timer->setInterval(300);
38 connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
39 connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
43 GSuggestCompletion::~GSuggestCompletion() {
47 bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) {
51 if (ev->type() == QEvent::MouseButtonPress) {
54 editor->setText(originalText);
58 if (ev->type() == QEvent::KeyPress) {
60 bool consumed = false;
62 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(ev);
63 int key = keyEvent->key();
64 // qDebug() << keyEvent->text();
68 if (popup->currentItem()) {
80 editor->setText(originalText);
90 case Qt::Key_PageDown:
94 // qDebug() << keyEvent->text();
107 void GSuggestCompletion::showCompletion(const QStringList &choices) {
109 if (choices.isEmpty())
112 popup->setUpdatesEnabled(false);
114 for (int i = 0; i < choices.count(); ++i) {
115 QListWidgetItem * item;
116 item = new QListWidgetItem(popup);
117 item->setText(choices[i]);
119 popup->setCurrentItem(0);
121 popup->setUpdatesEnabled(true);
123 int h = popup->sizeHintForRow(0) * choices.count() + 4;
124 popup->resize(buddy->width(), h);
126 popup->move(buddy->mapToGlobal(QPoint(0, buddy->height())));
132 void GSuggestCompletion::doneCompletion() {
136 QListWidgetItem *item = popup->currentItem();
138 editor->setText(item->text());
140 e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
141 QApplication::postEvent(editor, e);
142 e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
143 QApplication::postEvent(editor, e);
147 void GSuggestCompletion::preventSuggest() {
148 // qDebug() << "preventSuggest";
154 void GSuggestCompletion::enableSuggest() {
155 // qDebug() << "enableSuggest";
159 void GSuggestCompletion::autoSuggest() {
160 if (!enabled) return;
162 QString query = editor->text();
163 originalText = query;
164 // qDebug() << "originalText" << originalText;
165 if (query.isEmpty()) return;
167 QString locale = QLocale::system().name().replace("_", "-");
168 // case for system locales such as "C"
169 if (locale.length() < 2) {
173 QString url = QString(GSUGGEST_URL).arg(locale, query);
175 QObject *reply = The::http()->get(url);
176 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
179 void GSuggestCompletion::handleNetworkData(QByteArray response) {
180 if (!enabled) return;
184 QXmlStreamReader xml(response);
185 while (!xml.atEnd()) {
187 if (xml.tokenType() == QXmlStreamReader::StartElement)
188 if (xml.name() == "suggestion") {
189 QStringRef str = xml.attributes().value("data");
190 choices << str.toString();
194 showCompletion(choices);
198 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
200 // qDebug() << "current" << current->text();
201 current->setSelected(true);
202 editor->setText(current->text());
203 editor->setSelection(originalText.length(), editor->text().length());