Debian Codegraph @

src/app/qml/components/CurrentLocation.qml

raw
1 /*
2 * Copyright (C) 2015 Canonical Ltd.
3 *
4 * This file is part of Lomiri Weather App
5 *
6 * Lomiri Weather App is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3 as
8 * published by the Free Software Foundation.
9 *
10 * Lomiri Weather App is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 import QtLocation 5.12 //for geocoding
20 import QtPositioning 5.12 //for coordinate query
21 import QtQuick 2.12
22 import Lomiri.Components 1.3
23 import "../data/WeatherApi.js" as WeatherApi
24
25
26 Item {
27 id: currentLocation
28
29 property string string: "Undefined"
30
31 PositionSource {
32 id: currentPosition
33 updateInterval: 1000
34 active: settings.detectCurrentLocation
35
36 onPositionChanged: {
37 var coord = currentPosition.position.coordinate
38 if (coord.isValid) {
39 geocodeModel.query = coord
40 geocodeModel.update()
41 }
42 }
43 }
44
45 Plugin {
46 id: osmPlugin
47 name: "osm"
48
49 // Set a useragent so that osm can uniquely identify out requests
50 PluginParameter {
51 name: "osm.useragent"
52 value: "lomiri-weather-app"
53 }
54 }
55
56 GeocodeModel {
57 id: geocodeModel
58 autoUpdate: false
59 plugin: osmPlugin
60
61 onCountChanged: {
62 // Update the currentLocation if one is found and it does not match the stored location
63 // if there is a location request from url on startup, we ignore this first a few times when signal is triggered
64 // to avoid view position to current location. It works after requestLocationByUrl was set to undefined
65 if (weatherApp.requestLocationByUrl === undefined
66 && count > 0 && currentLocation.string !== geocodeModel.get(0).address.city) {
67 search();
68 }
69 }
70
71 function search() {
72 if (count > 0) {
73 var loc = geocodeModel.get(0);
74 currentLocation.string = loc.address.city;
75 searchForLocation("searchByPoint", loc.coordinate.latitude, loc.coordinate.longitude);
76 }
77 }
78 }
79
80 Connections {
81 target: settings
82 onDetectCurrentLocationChanged: {
83 if (settings.detectCurrentLocation) {
84 geocodeModel.search();
85 }
86 }
87 }
88 }