/* * Copyright (C) 2015 Canonical Ltd. * * This file is part of Lomiri Weather App * * Lomiri Weather App is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * Lomiri Weather App is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import QtQuick 2.12 import Lomiri.Components 1.3 import QtQuick.Layouts 1.1 ListItem { id: dayDelegate objectName:"dayDelegate" + index height: collapsedHeight width: weatherApp.width divider { visible: false } property int collapsedHeight: units.gu(7.5) property int expandedHeight: collapsedHeight + units.gu(4) + (expandedInfo.item ? expandedInfo.item.height : 0) property alias day: dayLabel.text property alias image: weatherImage.name property alias high: highLabel.text property alias low: lowLabel.text property var itemHeight: units.gu(2.5) property alias modelData: expandedInfo.modelData state: "normal" states: [ State { name: "normal" PropertyChanges { target: dayDelegate height: collapsedHeight } PropertyChanges { target: expandedInfo opacity: 0 } PropertyChanges { target: updownIcon name: "down" } }, State { name: "expanded" PropertyChanges { target: dayDelegate height: expandedHeight } PropertyChanges { target: expandedInfo opacity: 1 } PropertyChanges { target: updownIcon name: "up" } } ] transitions: [ Transition { from: "normal" to: "expanded" SequentialAnimation { ScriptAction { script: expandedInfo.active = true } NumberAnimation { easing.type: Easing.InOutQuad properties: "opacity" } ScriptAction { // run animation to ensure the listitem fits script: waitEnsureVisible.restart() } } }, Transition { from: "expanded" to: "normal" SequentialAnimation { NumberAnimation { easing.type: Easing.InOutQuad properties: "opacity" } ScriptAction { script: expandedInfo.active = false } } } ] onClicked: { state = state === "normal" ? "expanded" : "normal" locationPages.collapseOtherDelegates(index) } Item { id: mainInfo height: collapsedHeight width: parent.width // units.gu(45) minus two times extendedInfoMargin @ units.gu(2.5) anchors { horizontalCenter: parent.horizontalCenter } RowLayout { id: upperRow anchors { top: parent.top topMargin: units.gu(1) left: parent.left } width: parent.width Label { id: dayLabel Layout.alignment: Qt.AlignLeft //the remaining space total width minus margins minus fixedWidthItemsRow width: parent.width - fixedWidthItemsRow.width elide: Text.ElideRight font.weight: Font.Light font.pixelSize: units.gu(1.8) horizontalAlignment: Text.AlignLeft } RowLayout { id: fixedWidthItemsRow Layout.alignment: Qt.AlignRight //width based on the size of the items width: settings.tempScale === "°F" || parseFloat(low) <= -10 || parseFloat(high) <= -10 ? units.gu(28) : units.gu(27) spacing: units.gu(1.25) Icon { id: moonImage Layout.alignment: Qt.AlignHCenter height: units.gu(2.5) width: height source: modelData.moonImage } Icon { id: weatherImage Layout.alignment: Qt.AlignHCenter height: units.gu(3) width: height } Icon { id: lowImage Layout.alignment: Qt.AlignHCenter height: units.gu(2) width: height source: "qrc:/extended-information_moon.svg" color: settings.lowColor } Label { id: lowLabel //because Fahrenheit can go above 100 for three digits more space is needed Layout.preferredWidth: settings.tempScale === "°F" || parseFloat(low) <= -10 ? units.gu(4.5) : units.gu(4) Layout.alignment: Qt.AlignHCenter color: settings.lowColor font.weight: Font.Normal font.pixelSize: units.gu(2.2) horizontalAlignment: Text.AlignRight } Icon { id: highImage Layout.alignment: Qt.AlignHCenter height: units.gu(2) width: height name: "weather-clear-symbolic" color: settings.highColor } Label { id: highLabel //because Fahrenheit can go above 100 for three digits more space is needed Layout.preferredWidth: settings.tempScale === "°F" || parseFloat(high) <= -10 ? units.gu(4.5) : units.gu(4) Layout.alignment: Qt.AlignHCenter color: settings.highColor font.weight: Font.Normal font.pixelSize: units.gu(2.2) horizontalAlignment: Text.AlignRight } Icon { id: updownIcon Layout.alignment: Qt.AlignRight height: units.gu(2) width: height name: "down" } } } RowLayout { id: lowerRow width: parent.width - updownIcon.width anchors { top: upperRow.bottom left: parent.left } Item { id: rainInfoItem Layout.alignment: Qt.AlignLeft width: childrenRect.width height: itemHeight Icon { id: copIcon anchors.verticalCenter: parent.verticalCenter color: theme.palette.normal.baseText width: units.gu(2) height: width source: "qrc:/extended-information_chance-of-sleet.svg" } Label { id: copLabel anchors.left: copIcon.right anchors.leftMargin: units.gu(0.5) anchors.verticalCenter: parent.verticalCenter font.pixelSize: units.gu(2) font.weight: Font.Light text: modelData.chanceOfPrecip + " " + getCombinedRainSnowValue() function getCombinedRainSnowValue() { // 1. get snow value, 2. get rain value // combine values with rain unit if rain is greater than snow or both are equal // combine values with snow unit if snow is greater than rain and replace percentage icon with "chance of snow" icon // add unit according to setting // put in decimal separator according to set locale // I know, a bit stupid, but here we get already preformatted labels, so extract the numbers only, so we can sum them up var rainVolume = parseFloat(modelData.rain.split(" ")[0].replace(",",".")) //already includes showers var snowVolume = parseFloat(modelData.snow.split(" ")[0].replace(",",".")) var label = "" var value = rainVolume + snowVolume if (rainVolume == 0 && snowVolume == 0) { label = getRainLabel(value) } else if (rainVolume > 0 && snowVolume < rainVolume/50) { label = getRainLabel(value) } else if (snowVolume > 0 && rainVolume < snowVolume/50) { label = getSnowLabel(value) copIcon.source = "qrc:/extended-information_chance-of-snow.svg" } else { label = getRainLabel(value) copIcon.source = "qrc:/extended-information_chance-of-sleet.svg" } return label } } } Rectangle { id: sunshineWindRow Layout.alignment: Qt.AlignRight width: fixedWidthItemsRow.width height: itemHeight color: "transparent" Item { id: sunshineDurationRow anchors.left: parent.left anchors.leftMargin: units.gu(1.5) width: childrenRect.width height: itemHeight Icon { id: sunIcon anchors.verticalCenter: parent.verticalCenter color: theme.palette.normal.baseText width: units.gu(2) height: width name: sunshineLabel.text === "" ? "weather-few-clouds-night-symbolic" : "weather-clear-symbolic" } Label { id: sunshineLabel anchors.left: sunIcon.right anchors.leftMargin: units.gu(0.5) anchors.verticalCenter: parent.verticalCenter font.pixelSize: units.gu(2) font.weight: Font.Light text: modelData.sunshinetime } } Item { id: windSpeedRow anchors.right: parent.right anchors.rightMargin: units.gu(1.25) width: childrenRect.width height: itemHeight Icon { id: windIcon anchors.verticalCenter: parent.verticalCenter color: theme.palette.normal.baseText width: units.gu(2) height: width source: "qrc:/extended-information_wind.svg" } Label { id: windLabel anchors.left: windIcon.right anchors.leftMargin: units.gu(0.5) anchors.verticalCenter: parent.verticalCenter font.pixelSize: units.gu(2) font.weight: Font.Light text: getWindSpeed(modelData.windSpeed) } } } } } Loader { id: expandedInfo width: parent.width active: false anchors { horizontalCenter: parent.horizontalCenter top: mainInfo.bottom topMargin: units.gu(2) } asynchronous: true opacity: 0 source: "DayDelegateExtraInfo.qml" property var modelData } Behavior on height { NumberAnimation { id: heightAnimation easing.type: Easing.InOutQuad } } NumberAnimation { // animation to ensure the listitem fits by moving the contentY id: ensureVisibleAnimation easing.type: Easing.InOutQuad properties: "contentY" target: dayDelegate.parent.parent } Timer { id: waitEnsureVisible interval: 16 repeat: false onTriggered: { // Only trigger once the loader has loaded // and the animations have stopped // otherwise restart the timer if (expandedInfo.active && expandedInfo.status === Loader.Ready && !heightAnimation.running) { // stop the current animation ensureVisibleAnimation.running = false; // Get the current position var view = dayDelegate.parent.parent; var pos = view.contentY; // Tell the listview to make the listitem fit view.positionViewAtIndex(index, ListView.Contain); // Animate from the original position to the new position ensureVisibleAnimation.from = pos; ensureVisibleAnimation.to = view.contentY; ensureVisibleAnimation.running = true; } else { restart() } } } //daily forecast does only yield "rain" icon for all intensities of rain from small drizzle to heavy rain //use wrapper function to determine the rain icon based on the amount of rainfall //if "rain" icon is transmitted, skip "image" (system icon) and use a in-app svg graphic instead function getRainSnowIcon(rainVolume,snowVolume,weather) { var resulticon //remove unit from rain/snow volume string and parse to float rainVolume = parseFloat(rainVolume.split(" ")[0].replace(",",".")) snowVolume = parseFloat(snowVolume.split(" ")[0].replace(",",".")) if (settings.precipUnit === "in") { //convert from inch to mm rainVolume = rainVolume*25.4; snowVolume = snowVolume*25.4; } if (weather === "weather-showers-symbolic" || weather === "weather-snow-symbolic") { if (rainVolume > 0 && (!snowVolume || snowVolume < rainVolume/5)) { //see https://de.wikipedia.org/wiki/Regen#Regenformen for classification //rain volume in mm per hour if (rainVolume < 0.5) { //light rain resulticon = "qrc:/weather-1drop-symbolic.svg"; } else if (rainVolume >= 0.5 && rainVolume < 4) { //medium rain resulticon = "qrc:/weather-2drops-symbolic.svg"; } else if (rainVolume >= 4 && rainVolume < 10) { //rain resulticon = "qrc:/weather-3drops-small-symbolic.svg"; } else if (rainVolume >= 10 && rainVolume < 20) { //heavy rain resulticon = "qrc:/weather-3drops-big-symbolic.svg"; } else if (rainVolume >= 20) { //very heavy rain resulticon = "qrc:/weather-4drops-symbolic.svg"; } setIcon(resulticon) } else if (snowVolume > 0 && (!rainVolume || rainVolume < snowVolume/5)) { //categories based on rainfall volume, see above //snow volume in mm per hour if (snowVolume < 0.5) { //light snow resulticon = "qrc:/weather-1flake-symbolic.svg"; } else if (snowVolume >= 0.5 && snowVolume < 4) { //medium snow resulticon = "qrc:/weather-2flakes-symbolic.svg"; } else if (snowVolume >= 4 && snowVolume < 10) { //snow resulticon = "qrc:/weather-3flakes-small-symbolic.svg"; } else if (snowVolume >= 10 && snowVolume < 20) { //heavy snow resulticon = "qrc:/weather-3flakes-big-symbolic.svg"; } else if (snowVolume >= 20) { //very heavy snow resulticon = "qrc:/weather-4flakes-symbolic.svg"; } setIcon(resulticon) } else if (rainVolume >= snowVolume/5 && snowVolume >= rainVolume/5){ var combined = rainVolume + snowVolume if (combined < 1) { resulticon = "qrc:/weather-sleet1-symbolic.svg"; } else if (combined >= 1 && combined < 15) { resulticon = "qrc:/weather-sleet2-symbolic.svg"; } else if (combined >= 15) { resulticon = "qrc:/weather-sleet3-symbolic.svg"; } setIcon(resulticon) } else { resulticon = ""; } } return resulticon; } function setIcon(icon) { weatherImage.source = icon; weatherImage.height = units.gu(3); } function getRainLabel(value) { var label = "" if (settings.precipUnit == "in") { //TRANSLATORS: rain volume in inch, keep/remove the space before the unit according to your language specifications value = Math.round(calcInch(value)*100)/100 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0"))) } else if (settings.precipUnit == "l") { //TRANSLATORS: rain volume in litre per squaremeter, keep/remove the space before the unit according to your language specifications label = i18n.tr("%1 l/m²").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0"))) } else { //TRANSLATORS: rain volume in millimeter, keep/remove the space before the unit according to your language specifications label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0"))) } return label } function getSnowLabel(value) { var label = "" if (settings.snowUnit == "in") { //TRANSLATORS: snow volume in inch, keep/remove the space before the unit according to your language specifications value = Math.round(calcInch(value)*100)/100 label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2).replace("0.00","0"))) } else if (settings.snowUnit == "mm") { //TRANSLATORS: snow volume in millimeter, keep/remove the space before the unit according to your language specifications label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0"))) } else { //TRANSLATORS: snow volume in centimeter, keep/remove the space before the unit according to your language specifications label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value.toFixed(1).replace("0.0","0"))) } return label } Component.onCompleted: { getRainSnowIcon(modelData.rain, modelData.snow, image) locationPages.collapseOtherDelegates.connect(function(otherIndex) { if (dayDelegate && typeof index !== "undefined" && otherIndex !== index) { state = "normal" } }); } }