Debian Codegraph @

examples/unit-converter/unit-converter.qml

raw
1 /*
2 * Copyright 2012 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import "conversion.js" as Converter
20
21 /*!
22 \brief Example unit converter application.
23
24 The application demonstrates the usage of
25 - i18n
26 - units
27 - Label
28 - Loader
29 - Page
30 - PageHeader
31 - Action
32 - TextField and
33 - Button components
34
35 The application converts length and weight units between several metrics.
36 Related units are grouped in the same page and conversion happens when
37 pressing Enter/Return after entering a number in one of the input fields (i.e.
38 accepting the entered text), or by pressing the "Convert" button.
39
40 The navigation between converter pages is provided by the PageHeader component.
41 */
42
43 MainView {
44 id: root
45 // objectName for functional testing purposes (autopilot-qt5)
46 objectName: "mainView"
47
48 // Note! applicationName needs to match the .desktop filename
49 applicationName: "unit-converter"
50
51 /*
52 This property enables the application to change orientation
53 when the device is rotated. The default is false.
54 */
55 automaticOrientation: true
56
57 width: units.gu(100)
58 height: units.gu(75)
59
60 property real margins: units.gu(2)
61 property real labelWidth: units.gu(12)
62
63 // Length conversion model; the unit is Mile
64 property var lengthModel: [
65 {"unit": "Inch", "rate": 63360.0},
66 {"unit": "Meter", "rate": 1609.344},
67 {"unit": "Miles", "rate": 1.0},
68 {"unit": "Feets", "rate": 5280.0},
69 {"unit": "Yards", "rate": 1760.0},
70 {"unit": "Kilometers", "rate": 1.609344},
71 ]
72
73 // Weight conversion model; the base unit is Pound
74 property var weightModel: [
75 {"unit": "Pounds", "rate": 1.0},
76 {"unit": "Kilograms", "rate": 0.45359237},
77 {"unit": "Ounces", "rate": 16},
78 {"unit": "Stones", "rate": 0.0714285714},
79 {"unit": "US Tons", "rate": 0.0005},
80 {"unit": "UK Tons", "rate": 0.000446428571},
81 ]
82
83 // converter page template
84 Component {
85 id: pageContent
86 Page {
87 header: PageHeader {
88 title: "Lomiri header"
89 leadingActionBar.actions: [
90 Action {
91 text: "Lengths"
92 onTriggered: {
93 if (status === Loader.Ready && item) {
94 item.parent = parent;
95 item.model = lengthModel;
96 header.title="Lengths"
97 }
98 }
99 },
100 Action {
101 text: "Weights"
102 onTriggered: {
103 if (status === Loader.Ready && item) {
104 item.parent = parent;
105 item.model = weightModel;
106 header.title="Weights"
107
108 }
109 }
110 }
111 ]
112 trailingActionBar.actions: [
113 Action {
114 objectName: "action"
115 iconName: "toolkit_input-clear"
116 text: i18n.tr("Clear")
117 onTriggered: Converter.clear(converter)
118 }
119
120 ]
121 }
122
123 // expose Repeater's model for reusability, so we can set it from
124 // outside, when we build the tabs
125 property alias model: converter.model
126
127 // remove the input panel when pressed outside of any text input
128 MouseArea {
129 anchors.fill: parent
130 onPressed: Qt.inputMethod.hide();
131 }
132
133 Flickable {
134 id: flickable
135 anchors {
136 fill: parent
137 margins: root.margins
138 }
139 flickableDirection: Flickable.VerticalFlick
140 contentWidth: pageLayout.width
141 contentHeight: pageLayout.height
142 Column {
143 id: pageLayout
144 width: flickable.width
145 height: childrenRect.height
146
147 spacing: units.gu(1.2)
148 // show as many lines as many units we have in the model
149 // it is assumed that the model has "unit" and "rate" roles
150 Repeater {
151 id: converter
152 Row {
153 spacing: units.gu(1)
154 Label {
155 text: i18n.tr(modelData.unit)
156 textSize: Label.Large
157 width: root.labelWidth
158 height: input.height
159 verticalAlignment: Text.AlignVCenter
160 }
161 // input field performing conversion
162 TextField {
163 id: input
164 //errorHighlight: false
165 validator: DoubleValidator {notation: DoubleValidator.StandardNotation}
166 width: pageLayout.width - root.labelWidth - spacing
167 text: "0.0"
168 font.pixelSize: FontUtils.sizeToPixels("large")
169 height: units.gu(4)
170 // on-the-fly conversion
171 onTextChanged: if (activeFocus) Converter.convert(input, converter, index); else cursorPosition = 0
172 onAccepted: Qt.inputMethod.hide()
173 }
174 }
175 }
176 }
177 }
178 }
179 }
180 Loader {
181 sourceComponent: pageContent
182 onStatusChanged: {
183 if (status === Loader.Ready && item) {
184 item.parent = parent;
185 item.model = lengthModel;
186 item.header.title="Lengths"
187 }
188
189 }
190 }
191
192 }