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() {
147 // qDebug() << "preventSuggest";
153 void GSuggestCompletion::enableSuggest() {
154 // qDebug() << "enableSuggest";
158 void GSuggestCompletion::autoSuggest() {
159 if (!enabled) return;
161 QString query = editor->text();
162 originalText = query;
163 // qDebug() << "originalText" << originalText;
164 if (query.isEmpty()) return;
166 QString locale = QLocale::system().name().replace("_", "-");
167 // case for system locales such as "C"
168 if (locale.length() < 2) {
172 QString url = QString(GSUGGEST_URL).arg(locale, query);
174 QObject *reply = The::http()->get(url);
175 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
178 void GSuggestCompletion::handleNetworkData(QByteArray response) {
179 if (!enabled) return;
183 QXmlStreamReader xml(response);
184 while (!xml.atEnd()) {
186 if (xml.tokenType() == QXmlStreamReader::StartElement)
187 if (xml.name() == "suggestion") {
188 QStringRef str = xml.attributes().value("data");
189 choices << str.toString();
193 showCompletion(choices);
197 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
199 // qDebug() << "current" << current->text();
200 current->setSelected(true);
201 editor->setText(current->text());
202 editor->setSelection(originalText.length(), editor->text().length());