Debian Codegraph @

src/app/qml/ui/settings/ForecastDaysPage.qml

raw
1 /*
2 * Copyright (C) 2015-2016 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 "../../components"
22
23 Item {
24 id: forecastDaysPage
25 height: forecastDaysSetting.height
26
27 ListModel {
28 id: forecastDaysModel
29 function initialize() {
30 // TRANSLATORS: %1 will be replaced with the number of days
31 forecastDaysModel.append({"days": 5, "text": i18n.tr("%1 days").arg(5)})
32 forecastDaysModel.append({"days": 6, "text": i18n.tr("%1 days").arg(6)})
33 forecastDaysModel.append({"days": 7, "text": i18n.tr("%1 days").arg(7)})
34 forecastDaysModel.append({"days": 8, "text": i18n.tr("%1 days").arg(8)})
35 forecastDaysModel.append({"days": 9, "text": i18n.tr("%1 days").arg(9)})
36 forecastDaysModel.append({"days": 10, "text": i18n.tr("%1 days").arg(10)})
37 forecastDaysModel.append({"days": 11, "text": i18n.tr("%1 days").arg(11)})
38 forecastDaysModel.append({"days": 12, "text": i18n.tr("%1 days").arg(12)})
39 forecastDaysModel.append({"days": 13, "text": i18n.tr("%1 days").arg(13)})
40 forecastDaysModel.append({"days": 14, "text": i18n.tr("%1 days").arg(14)})
41 forecastDaysModel.append({"days": 15, "text": i18n.tr("%1 days").arg(15)})
42 }
43 }
44
45 OptionSelector {
46 id: forecastDaysSetting
47 text: i18n.tr("Number of forecast days")
48 model: forecastDaysModel
49 containerHeight: itemHeight * forecastDaysModel.count
50 delegate: OptionSelectorDelegate {
51 text: model.text
52 height: units.gu(4)
53 }
54 onDelegateClicked: {
55 // offset setting value by 1, because the current day's data is counted as one day
56 settings.forecastDays = model.get(index).days + 1
57 refreshData(false, true);
58 }
59 }
60 Component.onCompleted: {
61 /*
62 The Component.onCompleted of the OptionSelector finishes BEFORE
63 the onCompleted events of the delegates or the model.
64 That is why the initialize() needs to be called here rather than
65 in the Component.onCompleted of the ListModel.
66 */
67 forecastDaysModel.initialize()
68 for (var i = 0; i < forecastDaysModel.count; ++i) {
69 // offset setting value by 1, because the current day's data is counted as one day
70 if (forecastDaysModel.get(i).days === settings.forecastDays - 1) {
71 forecastDaysSetting.selectedIndex = i
72 return
73 }
74 }
75 forecastDaysSetting.selectedIndex = 0 // in case no match is found due to broken settings
76 }
77 }