Debian Codegraph @

src/app/qml/components/HomeHourly.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 QtQuick 2.12
20 import Lomiri.Components 1.3
21
22 ListView {
23 id: homeHourly
24
25 clip: true
26 height: parent ? parent.height : undefined
27 width: parent ? parent.width : undefined
28 model: hourlyForecastsData.length
29 orientation: ListView.Horizontal
30
31 property string currentDate: Qt.formatTime(new Date())
32
33 onVisibleChanged: {
34 if (visible) {
35 ListView.model = hourlyForecastsData.length
36 }
37 }
38
39 delegate: Item {
40 id: delegate
41
42 property var hourData: hourlyForecastsData[index]
43 property var itemHeight: units.gu(2.5)
44
45 height: parent.height
46 width: childrenRect.width
47
48 Column {
49 id: hourColumn
50
51 anchors.verticalCenter: parent.verticalCenter
52 height: childrenRect.height
53 //some AM/PM notations take up more space than other formats, allow some extra width for those
54 width: isAmPmTimeformat() ? units.gu(11.5) : units.gu(10)
55
56 function isAmPmTimeformat() {
57 var check = ["am","a.m.","AM","A.M.","pm","p.m.","PM","P.M."]
58 var isAmPmFormat = false
59 for (var str in check) {
60 if (getDate(hourData.date).toLocaleTimeString(Qt.locale(),Locale.ShortFormat).search(check[str]) !== -1) {
61 isAmPmFormat = true
62 }
63 }
64 return isAmPmFormat
65 }
66
67 Label {
68 id: timestampLabel
69 anchors.horizontalCenter: parent.horizontalCenter
70 fontSize: "small"
71 font.weight: Font.Light
72 text: "%1 %2".arg(formatTimestamp(hourData.date, 'ddd')).arg(getDate(hourData.date).toLocaleTimeString(Qt.locale(),Locale.ShortFormat))
73 }
74
75 Item {
76 id: iconItem
77 anchors.horizontalCenter: parent.horizontalCenter
78 height: units.gu(7)
79 width: units.gu(7)
80
81 Icon {
82 id: weatherIcon
83 anchors {
84 fill: parent
85 margins: units.gu(0.5)
86 }
87 name: (hourData.icon !== undefined && iconMap[hourData.icon] !== undefined) ? iconMap[hourData.icon] : ""
88 }
89 //daily forecast does only yield "rain" icon for all intensities of rain from small drizzle to heavy rain
90 //use wrapper function to determine the rain icon based on the amount of rainfall
91 //if "rain" icon is transmitted, skip "image" (system icon) and use a in-app svg graphic instead
92 function getRainSnowIcon(rainVolume,snowVolume,weather) {
93 var resulticon
94 if (weather === "rain" || weather === "snow") {
95 if (rainVolume > 0 && snowVolume < rainVolume/10) {
96 //see https://de.wikipedia.org/wiki/Regen#Regenformen for classification
97 //rain volume in mm per hour
98 if (rainVolume < 0.5) { //light rain
99 resulticon = "qrc:/weather-1drop-symbolic.svg";
100 } else if (rainVolume >= 0.5 && rainVolume < 4) { //medium rain
101 resulticon = "qrc:/weather-2drops-symbolic.svg";
102 } else if (rainVolume >= 4 && rainVolume < 10) { //rain
103 resulticon = "qrc:/weather-3drops-small-symbolic.svg";
104 } else if (rainVolume >= 10 && rainVolume < 20) { //heavy rain
105 resulticon = "qrc:/weather-3drops-big-symbolic.svg";
106 } else if (rainVolume >= 20) { //very heavy rain
107 resulticon = "qrc:/weather-4drops-symbolic.svg";
108 }
109 weatherIcon.source = resulticon;
110 } else if (snowVolume > 0 && rainVolume < snowVolume/10) {
111 //categories based on rainfall volume, see above
112 //snow volume in mm per hour
113 if (snowVolume < 0.5) { //light snow
114 resulticon = "qrc:/weather-1flake-symbolic.svg";
115 } else if (snowVolume >= 0.5 && snowVolume < 4) { //medium snow
116 resulticon = "qrc:/weather-2flakes-symbolic.svg";
117 } else if (snowVolume >= 4 && snowVolume < 10) { //snow
118 resulticon = "qrc:/weather-3flakes-small-symbolic.svg"; //TODO: create icon
119 } else if (snowVolume >= 10 && snowVolume < 20) { //heavy snow
120 resulticon = "qrc:/weather-3flakes-big-symbolic.svg"; //TODO: create icon
121 } else if (snowVolume >= 20) { //very heavy snow
122 resulticon = "qrc:/weather-4flakes-symbolic.svg";
123 }
124 weatherIcon.source = resulticon;
125 } else if (rainVolume >= snowVolume/5 && snowVolume >= rainVolume/5) {
126 var combined = rainVolume + snowVolume
127 if (combined == 0) {
128 // sometimes there is a chance for rain/snow and icon "rain" send, but forecast volume is zero
129 // show icon for light rain/light snow
130 if (weather === "rain") {
131 resulticon = "qrc:/weather-1drop-symbolic.svg";
132 } else if (weather === "snow") {
133 resulticon = "qrc:/weather-1flake-symbolic.svg";
134 } else {
135 // not sure if there can be another case, show sleet icon then
136 resulticon = "qrc:/weather-sleet1-symbolic.svg";
137 }
138 } else if (combined > 0 && combined < 1) {
139 resulticon = "qrc:/weather-sleet1-symbolic.svg";
140 } else if (combined >= 1 && combined < 15) {
141 resulticon = "qrc:/weather-sleet2-symbolic.svg";
142 } else if (combined >= 15) {
143 resulticon = "qrc:/weather-sleet3-symbolic.svg";
144 }
145 weatherIcon.source = resulticon;
146 } else {
147 resulticon = ""; //TODO: add mixed downfall icons, temporary use sleet icon for all
148 }
149 }
150 return resulticon;
151 }
152 Component.onCompleted: getRainSnowIcon(hourData["metric"].rain, hourData["metric"].snow, hourData.icon)
153 }
154
155 Label {
156 id: tempLabel
157 anchors.horizontalCenter: parent.horizontalCenter
158 font.pixelSize: units.gu(3)
159 font.weight: Font.Light
160 text: getTemp(hourData["metric"].temp, true)
161 }
162
163 Item {
164 id: chanceOfPrecipRow
165 width: childrenRect.width
166 height: itemHeight
167 anchors.horizontalCenter: parent.horizontalCenter
168 visible: popLabel.text !== ""
169 Icon {
170 id: popIcon
171 anchors.verticalCenter: parent.verticalCenter
172 color: theme.palette.normal.baseText
173 width: units.gu(2)
174 height: width
175 source: "qrc:/extended-information_chance-of-sleet.svg"
176 }
177 Label {
178 id: popLabel
179 anchors.left: popIcon.right
180 anchors.leftMargin: units.gu(0.5)
181 anchors.verticalCenter: parent.verticalCenter
182 font.pixelSize: units.gu(2)
183 font.weight: Font.Light
184 text: hourData.pop !== undefined ? i18n.ctr("%1 will be replaced with the value, unit % for chance of precipitation, hourly forecast", "%1%").arg(hourData.pop*100) : ""
185 }
186 }
187
188 Label {
189 id: snowrainLabel
190 height: itemHeight
191 anchors.horizontalCenter: parent.horizontalCenter
192 anchors.rightMargin: units.gu(2)
193 font.pixelSize: units.gu(2)
194 font.weight: Font.Light
195 // shows the combined value of total downfall rain + snow
196 text: getCombinedRainSnowValue()
197
198 function getCombinedRainSnowValue() {
199 // 1. get snow value, 2. get rain value
200 // combine values with rain unit if rain is greater than snow or both are equal
201 // combine values with snow unit if snow is greater than rain and replace percentage icon with "chance of snow" icon
202 // add unit according to setting
203 // put in decimal separator according to set locale
204
205 var rainVolume = hourData["metric"].rain + hourData["metric"].showers // test if undefined
206 var snowVolume = hourData["metric"].snow // test if undefined
207 var label = ""
208 var value = rainVolume + snowVolume
209
210 if (rainVolume == 0 && snowVolume == 0) {
211 label = getRainLabel(value)
212 } else if (rainVolume > 0 && snowVolume < rainVolume/50) {
213 label = getRainLabel(value)
214 } else if (snowVolume > 0 && rainVolume < snowVolume/50) {
215 label = getSnowLabel(value)
216 popIcon.source = "qrc:/extended-information_chance-of-snow.svg"
217 } else {
218 label = getRainLabel(value)
219 popIcon.source = "qrc:/extended-information_chance-of-sleet.svg"
220 }
221
222 return label
223 }
224 }
225
226 Item {
227 id: sunshineDurationRow
228 width: childrenRect.width
229 height: itemHeight
230 anchors.horizontalCenter: parent.horizontalCenter
231 Icon {
232 id: sunIcon
233 anchors.verticalCenter: parent.verticalCenter
234 color: theme.palette.normal.baseText
235 width: units.gu(2)
236 height: width
237 name: sunshineLabel.text === "" ? "weather-few-clouds-night-symbolic" : "weather-clear-symbolic"
238 }
239 Label {
240 id: sunshineLabel
241 anchors.left: sunIcon.right
242 anchors.leftMargin: units.gu(0.5)
243 anchors.verticalCenter: parent.verticalCenter
244 font.pixelSize: units.gu(2)
245 font.weight: Font.Light
246 text: hourData.sunshinetime
247 }
248 }
249
250 Label {
251 id: windLabel
252 height: itemHeight
253 anchors.horizontalCenter: parent.horizontalCenter
254 font.pixelSize: units.gu(2)
255 font.weight: Font.Light
256 text: getWindSpeed(hourData.windSpeed)
257 }
258 }
259
260 Rectangle {
261 anchors.verticalCenter: parent.verticalCenter
262 color: LomiriColors.darkGrey
263 height: hourColumn.height
264 opacity: 0.2
265 visible: index > 0
266 width: units.gu(0.1)
267 }
268
269 function getRainLabel(value) {
270 var label = ""
271 if (settings.precipUnit == "in") {
272 //TRANSLATORS: rain volume in inch, keep/remove the space before the unit according to your language specifications
273 value = Math.round(calcInch(value)*100)/100
274 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0")))
275 } else if (settings.precipUnit == "l") {
276 //TRANSLATORS: rain volume in litre per squaremeter, keep/remove the space before the unit according to your language specifications
277 label = i18n.tr("%1 l/m²").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
278 } else {
279 //TRANSLATORS: rain volume in millimeter, keep/remove the space before the unit according to your language specifications
280 label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
281 }
282 return label
283 }
284
285 function getSnowLabel(value) {
286 var label = ""
287 if (settings.snowUnit == "in") {
288 //TRANSLATORS: snow volume in inch, keep/remove the space before the unit according to your language specifications
289 value = Math.round(calcInch(value)*100)/100
290 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0")))
291 } else if (settings.snowUnit == "mm") {
292 //TRANSLATORS: snow volume in millimeter, keep/remove the space before the unit according to your language specifications
293 label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
294 } else {
295 //TRANSLATORS: snow volume in centimeter, keep/remove the space before the unit according to your language specifications
296 label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
297 }
298 return label
299 }
300 }
301 }