Debian Codegraph @

src/app/qml/ui/settings/SnowUnitPage.qml

raw
1 /*
2 * Copyright (C) 2020 UBports
3 *
4 * This file is part of Ubuntu Weather App
5 *
6 * Ubuntu 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 * Ubuntu 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: unitsPage
25 height: snowSetting.height
26
27 ListModel {
28 id: snowUnitModel
29 function initialize() {
30 // TRANSLATORS: millimeter, metric unit for rain/snow given as millimeter per hour, only use abbreviated
31 snowUnitModel.append({"text": i18n.tr("mm"), "value": "mm"})
32
33 // TRANSLATORS: inch, imperial unit for rain/snow given as inch per hour, only use abbreviated
34 snowUnitModel.append({"text": i18n.tr("in"), "value": "in"})
35
36 // TRANSLATORS: centimeter, metric unit for snow given as centimeter snow height, only use abbreviated
37 snowUnitModel.append({"text": i18n.tr("cm"), "value": "cm"})
38 }
39 }
40
41 OptionSelector {
42 id: snowSetting
43 text: i18n.tr("Snow volume unit")
44 model: snowUnitModel
45 containerHeight: itemHeight * snowUnitModel.count
46 delegate: OptionSelectorDelegate {
47 text: model.text
48 height: units.gu(4)
49 }
50 onDelegateClicked: {
51 settings.snowUnit = model.get(index).value
52 refreshData(true);
53 }
54 Component.onCompleted: {
55 /*
56 The Component.onCompleted of the OptionSelector finishes BEFORE
57 the onCompleted events of the delegates or the model.
58 That is why the initialize() needs to be called here rather than
59 in the Component.onCompleted of the ListModel.
60 */
61 snowUnitModel.initialize()
62 for (var i = 0; i < snowUnitModel.count; ++i) {
63 if (snowUnitModel.get(i).value === settings.snowUnit) {
64 snowSetting.selectedIndex = i
65 return
66 }
67 }
68 snowSetting.selectedIndex = 0 // in case no match is found due to broken settings
69 }
70 }
71 }