1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include <QtLocation/private/qdeclarativegeoroute_p.h>
38 #include "qmaprouteobject_p.h"
39 #include "qmaprouteobject_p_p.h"
40 
41 QT_BEGIN_NAMESPACE
42 
43 /*!
44     \qmltype MapRouteObject
45     \instantiates QMapRouteObject
46     \inqmlmodule Qt.labs.location
47     \ingroup qml-QtLocation5-maps
48     \inherits QGeoMapObject
49 
50     \brief The MapRouteObject displays a geographical route on a Map.
51 
52     The MapRouteObject type displays a Route obtained through a RouteModel or
53     other means, on the Map as a Polyline following the path of the Route.
54 */
55 
56 /*
57 
58     QGeoMapRoutePrivate
59 
60 */
61 
QMapRouteObjectPrivate(QGeoMapObject * q)62 QMapRouteObjectPrivate::QMapRouteObjectPrivate(QGeoMapObject *q) : QGeoMapObjectPrivate(q)
63 {
64 
65 }
66 
QMapRouteObjectPrivate(const QMapRouteObjectPrivate & other)67 QMapRouteObjectPrivate::QMapRouteObjectPrivate(const QMapRouteObjectPrivate &other) : QGeoMapObjectPrivate(other)
68 {
69     // QGeoMapRoutePrivate doesn't contain anything because QGeoRoute has already everything necessary.
70 }
71 
~QMapRouteObjectPrivate()72 QMapRouteObjectPrivate::~QMapRouteObjectPrivate()
73 {
74 
75 }
76 
type() const77 QGeoMapObject::Type QMapRouteObjectPrivate::type() const
78 {
79     return QGeoMapObject::RouteType;
80 }
81 
declarativeGeoRoute() const82 QDeclarativeGeoRoute *QMapRouteObjectPrivate::declarativeGeoRoute() const
83 {
84     const QMapRouteObject *r = static_cast<QMapRouteObject *>(q);
85     return r->m_route;
86 }
87 
88 /*!
89     \qmlproperty Route Qt.labs.location::MapRouteObject::route
90 
91     This property holds the route to be drawn.
92 */
route() const93 QGeoRoute QMapRouteObjectPrivate::route() const
94 {
95     const QDeclarativeGeoRoute *r = declarativeGeoRoute();
96     if (r)
97         return r->route();
98     return {};
99 }
100 
setRoute(const QDeclarativeGeoRoute * route)101 void QMapRouteObjectPrivate::setRoute(const QDeclarativeGeoRoute *route)
102 {
103     Q_UNUSED(route);
104 }
105 
equals(const QGeoMapObjectPrivate & other) const106 bool QMapRouteObjectPrivate::equals(const QGeoMapObjectPrivate &other) const
107 {
108     if (other.type() != type()) // This check might be unnecessary, depending on how equals gets used
109         return false;
110 
111     const QMapRouteObjectPrivate &o = static_cast<const QMapRouteObjectPrivate &>(other);
112     return (QGeoMapObjectPrivate::equals(o)
113             && route() == o.route()); // Could also be done shallow, comparing declarativeGeoRoute()
114 }
115 
clone()116 QGeoMapObjectPrivate *QMapRouteObjectPrivate::clone()
117 {
118     return new QMapRouteObjectPrivate(*this);
119 }
120 
geoShape() const121 QGeoShape QMapRouteObjectPrivate::geoShape() const
122 {
123     return route().bounds();
124 }
125 
setGeoShape(const QGeoShape &)126 void QMapRouteObjectPrivate::setGeoShape(const QGeoShape &/*shape*/)
127 {
128     // Not supported for MapRouteObject
129 }
130 
131 
132 /*
133 
134     QGeoMapRoute
135 
136 */
137 
QMapRouteObject(QObject * parent)138 QMapRouteObject::QMapRouteObject(QObject *parent)
139     : QGeoMapObject(QExplicitlySharedDataPointer<QGeoMapObjectPrivate>(new QMapRouteObjectPrivate(this)), parent)
140 {
141 
142 }
143 
~QMapRouteObject()144 QMapRouteObject::~QMapRouteObject()
145 {
146 
147 }
148 
route() const149 QDeclarativeGeoRoute *QMapRouteObject::route() const
150 {
151     return m_route;
152 }
153 
geoRoute() const154 QGeoRoute QMapRouteObject::geoRoute() const
155 {
156     if (m_route)
157         return m_route->route();
158     return {};
159 }
160 
setRoute(QDeclarativeGeoRoute * route)161 void QMapRouteObject::setRoute(QDeclarativeGeoRoute *route)
162 {
163     if (route == m_route)
164         return;
165 //    if ((!m_route && !route) || (m_route && route && m_route->route() == route->route()))
166 //        return;
167 
168     m_route = route;
169     QMapRouteObjectPrivate *d = static_cast<QMapRouteObjectPrivate *>(d_ptr.data());
170     d->setRoute(route);
171     emit routeChanged(route);
172 }
173 
setMap(QGeoMap * map)174 void QMapRouteObject::setMap(QGeoMap *map)
175 {
176     QMapRouteObjectPrivate *d = static_cast<QMapRouteObjectPrivate *>(d_ptr.data());
177     if (d->m_map == map)
178         return;
179 
180     QGeoMapObject::setMap(map); // This is where the specialized pimpl gets created and injected
181 
182     if (!map) {
183         // Map was set, now it has ben re-set to NULL, but not inside d_ptr.
184         // so m_map inside d_ptr can still be used to remove itself, inside the destructor.
185         d_ptr = new QMapRouteObjectPrivate(*d);  // This is not losing data: check MapRouteObjectPrivate::declarativeGeoRoute()
186         // Old pimpl deleted implicitly by QExplicitlySharedDataPointer
187     }
188 }
189 
190 QT_END_NAMESPACE
191