diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-01-22 18:14:27 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-01-26 17:59:44 +0100 |
commit | 0c87f4ad1bd6e540bb936dd7f1fd8c50c6f8f43a (patch) | |
tree | 700a952fd0fbf9e94e0951b86a85f433af69839f | |
parent | 1ac0324b10fac7795824f9055ccbedfd5a0789d7 (diff) |
places_map example: add support for LocationPermission
The example didn't make use of the new permissions API, and, as a
result, it was throwing errors when trying to use PositionSource.
Rework the example to use the LocationPermission API.
The idea is that the premission is checked during the application
startup. If the permission is not granted, a new window is shown. This
window contans some explanations on why the user should enable the
location permission and a "button" to do that.
If the permission is granted, the Loaded is used to load a normal
application page. Otherwise an error message is displayed.
Task-number: QTBUG-121412
Pick-to: 6.7 6.6
Change-Id: I992e485b71b193e4a106cf42aed8c17f57b9fc46
Reviewed-by: MohammadHossein Qanbari <mohammad.qanbari@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
-rw-r--r-- | examples/location/places_map/CMakeLists.txt | 4 | ||||
-rw-r--r-- | examples/location/places_map/Main.qml | 34 | ||||
-rw-r--r-- | examples/location/places_map/PermissionsScreen.qml | 48 | ||||
-rw-r--r-- | examples/location/places_map/PlacesMap.qml (renamed from examples/location/places_map/places_map.qml) | 0 | ||||
-rw-r--r-- | examples/location/places_map/doc/src/places_map.qdoc | 10 | ||||
-rw-r--r-- | examples/location/places_map/main.cpp | 2 | ||||
-rw-r--r-- | examples/location/places_map/places_map.qrc | 4 | ||||
-rw-r--r-- | src/location/doc/src/places.qdoc | 2 |
8 files changed, 95 insertions, 9 deletions
diff --git a/examples/location/places_map/CMakeLists.txt b/examples/location/places_map/CMakeLists.txt index af69ca09..05341372 100644 --- a/examples/location/places_map/CMakeLists.txt +++ b/examples/location/places_map/CMakeLists.txt @@ -38,7 +38,9 @@ target_link_libraries(places_map PUBLIC # Resources: set(places_map_resource_files "marker.png" - "places_map.qml" + "Main.qml" + "PermissionsScreen.qml" + "PlacesMap.qml" ) qt6_add_resources(places_map "places_map" diff --git a/examples/location/places_map/Main.qml b/examples/location/places_map/Main.qml new file mode 100644 index 00000000..9a571d5b --- /dev/null +++ b/examples/location/places_map/Main.qml @@ -0,0 +1,34 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtCore +import QtQuick + +Item { + id: root + anchors.fill: parent + + LocationPermission { + id: permission + accuracy: LocationPermission.Precise + availability: LocationPermission.WhenInUse + } + + PermissionsScreen { + anchors.fill: parent + visible: permission.status !== Qt.PermissionStatus.Granted + requestDenied: permission.status === Qt.PermissionStatus.Denied + onRequestPermission: permission.request() + } + + Component { + id: applicationComponent + PlacesMap {} + } + + Loader { + anchors.fill: parent + active: permission.status === Qt.PermissionStatus.Granted + sourceComponent: applicationComponent + } +} diff --git a/examples/location/places_map/PermissionsScreen.qml b/examples/location/places_map/PermissionsScreen.qml new file mode 100644 index 00000000..a725a8ce --- /dev/null +++ b/examples/location/places_map/PermissionsScreen.qml @@ -0,0 +1,48 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick + +Rectangle { + id: root + + property bool requestDenied: false + + signal requestPermission + + Text { + id: textItem + anchors.centerIn: parent + text: root.requestDenied + ? qsTr("The application cannot run because the Location permission\n" + + "was not granted.\n" + + "Please grant the permission and restart the application.") + : qsTr("The application requires the Location permission to get\n" + + "the position and satellite information.\n" + + "Please press the button to request the permission.") + } + // custom button without importing Quick.Controls + Rectangle { + anchors { + top: textItem.bottom + topMargin: 10 + horizontalCenter: parent.horizontalCenter + } + visible: !root.requestDenied + width: parent.width * 0.8 + height: buttonText.implicitHeight * 2 + border.width: 1 + + Text { + id: buttonText + anchors.fill: parent + text: qsTr("Request Permission") + horizontalAlignment: Qt.AlignHCenter + verticalAlignment: Qt.AlignVCenter + MouseArea { + anchors.fill: parent + onClicked: root.requestPermission() + } + } + } +} diff --git a/examples/location/places_map/places_map.qml b/examples/location/places_map/PlacesMap.qml index da2a611b..da2a611b 100644 --- a/examples/location/places_map/places_map.qml +++ b/examples/location/places_map/PlacesMap.qml diff --git a/examples/location/places_map/doc/src/places_map.qdoc b/examples/location/places_map/doc/src/places_map.qdoc index 32a7d892..d7f10630 100644 --- a/examples/location/places_map/doc/src/places_map.qdoc +++ b/examples/location/places_map/doc/src/places_map.qdoc @@ -21,7 +21,7 @@ To write the QML application that will show places on a map, we start by making the following import declarations. - \snippet places_map/places_map.qml Imports + \snippet places_map/PlacesMap.qml Imports Instantiate a \l Plugin instance. The \l Plugin is effectively the backend from where places are sourced from. Depending on the chosen plugin @@ -29,7 +29,7 @@ \l {Qt Location Open Street Map Plugin}{OSM plugin} is selected which does not have any mandatory parameters. - \snippet places_map/places_map.qml Initialize Plugin + \snippet places_map/PlacesMap.qml Initialize Plugin Next we instantiate a \l PlaceSearchModel which we can use to specify search parameters and perform a places search operation. For illustrative @@ -38,7 +38,7 @@ {PlaceSearchModel::update} {update()} would be invoked in response to a user action such as a button click. - \snippet places_map/places_map.qml PlaceSearchModel + \snippet places_map/PlacesMap.qml PlaceSearchModel The map is displayed by using the \l MapView type and inside we declare the \l MapItemView and supply the search model and a delegate. An inline delegate @@ -48,12 +48,12 @@ other search result types may not have a \e place \l {PlaceSearchModel Roles} {role}. - \snippet places_map/places_map.qml Places MapItemView + \snippet places_map/PlacesMap.qml Places MapItemView Finally, a \c PositionSource is used to reset the map to the curent location and find "food" places in the new area. The position information is updated every 2 minutes and if the new position is more than 500 meters away from the last food search area the place search is retriggered. - \snippet places_map/places_map.qml Current Location + \snippet places_map/PlacesMap.qml Current Location */ diff --git a/examples/location/places_map/main.cpp b/examples/location/places_map/main.cpp index accadbba..5deb86e8 100644 --- a/examples/location/places_map/main.cpp +++ b/examples/location/places_map/main.cpp @@ -8,7 +8,7 @@ int main(int argc, char **argv) { QGuiApplication app(argc,argv); QQuickView view; - view.setSource(QUrl(QStringLiteral("qrc:///places_map.qml"))); + view.setSource(QUrl(QStringLiteral("qrc:///Main.qml"))); view.setWidth(360); view.setHeight(640); view.show(); diff --git a/examples/location/places_map/places_map.qrc b/examples/location/places_map/places_map.qrc index 22395296..3fb2d5d6 100644 --- a/examples/location/places_map/places_map.qrc +++ b/examples/location/places_map/places_map.qrc @@ -1,6 +1,8 @@ <RCC> <qresource prefix="/"> <file>marker.png</file> - <file>places_map.qml</file> + <file>Main.qml</file> + <file>PermissionsScreen.qml</file> + <file>PlacesMap.qml</file> </qresource> </RCC> diff --git a/src/location/doc/src/places.qdoc b/src/location/doc/src/places.qdoc index b7f5d415..e064bc00 100644 --- a/src/location/doc/src/places.qdoc +++ b/src/location/doc/src/places.qdoc @@ -128,7 +128,7 @@ \table \row \li - \snippet places_map/places_map.qml Places MapItemView + \snippet places_map/PlacesMap.qml Places MapItemView \li \inlineimage places_map.png \endtable |