[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | //! [1]
|
---|
| 44 | #include "googlesuggest.h"
|
---|
| 45 |
|
---|
| 46 | #define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
|
---|
| 47 | //! [1]
|
---|
| 48 |
|
---|
| 49 | //! [2]
|
---|
| 50 | GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
|
---|
| 51 | {
|
---|
| 52 | popup = new QTreeWidget;
|
---|
| 53 | popup->setWindowFlags(Qt::Popup);
|
---|
| 54 | popup->setFocusPolicy(Qt::NoFocus);
|
---|
| 55 | popup->setFocusProxy(parent);
|
---|
| 56 | popup->setMouseTracking(true);
|
---|
| 57 |
|
---|
| 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 |
|
---|
| 67 | popup->installEventFilter(this);
|
---|
| 68 |
|
---|
| 69 | connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
|
---|
| 70 | SLOT(doneCompletion()));
|
---|
| 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 | //! [2]
|
---|
| 83 |
|
---|
| 84 | //! [3]
|
---|
| 85 | GSuggestCompletion::~GSuggestCompletion()
|
---|
| 86 | {
|
---|
| 87 | delete popup;
|
---|
| 88 | }
|
---|
| 89 | //! [3]
|
---|
| 90 |
|
---|
| 91 | //! [4]
|
---|
| 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 | }
|
---|
| 138 | //! [4]
|
---|
| 139 |
|
---|
| 140 | //! [5]
|
---|
| 141 | void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
|
---|
| 142 | {
|
---|
| 143 |
|
---|
| 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 | }
|
---|
| 173 | //! [5]
|
---|
| 174 |
|
---|
| 175 | //! [6]
|
---|
| 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));
|
---|
| 184 | QMetaObject::invokeMethod(editor, "returnPressed");
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | //! [6]
|
---|
| 188 |
|
---|
| 189 | //! [7]
|
---|
| 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 | }
|
---|
| 196 | //! [7]
|
---|
| 197 |
|
---|
| 198 | //! [8]
|
---|
| 199 | void GSuggestCompletion::preventSuggest()
|
---|
| 200 | {
|
---|
| 201 | timer->stop();
|
---|
| 202 | }
|
---|
| 203 | //! [8]
|
---|
| 204 |
|
---|
| 205 | //! [9]
|
---|
| 206 | void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
|
---|
| 207 | {
|
---|
| 208 | QUrl url = networkReply->url();
|
---|
| 209 | if (!networkReply->error()) {
|
---|
| 210 | QStringList choices;
|
---|
| 211 | QStringList hits;
|
---|
| 212 |
|
---|
| 213 | QByteArray response(networkReply->readAll());
|
---|
| 214 | QXmlStreamReader xml(response);
|
---|
| 215 | while (!xml.atEnd()) {
|
---|
| 216 | xml.readNext();
|
---|
| 217 | if (xml.tokenType() == QXmlStreamReader::StartElement)
|
---|
| 218 | if (xml.name() == "suggestion") {
|
---|
| 219 | QStringRef str = xml.attributes().value("data");
|
---|
| 220 | choices << str.toString();
|
---|
| 221 | }
|
---|
| 222 | if (xml.tokenType() == QXmlStreamReader::StartElement)
|
---|
| 223 | if (xml.name() == "num_queries") {
|
---|
| 224 | QStringRef str = xml.attributes().value("int");
|
---|
| 225 | hits << str.toString();
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | showCompletion(choices, hits);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | networkReply->deleteLater();
|
---|
| 233 | }
|
---|
| 234 | //! [9]
|
---|