| 1 |
/* |
| 2 |
* Copyright (C) 2015-2016 Canonical Ltd. |
| 3 |
* Copyright (C) 2016 Nekhelesh Ramananthan |
| 4 |
* Copyright (C) 2016-2017 Andrew Hayzen |
| 5 |
* Copyright (C) 2019 Joan CiberSheep |
| 6 |
* Copyright (C) 2019 Krille-chan |
| 7 |
* Copyright (C) 2019-2022 Daniel Frost |
| 8 |
* Copyright (C) 2022 Guido Berhoerster |
| 9 |
* Copyright (C) 2024 Mike Gabriel |
| 10 |
* Copyright (C) 2024 Maciej Sopyło <[email protected]> |
| 11 |
* |
| 12 |
* SPDX-License-Identifier: GPL-3.0-only |
| 13 |
* |
| 14 |
* This file is part of Lomiri Weather App |
| 15 |
* |
| 16 |
* Lomiri Weather App is free software: you can redistribute it and/or modify |
| 17 |
* it under the terms of the GNU General Public License version 3 as |
| 18 |
* published by the Free Software Foundation. |
| 19 |
* |
| 20 |
* Lomiri Weather App is distributed in the hope that it will be useful, |
| 21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
* GNU General Public License for more details. |
| 24 |
* |
| 25 |
* You should have received a copy of the GNU General Public License |
| 26 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 |
*/ |
| 28 |
|
| 29 |
import QtQuick 2.12 |
| 30 |
import Lomiri.Components 1.3 |
| 31 |
import "../components" |
| 32 |
|
| 33 |
Page { |
| 34 |
// Set to null otherwise the header is shown (but blank) over the top of the listview |
| 35 |
id: homePage |
| 36 |
objectName: "homePage" |
| 37 |
flickable: null |
| 38 |
width: weatherApp.width |
| 39 |
|
| 40 |
Rectangle { |
| 41 |
//when swiping BottomEdge the DayDelegate underneath gets clicked |
| 42 |
//transparent MouseArea to catch click events near BottomEdge |
| 43 |
id: avoidDayDelegateClick |
| 44 |
width: parent.width |
| 45 |
height: units.gu(4) |
| 46 |
color: "transparent" |
| 47 |
z:1 //raise above DayDelegate's |
| 48 |
anchors { |
| 49 |
bottom: parent.bottom |
| 50 |
left: parent.left |
| 51 |
right: parent.right |
| 52 |
} |
| 53 |
MouseArea { |
| 54 |
anchors.fill: parent |
| 55 |
cursorShape: Qt.PointingHandCursor |
| 56 |
propagateComposedEvents: false |
| 57 |
hoverEnabled: false |
| 58 |
onClicked: { bottomEdge.commit()} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
BottomEdge { |
| 63 |
id: bottomEdge |
| 64 |
height: parent.height |
| 65 |
preloadContent: false //needs to be set to false to allow theme changing without errors |
| 66 |
|
| 67 |
// Note: cannot use contentUrl and preload until pad.lv/1604509 |
| 68 |
contentComponent: locationsListPage |
| 69 |
|
| 70 |
Component { |
| 71 |
id: locationsListPage |
| 72 |
LocationsListPage { |
| 73 |
height: bottomEdge.height |
| 74 |
width: bottomEdge.width |
| 75 |
|
| 76 |
onPop: bottomEdge.collapse() |
| 77 |
onThemeChanged: { |
| 78 |
//HACK: Dirty hack to avoid bottomEdge stops working after changing themes |
| 79 |
//preloadContent needs to be set to false for this to work |
| 80 |
bottomEdge.commit() |
| 81 |
bottomEdge.collapse() |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
hint.text: bottomEdge.hint.status == BottomEdgeHint.Locked ? i18n.tr("Locations") : "" |
| 86 |
} |
| 87 |
|
| 88 |
//system weather icons are used for forecast icons (3h and daily) |
| 89 |
//rain and snow icons are later replaced depending on downfall volume |
| 90 |
property var iconMap: { |
| 91 |
"sun": "weather-clear-symbolic", |
| 92 |
"moon": "weather-clear-night-symbolic", |
| 93 |
"few_cloud_sun": "weather-few-clouds-symbolic", |
| 94 |
"few_cloud_moon": "weather-few-clouds-night-symbolic", |
| 95 |
"scattered_cloud": "weather-clouds-symbolic", |
| 96 |
"cloud": "weather-overcast-symbolic", |
| 97 |
"showers": "weather-showers-scattered-symbolic", |
| 98 |
"rain": "weather-showers-symbolic", //no rain icon available (??) -> maybe rename icon upstream? |
| 99 |
"thunder": "weather-storm-symbolic", |
| 100 |
"snow": "weather-snow-symbolic", |
| 101 |
"fog": "weather-fog-symbolic" |
| 102 |
} |
| 103 |
//other available weather icons currently unused |
| 104 |
// weather-flurries-symbolic |
| 105 |
// weather-sleet-symbolic |
| 106 |
// weather-hazy-symbolic |
| 107 |
// weather-severe-alert-symbolic |
| 108 |
|
| 109 |
//local stored svg's are used for todays condition icon |
| 110 |
//"normal" icons lines are too thick when scaled up that big |
| 111 |
property var imageMap: { |
| 112 |
"sun": Qt.resolvedUrl("qrc:/weather-clear.svg"), |
| 113 |
"moon": Qt.resolvedUrl("qrc:/weather-clear-night.svg"), |
| 114 |
"few_cloud_sun": Qt.resolvedUrl("qrc:/weather-few-clouds.svg"), |
| 115 |
"few_cloud_moon": Qt.resolvedUrl("qrc:/weather-few-clouds-night.svg"), |
| 116 |
"scattered_cloud": Qt.resolvedUrl("qrc:/weather-clouds.svg"), |
| 117 |
"cloud": Qt.resolvedUrl("qrc:/weather-overcast.svg"), |
| 118 |
"showers": Qt.resolvedUrl("qrc:/weather-showers-scattered.svg"), |
| 119 |
"rain": Qt.resolvedUrl("qrc:/weather-showers.svg"), |
| 120 |
"thunder": Qt.resolvedUrl("qrc:/weather-storm.svg"), |
| 121 |
"snow": Qt.resolvedUrl("qrc:/weather-snow.svg"), |
| 122 |
"fog": Qt.resolvedUrl("qrc:/weather-fog.svg"), |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
wrapper function to extract wind bearings delivered from API and return them as translatable strings |
| 127 |
*/ |
| 128 |
function getWindBearing(windBearing) { |
| 129 |
|
| 130 |
if (windBearing == "N") { |
| 131 |
// TRANSLATORS: N = North, wind bearing, abbreviated |
| 132 |
return i18n.tr("N"); |
| 133 |
} else if (windBearing == "NE") { |
| 134 |
// TRANSLATORS: NE = North East, wind bearing, abbreviated |
| 135 |
return i18n.tr("NE"); |
| 136 |
} else if (windBearing == "E") { |
| 137 |
// TRANSLATORS: E = East, wind bearing, abbreviated |
| 138 |
return i18n.tr("E"); |
| 139 |
} else if (windBearing == "SE") { |
| 140 |
// TRANSLATORS: SE = South East, wind bearing, abbreviated |
| 141 |
return i18n.tr("SE"); |
| 142 |
} else if (windBearing == "S") { |
| 143 |
// TRANSLATORS: S = South, wind bearing, abbreviated |
| 144 |
return i18n.tr("S"); |
| 145 |
} else if (windBearing == "SW") { |
| 146 |
// TRANSLATORS: SW = South West, wind bearing, abbreviated |
| 147 |
return i18n.tr("SW"); |
| 148 |
} else if (windBearing == "W") { |
| 149 |
// TRANSLATORS: W = West, wind bearing, abbreviated |
| 150 |
return i18n.tr("W"); |
| 151 |
} else if (windBearing == "NW") { |
| 152 |
// TRANSLATORS: NW = NorthWest, wind bearing, abbreviated |
| 153 |
return i18n.tr("NW"); |
| 154 |
} else { |
| 155 |
//if wind bearing is none of the above just push it trough without translation |
| 156 |
return windBearing; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
Format date object by given format. |
| 162 |
*/ |
| 163 |
function formatTimestamp(dateData, format) { |
| 164 |
return Qt.formatDate(getDate(dateData), i18n.tr(format)) |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
Return date short format, which is default, so no special formatting needed. |
| 169 |
*/ |
| 170 |
function formatTimestampLocaleShort(dateData) { |
| 171 |
return Qt.formatDate(getDate(dateData)) |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
Format time object by given format. |
| 176 |
*/ |
| 177 |
function formatTime(dateData, format) { |
| 178 |
return Qt.formatTime(getDate(dateData), i18n.tr(format)) |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
Get Date object from dateData including hour data. |
| 183 |
*/ |
| 184 |
function getDate(dateData) { |
| 185 |
return new Date(dateData.year, dateData.month, dateData.date, dateData.hours, dateData.minutes) |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
Get Date object from dateData with additional offset [in minutes]. |
| 190 |
*/ |
| 191 |
function getCustomDate(dateData,offsetMin) { |
| 192 |
return new Date(dateData.year, dateData.month, dateData.date, dateData.hours, dateData.minutes + offsetMin) |
| 193 |
} |
| 194 |
|
| 195 |
function emptyIfUndefined(variable, append) { |
| 196 |
if (append === undefined) { |
| 197 |
append = "" |
| 198 |
} |
| 199 |
if (variable === undefined || variable === -255) { |
| 200 |
variable = "" |
| 201 |
append = "" //without valid value, drop append as well |
| 202 |
} |
| 203 |
return variable + append |
| 204 |
} |
| 205 |
|
| 206 |
//add temperature unit and convert if necessary |
| 207 |
function getTemp(tempToConvert, showunit) { |
| 208 |
var value = tempToConvert |
| 209 |
//query settings here, so function can be called without unit, used for current temperature |
| 210 |
if (settings.tempScale === "°F") { |
| 211 |
value = Math.round(calcFahrenheit(value)) |
| 212 |
if (showunit) { |
| 213 |
//TRANSLATORS: temperatures in °F, keep/remove the space before the unit according to your language specifications |
| 214 |
value = i18n.tr("%1 °F").arg(value) |
| 215 |
} |
| 216 |
} else { |
| 217 |
value = Math.round(value) |
| 218 |
if (showunit) { |
| 219 |
//TRANSLATORS: temperatures in °C, keep/remove the space before the unit according to your language specifications |
| 220 |
value = i18n.tr("%1 °C").arg(value) |
| 221 |
} |
| 222 |
} |
| 223 |
return value; |
| 224 |
} |
| 225 |
|
| 226 |
//add wind speed unit and convert if necessary |
| 227 |
function getWindSpeed(speed) { |
| 228 |
var value = speed //raw wind speed in m/s |
| 229 |
var speedstring //to hold the string, so translated units with/without space after the number can be used |
| 230 |
if (settings.windUnit === "m/s") { |
| 231 |
value = Math.round(value*10)/10 // keep one decimal |
| 232 |
//TRANSLATORS: wind speed in m/s, keep/remove the space before the unit according to your language specifications |
| 233 |
speedstring = i18n.tr("%1 m/s").arg(value.toString().replace(".",Qt.locale().decimalPoint)); |
| 234 |
} else if (settings.windUnit === "km/h") { |
| 235 |
value = Math.round(calcKmh(value)) |
| 236 |
//TRANSLATORS: wind speed in km/h, keep/remove the space before the unit according to your language specifications |
| 237 |
speedstring = i18n.tr("%1 km/h").arg(value); |
| 238 |
} else if (settings.windUnit === "mph") { |
| 239 |
value = Math.round(calcMph(value)) |
| 240 |
//TRANSLATORS: wind speed in mph, keep/remove the space before the unit according to your language specifications |
| 241 |
speedstring = i18n.tr("%1 mph").arg(value); |
| 242 |
} else { |
| 243 |
console.log("wind unit error ") |
| 244 |
} |
| 245 |
return speedstring; |
| 246 |
} |
| 247 |
|
| 248 |
//add rain volume unit, adjust decimals and convert if necessary |
| 249 |
//called from LocationPage for current info (currently na) and DayDelegateExtraInfo/daily info, HomeHourly for hourly info |
| 250 |
function getRain(rain) { |
| 251 |
var value = rain |
| 252 |
var label = "" |
| 253 |
if (settings.precipUnit == "in") { |
| 254 |
// convert to inch if needed and round numbers |
| 255 |
// keep two dezimals |
| 256 |
value = Math.round(calcInch(value)*100)/100; |
| 257 |
} else { |
| 258 |
// keep one dezimal |
| 259 |
// l/sqm is the same number as mm, no further adjustment needed |
| 260 |
value = Math.round(value*10)/10 |
| 261 |
} |
| 262 |
// add unit according to setting |
| 263 |
// put in decimal separator according to set locale |
| 264 |
if (settings.precipUnit == "in") { |
| 265 |
//TRANSLATORS: rain/snow volume in inch, keep/remove the space before the unit according to your language specifications |
| 266 |
label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2))) |
| 267 |
} else if (settings.precipUnit == "l") { |
| 268 |
//TRANSLATORS: rain volume in litre per squaremeter, keep/remove the space before the unit according to your language specifications |
| 269 |
label = i18n.tr("%1 l/m²").arg(localeDezimalSeparator(value)) |
| 270 |
} else { |
| 271 |
//TRANSLATORS: rain/snow volume in millimeter, keep/remove the space before the unit according to your language specifications |
| 272 |
label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value)) |
| 273 |
} |
| 274 |
return label; |
| 275 |
} |
| 276 |
|
| 277 |
//add snow volume unit, adjust decimals and convert if necessary |
| 278 |
//called from LocationPage for current info (currently na) and DayDelegateExtraInfo/daily info, HomeHourly for hourly info |
| 279 |
function getSnow(snow) { |
| 280 |
var value = snow |
| 281 |
var label = "" |
| 282 |
if (settings.snowUnit == "in") { |
| 283 |
// convert to inch if needed and round numbers |
| 284 |
// keep two dezimals |
| 285 |
value = Math.round(calcInch(value)*100)/100; |
| 286 |
} else { |
| 287 |
// keep one dezimal |
| 288 |
// l/sqm is the same number as mm, no further adjustment needed |
| 289 |
value = Math.round(value*10)/10 |
| 290 |
} |
| 291 |
// add unit according to setting |
| 292 |
// put in decimal separator according to set locale |
| 293 |
if (settings.snowUnit == "in") { |
| 294 |
//TRANSLATORS: rain/snow volume in inch, keep/remove the space before the unit according to your language specifications |
| 295 |
label = i18n.tr("%1 in").arg(localeDezimalSeparator(value.toFixed(2))) |
| 296 |
} else if (settings.snowUnit == "mm") { |
| 297 |
//TRANSLATORS: rain/snow volume in millimeter, keep/remove the space before the unit according to your language specifications |
| 298 |
label = i18n.tr("%1 mm").arg(localeDezimalSeparator(value)) |
| 299 |
} else { |
| 300 |
//TRANSLATORS: snow volume in centimeter, keep/remove the space before the unit according to your language specifications |
| 301 |
label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value)) |
| 302 |
//TRANSLATORS: snow volume in centimeter, keep/remove the space before the unit according to your language specifications |
| 303 |
if (value < 0.95) { //avoid 0.95 being rounded to 1.0 cm instead of 1 cm |
| 304 |
label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value.toFixed(1))) |
| 305 |
} else { |
| 306 |
label = i18n.tr("%1 cm").arg(localeDezimalSeparator(value.toFixed(0))) |
| 307 |
} |
| 308 |
} |
| 309 |
return label; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
add UV-index level based on WHO classification |
| 314 |
https://www.who.int/news-room/questions-and-answers/item/radiation-the-ultraviolet-(uv)-index |
| 315 |
*/ |
| 316 |
function getUVLevel(value) { |
| 317 |
var level |
| 318 |
if (value >= 0 && value < 3) { |
| 319 |
level = i18n.tr("low") |
| 320 |
} else if (value >= 3 && value < 6) { |
| 321 |
level = i18n.tr("moderate") |
| 322 |
} else if (value >= 6 && value < 8) { |
| 323 |
level = i18n.tr("high") |
| 324 |
} else if (value >= 8 && value < 11) { |
| 325 |
level = i18n.tr("very high") |
| 326 |
} else if (value >= 11) { |
| 327 |
level = i18n.tr("extreme") |
| 328 |
} else { |
| 329 |
level = i18n.tr("no valid value") |
| 330 |
} |
| 331 |
|
| 332 |
return " (%1)".arg(level) |
| 333 |
} |
| 334 |
|
| 335 |
/* |
| 336 |
provide visibility distance based on visibility range in different units |
| 337 |
*/ |
| 338 |
function getVisibility(value) { |
| 339 |
var visibility |
| 340 |
|
| 341 |
if (value > 1000) { |
| 342 |
visibility = localeDezimalSeparator((value / 1000).toFixed(1)) + " km" |
| 343 |
} else { |
| 344 |
visibility = localeDezimalSeparator(value) + " m" |
| 345 |
} |
| 346 |
|
| 347 |
return " %1".arg(visibility) |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
unit converting helper functions |
| 352 |
*/ |
| 353 |
//convert mm to in, both volume per hour |
| 354 |
function calcInch(mm) { |
| 355 |
return mm/25.4; |
| 356 |
} |
| 357 |
//convert celsius to fahrenheit |
| 358 |
function calcFahrenheit(celsius) { |
| 359 |
return celsius * 1.8 + 32; |
| 360 |
} |
| 361 |
// convert m/s to mph |
| 362 |
function calcMph(ms) { |
| 363 |
return ms*2.24; |
| 364 |
} |
| 365 |
|
| 366 |
//convert |
| 367 |
function calcKmh(ms) { |
| 368 |
return ms*3.6; |
| 369 |
} |
| 370 |
// |
| 371 |
function convertKmhToMph(kmh) { |
| 372 |
return kmh*0.621; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
// Do not show the Page Header |
| 377 |
header: PageHeader { |
| 378 |
visible: false |
| 379 |
} |
| 380 |
|
| 381 |
/* |
| 382 |
ListView for locations with snap-scrolling horizontally. |
| 383 |
*/ |
| 384 |
ListView { |
| 385 |
id: locationPages |
| 386 |
objectName: "locationPages" |
| 387 |
anchors { |
| 388 |
fill: parent |
| 389 |
} |
| 390 |
contentHeight: parent.height - bottomEdge.hint.height |
| 391 |
currentIndex: settings.current |
| 392 |
delegate: LocationPane { |
| 393 |
objectName: "locationPane" + index |
| 394 |
} |
| 395 |
highlightRangeMode: ListView.StrictlyEnforceRange |
| 396 |
highlightMoveDuration: 50 |
| 397 |
highlightMoveVelocity: 600 |
| 398 |
model: weatherApp.locationsList.length |
| 399 |
orientation: ListView.Horizontal |
| 400 |
// TODO with snapMode, currentIndex is not properly set and setting currentIndex fails |
| 401 |
//snapMode: ListView.SnapOneItem |
| 402 |
|
| 403 |
property bool loaded: false |
| 404 |
|
| 405 |
signal collapseOtherDelegates(int index) |
| 406 |
|
| 407 |
onCurrentIndexChanged: { |
| 408 |
if (loaded) { |
| 409 |
// FIXME: when a model is reloaded this causes the currentIndex to be lost |
| 410 |
settings.current = currentIndex |
| 411 |
|
| 412 |
collapseOtherDelegates(-1) // collapse all |
| 413 |
} |
| 414 |
} |
| 415 |
onModelChanged: { |
| 416 |
currentIndex = settings.current |
| 417 |
|
| 418 |
if (model > 0) { |
| 419 |
loading = false |
| 420 |
loaded = true |
| 421 |
} |
| 422 |
} |
| 423 |
onVisibleChanged: { |
| 424 |
if (!visible && loaded) { |
| 425 |
collapseOtherDelegates(-1) // collapse all |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// TODO: workaround for not being able to use snapMode property |
| 430 |
Component.onCompleted: { |
| 431 |
var scaleFactor = units.gridUnit * 10; |
| 432 |
maximumFlickVelocity = maximumFlickVelocity * scaleFactor; |
| 433 |
flickDeceleration = flickDeceleration * scaleFactor; |
| 434 |
} |
| 435 |
|
| 436 |
Connections { |
| 437 |
target: settings |
| 438 |
onCurrentChanged: { |
| 439 |
locationPages.currentIndex = settings.current |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
LoadingIndicator { |
| 445 |
id: loadingIndicator |
| 446 |
anchors { |
| 447 |
bottom: parent.bottom |
| 448 |
left: parent.left |
| 449 |
right: parent.right |
| 450 |
} |
| 451 |
processing: loading |
| 452 |
} |
| 453 |
|
| 454 |
Loader { |
| 455 |
active: (locationsList === null || locationsList.length === 0) && mainPageStack.depth === 1 |
| 456 |
anchors { |
| 457 |
fill: parent |
| 458 |
} |
| 459 |
asynchronous: true |
| 460 |
source: "../components/HomePageEmptyStateComponent.qml" |
| 461 |
visible: status === Loader.Ready && active |
| 462 |
} |
| 463 |
|
| 464 |
Loader { |
| 465 |
// NOTE: re-enable when adding a provider that requires an API key |
| 466 |
active: false;// mainPageStack.depth === 1 && !OpenWeatherMap.key |
| 467 |
anchors { |
| 468 |
fill: parent |
| 469 |
} |
| 470 |
asynchronous: true |
| 471 |
source: "../components/NoAPIKeyErrorStateComponent.qml" |
| 472 |
visible: status === Loader.Ready && active |
| 473 |
} |
| 474 |
} |