-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.qml
141 lines (117 loc) · 8.79 KB
/
main.qml
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
import QtQuick
import QtQuick.Controls
import org.qfield 1.0
import Theme 1.0
Item {
id: plugin
property var mainWindow: iface.mainWindow()
property var mapSettings: iface.mapCanvas().mapSettings
property var position: iface.findItemByObjectName('positionSource')
property var weatherCodeInformation: JSON.parse('{"0":{"day":{"description":"Sunny","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Clear","image":"http://openweathermap.org/img/wn/[email protected]"}},"1":{"day":{"description":"Mainly Sunny","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Mainly Clear","image":"http://openweathermap.org/img/wn/[email protected]"}},"2":{"day":{"description":"Partly Cloudy","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Partly Cloudy","image":"http://openweathermap.org/img/wn/[email protected]"}},"3":{"day":{"description":"Cloudy","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Cloudy","image":"http://openweathermap.org/img/wn/[email protected]"}},"45":{"day":{"description":"Foggy","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Foggy","image":"http://openweathermap.org/img/wn/[email protected]"}},"48":{"day":{"description":"Rime Fog","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Rime Fog","image":"http://openweathermap.org/img/wn/[email protected]"}},"51":{"day":{"description":"Light Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"}},"53":{"day":{"description":"Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"}},"55":{"day":{"description":"Heavy Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Heavy Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"}},"56":{"day":{"description":"Light Freezing Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Freezing Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"}},"57":{"day":{"description":"Freezing Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Freezing Drizzle","image":"http://openweathermap.org/img/wn/[email protected]"}},"61":{"day":{"description":"Light Rain","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Rain","image":"http://openweathermap.org/img/wn/[email protected]"}},"63":{"day":{"description":"Rain","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Rain","image":"http://openweathermap.org/img/wn/[email protected]"}},"65":{"day":{"description":"Heavy Rain","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Heavy Rain","image":"http://openweathermap.org/img/wn/[email protected]"}},"66":{"day":{"description":"Light Freezing Rain","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Freezing Rain","image":"http://openweathermap.org/img/wn/[email protected]"}},"67":{"day":{"description":"Freezing Rain","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Freezing Rain","image":"http://openweathermap.org/img/wn/[email protected]"}},"71":{"day":{"description":"Light Snow","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Snow","image":"http://openweathermap.org/img/wn/[email protected]"}},"73":{"day":{"description":"Snow","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Snow","image":"http://openweathermap.org/img/wn/[email protected]"}},"75":{"day":{"description":"Heavy Snow","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Heavy Snow","image":"http://openweathermap.org/img/wn/[email protected]"}},"77":{"day":{"description":"Snow Grains","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Snow Grains","image":"http://openweathermap.org/img/wn/[email protected]"}},"80":{"day":{"description":"Light Showers","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Showers","image":"http://openweathermap.org/img/wn/[email protected]"}},"81":{"day":{"description":"Showers","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Showers","image":"http://openweathermap.org/img/wn/[email protected]"}},"82":{"day":{"description":"Heavy Showers","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Heavy Showers","image":"http://openweathermap.org/img/wn/[email protected]"}},"85":{"day":{"description":"Light Snow Showers","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Snow Showers","image":"http://openweathermap.org/img/wn/[email protected]"}},"86":{"day":{"description":"Snow Showers","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Snow Showers","image":"http://openweathermap.org/img/wn/[email protected]"}},"95":{"day":{"description":"Thunderstorm","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Thunderstorm","image":"http://openweathermap.org/img/wn/[email protected]"}},"96":{"day":{"description":"Light Thunderstorms With Hail","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Light Thunderstorms With Hail","image":"http://openweathermap.org/img/wn/[email protected]"}},"99":{"day":{"description":"Thunderstorm With Hail","image":"http://openweathermap.org/img/wn/[email protected]"},"night":{"description":"Thunderstorm With Hail","image":"http://openweathermap.org/img/wn/[email protected]"}}}')
property string weatherForecastText: ""
Dialog {
id: aboutDialog
parent: mainWindow.contentItem
title: "Information"
visible: false
modal: true
font: Theme.defaultFont
z: 10000
x: (parent.width - width) / 2
y: (parent.height - height) / 2
Label {
width: parent.width
wrapMode: Text.WordWrap
text: weatherForecastText + "\n\nWeather provided by Open-Meteo API"
}
standardButtons: Dialog.Ok
}
QfToolButtonDrawer {
id: weatherForecastDrawer
visible: position.active
bgcolor: Theme.darkGray
round: true
QfToolButton {
id: temperatureButton
bgcolor: Theme.darkGraySemiOpaque
iconColor: "transparent"
width: 40
height: 40
padding: 0
round: true
font.pointSize: 10
font.bold: true
Label {
anchors.centerIn: parent
font.pointSize: 10
font.bold: true
color: "white"
style: Text.Outline
styleColor: Theme.darkGray
text: parent.text
}
onClicked: {
aboutDialog.open()
}
}
}
Timer {
id: fetchWeatherTimer
interval: 10000
repeat: true
running: false
triggeredOnStart: true
onTriggered: {
let info = position.positionInformation
if (info.longitudeValid && info.latitudeValid) {
// 30 minute check following the first forecast acquired
interval = 1800000
fetchWeatherForecast();
} else {
// 10 second check to acquire a first forecast
interval = 10000
}
}
}
function fetchWeatherForecast() {
let info = position.positionInformation
let request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE) {
var responseObject = JSON.parse(request.response)
for (let i = 0; i < responseObject.hourly.time.length; i++) {
if (responseObject.hourly.time[i] > responseObject.current.time) {
weatherForecastDrawer.iconSource = weatherCodeInformation[responseObject.hourly.weather_code[i]].day.image
temperatureButton.text = responseObject.hourly.temperature_2m[i] + responseObject.hourly_units.temperature_2m
weatherForecastText = qsTr("Weather for the next hour:") + " " + weatherCodeInformation[responseObject.hourly.weather_code[i]].day.description
mainWindow.displayToast(weatherForecastText)
break
}
}
}
}
request.open("GET", "https://api.open-meteo.com/v1/forecast?latitude=" + info.latitude + "&longitude=" + info.longitude + "¤t=temperature_2m&hourly=temperature_2m,rain,weather_code,cloud_cover&forecast_days=2")
request.send();
}
Connections {
target: position
function onActiveChanged() {
if (position.active) {
// 10 second check to acquire a first forecast
fetchWeatherTimer.interval = 10000
fetchWeatherTimer.restart()
} else {
fetchWeatherTimer.stop()
}
}
}
Component.onCompleted: {
iface.addItemToPluginsToolbar(weatherForecastDrawer)
if (position.active) {
fetchWeatherTimer.interval = 10000
fetchWeatherTimer.restart()
} else {
fetchWeatherTimer.stop()
}
}
}