1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51import QtQuick 2.5
52import QtLocation 5.6
53import QtPositioning 5.5
54
55RouteAddressForm {
56    property alias plugin : tempGeocodeModel.plugin;
57    property variant fromAddress;
58    property variant toAddress;
59    signal showMessage(string topic, string message)
60    signal showRoute(variant startCoordinate,variant endCoordinate)
61    signal closeForm()
62
63    goButton.onClicked: {
64        tempGeocodeModel.reset()
65        fromAddress.country =  fromCountry.text
66        fromAddress.street = fromStreet.text
67        fromAddress.city =  fromCity.text
68        toAddress.country = toCountry.text
69        toAddress.street = toStreet.text
70        toAddress.city = toCity.text
71        tempGeocodeModel.startCoordinate = QtPositioning.coordinate()
72        tempGeocodeModel.endCoordinate = QtPositioning.coordinate()
73        tempGeocodeModel.query = fromAddress
74        tempGeocodeModel.update();
75        goButton.enabled = false;
76    }
77
78    clearButton.onClicked: {
79        fromStreet.text = ""
80        fromCity.text = ""
81        fromCountry.text = ""
82        toStreet.text = ""
83        toCity.text = ""
84        toCountry.text = ""
85    }
86
87    cancelButton.onClicked: {
88        closeForm()
89    }
90
91    Component.onCompleted: {
92        fromStreet.text  = fromAddress.street
93        fromCity.text =  fromAddress.city
94        fromCountry.text = fromAddress.country
95        toStreet.text = toAddress.street
96        toCity.text = toAddress.city
97        toCountry.text = toAddress.country
98    }
99
100    GeocodeModel {
101        id: tempGeocodeModel
102
103        property int success: 0
104        property variant startCoordinate
105        property variant endCoordinate
106
107        onCountChanged: {
108            if (success == 1 && count == 1) {
109                query = toAddress
110                update();
111            }
112        }
113
114        onStatusChanged: {
115            if ((status == GeocodeModel.Ready) && (count == 1)) {
116                success++
117                if (success == 1) {
118                    startCoordinate.latitude = get(0).coordinate.latitude
119                    startCoordinate.longitude = get(0).coordinate.longitude
120                }
121                if (success == 2) {
122                    endCoordinate.latitude = get(0).coordinate.latitude
123                    endCoordinate.longitude = get(0).coordinate.longitude
124                    success = 0
125                    if (startCoordinate.isValid && endCoordinate.isValid)
126                        showRoute(startCoordinate,endCoordinate)
127                    else
128                        goButton.enabled = true
129                }
130            } else if ((status == GeocodeModel.Ready) || (status == GeocodeModel.Error)) {
131                var st = (success == 0 ) ? "start" : "end"
132                success = 0
133                if ((status == GeocodeModel.Ready) && (count == 0 )) {
134                    showMessage(qsTr("Geocode Error"),qsTr("Unsuccessful geocode"));
135                    goButton.enabled = true;
136                }
137                else if (status == GeocodeModel.Error) {
138                    showMessage(qsTr("Geocode Error"),
139                                qsTr("Unable to find location for the") + " " +
140                                st + " " +qsTr("point"))
141                    goButton.enabled = true;
142                }
143                else if ((status == GeocodeModel.Ready) && (count > 1 )) {
144                    showMessage(qsTr("Ambiguous geocode"),
145                                count + " " + qsTr("results found for the") +
146                                " " + st + " " +qsTr("point, please specify location"))
147                    goButton.enabled = true;
148                }
149            }
150        }
151    }
152}
153