Debian Codegraph @

src/app/qml/ui/settings/RainUnitPage.qml

raw
1 /*
2 * Copyright (C) 2020 UBports Foundation
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: rainUnitsPage
25 height: rainSetting.height
26
27 ListModel {
28 id: rainUnitModel
29 function initialize() {
30 // TRANSLATORS: millimeter, metric unit for rain/snow given as millimeter per hour, only use abbreviated
31 rainUnitModel.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 rainUnitModel.append({"text": i18n.tr("in"), "value": "in"})
35
36 // TRANSLATORS: liter, metric unit for rain/snow given as liter per squaremeter per hour, only use abbreviated
37 rainUnitModel.append({"text": i18n.tr("l/m²"), "value": "l"})
38 }
39 }
40
41 OptionSelector {
42 id: rainSetting
43 text: i18n.tr("Rain volume unit")
44 model: rainUnitModel
45 containerHeight: itemHeight * rainUnitModel.count
46 delegate: OptionSelectorDelegate {
47 text: model.text
48 height: units.gu(4)
49 }
50 onDelegateClicked: {
51 settings.precipUnit = 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 rainUnitModel.initialize()
62 for (var i = 0; i < rainUnitModel.count; ++i) {
63 if (rainUnitModel.get(i).value === settings.precipUnit) {
64 rainSetting.selectedIndex = i
65 return
66 }
67 }
68 rainSetting.selectedIndex = 0 // in case no match is found due to broken settings
69 }
70 }
71 }