aboutsummaryrefslogtreecommitdiffstats
path: root/examples/core/permissions/Main.qml
blob: 5d2aeaa16fd2d2be88d304a81758e0831d44d081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtCore
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

Window {
    id: window
    visible: true
    title: qsTr("Qt QML Permissions")

    readonly property int margin: 11

    width: layout.implicitWidth + 2 * margin
    height: layout.implicitHeight + 2 * margin

    Rectangle {
        anchors.fill: parent
        color: locationPermission.status === Qt.Granted ? "green" :
               locationPermission.status === Qt.Denied ? "red" : "blue"

        LocationPermission {
            id: locationPermission

            // If any of the properties of the permission are changed,
            // checkPermision is used to check, and the status will
            // update accordingly. For example, upgrading from Approximate
            // to Precise would either result in Undetermined, if we
            // can still request Precise, or Denied, if we already did,
            // or it's not possible to upgrade the permission.
            accuracy: accuracyCombo.currentValue === qsTr("Precise") ? LocationPermission.Precise
                                                                     : LocationPermission.Approximate
            availability: availabilityCombo.currentValue === qsTr("Always") ? LocationPermission.Always
                                                                            : LocationPermission.WhenInUse
            // However, the permission will not be affected by other
            // permissions of the same type. In other words, a more
            // extensive permission being granted does not reflect
            // in this permission. Nor does losing an already granted
            // permission, for example due to the user updating their
            // system settings.
            onStatusChanged: console.log("Status: " + status)
            onAccuracyChanged: console.log("Accuracy: " + accuracy)
            onAvailabilityChanged: console.log("Availability: " + availability)
        }

        ColumnLayout {
            id: layout

            anchors.fill: parent
            anchors.margins: window.margin

            Text {
                readonly property string statusAsString: locationPermission.status === Qt.Granted ? qsTr("Granted") :
                                                         locationPermission.status === Qt.Undetermined ? qsTr("Undetermined")
                                                                                                       : qsTr("Denied")
                text: qsTr(`Location services, status: ${statusAsString}`)

                font.bold: true
                font.pointSize: 16
                color: locationPermission.status === Qt.Undetermined ? "white" : "black"

                Layout.alignment: Qt.AlignHCenter
            }

            RowLayout {
                ComboBox {
                    id: accuracyCombo

                    model: ["Precise", "Approximate"]
                    implicitContentWidthPolicy: ComboBox.WidestText
                    Layout.fillWidth: true
                }
                ComboBox {
                    id: availabilityCombo

                    model: ["Always", "When in use"]

                    implicitContentWidthPolicy: ComboBox.WidestText
                    Layout.fillWidth: true
                }
            }

            Button {
                text: qsTr("Request location permissions")
                enabled: locationPermission.status !== Qt.Denied
                Layout.alignment: Qt.AlignHCenter
                Layout.fillWidth: false

                onClicked: locationPermission.request()
            }
        }

        Component.onCompleted: {
            console.log(locationPermission.status)
        }
    }
}