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);
15 popup->installEventFilter(this);
16 popup->setMouseTracking(true);
18 connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
19 SLOT(doneCompletion()));
21 connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
22 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(250);
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;
57 int key = static_cast<QKeyEvent*>(ev)->key();
61 if (popup->currentItem()) {
73 editor->setText(originalText);
83 case Qt::Key_PageDown:
99 void GSuggestCompletion::showCompletion(const QStringList &choices) {
101 if (choices.isEmpty())
104 popup->setUpdatesEnabled(false);
106 for (int i = 0; i < choices.count(); ++i) {
107 QListWidgetItem * item;
108 item = new QListWidgetItem(popup);
109 item->setText(choices[i]);
111 popup->setCurrentItem(0);
113 popup->setUpdatesEnabled(true);
115 popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
120 void GSuggestCompletion::doneCompletion() {
124 QListWidgetItem *item = popup->currentItem();
126 editor->setText(item->text());
128 e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
129 QApplication::postEvent(editor, e);
130 e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
131 QApplication::postEvent(editor, e);
135 void GSuggestCompletion::preventSuggest() {
139 void GSuggestCompletion::autoSuggest() {
140 QString str = editor->text();
142 qDebug() << "originalText" << originalText;
143 if (str.isEmpty()) return;
145 QString url = QString(GSUGGEST_URL).arg(QLocale::system().name().replace("_", "-"), str);
147 QObject *reply = The::http()->get(url);
148 connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
151 void GSuggestCompletion::handleNetworkData(QByteArray response) {
155 QXmlStreamReader xml(response);
156 while (!xml.atEnd()) {
158 if (xml.tokenType() == QXmlStreamReader::StartElement)
159 if (xml.name() == "suggestion") {
160 QStringRef str = xml.attributes().value("data");
161 choices << str.toString();
165 showCompletion(choices);
169 void GSuggestCompletion::currentItemChanged(QListWidgetItem *current) {
171 qDebug() << "current" << current->text();
172 current->setSelected(true);
173 editor->setText(current->text());
174 editor->setSelection(originalText.length(), editor->text().length());
176 popup->clearSelection();