-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.qml
295 lines (275 loc) · 10.6 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
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
// Copyright (C) 2022 Kambiz Asadzadeh
// SPDX-License-Identifier: LGPL-3.0-only
import QtQuick
import QtQuick.Window
import QtQuick.Controls.Basic
import QtQuick.Layouts
ApplicationWindow {
id: appRoot
width: 420
height: 290
visible: true
title: qsTr("eWeather")
QtObject {
id: appObject
readonly property string apiUrl : "http://api.openweathermap.org/geo/1.0/direct?q="; //From https://openweathermap.org
readonly property string weatherApiUrl : "https://api.openweathermap.org/data/2.5/weather?"; //From https://openweathermap.org
readonly property string apiKey : "8a25f223e60c89f2987f69916dcf0a99";
readonly property string method : "GET";
property double lon : 0.0;
property double lat : 0.0;
property string icon;
property string main: "";
property string description: " ";
property double speed: 0.0;
property double deg: 0.0;
property double temp : 0.0;
property double feels_like : 0.0;
property double pressure : 0.0;
property int humidity: 0;
property bool isCelsius : unitType.checked ? true : false
}
function dataRequest(type)
{
var req = new XMLHttpRequest();
req.open(appObject.method, appObject.apiUrl + cityValue.text + "&limit=1&appid=" + appObject.apiKey);
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
let result = JSON.parse(req.responseText);
//Data
appObject.lat = JSON.stringify(result[0].lat);
appObject.lon = JSON.stringify(result[0].lon);
//Information
{
console.log("lat: " + JSON.stringify(result[0].lat))
console.log("lon: " + JSON.stringify(result[0].lon))
}
weatherRequest();
}
}
req.onerror = function(){
console.log("Error!")
}
req.send()
}
//! Remove extra double quote for some json outputs.
function stringFixer(variable)
{
return variable.replace(/['"]+/g, '')
}
function weatherRequest()
{
var req = new XMLHttpRequest();
var unit = appObject.isCelsius ? "metric" : "imperial";
req.open(appObject.method, appObject.weatherApiUrl + "lat="+appObject.lat+"&lon="+appObject.lon+"&units=" + unit + "&exclude=hourly,daily&appid=" + appObject.apiKey);
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
let result = JSON.parse(req.responseText);
appObject.icon = JSON.stringify(result.weather[0].icon)
appObject.main = JSON.stringify(result.weather[0].main)
appObject.description = JSON.stringify(result.weather[0].description)
appObject.humidity = JSON.stringify(result.main.humidity)
appObject.feels_like = JSON.stringify(result.main.feels_like)
appObject.pressure = JSON.stringify(result.main.pressure)
appObject.temp = JSON.stringify(result.main.temp)
appObject.speed = JSON.stringify(result.wind.speed)
appObject.deg = JSON.stringify(result.wind.deg)
var icon = "qrc:/resources/icons/";
icon+= appObject.icon;
icon+="@2x.png";
iconImage.source = icon.replace(/['"]+/g, '');
busyIndicator.running = false;
}
}
req.onerror = function(){
console.log("Error!")
}
req.send()
}
Pane {
width: parent.width
Layout.fillWidth: true
ColumnLayout {
width: parent.width
Layout.fillWidth: true
spacing: 10
TextField {
id: cityValue
width: parent.width
height: 32
Layout.fillWidth: true
placeholderText: "Enter city name"
background: Rectangle {
height: 32
border.width: cityValue.focus ? 2 : 1
border.color: "#276fff"
radius: 5
}
}
RowLayout {
spacing: 10
width: parent.width
Layout.fillWidth: true
Button {
id: control
width: 140
height: 48
Layout.fillWidth: true
text: "Get Data"
background: Rectangle {
height: 48
color: "#276fff"
anchors.fill: parent
radius: 5
}
contentItem: Text {
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: "#fff"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
scale: control.down ? 0.9 : 1.0
Behavior on scale {
NumberAnimation {duration: 70;}
}
}
onClicked: {
if(cityValue.text !== "") {
busyIndicator.running = true;
dataRequest();
}
}
}
Text {
font.pixelSize: 12
font.bold: true
text: qsTr("Fahrenheit")
}
Switch {
id: unitType
width: 64
indicator: Rectangle {
implicitWidth: 48
implicitHeight: 26
x: unitType.leftPadding
y: parent.height / 2 - height / 2
radius: 13
color: unitType.checked ? "#276fff" : "#ffffff"
border.color: unitType.checked ? "#276fff" : "#cccccc"
Rectangle {
x: unitType.checked ? parent.width - width : 0
width: 26
height: 26
radius: 13
color: unitType.down ? "#cccccc" : "#ffffff"
border.color: unitType.checked ? (unitType.down ? "#276fff" : "#276fff") : "#999999"
}
}
contentItem: Text {
text: unitType.text
font: unitType.font
opacity: enabled ? 1.0 : 0.3
color: unitType.down ? "#276fff" : "#21be2b"
verticalAlignment: Text.AlignVCenter
leftPadding: unitType.indicator.width + unitType.spacing
}
}
Text {
font.pixelSize: 12
font.bold: true
text: qsTr("Celsius")
}
}
ColumnLayout {
width: parent.width
Layout.fillWidth: true
Text {
font.pixelSize: 16
font.bold: true
text: qsTr("Wind")
}
RowLayout {
spacing: 25
width: parent.width
Layout.fillWidth: true
Text {
font.pixelSize: 14
font.bold: false
text: qsTr("Speed: " + appObject.speed)
}
Text {
font.pixelSize: 14
font.bold: false
text: qsTr("Deg: " + appObject.deg)
}
Item {
width: 64
height: 64
Image {
id: iconImage
width: 64
height: 64
fillMode: Image.PreserveAspectCrop
}
}
Column {
spacing: 5
Text {
font.pixelSize: 24
font.bold: true
text: qsTr(stringFixer(appObject.main))
}
Text {
font.pixelSize: 14
font.bold: false
font.capitalization: Font.AllUppercase
text: qsTr(stringFixer(appObject.description))
}
}
}
///
Text {
font.pixelSize: 16
font.bold: true
text: qsTr("Main")
}
Item { height: 20; }
Row {
spacing: 25
Row {Text {
font.pixelSize: 14
font.bold: false
text: qsTr("Temp: " + appObject.temp)
}
Text {
font.pixelSize: 10
font.bold: true
text: qsTr(appObject.isCelsius ? "°C" : "°F")
y: -10
}
}
Text {
font.pixelSize: 14
font.bold: false
text: qsTr("Pressure: " + appObject.pressure)
}
Text {
font.pixelSize: 14
font.bold: false
text: qsTr("Humidity: " + appObject.humidity)
}
}
}
}
}
BusyIndicator {
id: busyIndicator
width: 48
height: 48
anchors.right: parent.right
anchors.bottom: parent.bottom
running: false
}
}