aboutsummaryrefslogtreecommitdiffstats
path: root/apps/com.pelagicore.map/stores/MapStore.qml
blob: 5c35d6fe2946f64b92ebe18bfd9f96187b1b5bb0 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune 3 UI.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

import QtQuick 2.8
import QtApplicationManager.Application 2.0
import QtPositioning 5.9
import QtLocation 5.9
import Qt.labs.platform 1.0
import shared.utils 1.0

QtObject {
    id: root

    readonly property alias model: routeModel
    property string routeDistance
    property string routeTime
    property string homeRouteTime
    property string workRouteTime
    property bool searchViewEnabled: false
    property bool offlineMapsEnabled: false
    onOfflineMapsEnabledChanged: {
        //Fetch again here as this usually changes to false after
        //the getAvailableMapsAndLocation() function is executed.
        if (!root.offlineMapsEnabled) {
            fetchCurrentLocation();
        }
    }

    property var positionCoordinate: QtPositioning.coordinate(48.135771, 11.574052) // Munich
    property var originalPosition: positionCoordinate
    readonly property string defaultLightThemeId: "mapbox://styles/qtauto/cjcm1by3q12dk2sqnquu0gju9"
    readonly property string defaultDarkThemeId: "mapbox://styles/qtauto/cjcm1czb812co2sno1ypmp1r8"

    property var currentLocationCoord
    property var homeCoord
    property var workCoord

    property var startCoord: QtPositioning.coordinate()
    property var destCoord: QtPositioning.coordinate()
    property string destination

    property alias navigationDemoActive: navigationStore.active
    readonly property var navigationStore: NavigationStore {
        id: navigationStore
        model: root.routeModel
    }

    property var appInterface: Connections {
        target: ApplicationInterface
        onOpenDocument: {
            var request = documentUrl.slice(8, documentUrl.length);
            var dest = "";
            if (request.indexOf("getmeto/") >= 0) {
                dest = request.slice(8, request.length);
                root.destination = dest;
                requestGeoCodeModel.reset();
                requestGeoCodeModel.query = dest;
                requestGeoCodeModel.update();
            }
        }
    }

    property GeocodeModel requestGeoCodeModel: GeocodeModel {
        plugin: herePlugin
        limit: 20
        onCountChanged: {
            if (count > 0) {
                root.requestNavigationReceived(get(0).address.text, get(0).coordinate, get(0).boundingBox);
            }
        }
    }

    readonly property Plugin herePlugin: Plugin {
        name: "here";
        locales: Config.languageLocale
        PluginParameter { name: "here.app_id"; value: "g98JIvGyhYloA79p8e7O" }
        PluginParameter { name: "here.token"; value: "KBpimsQlfj7yTILmIr1-Pw" }
    }

    signal requestNavigationReceived(string address, var coord, var boundingBox)

    function fetchCurrentLocation() { // PositionSource doesn't work on Linux
        var req = new XMLHttpRequest;
        req.onreadystatechange = function() {
            if (req.readyState === XMLHttpRequest.DONE) {
                var objectArray = JSON.parse(req.responseText);
                if (objectArray.errors !== undefined) {
                    console.warn("Error fetching location:", objectArray.errors[0].message);
                } else {
                    root.positionCoordinate = QtPositioning.coordinate(objectArray.location.lat, objectArray.location.lng);
                    console.info("Current location:", root.positionCoordinate);
                }
            }
        }
        req.open("GET", "https://location.services.mozilla.com/v1/geolocate?key=geoclue");
        req.send();
    }

    function getAvailableMapsAndLocation(mapReady, supportedMapTypes) {
        if (mapReady) {
            mapTypeModel.clear();
            console.info("Supported map types:");
            for (var i = 0; i < supportedMapTypes.length; i++) {
                var map = supportedMapTypes[i];
                mapTypeModel.append({"name": map.name, "data": map}) // fill the map type model
                console.info("\t", map.name, ", description:", map.description, ", style:", map.style, ", night mode:", map.night);
            }
            if (!root.offlineMapsEnabled) {
                fetchCurrentLocation();
            }
        }
    }

    function getMapType(mapBoxPanelReady, name) {
        if (!mapBoxPanelReady || !mapTypeModel.count) {
            return
        }
        for (var i = 0; i < mapTypeModel.count; i++) {
            var map = mapTypeModel.get(i);
            if (map && map.name === name) {
                return map.data;
            }
        }
    }

    function formatMeters(meters) {
        return qsTr("%n kilometer(s)", "", meters/1000)
    }

    function formatSeconds(seconds) {
        const numdays = Math.floor((seconds % 31536000) / 86400)
        const numhours = Math.floor(((seconds % 31536000) % 86400) / 3600)
        const numminutes = Math.ceil((((seconds % 31536000) % 86400) % 3600) / 60)

        var result = "";
        if (numdays > 0)
            result += qsTr("%n day(s)", "", numdays) + ", ";
        if (numhours > 0)
            result += qsTr("%n hour(s)", "", numhours) + ", ";
        if (numminutes > 0)
            result += qsTr("%n minute(s)", "", numminutes);

        return result;
    }

    // lists the various map styles (including the custom ones); filled in Map.onMapReadyChanged
    readonly property ListModel mapTypeModel: ListModel { }

    readonly property Plugin mapPlugin: Plugin {
        preferred: ["mapboxgl", "osm"]
        locales: Config.languageLocale

        readonly property string cacheDirUrl: StandardPaths.writableLocation(StandardPaths.CacheLocation);

        // OSM Plugin Parameters
        PluginParameter { name: "osm.useragent"; value: "Neptune UI" }

        // Mapbox Plugin Parameters
        PluginParameter {
            name: "mapboxgl.access_token"
            value: "pk.eyJ1IjoicXRhdXRvIiwiYSI6ImNqY20wbDZidzBvcTQyd3J3NDlkZ21jdjUifQ.4KYDlP7UmQEVPYffr6VuVQ"
        }
        PluginParameter {
            name: "mapboxgl.mapping.additional_style_urls"
            value: [root.defaultLightThemeId, root.defaultDarkThemeId].join(",")
        }

        // Offline maps support
        PluginParameter {
            name: "mapboxgl.mapping.cache.size";
            value: "50 MiB"
        }
        PluginParameter {
            name: "mapboxgl.mapping.cache.directory";
            value: mapPlugin.cacheDirUrl.toString().substring(mapPlugin.cacheDirUrl.indexOf(':')+1)
        }
    }

    readonly property GeocodeModel geocodeModel: GeocodeModel {
        plugin: herePlugin
        onStatusChanged: {
            if (status === GeocodeModel.Null) {
                console.info("Search model idle");
            } else if (status === GeocodeModel.Ready) {
                console.info("Search model ready, results:", count)
            } else if (status === GeocodeModel.Loading) {
                console.info("Search model busy");
            } else if (status === GeocodeModel.Error) {
                console.warn("Search model error:", error, errorString);
            }
        }
        limit: 20
    }

    readonly property RouteModel routeModel: RouteModel {
        id: routeModel
        autoUpdate: !!root.startCoord && !!root.destCoord
        query: RouteQuery {
            waypoints: [root.startCoord, root.destCoord]
        }
        plugin: herePlugin

        onStatusChanged: {
            if (status === RouteModel.Null) {
                console.info("Route model idle");
            } else if (status === RouteModel.Ready) {
                console.info("Route model ready, results:", count)
                if (count > 0) {
                    root.routeDistance = formatMeters(get(0).distance);
                    root.routeTime = formatSeconds(get(0).travelTime);
                    console.info("Route distance (km):", root.routeDistance
                                 , ", time:", root.routeTime);
                    console.info("First coord:", get(0).segments[0].path[0])
                }
            } else if (status === RouteModel.Loading) {
                console.info("Route model busy");
            } else if (status === RouteModel.Error) {
                console.warn("Route model error:", error, errorString);
            }
        }
    }

    readonly property RouteModel homeRouteModel: RouteModel {
        autoUpdate: !!root.currentLocationCoord && !!root.homeCoord
        query: RouteQuery {
            waypoints: [root.currentLocationCoord, root.homeCoord]
        }
        plugin: herePlugin

        onStatusChanged: {
            if (status === RouteModel.Ready) {
                if (count > 0) {
                    root.homeRouteTime = formatSeconds(get(0).travelTime);
                    console.info("Home route distance (km):", formatMeters(get(0).distance), ", time:", root.homeRouteTime)
                }
            }
        }
    }

    readonly property RouteModel workRouteModel: RouteModel {
        autoUpdate: !!root.currentLocationCoord && !!root.workCoord
        query: RouteQuery {
            waypoints: [root.currentLocationCoord, root.workCoord]
        }
        plugin: herePlugin
        onStatusChanged: {
            if (status === RouteModel.Ready) {
                if (count > 0) {
                    root.workRouteTime = formatSeconds(get(0).travelTime);
                    console.info("Work route distance (km):", formatMeters(get(0).distance), ", time:", root.workRouteTime)
                }
            }
        }
    }

    property Timer notificationTimer: Timer {
        id: notificationTimer
        interval: 3000
        running: false
        onTriggered: showOfflineMapInfo()
    }

    function showOfflineNotification() {
        var notification = ApplicationInterface.createNotification();
        notification.summary = qsTr("Offline mode");
        notification.body = qsTr("Search and navigation are not available in offline mode");
        notification.sticky = true;
        notification.show();
        notificationTimer.start();
    }

    function showOfflineMapInfo() {
        var notification = ApplicationInterface.createNotification();
        notification.summary = qsTr("Offline map");
        notification.body = qsTr("Offline Map only available in Light Theme");
        notification.sticky = true;
        notification.show();
    }

    function showOnlineNotification() {
        var notification = ApplicationInterface.createNotification();
        notification.summary = qsTr("Online map");
        notification.body = qsTr("You are now using online map from Mapbox server");
        notification.sticky = true;
        notification.show();
    }
}