0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtCore>
|
|
43 |
#include <QtGui>
|
|
44 |
#include <QtNetwork>
|
|
45 |
|
|
46 |
#include "googlesuggest.h"
|
|
47 |
|
|
48 |
#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
|
|
49 |
|
|
50 |
GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
|
|
51 |
{
|
|
52 |
popup = new QTreeWidget;
|
|
53 |
popup->setColumnCount(2);
|
|
54 |
popup->setUniformRowHeights(true);
|
|
55 |
popup->setRootIsDecorated(false);
|
|
56 |
popup->setEditTriggers(QTreeWidget::NoEditTriggers);
|
|
57 |
popup->setSelectionBehavior(QTreeWidget::SelectRows);
|
|
58 |
popup->setFrameStyle(QFrame::Box | QFrame::Plain);
|
|
59 |
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
60 |
|
|
61 |
popup->header()->hide();
|
|
62 |
popup->installEventFilter(this);
|
|
63 |
popup->setMouseTracking(true);
|
|
64 |
|
|
65 |
connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
|
|
66 |
SLOT(doneCompletion()));
|
|
67 |
|
|
68 |
popup->setWindowFlags(Qt::Popup);
|
|
69 |
popup->setFocusPolicy(Qt::NoFocus);
|
|
70 |
popup->setFocusProxy(parent);
|
|
71 |
|
|
72 |
timer = new QTimer(this);
|
|
73 |
timer->setSingleShot(true);
|
|
74 |
timer->setInterval(500);
|
|
75 |
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
|
|
76 |
connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
|
|
77 |
|
|
78 |
connect(&networkManager, SIGNAL(finished(QNetworkReply*)),
|
|
79 |
this, SLOT(handleNetworkData(QNetworkReply*)));
|
|
80 |
|
|
81 |
}
|
|
82 |
|
|
83 |
GSuggestCompletion::~GSuggestCompletion()
|
|
84 |
{
|
|
85 |
delete popup;
|
|
86 |
}
|
|
87 |
|
|
88 |
bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
|
|
89 |
{
|
|
90 |
if (obj != popup)
|
|
91 |
return false;
|
|
92 |
|
|
93 |
if (ev->type() == QEvent::MouseButtonPress) {
|
|
94 |
popup->hide();
|
|
95 |
editor->setFocus();
|
|
96 |
return true;
|
|
97 |
}
|
|
98 |
|
|
99 |
if (ev->type() == QEvent::KeyPress) {
|
|
100 |
|
|
101 |
bool consumed = false;
|
|
102 |
int key = static_cast<QKeyEvent*>(ev)->key();
|
|
103 |
switch (key) {
|
|
104 |
case Qt::Key_Enter:
|
|
105 |
case Qt::Key_Return:
|
|
106 |
doneCompletion();
|
|
107 |
consumed = true;
|
|
108 |
|
|
109 |
case Qt::Key_Escape:
|
|
110 |
editor->setFocus();
|
|
111 |
popup->hide();
|
|
112 |
consumed = true;
|
|
113 |
|
|
114 |
case Qt::Key_Up:
|
|
115 |
case Qt::Key_Down:
|
|
116 |
case Qt::Key_Home:
|
|
117 |
case Qt::Key_End:
|
|
118 |
case Qt::Key_PageUp:
|
|
119 |
case Qt::Key_PageDown:
|
|
120 |
break;
|
|
121 |
|
|
122 |
default:
|
|
123 |
editor->setFocus();
|
|
124 |
editor->event(ev);
|
|
125 |
popup->hide();
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
|
|
129 |
return consumed;
|
|
130 |
}
|
|
131 |
|
|
132 |
return false;
|
|
133 |
}
|
|
134 |
|
|
135 |
void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
|
|
136 |
{
|
|
137 |
if (choices.isEmpty() || choices.count() != hits.count())
|
|
138 |
return;
|
|
139 |
|
|
140 |
const QPalette &pal = editor->palette();
|
|
141 |
QColor color = pal.color(QPalette::Disabled, QPalette::WindowText);
|
|
142 |
|
|
143 |
popup->setUpdatesEnabled(false);
|
|
144 |
popup->clear();
|
|
145 |
for (int i = 0; i < choices.count(); ++i) {
|
|
146 |
QTreeWidgetItem * item;
|
|
147 |
item = new QTreeWidgetItem(popup);
|
|
148 |
item->setText(0, choices[i]);
|
|
149 |
item->setText(1, hits[i]);
|
|
150 |
item->setTextAlignment(1, Qt::AlignRight);
|
|
151 |
item->setTextColor(1, color);
|
|
152 |
}
|
|
153 |
popup->setCurrentItem(popup->topLevelItem(0));
|
|
154 |
popup->resizeColumnToContents(0);
|
|
155 |
popup->resizeColumnToContents(1);
|
|
156 |
popup->adjustSize();
|
|
157 |
popup->setUpdatesEnabled(true);
|
|
158 |
|
|
159 |
int h = popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3;
|
|
160 |
popup->resize(popup->width(), h);
|
|
161 |
|
|
162 |
popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
|
|
163 |
popup->setFocus();
|
|
164 |
popup->show();
|
|
165 |
}
|
|
166 |
|
|
167 |
void GSuggestCompletion::doneCompletion()
|
|
168 |
{
|
|
169 |
timer->stop();
|
|
170 |
popup->hide();
|
|
171 |
editor->setFocus();
|
|
172 |
QTreeWidgetItem *item = popup->currentItem();
|
|
173 |
if (item) {
|
|
174 |
editor->setText(item->text(0));
|
|
175 |
QKeyEvent *e;
|
|
176 |
e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
|
|
177 |
QApplication::postEvent(editor, e);
|
|
178 |
e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
|
|
179 |
QApplication::postEvent(editor, e);
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
void GSuggestCompletion::preventSuggest()
|
|
184 |
{
|
|
185 |
timer->stop();
|
|
186 |
}
|
|
187 |
|
|
188 |
void GSuggestCompletion::autoSuggest()
|
|
189 |
{
|
|
190 |
QString str = editor->text();
|
|
191 |
QString url = QString(GSUGGEST_URL).arg(str);
|
|
192 |
networkManager.get(QNetworkRequest(QString(url)));
|
|
193 |
}
|
|
194 |
|
|
195 |
void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
|
|
196 |
{
|
|
197 |
QUrl url = networkReply->url();
|
|
198 |
if (!networkReply->error()) {
|
|
199 |
QStringList choices;
|
|
200 |
QStringList hits;
|
|
201 |
|
|
202 |
QString response(networkReply->readAll());
|
|
203 |
QXmlStreamReader xml(response);
|
|
204 |
while (!xml.atEnd()) {
|
|
205 |
xml.readNext();
|
|
206 |
if (xml.isStartElement()) {
|
|
207 |
if (xml.name() == "suggestion") {
|
|
208 |
QStringRef str = xml.attributes().value("data");
|
|
209 |
choices << str.toString();
|
|
210 |
}
|
|
211 |
else if (xml.name() == "num_queries") {
|
|
212 |
QStringRef str = xml.attributes().value("int");
|
|
213 |
hits << str.toString();
|
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
showCompletion(choices, hits);
|
|
219 |
}
|
|
220 |
|
|
221 |
networkReply->deleteLater();
|
|
222 |
}
|