~yohanboniface/osmtouch/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import QtQuick 2.0
import QtLocation 5.0
import QtPositioning 5.2
import Ubuntu.Components 0.1
import Ubuntu.Components.Popups 0.1
import QtQuick.XmlListModel 2.0
import Ubuntu.Components.ListItems 0.1 as ListItem
import "components" as Components
import "models" as Models
import "components/Helpers.js" as Helpers


/*!
    \brief MainView with a Label and Button elements.
*/

MainView {

    id: mapView
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "me.yohanboniface.osmtouch"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true
    width: units.gu(50)
    height: units.gu(75)
    backgroundColor: "#fff"

    actions: [
        Action {
            id: searchPlaceAction
            text: i18n.tr("Search a place")
            keywords: i18n.tr("Search a city, street, restaurant")
            onTriggered: {
                //searchLabel.text = "";
                //searchModel.source = "";
                PopupUtils.open(searchManager);
            }
        },
        Action {
            id: locateAction
            text: i18n.tr("Where am I")
            keywords: "Center the map on my position"
            onTriggered: {
                src.update();
                if (src.position.latitudeValid && src.position.longitudeValid) {
                    var coord = src.position.coordinate;
                    map.center.latitude = coord.latitude;
                    map.center.longitude = coord.longitude;
                    map.updateUserPosition(src.position);
                    map.zoomLevel = Math.max(map.zoomLevel, 17);
                } else if (mapPage.lastKnownLat && mapPage.lastKnownLng) {
                    map.center.latitude = mapPage.lastKnownLat;
                    map.center.longitude = mapPage.lastKnownLng;
                    map.zoomLevel = Math.max(map.zoomLevel, 14);
                    approximateGeolocation.show();
                } else {
                    PopupUtils.open(dialog);
                }
            }
        },
        Action {
            id: selectPoiAction
            // If I make those strings translatable, qmlscene crashes at start...
            text: "Points of interest nearby"
            keywords: "Restaurants, stations, shops…"
            onTriggered: {
                PopupUtils.open(poiManager);
            }
        }
    ]

    Rectangle {
        id: pageContainer
        color: UbuntuColors.warmGrey
        anchors.fill: parent

        Page {
            id: mapPage
            visible: true
            property double lastKnownLat
            property double lastKnownLng

            actions: [
                Action {
                    id: clearPoiAction
                    text: "Clear"
                    onTriggered: {
                        poiPlaceModel.clear();
                    }
                }
            ]

            Map {
                id: map
                zoomLevel: 5
                center {
                    latitude: 49.2
                    longitude: 4.1003
                }
                StateSaver.properties: "zoomLevel,center.latitude,center.longitude"
                property var poiBbox
                readonly property int minPoiZoom: 15

                plugin: Plugin {
                    id: osmPlugin
                    preferred: ["osm"]
                    PluginParameter { name: "useragent"; value: mapView.applicationName }
                }

                // Enable pinch gestures to zoom in and out
                gesture.flickDeceleration: 3000
                gesture.enabled: true
                anchors.fill: parent

                Component.onCompleted: {
                    src.update();
                }

                Models.PoiModel {
                    id: poiPlaceModel
                    onStatusChanged: {
                        if(status == XmlListModel.Error) {
                            httpFailedSearch.show();
                        }
                        if (status == XmlListModel.Loading) {
                            mapLoading.show();
                        } else {
                            mapLoading.hide();
                        }
                    }
                }

                Components.SplashComponent {
                    id: approximateGeolocation
                    objectName: "approximateGeolocation"
                    message: i18n.tr("No GPS available. Position is approximate.")
                }

                Components.SplashComponent {
                    id: httpFailedSearch
                    objectName: "HTTPFailedSearch"
                }

                Components.MapLoading {
                    id: mapLoading
                }

                MapItemView {
                    id: poiView

                    model: poiPlaceModel
                    delegate: MapQuickItem {
                        id: poiItem

                        coordinate.latitude: lat
                        coordinate.longitude: lng

                        anchorPoint.x: poiImage.width * 0.5
                        anchorPoint.y: poiImage.height
                        visible: map.zoomLevel >= map.minPoiZoom

                        sourceItem: Image {
                            id: poiImage

                            source: "icons/marker.svg"
                            width: 42
                            height: 54
                            z: 9
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    placePopover.update(model);
                                    placePopover.show();
                                    poiImage.state = "HIGHLIGHT"
                                }
                            }
                            states: State {
                                        name: "HIGHLIGHT"
                                        when: placePopover.visible && placePopover.osm_id === model.osm_id
                                        PropertyChanges {target: poiImage; source: "icons/marker_hl.svg"}
                                    }
                        }
                    }
                }

                MapCircle {
                    id: positionAccuracy
                    z: 1
                    visible: !!center.latitude
                    opacity: 0.3
                    radius: 100
                    color: 'blue'
                    border.width: 0
                }

                MapQuickItem {
                    id: userPosition
                    z: 10
                    //coordinate.latitude: 49.2
                    //coordinate.longitude: 4
                    visible: !!coordinate.latitude
                    anchorPoint.x: centerPositionImage.width/2
                    anchorPoint.y: centerPositionImage.height/2
                    sourceItem: Image {
                        id: centerPositionImage
                        width: 24
                        height: 24
                        source: "icons/position.svg"
                    }
                }

                MapQuickItem {
                    id: searchMarker
                    z: 11
                    //coordinate.latitude: 49.2
                    //coordinate.longitude: 4
                    visible: !!coordinate.latitude
                    anchorPoint.x: searchMarkerImage.width/2
                    anchorPoint.y: searchMarkerImage.height
                    sourceItem: Image {
                        id: searchMarkerImage
                        width: 42
                        height: 54
                        source: "icons/marker_neutral.svg"
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                searchMarker.coordinate.latitude = 0;
                                searchMarker.coordinate.longitude = 0;
                            }
                        }
                    }
                }

                onCenterChanged: {
                    if ((poiPlaceModel.clause && !poiBbox) || (poiBbox && !isInBbox(poiBbox))) {
                        fetchPoi();
                    }
                }

                function isInBbox (other) {
                    var b = bbox();
                    return !(b.left < other.left || b.top > other.top || b.right > other.right || b.bottom < other.bottom);
                }

                function bbox () {
                    var topLeft = map.toCoordinate(Qt.point(0, 0)),
                            bottomRight = map.toCoordinate(Qt.point(map.width, map.height)),
                            b = {
                                top: topLeft.latitude,
                                left: topLeft.longitude,
                                bottom: bottomRight.latitude,
                                right: bottomRight.longitude
                            };
                    return b;
                }

                function extendedBbox () {
                    var b = bbox(),
                            w = b.right - b.left,
                            h = b.top - b.bottom;
                    b.left = b.left - w;
                    b.right = b.right + w;
                    b.top = b.top + h;
                    b.bottom = b.bottom - h;
                    return b;
                }

                function toBboxString (b) {
                    // Left, top, right, bottom
                    b = b || bbox();
                    return [b.left, b.top, b.right, b.bottom].join(",");
                }

                function toBboxStringLbrt (b) {
                    // Left, bottom, right, top
                    b = b || bbox();
                    return [b.left, b.bottom, b.right, b.top].join(",");
                }

                function updateUserPosition (position) {
                    userPosition.coordinate = position.coordinate;
                    positionAccuracy.center = position.coordinate;
                    if (position.horizontalAccuracyValid && position.horizontalAccuracy) {
                        positionAccuracy.radius = position.horizontalAccuracy;
                    }
                }

                function resetPoi (clause, label) {
                    poiPlaceModel.clause = clause;
                    poiPlaceModel.label = label;
                    map.zoomLevel = Math.max(map.zoomLevel, map.minPoiZoom)
                    fetchPoi();
                }

                function fetchPoi () {
                    if (zoomLevel >= map.minPoiZoom) {
                        poiBbox = extendedBbox();
                        poiPlaceModel.search(toBboxStringLbrt(poiBbox));
                    }
                }
            }

            Rectangle {
                anchors {
                    right: parent.right
                    bottom: parent.bottom
                }
                height: units.gu(2)
                width: units.gu(22)
                opacity: 0.7
                Label {
                    text: " © OpenStreetMap contributors"
                    fontSize: "small"
                }
            }

            Components.PlacePopover {
                id: placePopover
            }

            PositionSource {
                id: src
                active: true
                updateInterval: 1000
                onPositionChanged: map.updateUserPosition(position)
            }

            tools: ToolbarItems {
                back: ToolbarButton {
                    action: clearPoiAction
                    text: i18n.tr("Clear")
                    iconSource: Helpers.getIcon("back")
                    visible: poiPlaceModel.isActive()
                }
                ToolbarButton {
                    action: locateAction
                    text: i18n.tr("Position")
                    iconSource: Qt.resolvedUrl("icons/locate.svg")
                }
                ToolbarButton {
                    action: searchPlaceAction
                    text: i18n.tr("Search")
                    iconSource: Helpers.getIcon("search")
                }
                ToolbarButton {
                    action: selectPoiAction
                    text: i18n.tr("Nearby")
                    iconSource: Helpers.getIcon("location")
                }
            }

            Models.GeoIPModel {
                // TODO find a way to trigger call only at button click
                id: geoIP
                onStatusChanged: {
                    if (status == XmlListModel.Ready && geoIP.get(0).city != "None") {
                        mapPage.lastKnownLng = geoIP.get(0).lng;
                        mapPage.lastKnownLat = geoIP.get(0).lat;
                    }
                }
            }

            Component {
                 id: dialog
                 Dialog {
                     id: dialogue
                     title: i18n.tr("Error")
                     text: i18n.tr("Sorry, no location available. Please check your location settings.")
                     Button {
                         text: i18n.tr("OK, too bad…")
                         onClicked: PopupUtils.close(dialogue)
                     }
                 }
            }
        }

        Components.SearchSheet {
            id: searchManager
        }

        Components.PoiSheet {
            id: poiManager
        }
    }
}