1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPositioning module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qdeclarativegeolocation_p.h"
41 
42 QT_USE_NAMESPACE
43 
44 /*!
45     \qmltype Location
46     \inqmlmodule QtPositioning
47     \since 5.2
48 
49     \brief The Location type holds location data.
50 
51     Location types represent a geographic "location", in a human sense. This
52     consists of a specific \l {coordinate}, an \l {address} and a \l {boundingBox}{bounding box}.
53     The \l {boundingBox}{bounding box} represents the recommended region
54     to display when viewing this location.
55 
56     The Location type is most commonly seen as the contents of a search
57     model such as the GeocodeModel. When a GeocodeModel returns the list of
58     locations found for a given query, it represents these as Location objects.
59 
60     \section2 Example Usage
61 
62     The following example shows a simple Location object being declared:
63 
64     \code
65     Location {
66         coordinate {
67             latitude: -27.3
68             longitude: 153.1
69         }
70         address: Address {
71             ...
72         }
73     }
74     \endcode
75 */
76 
77 /*!
78     \qmlproperty VariantMap QDeclarativeGeoLocation::extendedAttributes
79 
80     This property holds the extended attributes for this Location.
81     Extended attributes are backend-dependent and can be location-dependent.
82 
83     \since 5.13
84 */
85 
QDeclarativeGeoLocation(QObject * parent)86 QDeclarativeGeoLocation::QDeclarativeGeoLocation(QObject *parent)
87 :   QObject(parent)
88 
89 {
90     setLocation(QGeoLocation());
91 }
92 
QDeclarativeGeoLocation(const QGeoLocation & src,QObject * parent)93 QDeclarativeGeoLocation::QDeclarativeGeoLocation(const QGeoLocation &src, QObject *parent)
94 :   QObject(parent)
95 {
96     setLocation(src);
97 }
98 
~QDeclarativeGeoLocation()99 QDeclarativeGeoLocation::~QDeclarativeGeoLocation()
100 {
101 }
102 
103 /*!
104     \qmlproperty QGeoLocation QtPositioning::Location::location
105 
106     For details on how to use this property to interface between C++ and QML see
107     "\l {Location - QGeoLocation} {Interfaces between C++ and QML Code}".
108 */
setLocation(const QGeoLocation & src)109 void QDeclarativeGeoLocation::setLocation(const QGeoLocation &src)
110 {
111     if (m_address && m_address->parent() == this) {
112         m_address->setAddress(src.address());
113     } else if (!m_address || m_address->parent() != this) {
114         m_address = new QDeclarativeGeoAddress(src.address(), this);
115         emit addressChanged();
116     }
117 
118     setCoordinate(src.coordinate());
119     setBoundingBox(src.boundingBox());
120     setProperty("extendedAttributes", src.extendedAttributes());
121 }
122 
location() const123 QGeoLocation QDeclarativeGeoLocation::location() const
124 {
125     QGeoLocation retValue;
126     retValue.setAddress(m_address ? m_address->address() : QGeoAddress());
127     retValue.setCoordinate(m_coordinate);
128     retValue.setBoundingBox(m_boundingBox);
129     retValue.setExtendedAttributes(m_extendedAttributes);
130     return retValue;
131 }
132 
133 /*!
134     \qmlproperty Address QtPositioning::Location::address
135 
136     This property holds the address of the location which can be use to retrieve address details of the location.
137 */
setAddress(QDeclarativeGeoAddress * address)138 void QDeclarativeGeoLocation::setAddress(QDeclarativeGeoAddress *address)
139 {
140     if (m_address == address)
141         return;
142 
143     if (m_address && m_address->parent() == this)
144         delete m_address;
145 
146     m_address = address;
147     emit addressChanged();
148 }
149 
address() const150 QDeclarativeGeoAddress *QDeclarativeGeoLocation::address() const
151 {
152     return m_address;
153 }
154 
155 /*!
156     \qmlproperty coordinate QtPositioning::Location::coordinate
157 
158     This property holds the exact geographical coordinate of the location which can be used to retrieve the latitude, longitude and altitude of the location.
159 
160     \note this property's changed() signal is currently emitted only if the
161     whole object changes, not if only the contents of the object change.
162 */
setCoordinate(const QGeoCoordinate coordinate)163 void QDeclarativeGeoLocation::setCoordinate(const QGeoCoordinate coordinate)
164 {
165     if (m_coordinate == coordinate)
166         return;
167 
168     m_coordinate = coordinate;
169     emit coordinateChanged();
170 }
171 
coordinate() const172 QGeoCoordinate QDeclarativeGeoLocation::coordinate() const
173 {
174     return m_coordinate;
175 }
176 
177 /*!
178     \qmlproperty georectangle QtPositioning::Location::boundingBox
179 
180     This property holds the recommended region to use when displaying the location.
181     For example, a building's location may have a region centered around the building,
182     but the region is large enough to show it's immediate surrounding geographical
183     context.
184 
185     Note: this property's changed() signal is currently emitted only if the
186     whole object changes, not if only the contents of the object change.
187 */
setBoundingBox(const QGeoRectangle & boundingBox)188 void QDeclarativeGeoLocation::setBoundingBox(const QGeoRectangle &boundingBox)
189 {
190     if (m_boundingBox == boundingBox)
191         return;
192 
193     m_boundingBox = boundingBox;
194     emit boundingBoxChanged();
195 }
196 
boundingBox() const197 QGeoRectangle QDeclarativeGeoLocation::boundingBox() const
198 {
199     return m_boundingBox;
200 }
201