diff options
Diffstat (limited to 'examples/network/googlesuggest')
-rw-r--r-- | examples/network/googlesuggest/CMakeLists.txt | 43 | ||||
-rw-r--r-- | examples/network/googlesuggest/googlesuggest.cpp | 231 | ||||
-rw-r--r-- | examples/network/googlesuggest/googlesuggest.h | 84 | ||||
-rw-r--r-- | examples/network/googlesuggest/googlesuggest.pro | 8 | ||||
-rw-r--r-- | examples/network/googlesuggest/main.cpp | 61 | ||||
-rw-r--r-- | examples/network/googlesuggest/searchbox.cpp | 82 | ||||
-rw-r--r-- | examples/network/googlesuggest/searchbox.h | 75 |
7 files changed, 0 insertions, 584 deletions
diff --git a/examples/network/googlesuggest/CMakeLists.txt b/examples/network/googlesuggest/CMakeLists.txt deleted file mode 100644 index ade5b85f2d..0000000000 --- a/examples/network/googlesuggest/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Generated from googlesuggest.pro. - -cmake_minimum_required(VERSION 3.14) -project(googlesuggest LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/network/googlesuggest") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Network) -find_package(Qt6 COMPONENTS Widgets) - -qt_add_executable(googlesuggest - googlesuggest.cpp googlesuggest.h - main.cpp - searchbox.cpp searchbox.h -) -set_target_properties(googlesuggest PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(googlesuggest PUBLIC - Qt::Core - Qt::Gui - Qt::Network - Qt::Widgets -) - -install(TARGETS googlesuggest - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/network/googlesuggest/googlesuggest.cpp b/examples/network/googlesuggest/googlesuggest.cpp deleted file mode 100644 index b7b6d3ab62..0000000000 --- a/examples/network/googlesuggest/googlesuggest.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "googlesuggest.h" - -//! [1] -const QString gsuggestUrl(QStringLiteral("http://google.com/complete/search?output=toolbar&q=%1")); -//! [1] - -//! [2] -GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent) -{ - popup = new QTreeWidget; - popup->setWindowFlags(Qt::Popup); - popup->setFocusPolicy(Qt::NoFocus); - popup->setFocusProxy(parent); - popup->setMouseTracking(true); - - popup->setColumnCount(1); - 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); - - connect(popup, &QTreeWidget::itemClicked, - this, &GSuggestCompletion::doneCompletion); - - timer.setSingleShot(true); - timer.setInterval(500); - connect(&timer, &QTimer::timeout, - this, &GSuggestCompletion::autoSuggest); - connect(editor, &QLineEdit::textEdited, - &timer, QOverload<>::of(&QTimer::start)); - - connect(&networkManager, &QNetworkAccessManager::finished, - this, &GSuggestCompletion::handleNetworkData); - -} -//! [2] - -//! [3] -GSuggestCompletion::~GSuggestCompletion() -{ - delete popup; -} -//! [3] - -//! [4] -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<QKeyEvent*>(ev)->key(); - switch (key) { - case Qt::Key_Enter: - case Qt::Key_Return: - doneCompletion(); - consumed = true; - break; - - case Qt::Key_Escape: - editor->setFocus(); - popup->hide(); - consumed = true; - break; - - 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; -} -//! [4] - -//! [5] -void GSuggestCompletion::showCompletion(const QList<QString> &choices) -{ - if (choices.isEmpty()) - return; - - const QPalette &pal = editor->palette(); - QColor color = pal.color(QPalette::Disabled, QPalette::WindowText); - - popup->setUpdatesEnabled(false); - popup->clear(); - - for (const auto &choice : choices) { - auto item = new QTreeWidgetItem(popup); - item->setText(0, choice); - item->setForeground(0, color); - } - - popup->setCurrentItem(popup->topLevelItem(0)); - popup->resizeColumnToContents(0); - popup->setUpdatesEnabled(true); - - popup->move(editor->mapToGlobal(QPoint(0, editor->height()))); - popup->setFocus(); - popup->show(); -} -//! [5] - -//! [6] -void GSuggestCompletion::doneCompletion() -{ - timer.stop(); - popup->hide(); - editor->setFocus(); - QTreeWidgetItem *item = popup->currentItem(); - if (item) { - editor->setText(item->text(0)); - QMetaObject::invokeMethod(editor, "returnPressed"); - } -} -//! [6] - -//! [7] -void GSuggestCompletion::autoSuggest() -{ - QString str = editor->text(); - QString url = gsuggestUrl.arg(str); - networkManager.get(QNetworkRequest(url)); -} -//! [7] - -//! [8] -void GSuggestCompletion::preventSuggest() -{ - timer.stop(); -} -//! [8] - -//! [9] -void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) -{ - QUrl url = networkReply->url(); - if (networkReply->error() == QNetworkReply::NoError) { - QList<QString> choices; - - QByteArray response(networkReply->readAll()); - QXmlStreamReader xml(response); - while (!xml.atEnd()) { - xml.readNext(); - if (xml.tokenType() == QXmlStreamReader::StartElement) - if (xml.name() == u"suggestion") { - auto str = xml.attributes().value("data"); - choices << str.toString(); - } - } - - showCompletion(choices); - } - - networkReply->deleteLater(); -} -//! [9] diff --git a/examples/network/googlesuggest/googlesuggest.h b/examples/network/googlesuggest/googlesuggest.h deleted file mode 100644 index 909386569b..0000000000 --- a/examples/network/googlesuggest/googlesuggest.h +++ /dev/null @@ -1,84 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef GOOGLESUGGEST_H -#define GOOGLESUGGEST_H - -#include <QtWidgets> -#include <QtNetwork> -#include <QtCore> - -//! [1] -class GSuggestCompletion : public QObject -{ - Q_OBJECT - -public: - explicit GSuggestCompletion(QLineEdit *parent = nullptr); - ~GSuggestCompletion(); - bool eventFilter(QObject *obj, QEvent *ev) override; - void showCompletion(const QList<QString> &choices); - -public slots: - - void doneCompletion(); - void preventSuggest(); - void autoSuggest(); - void handleNetworkData(QNetworkReply *networkReply); - -private: - QLineEdit *editor = nullptr; - QTreeWidget *popup = nullptr; - QTimer timer; - QNetworkAccessManager networkManager; -}; -//! [1] -#endif // GOOGLESUGGEST_H - diff --git a/examples/network/googlesuggest/googlesuggest.pro b/examples/network/googlesuggest/googlesuggest.pro deleted file mode 100644 index 6e73906303..0000000000 --- a/examples/network/googlesuggest/googlesuggest.pro +++ /dev/null @@ -1,8 +0,0 @@ -QT += network widgets -requires(qtConfig(itemviews)) -SOURCES = main.cpp searchbox.cpp googlesuggest.cpp -HEADERS = searchbox.h googlesuggest.h - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/network/googlesuggest -INSTALLS += target diff --git a/examples/network/googlesuggest/main.cpp b/examples/network/googlesuggest/main.cpp deleted file mode 100644 index ab819c5502..0000000000 --- a/examples/network/googlesuggest/main.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QApplication> - -#include "searchbox.h" - -int main(int argc, char * argv[]) -{ - QApplication app(argc, argv); - SearchBox searchEdit; - searchEdit.show(); - return app.exec(); -} diff --git a/examples/network/googlesuggest/searchbox.cpp b/examples/network/googlesuggest/searchbox.cpp deleted file mode 100644 index 9057c1ccdf..0000000000 --- a/examples/network/googlesuggest/searchbox.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QDesktopServices> -#include <QUrl> - -#include "searchbox.h" -#include "googlesuggest.h" - -const QString gsearchUrl = QStringLiteral("http://www.google.com/search?q=%1"); - -//! [1] -SearchBox::SearchBox(QWidget *parent) - : QLineEdit(parent) - , completer(new GSuggestCompletion(this)) -{ - connect(this, &SearchBox::returnPressed, this, &SearchBox::doSearch); - - setWindowTitle("Search with Google"); - - adjustSize(); - resize(400, height()); - setFocus(); -} -//! [1] - -//! [2] -void SearchBox::doSearch() -{ - completer->preventSuggest(); - QString url = gsearchUrl.arg(text()); - QDesktopServices::openUrl(url); -} -//! [2] - diff --git a/examples/network/googlesuggest/searchbox.h b/examples/network/googlesuggest/searchbox.h deleted file mode 100644 index fbd33011b7..0000000000 --- a/examples/network/googlesuggest/searchbox.h +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef SEARCHBOX_H -#define SEARCHBOX_H - -//! [1] -#include <QLineEdit> - -class GSuggestCompletion; - -class SearchBox: public QLineEdit -{ - Q_OBJECT - -public: - explicit SearchBox(QWidget *parent = nullptr); - -protected slots: - void doSearch(); - -private: - GSuggestCompletion *completer = nullptr; -//! [1] -}; - - -#endif // SEARCHBOX_H |