author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Thu, 02 Sep 2010 21:20:32 +0300 | |
changeset 34 | a33bf25e6f72 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
42 |
//! [1] |
0 | 43 |
#include "googlesuggest.h" |
44 |
||
45 |
#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
|
46 |
//! [1] |
0 | 47 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
48 |
//! [2] |
0 | 49 |
GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) |
50 |
{ |
|
51 |
popup = new QTreeWidget; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
popup->setWindowFlags(Qt::Popup); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
popup->setFocusPolicy(Qt::NoFocus); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
popup->setFocusProxy(parent); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
popup->setMouseTracking(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
|
0 | 57 |
popup->setColumnCount(2); |
58 |
popup->setUniformRowHeights(true); |
|
59 |
popup->setRootIsDecorated(false); |
|
60 |
popup->setEditTriggers(QTreeWidget::NoEditTriggers); |
|
61 |
popup->setSelectionBehavior(QTreeWidget::SelectRows); |
|
62 |
popup->setFrameStyle(QFrame::Box | QFrame::Plain); |
|
63 |
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
64 |
popup->header()->hide(); |
|
65 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
popup->installEventFilter(this); |
0 | 67 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
SLOT(doneCompletion())); |
0 | 70 |
|
71 |
timer = new QTimer(this); |
|
72 |
timer->setSingleShot(true); |
|
73 |
timer->setInterval(500); |
|
74 |
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); |
|
75 |
connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start())); |
|
76 |
||
77 |
connect(&networkManager, SIGNAL(finished(QNetworkReply*)), |
|
78 |
this, SLOT(handleNetworkData(QNetworkReply*))); |
|
79 |
||
80 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
//! [2] |
0 | 82 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
//! [3] |
0 | 84 |
GSuggestCompletion::~GSuggestCompletion() |
85 |
{ |
|
86 |
delete popup; |
|
87 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
//! [3] |
0 | 89 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
//! [4] |
0 | 91 |
bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) |
92 |
{ |
|
93 |
if (obj != popup) |
|
94 |
return false; |
|
95 |
||
96 |
if (ev->type() == QEvent::MouseButtonPress) { |
|
97 |
popup->hide(); |
|
98 |
editor->setFocus(); |
|
99 |
return true; |
|
100 |
} |
|
101 |
||
102 |
if (ev->type() == QEvent::KeyPress) { |
|
103 |
||
104 |
bool consumed = false; |
|
105 |
int key = static_cast<QKeyEvent*>(ev)->key(); |
|
106 |
switch (key) { |
|
107 |
case Qt::Key_Enter: |
|
108 |
case Qt::Key_Return: |
|
109 |
doneCompletion(); |
|
110 |
consumed = true; |
|
111 |
||
112 |
case Qt::Key_Escape: |
|
113 |
editor->setFocus(); |
|
114 |
popup->hide(); |
|
115 |
consumed = true; |
|
116 |
||
117 |
case Qt::Key_Up: |
|
118 |
case Qt::Key_Down: |
|
119 |
case Qt::Key_Home: |
|
120 |
case Qt::Key_End: |
|
121 |
case Qt::Key_PageUp: |
|
122 |
case Qt::Key_PageDown: |
|
123 |
break; |
|
124 |
||
125 |
default: |
|
126 |
editor->setFocus(); |
|
127 |
editor->event(ev); |
|
128 |
popup->hide(); |
|
129 |
break; |
|
130 |
} |
|
131 |
||
132 |
return consumed; |
|
133 |
} |
|
134 |
||
135 |
return false; |
|
136 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
//! [4] |
0 | 138 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
//! [5] |
0 | 140 |
void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits) |
141 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
|
0 | 143 |
if (choices.isEmpty() || choices.count() != hits.count()) |
144 |
return; |
|
145 |
||
146 |
const QPalette &pal = editor->palette(); |
|
147 |
QColor color = pal.color(QPalette::Disabled, QPalette::WindowText); |
|
148 |
||
149 |
popup->setUpdatesEnabled(false); |
|
150 |
popup->clear(); |
|
151 |
for (int i = 0; i < choices.count(); ++i) { |
|
152 |
QTreeWidgetItem * item; |
|
153 |
item = new QTreeWidgetItem(popup); |
|
154 |
item->setText(0, choices[i]); |
|
155 |
item->setText(1, hits[i]); |
|
156 |
item->setTextAlignment(1, Qt::AlignRight); |
|
157 |
item->setTextColor(1, color); |
|
158 |
} |
|
159 |
popup->setCurrentItem(popup->topLevelItem(0)); |
|
160 |
popup->resizeColumnToContents(0); |
|
161 |
popup->resizeColumnToContents(1); |
|
162 |
popup->adjustSize(); |
|
163 |
popup->setUpdatesEnabled(true); |
|
164 |
||
165 |
int h = popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3; |
|
166 |
popup->resize(popup->width(), h); |
|
167 |
||
168 |
popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); |
|
169 |
popup->setFocus(); |
|
170 |
popup->show(); |
|
171 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
//! [5] |
0 | 173 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
//! [6] |
0 | 175 |
void GSuggestCompletion::doneCompletion() |
176 |
{ |
|
177 |
timer->stop(); |
|
178 |
popup->hide(); |
|
179 |
editor->setFocus(); |
|
180 |
QTreeWidgetItem *item = popup->currentItem(); |
|
181 |
if (item) { |
|
182 |
editor->setText(item->text(0)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
QMetaObject::invokeMethod(editor, "returnPressed"); |
0 | 184 |
} |
185 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
186 |
//! [6] |
0 | 187 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
//! [7] |
0 | 189 |
void GSuggestCompletion::autoSuggest() |
190 |
{ |
|
191 |
QString str = editor->text(); |
|
192 |
QString url = QString(GSUGGEST_URL).arg(str); |
|
193 |
networkManager.get(QNetworkRequest(QString(url))); |
|
194 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
195 |
//! [7] |
0 | 196 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
197 |
//! [8] |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
198 |
void GSuggestCompletion::preventSuggest() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
timer->stop(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
201 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
202 |
//! [8] |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
//! [9] |
0 | 205 |
void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) |
206 |
{ |
|
207 |
QUrl url = networkReply->url(); |
|
208 |
if (!networkReply->error()) { |
|
209 |
QStringList choices; |
|
210 |
QStringList hits; |
|
211 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
QByteArray response(networkReply->readAll()); |
0 | 213 |
QXmlStreamReader xml(response); |
214 |
while (!xml.atEnd()) { |
|
215 |
xml.readNext(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
if (xml.tokenType() == QXmlStreamReader::StartElement) |
0 | 217 |
if (xml.name() == "suggestion") { |
218 |
QStringRef str = xml.attributes().value("data"); |
|
219 |
choices << str.toString(); |
|
220 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
if (xml.tokenType() == QXmlStreamReader::StartElement) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
if (xml.name() == "num_queries") { |
0 | 223 |
QStringRef str = xml.attributes().value("int"); |
224 |
hits << str.toString(); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
showCompletion(choices, hits); |
|
229 |
} |
|
230 |
||
231 |
networkReply->deleteLater(); |
|
232 |
} |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
233 |
//! [9] |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
234 |