Debian Codegraph @

src/app/qml/components/DayDelegate.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 import QtQuick.Layouts 1.1
22
23 ListItem {
24 id: dayDelegate
25 objectName:"dayDelegate" + index
26 height: collapsedHeight
27 width: weatherApp.width
28
29 divider { visible: false }
30
31 property int collapsedHeight: units.gu(7.5)
32 property int expandedHeight: collapsedHeight + units.gu(4) + (expandedInfo.item ? expandedInfo.item.height : 0)
33
34 property alias day: dayLabel.text
35 property alias image: weatherImage.name
36 property alias high: highLabel.text
37 property alias low: lowLabel.text
38
39 property var itemHeight: units.gu(2.5)
40
41 property alias modelData: expandedInfo.modelData
42
43 state: "normal"
44 states: [
45 State {
46 name: "normal"
47 PropertyChanges {
48 target: dayDelegate
49 height: collapsedHeight
50 }
51 PropertyChanges {
52 target: expandedInfo
53 opacity: 0
54 }
55 PropertyChanges {
56 target: updownIcon
57 name: "down"
58 }
59 },
60 State {
61 name: "expanded"
62 PropertyChanges {
63 target: dayDelegate
64 height: expandedHeight
65 }
66 PropertyChanges {
67 target: expandedInfo
68 opacity: 1
69 }
70 PropertyChanges {
71 target: updownIcon
72 name: "up"
73 }
74 }
75 ]
76
77 transitions: [
78 Transition {
79 from: "normal"
80 to: "expanded"
81 SequentialAnimation {
82 ScriptAction {
83 script: expandedInfo.active = true
84 }
85 NumberAnimation {
86 easing.type: Easing.InOutQuad
87 properties: "opacity"
88 }
89 ScriptAction { // run animation to ensure the listitem fits
90 script: waitEnsureVisible.restart()
91 }
92 }
93 },
94 Transition {
95 from: "expanded"
96 to: "normal"
97 SequentialAnimation {
98 NumberAnimation {
99 easing.type: Easing.InOutQuad
100 properties: "opacity"
101 }
102 ScriptAction {
103 script: expandedInfo.active = false
104 }
105 }
106 }
107 ]
108
109 onClicked: {
110 state = state === "normal" ? "expanded" : "normal"
111 locationPages.collapseOtherDelegates(index)
112 }
113
114 Item {
115 id: mainInfo
116
117 height: collapsedHeight
118 width: parent.width // units.gu(45) minus two times extendedInfoMargin @ units.gu(2.5)
119 anchors {
120 horizontalCenter: parent.horizontalCenter
121 }
122
123 RowLayout {
124 id: upperRow
125 anchors {
126 top: parent.top
127 topMargin: units.gu(1)
128 left: parent.left
129 }
130 width: parent.width
131 Label {
132 id: dayLabel
133 Layout.alignment: Qt.AlignLeft
134 //the remaining space total width minus margins minus fixedWidthItemsRow
135 width: parent.width - fixedWidthItemsRow.width
136 elide: Text.ElideRight
137 font.weight: Font.Light
138 font.pixelSize: units.gu(1.8)
139 horizontalAlignment: Text.AlignLeft
140 }
141
142 RowLayout {
143 id: fixedWidthItemsRow
144 Layout.alignment: Qt.AlignRight
145 //width based on the size of the items
146 width: settings.tempScale === "°F" || parseFloat(low) <= -10 || parseFloat(high) <= -10 ? units.gu(28) : units.gu(27)
147 spacing: units.gu(1.25)
148
149 Icon {
150 id: moonImage
151 Layout.alignment: Qt.AlignHCenter
152 height: units.gu(2.5)
153 width: height
154 source: modelData.moonImage
155 }
156
157 Icon {
158 id: weatherImage
159 Layout.alignment: Qt.AlignHCenter
160 height: units.gu(3)
161 width: height
162 }
163
164 Icon {
165 id: lowImage
166 Layout.alignment: Qt.AlignHCenter
167 height: units.gu(2)
168 width: height
169 source: "qrc:/extended-information_moon.svg"
170 color: settings.lowColor
171 }
172
173 Label {
174 id: lowLabel
175 //because Fahrenheit can go above 100 for three digits more space is needed
176 Layout.preferredWidth: settings.tempScale === "°F" || parseFloat(low) <= -10 ? units.gu(4.5) : units.gu(4)
177 Layout.alignment: Qt.AlignHCenter
178 color: settings.lowColor
179 font.weight: Font.Normal
180 font.pixelSize: units.gu(2.2)
181 horizontalAlignment: Text.AlignRight
182 }
183
184 Icon {
185 id: highImage
186 Layout.alignment: Qt.AlignHCenter
187 height: units.gu(2)
188 width: height
189 name: "weather-clear-symbolic"
190 color: settings.highColor
191 }
192
193 Label {
194 id: highLabel
195 //because Fahrenheit can go above 100 for three digits more space is needed
196 Layout.preferredWidth: settings.tempScale === "°F" || parseFloat(high) <= -10 ? units.gu(4.5) : units.gu(4)
197 Layout.alignment: Qt.AlignHCenter
198 color: settings.highColor
199 font.weight: Font.Normal
200 font.pixelSize: units.gu(2.2)
201 horizontalAlignment: Text.AlignRight
202 }
203
204 Icon {
205 id: updownIcon
206 Layout.alignment: Qt.AlignRight
207 height: units.gu(2)
208 width: height
209 name: "down"
210 }
211 }
212 }
213 RowLayout {
214 id: lowerRow
215 width: parent.width - updownIcon.width
216 anchors {
217 top: upperRow.bottom
218 left: parent.left
219 }
220
221 Item {
222 id: rainInfoItem
223 Layout.alignment: Qt.AlignLeft
224 width: childrenRect.width
225 height: itemHeight
226 Icon {
227 id: copIcon
228 anchors.verticalCenter: parent.verticalCenter
229 color: theme.palette.normal.baseText
230 width: units.gu(2)
231 height: width
232 source: "qrc:/extended-information_chance-of-sleet.svg"
233 }
234 Label {
235 id: copLabel
236 anchors.left: copIcon.right
237 anchors.leftMargin: units.gu(0.5)
238 anchors.verticalCenter: parent.verticalCenter
239 font.pixelSize: units.gu(2)
240 font.weight: Font.Light
241 text: modelData.chanceOfPrecip + " " + getCombinedRainSnowValue()
242
243 function getCombinedRainSnowValue() {
244 // 1. get snow value, 2. get rain value
245 // combine values with rain unit if rain is greater than snow or both are equal
246 // combine values with snow unit if snow is greater than rain and replace percentage icon with "chance of snow" icon
247 // add unit according to setting
248 // put in decimal separator according to set locale
249
250 // I know, a bit stupid, but here we get already preformatted labels, so extract the numbers only, so we can sum them up
251 var rainVolume = parseFloat(modelData.rain.split(" ")[0].replace(",",".")) //already includes showers
252 var snowVolume = parseFloat(modelData.snow.split(" ")[0].replace(",","."))
253 var label = ""
254 var value = rainVolume + snowVolume
255
256 if (rainVolume == 0 && snowVolume == 0) {
257 label = getRainLabel(value)
258 } else if (rainVolume > 0 && snowVolume < rainVolume/50) {
259 label = getRainLabel(value)
260 } else if (snowVolume > 0 && rainVolume < snowVolume/50) {
261 label = getSnowLabel(value)
262 copIcon.source = "qrc:/extended-information_chance-of-snow.svg"
263 } else {
264 label = getRainLabel(value)
265 copIcon.source = "qrc:/extended-information_chance-of-sleet.svg"
266 }
267
268 return label
269 }
270 }
271 }
272
273 Rectangle {
274 id: sunshineWindRow
275 Layout.alignment: Qt.AlignRight
276 width: fixedWidthItemsRow.width
277 height: itemHeight
278 color: "transparent"
279
280 Item {
281 id: sunshineDurationRow
282 anchors.left: parent.left
283 anchors.leftMargin: units.gu(1.5)
284 width: childrenRect.width
285 height: itemHeight
286 Icon {
287 id: sunIcon
288 anchors.verticalCenter: parent.verticalCenter
289 color: theme.palette.normal.baseText
290 width: units.gu(2)
291 height: width
292 name: sunshineLabel.text === "" ? "weather-few-clouds-night-symbolic" : "weather-clear-symbolic"
293 }
294 Label {
295 id: sunshineLabel
296 anchors.left: sunIcon.right
297 anchors.leftMargin: units.gu(0.5)
298 anchors.verticalCenter: parent.verticalCenter
299 font.pixelSize: units.gu(2)
300 font.weight: Font.Light
301 text: modelData.sunshinetime
302 }
303 }
304
305 Item {
306 id: windSpeedRow
307 anchors.right: parent.right
308 anchors.rightMargin: units.gu(1.25)
309 width: childrenRect.width
310 height: itemHeight
311 Icon {
312 id: windIcon
313 anchors.verticalCenter: parent.verticalCenter
314 color: theme.palette.normal.baseText
315 width: units.gu(2)
316 height: width
317 source: "qrc:/extended-information_wind.svg"
318 }
319 Label {
320 id: windLabel
321 anchors.left: windIcon.right
322 anchors.leftMargin: units.gu(0.5)
323 anchors.verticalCenter: parent.verticalCenter
324 font.pixelSize: units.gu(2)
325 font.weight: Font.Light
326 text: getWindSpeed(modelData.windSpeed)
327 }
328 }
329 }
330 }
331 }
332
333 Loader {
334 id: expandedInfo
335 width: parent.width
336 active: false
337 anchors {
338 horizontalCenter: parent.horizontalCenter
339 top: mainInfo.bottom
340 topMargin: units.gu(2)
341 }
342 asynchronous: true
343 opacity: 0
344 source: "DayDelegateExtraInfo.qml"
345
346 property var modelData
347 }
348
349 Behavior on height {
350 NumberAnimation {
351 id: heightAnimation
352 easing.type: Easing.InOutQuad
353 }
354 }
355
356 NumberAnimation {
357 // animation to ensure the listitem fits by moving the contentY
358 id: ensureVisibleAnimation
359 easing.type: Easing.InOutQuad
360 properties: "contentY"
361 target: dayDelegate.parent.parent
362 }
363
364 Timer {
365 id: waitEnsureVisible
366 interval: 16
367 repeat: false
368
369 onTriggered: {
370 // Only trigger once the loader has loaded
371 // and the animations have stopped
372 // otherwise restart the timer
373 if (expandedInfo.active && expandedInfo.status === Loader.Ready
374 && !heightAnimation.running) {
375 // stop the current animation
376 ensureVisibleAnimation.running = false;
377
378 // Get the current position
379 var view = dayDelegate.parent.parent;
380 var pos = view.contentY;
381
382 // Tell the listview to make the listitem fit
383 view.positionViewAtIndex(index, ListView.Contain);
384
385 // Animate from the original position to the new position
386 ensureVisibleAnimation.from = pos;
387 ensureVisibleAnimation.to = view.contentY;
388 ensureVisibleAnimation.running = true;
389 } else {
390 restart()
391 }
392 }
393 }
394
395 //daily forecast does only yield "rain" icon for all intensities of rain from small drizzle to heavy rain
396 //use wrapper function to determine the rain icon based on the amount of rainfall
397 //if "rain" icon is transmitted, skip "image" (system icon) and use a in-app svg graphic instead
398 function getRainSnowIcon(rainVolume,snowVolume,weather) {
399 var resulticon
400 //remove unit from rain/snow volume string and parse to float
401 rainVolume = parseFloat(rainVolume.split(" ")[0].replace(",","."))
402 snowVolume = parseFloat(snowVolume.split(" ")[0].replace(",","."))
403 if (settings.precipUnit === "in") {
404 //convert from inch to mm
405 rainVolume = rainVolume*25.4;
406 snowVolume = snowVolume*25.4;
407 }
408 if (weather === "weather-showers-symbolic" || weather === "weather-snow-symbolic") {
409 if (rainVolume > 0 && (!snowVolume || snowVolume < rainVolume/5)) {
410 //see https://de.wikipedia.org/wiki/Regen#Regenformen for classification
411 //rain volume in mm per hour
412 if (rainVolume < 0.5) { //light rain
413 resulticon = "qrc:/weather-1drop-symbolic.svg";
414 } else if (rainVolume >= 0.5 && rainVolume < 4) { //medium rain
415 resulticon = "qrc:/weather-2drops-symbolic.svg";
416 } else if (rainVolume >= 4 && rainVolume < 10) { //rain
417 resulticon = "qrc:/weather-3drops-small-symbolic.svg";
418 } else if (rainVolume >= 10 && rainVolume < 20) { //heavy rain
419 resulticon = "qrc:/weather-3drops-big-symbolic.svg";
420 } else if (rainVolume >= 20) { //very heavy rain
421 resulticon = "qrc:/weather-4drops-symbolic.svg";
422 }
423 setIcon(resulticon)
424 } else if (snowVolume > 0 && (!rainVolume || rainVolume < snowVolume/5)) {
425 //categories based on rainfall volume, see above
426 //snow volume in mm per hour
427 if (snowVolume < 0.5) { //light snow
428 resulticon = "qrc:/weather-1flake-symbolic.svg";
429 } else if (snowVolume >= 0.5 && snowVolume < 4) { //medium snow
430 resulticon = "qrc:/weather-2flakes-symbolic.svg";
431 } else if (snowVolume >= 4 && snowVolume < 10) { //snow
432 resulticon = "qrc:/weather-3flakes-small-symbolic.svg";
433 } else if (snowVolume >= 10 && snowVolume < 20) { //heavy snow
434 resulticon = "qrc:/weather-3flakes-big-symbolic.svg";
435 } else if (snowVolume >= 20) { //very heavy snow
436 resulticon = "qrc:/weather-4flakes-symbolic.svg";
437 }
438 setIcon(resulticon)
439 } else if (rainVolume >= snowVolume/5 && snowVolume >= rainVolume/5){
440 var combined = rainVolume + snowVolume
441 if (combined < 1) {
442 resulticon = "qrc:/weather-sleet1-symbolic.svg";
443 } else if (combined >= 1 && combined < 15) {
444 resulticon = "qrc:/weather-sleet2-symbolic.svg";
445 } else if (combined >= 15) {
446 resulticon = "qrc:/weather-sleet3-symbolic.svg";
447 }
448 setIcon(resulticon)
449 } else {
450 resulticon = "";
451 }
452 }
453 return resulticon;
454 }
455
456 function setIcon(icon) {
457 weatherImage.source = icon;
458 weatherImage.height = units.gu(3);
459 }
460
461 function getRainLabel(value) {
462 var label = ""
463 if (settings.precipUnit == "in") {
464 //TRANSLATORS: rain volume in inch, keep/remove the space before the unit according to your language specifications
465 value = Math.round(calcInch(value)*100)/100
466 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0")))
467 } else if (settings.precipUnit == "l") {
468 //TRANSLATORS: rain volume in litre per squaremeter, keep/remove the space before the unit according to your language specifications
469 label = i18n.tr("%1 l/m²").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
470 } else {
471 //TRANSLATORS: rain volume in millimeter, keep/remove the space before the unit according to your language specifications
472 label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
473 }
474 return label
475 }
476
477 function getSnowLabel(value) {
478 var label = ""
479 if (settings.snowUnit == "in") {
480 //TRANSLATORS: snow volume in inch, keep/remove the space before the unit according to your language specifications
481 value = Math.round(calcInch(value)*100)/100
482 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0")))
483 } else if (settings.snowUnit == "mm") {
484 //TRANSLATORS: snow volume in millimeter, keep/remove the space before the unit according to your language specifications
485 label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
486 } else {
487 //TRANSLATORS: snow volume in centimeter, keep/remove the space before the unit according to your language specifications
488 label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0")))
489 }
490 return label
491 }
492
493 Component.onCompleted: {
494 getRainSnowIcon(modelData.rain, modelData.snow, image)
495 locationPages.collapseOtherDelegates.connect(function(otherIndex) {
496 if (dayDelegate && typeof index !== "undefined" && otherIndex !== index) {
497 state = "normal"
498 }
499 });
500 }
501 }