author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Fri, 16 Apr 2010 11:39:52 +0300 | |
branch | RCL_3 |
changeset 8 | 740e5562c97f |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
43 |
//! [1] |
0 | 44 |
#include "googlesuggest.h" |
45 |
||
46 |
#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
47 |
//! [1] |
0 | 48 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
//! [2] |
0 | 50 |
GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) |
51 |
{ |
|
52 |
popup = new QTreeWidget; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
popup->setWindowFlags(Qt::Popup); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
popup->setFocusPolicy(Qt::NoFocus); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
popup->setFocusProxy(parent); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
popup->setMouseTracking(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
|
0 | 58 |
popup->setColumnCount(2); |
59 |
popup->setUniformRowHeights(true); |
|
60 |
popup->setRootIsDecorated(false); |
|
61 |
popup->setEditTriggers(QTreeWidget::NoEditTriggers); |
|
62 |
popup->setSelectionBehavior(QTreeWidget::SelectRows); |
|
63 |
popup->setFrameStyle(QFrame::Box | QFrame::Plain); |
|
64 |
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
65 |
popup->header()->hide(); |
|
66 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
popup->installEventFilter(this); |
0 | 68 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
SLOT(doneCompletion())); |
0 | 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 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
//! [2] |
0 | 83 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
//! [3] |
0 | 85 |
GSuggestCompletion::~GSuggestCompletion() |
86 |
{ |
|
87 |
delete popup; |
|
88 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
//! [3] |
0 | 90 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
//! [4] |
0 | 92 |
bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) |
93 |
{ |
|
94 |
if (obj != popup) |
|
95 |
return false; |
|
96 |
||
97 |
if (ev->type() == QEvent::MouseButtonPress) { |
|
98 |
popup->hide(); |
|
99 |
editor->setFocus(); |
|
100 |
return true; |
|
101 |
} |
|
102 |
||
103 |
if (ev->type() == QEvent::KeyPress) { |
|
104 |
||
105 |
bool consumed = false; |
|
106 |
int key = static_cast<QKeyEvent*>(ev)->key(); |
|
107 |
switch (key) { |
|
108 |
case Qt::Key_Enter: |
|
109 |
case Qt::Key_Return: |
|
110 |
doneCompletion(); |
|
111 |
consumed = true; |
|
112 |
||
113 |
case Qt::Key_Escape: |
|
114 |
editor->setFocus(); |
|
115 |
popup->hide(); |
|
116 |
consumed = true; |
|
117 |
||
118 |
case Qt::Key_Up: |
|
119 |
case Qt::Key_Down: |
|
120 |
case Qt::Key_Home: |
|
121 |
case Qt::Key_End: |
|
122 |
case Qt::Key_PageUp: |
|
123 |
case Qt::Key_PageDown: |
|
124 |
break; |
|
125 |
||
126 |
default: |
|
127 |
editor->setFocus(); |
|
128 |
editor->event(ev); |
|
129 |
popup->hide(); |
|
130 |
break; |
|
131 |
} |
|
132 |
||
133 |
return consumed; |
|
134 |
} |
|
135 |
||
136 |
return false; |
|
137 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
//! [4] |
0 | 139 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
//! [5] |
0 | 141 |
void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits) |
142 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
|
0 | 144 |
if (choices.isEmpty() || choices.count() != hits.count()) |
145 |
return; |
|
146 |
||
147 |
const QPalette &pal = editor->palette(); |
|
148 |
QColor color = pal.color(QPalette::Disabled, QPalette::WindowText); |
|
149 |
||
150 |
popup->setUpdatesEnabled(false); |
|
151 |
popup->clear(); |
|
152 |
for (int i = 0; i < choices.count(); ++i) { |
|
153 |
QTreeWidgetItem * item; |
|
154 |
item = new QTreeWidgetItem(popup); |
|
155 |
item->setText(0, choices[i]); |
|
156 |
item->setText(1, hits[i]); |
|
157 |
item->setTextAlignment(1, Qt::AlignRight); |
|
158 |
item->setTextColor(1, color); |
|
159 |
} |
|
160 |
popup->setCurrentItem(popup->topLevelItem(0)); |
|
161 |
popup->resizeColumnToContents(0); |
|
162 |
popup->resizeColumnToContents(1); |
|
163 |
popup->adjustSize(); |
|
164 |
popup->setUpdatesEnabled(true); |
|
165 |
||
166 |
int h = popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3; |
|
167 |
popup->resize(popup->width(), h); |
|
168 |
||
169 |
popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); |
|
170 |
popup->setFocus(); |
|
171 |
popup->show(); |
|
172 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
//! [5] |
0 | 174 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
//! [6] |
0 | 176 |
void GSuggestCompletion::doneCompletion() |
177 |
{ |
|
178 |
timer->stop(); |
|
179 |
popup->hide(); |
|
180 |
editor->setFocus(); |
|
181 |
QTreeWidgetItem *item = popup->currentItem(); |
|
182 |
if (item) { |
|
183 |
editor->setText(item->text(0)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
QMetaObject::invokeMethod(editor, "returnPressed"); |
0 | 185 |
} |
186 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
//! [6] |
0 | 188 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
//! [7] |
0 | 190 |
void GSuggestCompletion::autoSuggest() |
191 |
{ |
|
192 |
QString str = editor->text(); |
|
193 |
QString url = QString(GSUGGEST_URL).arg(str); |
|
194 |
networkManager.get(QNetworkRequest(QString(url))); |
|
195 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
196 |
//! [7] |
0 | 197 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
198 |
//! [8] |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
void GSuggestCompletion::preventSuggest() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
201 |
timer->stop(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
202 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
//! [8] |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
//! [9] |
0 | 206 |
void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) |
207 |
{ |
|
208 |
QUrl url = networkReply->url(); |
|
209 |
if (!networkReply->error()) { |
|
210 |
QStringList choices; |
|
211 |
QStringList hits; |
|
212 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
QByteArray response(networkReply->readAll()); |
0 | 214 |
QXmlStreamReader xml(response); |
215 |
while (!xml.atEnd()) { |
|
216 |
xml.readNext(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
if (xml.tokenType() == QXmlStreamReader::StartElement) |
0 | 218 |
if (xml.name() == "suggestion") { |
219 |
QStringRef str = xml.attributes().value("data"); |
|
220 |
choices << str.toString(); |
|
221 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
if (xml.tokenType() == QXmlStreamReader::StartElement) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
if (xml.name() == "num_queries") { |
0 | 224 |
QStringRef str = xml.attributes().value("int"); |
225 |
hits << str.toString(); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
showCompletion(choices, hits); |
|
230 |
} |
|
231 |
||
232 |
networkReply->deleteLater(); |
|
233 |
} |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
234 |
//! [9] |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
235 |