From 82547509149fe96da6fc808b67df8ffc4d40dce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Tue, 18 Apr 2017 17:37:02 +0200 Subject: Create example for geolocation in WebEngine Task-number: QTBUG-59822 Change-Id: Iee69f10fefa223313ca112f096b65446b4156ac1 Reviewed-by: Joerg Bornemann --- .../maps/doc/images/maps-example.png | Bin 0 -> 47821 bytes examples/webenginewidgets/maps/doc/src/maps.qdoc | 98 +++++++++++++++++++++ examples/webenginewidgets/maps/main.cpp | 52 +++++++++++ examples/webenginewidgets/maps/mainwindow.cpp | 73 +++++++++++++++ examples/webenginewidgets/maps/mainwindow.h | 56 ++++++++++++ examples/webenginewidgets/maps/maps.pro | 12 +++ examples/webenginewidgets/webenginewidgets.pro | 2 + 7 files changed, 293 insertions(+) create mode 100644 examples/webenginewidgets/maps/doc/images/maps-example.png create mode 100644 examples/webenginewidgets/maps/doc/src/maps.qdoc create mode 100644 examples/webenginewidgets/maps/main.cpp create mode 100644 examples/webenginewidgets/maps/mainwindow.cpp create mode 100644 examples/webenginewidgets/maps/mainwindow.h create mode 100644 examples/webenginewidgets/maps/maps.pro (limited to 'examples/webenginewidgets') diff --git a/examples/webenginewidgets/maps/doc/images/maps-example.png b/examples/webenginewidgets/maps/doc/images/maps-example.png new file mode 100644 index 000000000..4f699f521 Binary files /dev/null and b/examples/webenginewidgets/maps/doc/images/maps-example.png differ diff --git a/examples/webenginewidgets/maps/doc/src/maps.qdoc b/examples/webenginewidgets/maps/doc/src/maps.qdoc new file mode 100644 index 000000000..1082cd1e2 --- /dev/null +++ b/examples/webenginewidgets/maps/doc/src/maps.qdoc @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webenginewidgets/maps + \title WebEngine Widgets Maps Example + \ingroup webengine-widgetexamples + \brief Demonstrates how to handle geolocation requests + + \image maps-example.png + + \e {Maps} demonstrates how to handle geolocation requests originating from + a \l QWebEnginePage. + + The \l {https://www.w3.org/TR/geolocation-API/}{Geolocation API} is a + JavaScript API that web applications can use to determine the user's + physical location to show on a map, for example. As Qt WebEngine relies on + \l {Qt Location} to power this API a viable location backend is needed for + the target platform. + + To avoid accidentally sending location information to third parties + geolocation requests are denied by default. This example demonstrates the + steps an application must take in order to start accepting these requests. + + \include examples-run.qdocinc + + \section1 The Code + + The example program consists of a single class, \c MainWindow, inheriting + from \l QMainWindow: + + \quotefromfile webenginewidgets/maps/mainwindow.h + \skipto #include + \printuntil /^\}/ + + In the constructor we first set up the \l QWebEngineView as the central + widget: + + \quotefromfile webenginewidgets/maps/mainwindow.cpp + \skipto MainWindow::MainWindow + \printuntil setCentralWidget + + We then proceed to connect a lambda function to the \l + QWebEnginePage::featurePermissionRequested signal: + + \skipto m_view->page() + \printuntil QWebEnginePage::Feature + + This signal is emitted whenever a web page requests to make use of a certain + feature or device, including not only location services but also audio + capture devices or mouse locking, for example. In this example we only + handle requests for location services: + + \printuntil return + + Now comes the part where we actually ask the user for permission: + + \printuntil securityOrigin + \printuntil }); + + Note that the question includes the host component of the web site's URI (\c + securityOrigin) to inform the user as to exactly which web site will be + receiving their location data. + + We use the \l QWebEnginePage::setFeaturePermission method to communicate the + user's answer back to the web page. + + Finally we ask the \l QWebEnginePage to load the web page that might want to + use location services: + + \printuntil /^\}/ + + \sa {html5-geolocation}{Qt WebEngine HTML5 Geolocation}, {Qt Location} +*/ diff --git a/examples/webenginewidgets/maps/main.cpp b/examples/webenginewidgets/maps/main.cpp new file mode 100644 index 000000000..c2d711106 --- /dev/null +++ b/examples/webenginewidgets/maps/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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$ +** 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 "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication app(argc, argv); + + MainWindow mainWindow; + mainWindow.show(); + + return app.exec(); +} diff --git a/examples/webenginewidgets/maps/mainwindow.cpp b/examples/webenginewidgets/maps/mainwindow.cpp new file mode 100644 index 000000000..f548e67c2 --- /dev/null +++ b/examples/webenginewidgets/maps/mainwindow.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** 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$ +** 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 "mainwindow.h" + +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , m_view(new QWebEngineView(this)) +{ + setCentralWidget(m_view); + + QWebEnginePage *page = m_view->page(); + + connect(page, &QWebEnginePage::featurePermissionRequested, + [this, page](const QUrl &securityOrigin, QWebEnginePage::Feature feature) { + if (feature != QWebEnginePage::Geolocation) + return; + + QMessageBox msgBox(this); + msgBox.setText(tr("%1 wants to know your location").arg(securityOrigin.host())); + msgBox.setInformativeText(tr("Do you want to send your current location to this website?")); + msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + msgBox.setDefaultButton(QMessageBox::Yes); + + if (msgBox.exec() == QMessageBox::Yes) { + page->setFeaturePermission( + securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser); + } else { + page->setFeaturePermission( + securityOrigin, feature, QWebEnginePage::PermissionDeniedByUser); + } + }); + + page->load(QUrl(QStringLiteral("https://maps.google.com"))); +} diff --git a/examples/webenginewidgets/maps/mainwindow.h b/examples/webenginewidgets/maps/mainwindow.h new file mode 100644 index 000000000..c4377caaf --- /dev/null +++ b/examples/webenginewidgets/maps/mainwindow.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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$ +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + explicit MainWindow(QWidget *parent = nullptr); + +private: + QWebEngineView *m_view; +}; + +#endif // MAINWINDOW_H diff --git a/examples/webenginewidgets/maps/maps.pro b/examples/webenginewidgets/maps/maps.pro new file mode 100644 index 000000000..3fee49077 --- /dev/null +++ b/examples/webenginewidgets/maps/maps.pro @@ -0,0 +1,12 @@ +TEMPLATE = app + +QT += webenginewidgets + +HEADERS += \ + mainwindow.h + +SOURCES += main.cpp \ + mainwindow.cpp + +target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/maps +INSTALLS += target diff --git a/examples/webenginewidgets/webenginewidgets.pro b/examples/webenginewidgets/webenginewidgets.pro index 41582bbf7..78db442b9 100644 --- a/examples/webenginewidgets/webenginewidgets.pro +++ b/examples/webenginewidgets/webenginewidgets.pro @@ -9,6 +9,8 @@ SUBDIRS += \ simplebrowser \ videoplayer +qtHaveModule(positioning): SUBDIRS += maps + contains(WEBENGINE_CONFIG, use_spellchecker):!cross_compile { !contains(WEBENGINE_CONFIG, use_native_spellchecker) { SUBDIRS += spellchecker -- cgit v1.2.3