From 5a46b82feea371868a9c793f6df14d92654c2423 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Thu, 23 Apr 2009 15:44:49 +0200 Subject: Add Google Suggest as a new network example Reviewed-by: Simon Hausmann --- demos/qtdemo/xml/examples.xml | 1 + doc/src/examples.qdoc | 1 + doc/src/examples/googlesuggest.qdoc | 51 ++++++ doc/src/images/googlesuggest-example.png | Bin 0 -> 18809 bytes examples/network/googlesuggest/googlesuggest.cpp | 223 +++++++++++++++++++++++ examples/network/googlesuggest/googlesuggest.h | 80 ++++++++ examples/network/googlesuggest/googlesuggest.pro | 9 + examples/network/googlesuggest/main.cpp | 52 ++++++ examples/network/googlesuggest/searchbox.cpp | 69 +++++++ examples/network/googlesuggest/searchbox.h | 64 +++++++ examples/network/network.pro | 1 + 11 files changed, 551 insertions(+) create mode 100644 doc/src/examples/googlesuggest.qdoc create mode 100644 doc/src/images/googlesuggest-example.png create mode 100644 examples/network/googlesuggest/googlesuggest.cpp create mode 100644 examples/network/googlesuggest/googlesuggest.h create mode 100644 examples/network/googlesuggest/googlesuggest.pro create mode 100644 examples/network/googlesuggest/main.cpp create mode 100644 examples/network/googlesuggest/searchbox.cpp create mode 100644 examples/network/googlesuggest/searchbox.h diff --git a/demos/qtdemo/xml/examples.xml b/demos/qtdemo/xml/examples.xml index 667f9f2..96a3e80 100644 --- a/demos/qtdemo/xml/examples.xml +++ b/demos/qtdemo/xml/examples.xml @@ -118,6 +118,7 @@ + diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index efe9b2f..29c6c0b 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -209,6 +209,7 @@ \o \l{network/loopback}{Loopback} \o \l{network/threadedfortuneserver}{Threaded Fortune Server}\raisedaster \o \l{network/torrent}{Torrent} + \o \l{network/googlesuggest}{Google Suggest} \endlist \section1 OpenGL diff --git a/doc/src/examples/googlesuggest.qdoc b/doc/src/examples/googlesuggest.qdoc new file mode 100644 index 0000000..a9b09f9 --- /dev/null +++ b/doc/src/examples/googlesuggest.qdoc @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example network/googlesuggest + \title Google Suggest Example + + The Google Suggest example shows how to use the network access manager + to get the list of suggested search terms from Google. + + \image googlesuggest-example.png + +*/ diff --git a/doc/src/images/googlesuggest-example.png b/doc/src/images/googlesuggest-example.png new file mode 100644 index 0000000..4ef072a Binary files /dev/null and b/doc/src/images/googlesuggest-example.png differ diff --git a/examples/network/googlesuggest/googlesuggest.cpp b/examples/network/googlesuggest/googlesuggest.cpp new file mode 100644 index 0000000..285a018 --- /dev/null +++ b/examples/network/googlesuggest/googlesuggest.cpp @@ -0,0 +1,223 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +#include "googlesuggest.h" + +#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1" + +GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) +{ + popup = new QTreeWidget; + popup->setColumnCount(2); + popup->setUniformRowHeights(true); + popup->setRootIsDecorated(false); + popup->setEditTriggers(QTreeWidget::NoEditTriggers); + popup->setSelectionBehavior(QTreeWidget::SelectRows); + popup->setFrameStyle(QFrame::Box | QFrame::Plain); + popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + popup->header()->hide(); + popup->installEventFilter(this); + popup->setMouseTracking(true); + + connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*, int)), + SLOT(doneCompletion())); + + popup->setWindowFlags(Qt::Popup); + popup->setFocusPolicy(Qt::NoFocus); + popup->setFocusProxy(parent); + + timer = new QTimer(this); + timer->setSingleShot(true); + timer->setInterval(500); + connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); + connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start())); + + connect(&networkManager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(handleNetworkData(QNetworkReply*))); + +} + +GSuggestCompletion::~GSuggestCompletion() +{ + delete popup; +} + +bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) +{ + if (obj != popup) + return false; + + if (ev->type() == QEvent::MouseButtonPress) { + popup->hide(); + editor->setFocus(); + return true; + } + + if (ev->type() == QEvent::KeyPress) { + + bool consumed = false; + int key = static_cast(ev)->key(); + switch (key) { + case Qt::Key_Enter: + case Qt::Key_Return: + doneCompletion(); + consumed = true; + + case Qt::Key_Escape: + editor->setFocus(); + popup->hide(); + consumed = true; + + case Qt::Key_Up: + case Qt::Key_Down: + case Qt::Key_Home: + case Qt::Key_End: + case Qt::Key_PageUp: + case Qt::Key_PageDown: + break; + + default: + editor->setFocus(); + editor->event(ev); + popup->hide(); + break; + } + + return consumed; + } + + return false; +} + +void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits) +{ + + if (choices.isEmpty() || choices.count() != hits.count()) + return; + + const QPalette &pal = editor->palette(); + QColor color = pal.color(QPalette::Disabled, QPalette::WindowText); + + popup->setUpdatesEnabled(false); + popup->clear(); + for (int i = 0; i < choices.count(); ++i) { + QTreeWidgetItem * item; + item = new QTreeWidgetItem(popup); + item->setText(0, choices[i]); + item->setText(1, hits[i]); + item->setTextAlignment(1, Qt::AlignRight); + item->setTextColor(1, color); + } + popup->setCurrentItem(popup->topLevelItem(0)); + popup->resizeColumnToContents(0); + popup->resizeColumnToContents(1); + popup->adjustSize(); + popup->setUpdatesEnabled(true); + + int h = popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3; + popup->resize(popup->width(), h); + + popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); + popup->setFocus(); + popup->show(); +} + +void GSuggestCompletion::doneCompletion() +{ + timer->stop(); + popup->hide(); + editor->setFocus(); + QTreeWidgetItem *item = popup->currentItem(); + if (item) { + editor->setText(item->text(0)); + QKeyEvent *e; + e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); + QApplication::postEvent(editor, e); + e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier); + QApplication::postEvent(editor, e); + } +} + +void GSuggestCompletion::preventSuggest() +{ + timer->stop(); +} + +void GSuggestCompletion::autoSuggest() +{ + QString str = editor->text(); + QString url = QString(GSUGGEST_URL).arg(str); + networkManager.get(QNetworkRequest(QString(url))); +} + +void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) +{ + QUrl url = networkReply->url(); + if (!networkReply->error()) { + QStringList choices; + QStringList hits; + + QString response(networkReply->readAll()); + QXmlStreamReader xml(response); + while (!xml.atEnd()) { + xml.readNext(); + if (xml.tokenType() == QXmlStreamReader::StartElement) + if (xml.name() == "suggestion") { + QStringRef str = xml.attributes().value("data"); + choices << str.toString(); + } + if (xml.tokenType() == QXmlStreamReader::StartElement) + if (xml.name() == "num_queries") { + QStringRef str = xml.attributes().value("int"); + hits << str.toString(); + } + } + + showCompletion(choices, hits); + } + + networkReply->deleteLater(); +} diff --git a/examples/network/googlesuggest/googlesuggest.h b/examples/network/googlesuggest/googlesuggest.h new file mode 100644 index 0000000..0267709 --- /dev/null +++ b/examples/network/googlesuggest/googlesuggest.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GOOGLESUGGEST_H +#define GOOGLESUGGEST_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QLineEdit; +class QNetworkReply; +class QTimer; +class QTreeWidget; +QT_END_NAMESPACE + +class GSuggestCompletion : public QObject +{ + Q_OBJECT + +public: + GSuggestCompletion(QLineEdit *parent = 0); + ~GSuggestCompletion(); + bool eventFilter(QObject *obj, QEvent *ev); + void showCompletion(const QStringList &choices, const QStringList &hits); + +public slots: + + void doneCompletion(); + void preventSuggest(); + void autoSuggest(); + void handleNetworkData(QNetworkReply *networkReply); + +private: + QLineEdit *editor; + QTreeWidget *popup; + QTimer *timer; + QNetworkAccessManager networkManager; +}; + +#endif // GOOGLESUGGEST_H + diff --git a/examples/network/googlesuggest/googlesuggest.pro b/examples/network/googlesuggest/googlesuggest.pro new file mode 100644 index 0000000..afd600f --- /dev/null +++ b/examples/network/googlesuggest/googlesuggest.pro @@ -0,0 +1,9 @@ +QT += network +SOURCES = main.cpp searchbox.cpp googlesuggest.cpp +HEADERS = searchbox.h googlesuggest.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/webkit/googlesuggest +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/webkit/googlesuggest +INSTALLS += target sources diff --git a/examples/network/googlesuggest/main.cpp b/examples/network/googlesuggest/main.cpp new file mode 100644 index 0000000..f8d9bc5 --- /dev/null +++ b/examples/network/googlesuggest/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "searchbox.h" + +int main(int argc, char * argv[]) +{ + QApplication app(argc, argv); + SearchBox *searchEdit = new SearchBox; + searchEdit->show(); + return app.exec(); +} diff --git a/examples/network/googlesuggest/searchbox.cpp b/examples/network/googlesuggest/searchbox.cpp new file mode 100644 index 0000000..cd7b122 --- /dev/null +++ b/examples/network/googlesuggest/searchbox.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "searchbox.h" +#include "googlesuggest.h" + +#define GSEARCH_URL "http://www.google.com/search?q=%1" + + +SearchBox::SearchBox(QWidget *parent): QLineEdit(parent) +{ + completer = new GSuggestCompletion(this); + + connect(this, SIGNAL(returnPressed()), SLOT(doSearch())); + + setWindowTitle("Search with Google"); + + adjustSize(); + resize(400, height()); + setFocus(); +} + +void SearchBox::doSearch() +{ + completer->preventSuggest(); + QString url = QString(GSEARCH_URL).arg(text()); + QDesktopServices::openUrl(QUrl(url)); +} diff --git a/examples/network/googlesuggest/searchbox.h b/examples/network/googlesuggest/searchbox.h new file mode 100644 index 0000000..75ceffd --- /dev/null +++ b/examples/network/googlesuggest/searchbox.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SEARCHBOX_H +#define SEARCHBOX_H + +#include + +class GSuggestCompletion; + +class SearchBox: public QLineEdit +{ + Q_OBJECT + +public: + SearchBox(QWidget *parent = 0); + +protected slots: + void doSearch(); + +private: + GSuggestCompletion *completer; +}; + + +#endif // SEARCHBOX_H diff --git a/examples/network/network.pro b/examples/network/network.pro index 13b3116..8c45745 100644 --- a/examples/network/network.pro +++ b/examples/network/network.pro @@ -11,6 +11,7 @@ SUBDIRS = blockingfortuneclient \ http \ loopback \ threadedfortuneserver \ + googlesuggest \ torrent contains(QT_CONFIG, openssl):SUBDIRS += securesocketclient -- cgit v0.12