Debian Codegraph @

src/app/qml/ui/LocationsListPage.qml

raw
1 /*
2 * Copyright (C) 2015, 2017 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 import "../components/HeadState"
23 import "../components/ListItemActions"
24
25
26 Page {
27 id: locationsListPage
28 objectName: "locationsListPage"
29 state: locationsListView.state === "multiselectable" ? "selection" : "default"
30 states: [
31 LocationsHeadState {
32 thisPage: locationsListPage
33 },
34 MultiSelectHeadState {
35 listview: locationsListView
36 removable: true
37 thisPage: locationsListPage
38
39 onRemoved: storage.removeMultiLocations(selectedIndices.slice());
40 }
41 ]
42
43 // This is used by the header state (LocationsHeadState) to go back
44 signal pop()
45
46 ListModel {
47 id: currentLocationModel
48 }
49
50 MultiSelectListView {
51 id: locationsListView
52 anchors {
53 bottom: parent.bottom
54 left: parent.left
55 right: parent.right
56 top: locationsListPage.header.bottom
57 }
58 model: ListModel {
59 id: locationsModel
60 }
61 header: MultiSelectListView {
62 id: currentLocationListView
63 anchors {
64 left: parent.left
65 right: parent.right
66 }
67 height: settings.addedCurrentLocation && settings.detectCurrentLocation ? contentHeight : units.gu(0)
68 interactive: false
69 model: currentLocationModel
70 delegate: WeatherListItem {
71 id: currentLocationListItem
72 height: currentListItemLayout.height + (divider.visible ? divider.height : 0) + (fakeDivider.visible ? fakeDivider.height : 0)
73
74 onItemClicked: {
75 settings.current = index;
76
77 // Ensure any selections are closed
78 locationsListView.closeSelection();
79
80 locationsListPage.pop()
81 }
82
83 ListItemLayout {
84 id: currentListItemLayout
85 // uncomment to show locations index in front of location name
86 // Label {
87 // anchors {
88 // verticalCenter: parent.verticalCenter
89 // }
90 // font {
91 // pixelSize: parent.height / 3
92 // weight: Font.Light
93 // }
94 // horizontalAlignment: Text.AlignLeft
95 // SlotsLayout.position: SlotsLayout.Leading
96 // SlotsLayout.overrideVerticalPositioning: true
97 // text: index+1
98 // }
99 title {
100 elide: Text.ElideRight
101 fontSize: "medium"
102 text: i18n.tr("Current Location")
103 }
104 subtitle {
105 elide: Text.ElideRight
106 fontSize: "small"
107 text: name + ", " + (adminName1 == name ? countryName : adminName1)
108 }
109 summary {
110 elide: Text.ElideRight
111 fontSize: "small"
112 text: provider_coords
113 }
114
115 Icon {
116 anchors {
117 verticalCenter: parent.verticalCenter
118 }
119 height: parent.height / 2
120 width: height
121 name: icon
122 SlotsLayout.position: SlotsLayout.Trailing
123 SlotsLayout.overrideVerticalPositioning: true
124
125 }
126
127 Label {
128 anchors {
129 verticalCenter: parent.verticalCenter
130 }
131 width: units.gu(8.5) //needed to align weather icon regardless of number of digits
132 font {
133 pixelSize: parent.height / 3
134 weight: Font.Light
135 }
136 horizontalAlignment: Text.AlignRight
137 SlotsLayout.position: SlotsLayout.Trailing
138 SlotsLayout.overrideVerticalPositioning: true
139 text: temp
140 }
141 }
142
143 // Inject extra divider when we are the last as the SDK hides it
144 // but we need one as we have another listview directly below
145 ListItem {
146 id: fakeDivider
147 anchors {
148 bottom: parent.bottom
149 }
150 divider {
151 visible: true
152 }
153 height: divider.height
154 visible: currentLocationModel.count - 1 == index
155 }
156 }
157 }
158
159 delegate: WeatherListItem {
160 id: locationsListItem
161 height: listItemLayout.height + (divider.visible ? divider.height : 0)
162 leadingActions: ListItemActions {
163 actions: [
164 Remove {
165 onTriggered: storage.removeLocation(index)
166 }
167 ]
168 }
169 multiselectable: true
170 objectName: "location" + index
171 reorderable: true
172
173 onItemClicked: {
174 if (settings.addedCurrentLocation && settings.detectCurrentLocation) {
175 settings.current = index + 1;
176 } else {
177 settings.current = index;
178 }
179
180 locationsListPage.pop()
181 }
182
183 ListItemLayout {
184 id: listItemLayout
185 // uncomment to show locations index infront of location name
186 // Label {
187 // anchors.verticalCenter: parent.verticalCenter
188 // font {
189 // pixelSize: parent.height / 4
190 // weight: Font.Light
191 // }
192 // horizontalAlignment: Text.AlignLeft
193 // SlotsLayout.position: SlotsLayout.Leading
194 // SlotsLayout.overrideVerticalPositioning: true
195 // text: index+1
196 // }
197 title {
198 elide: Text.ElideRight
199 fontSize: "medium"
200 text: name
201 }
202 subtitle {
203 elide: Text.ElideRight
204 fontSize: "small"
205 text: adminName1 == name ? countryName : adminName1 + ", " + countryName
206 }
207 summary {
208 elide: Text.ElideRight
209 fontSize: "small"
210 text: geonames_id + "\n" + geonames_coords
211 }
212
213 Icon {
214 anchors {
215 verticalCenter: parent.verticalCenter
216 }
217 height: parent.height / 2
218 width: height
219 name: icon
220 SlotsLayout.position: SlotsLayout.Trailing
221 SlotsLayout.overrideVerticalPositioning: true
222 visible: locationsListView.state === "default"
223 }
224
225 Label {
226 anchors {
227 verticalCenter: parent.verticalCenter
228 }
229 width: units.gu(8.5) //needed to align weather icon regardless of number of digits
230 font {
231 pixelSize: parent.height / 3
232 weight: Font.Light
233 }
234 horizontalAlignment: Text.AlignRight
235 SlotsLayout.position: SlotsLayout.Trailing
236 SlotsLayout.overrideVerticalPositioning: true
237 text: temp
238 visible: locationsListView.state === "default"
239 }
240 }
241 }
242
243 onReorder: {
244 console.debug("Move: ", from, to);
245
246 storage.moveLocation(from, to);
247 }
248 }
249
250 LoadingIndicator {
251 id: loadingIndicator
252 }
253
254 Loader {
255 id: pageLoader
256 active: locationsList === null || locationsList.length === 0
257 anchors {
258 fill: parent
259 }
260 asynchronous: true
261 source: "../components/LocationsListPageEmptyStateComponent.qml"
262 visible: status === Loader.Ready && active
263 }
264
265 function populateLocationsModel() {
266 currentLocationModel.clear()
267 locationsModel.clear()
268 var loc = {}, data = {};
269
270 for (var i=0; i < weatherApp.locationsList.length; i++) {
271 data = weatherApp.locationsList[i];
272 loc = {
273 "name": data.location.name,
274 "adminName1": data.location.adminName1,
275 "areaLabel": data.location.areaLabel,
276 "countryName": data.location.countryName,
277 "temp": getTemp(data.data[0].current["metric"].temp,true),
278 "icon": iconMap[data.data[0].current.icon],
279 // "provider_id": i18n.ctr("Location ID from weather-data provider","Station ID: %1").arg(data.data[0].current.service_id),
280 // provider coords are quite a bit off with open-meteo.com data, so for the moment we better use geonames.org station data
281 // "provider_coords": i18n.ctr("Latitude and longitude (coordinates)","lat: %1, lon: %2").arg(data.data[0].current.coordLAT).arg(data.data[0].current.coordLON),
282 // use the function localeDezimalSeparator() to convert the "11.222, 33.444" formating to use the locale-decimal separator if needed, then use ; to separate the two numbers
283 "geonames_id": i18n.ctr("Location ID from geonames.org","geonames.org ID: %1").arg(data.location.services.geonames),
284 "geonames_coords": i18n.ctr("Latitude and longitude (coordinates)","lat: %1, lon: %2").arg(data.location.coord.lat).arg(data.location.coord.lon)
285 }
286
287 if (!settings.addedCurrentLocation || i > 0) {
288 locationsModel.append(loc)
289 } else {
290 currentLocationModel.append(loc)
291 }
292 }
293 }
294
295 Connections {
296 target: weatherApp
297 onLocationsListChanged: populateLocationsModel()
298 }
299
300 Component.onCompleted: populateLocationsModel()
301 }