1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 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 #include "qgeocameradata_p.h"
37 #include <QtPositioning/private/qgeocoordinate_p.h>
38 #include <QtPositioning/private/qwebmercator_p.h>
39 #include <QtCore/QVariant>
40 #include <QtCore/QVariantAnimation>
41 
42 QT_BEGIN_NAMESPACE
43 
44 class QGeoCameraDataPrivate : public QSharedData
45 {
46 public:
47     QGeoCameraDataPrivate();
48     QGeoCameraDataPrivate(const QGeoCameraDataPrivate &rhs);
49 
50     QGeoCameraDataPrivate &operator = (const QGeoCameraDataPrivate &rhs);
51 
52     bool operator == (const QGeoCameraDataPrivate &rhs) const;
53 
54     QGeoCoordinate m_center;
55     double m_bearing;
56     double m_tilt;
57     double m_roll;
58     double m_fieldOfView;
59     double m_zoomLevel;
60 };
61 
QGeoCameraDataPrivate()62 QGeoCameraDataPrivate::QGeoCameraDataPrivate()
63     : QSharedData(),
64       m_center(0, 0),
65       m_bearing(0.0),
66       m_tilt(0.0),
67       m_roll(0.0),
68       m_fieldOfView(45.0),
69       m_zoomLevel(0.0) {}
70 
QGeoCameraDataPrivate(const QGeoCameraDataPrivate & rhs)71 QGeoCameraDataPrivate::QGeoCameraDataPrivate(const QGeoCameraDataPrivate &rhs)
72     : QSharedData(rhs),
73       m_center(rhs.m_center),
74       m_bearing(rhs.m_bearing),
75       m_tilt(rhs.m_tilt),
76       m_roll(rhs.m_roll),
77       m_fieldOfView(rhs.m_fieldOfView),
78       m_zoomLevel(rhs.m_zoomLevel) {}
79 
operator =(const QGeoCameraDataPrivate & rhs)80 QGeoCameraDataPrivate &QGeoCameraDataPrivate::operator = (const QGeoCameraDataPrivate &rhs)
81 {
82     if (this == &rhs)
83         return *this;
84 
85     m_center = rhs.m_center;
86     m_bearing = rhs.m_bearing;
87     m_tilt = rhs.m_tilt;
88     m_roll = rhs.m_roll;
89     m_fieldOfView = rhs.m_fieldOfView;
90     m_zoomLevel = rhs.m_zoomLevel;
91 
92     return *this;
93 }
94 
operator ==(const QGeoCameraDataPrivate & rhs) const95 bool QGeoCameraDataPrivate::operator == (const QGeoCameraDataPrivate &rhs) const
96 {
97     return ((m_center == rhs.m_center)
98             && (m_bearing == rhs.m_bearing)
99             && (m_tilt == rhs.m_tilt)
100             && (m_roll == rhs.m_roll)
101             && (m_fieldOfView == rhs.m_fieldOfView)
102             && (m_zoomLevel == rhs.m_zoomLevel));
103 }
104 
cameraInterpolator(const QGeoCameraData & start,const QGeoCameraData & end,qreal progress)105 QVariant cameraInterpolator(const QGeoCameraData &start,
106                             const QGeoCameraData &end,
107                             qreal progress)
108 {
109     QGeoCameraData result = start;
110     QGeoCoordinate from = start.center();
111     QGeoCoordinate to = end.center();
112 
113     if (from == to) {
114         if (progress < 0.5) {
115             result.setCenter(from);
116         } else {
117             result.setCenter(to);
118         }
119     }
120     else {
121         QGeoCoordinate coordinateResult = QWebMercator::coordinateInterpolation(from, to, progress);
122         result.setCenter(coordinateResult);
123     }
124 
125     double sf = 1.0 - progress;
126     double ef = progress;
127 
128     result.setBearing(sf * start.bearing() + ef * end.bearing());
129     result.setTilt(sf * start.tilt() + ef * end.tilt());
130     result.setRoll(sf * start.roll() + ef * end.roll());
131     result.setFieldOfView(sf * start.fieldOfView() + ef * end.fieldOfView());
132     result.setZoomLevel(sf * start.zoomLevel() + ef * end.zoomLevel());
133 
134     return QVariant::fromValue(result);
135 }
136 
QGeoCameraData()137 QGeoCameraData::QGeoCameraData()
138     : d(new QGeoCameraDataPrivate())
139 {
140     qRegisterMetaType<QGeoCameraData>();
141     qRegisterAnimationInterpolator<QGeoCameraData>(cameraInterpolator);
142 }
143 
QGeoCameraData(const QGeoCameraData & other)144 QGeoCameraData::QGeoCameraData(const QGeoCameraData &other)
145     : d(other.d) {}
146 
~QGeoCameraData()147 QGeoCameraData::~QGeoCameraData()
148 {
149 }
150 
operator =(const QGeoCameraData & other)151 QGeoCameraData &QGeoCameraData::operator = (const QGeoCameraData &other)
152 {
153     if (this == &other)
154         return *this;
155 
156     d = other.d;
157     return *this;
158 }
159 
operator ==(const QGeoCameraData & rhs) const160 bool QGeoCameraData::operator == (const QGeoCameraData &rhs) const
161 {
162     return (*(d.constData()) == *(rhs.d.constData()));
163 }
164 
operator !=(const QGeoCameraData & other) const165 bool QGeoCameraData::operator != (const QGeoCameraData &other) const
166 {
167     return !(operator==(other));
168 }
169 
setCenter(const QGeoCoordinate & center)170 void QGeoCameraData::setCenter(const QGeoCoordinate &center)
171 {
172     d->m_center = center;
173 }
174 
center() const175 QGeoCoordinate QGeoCameraData::center() const
176 {
177     return d->m_center;
178 }
179 
setBearing(double bearing)180 void QGeoCameraData::setBearing(double bearing)
181 {
182     d->m_bearing = bearing;
183 }
184 
bearing() const185 double QGeoCameraData::bearing() const
186 {
187     return d->m_bearing;
188 }
189 
setTilt(double tilt)190 void QGeoCameraData::setTilt(double tilt)
191 {
192     d->m_tilt = tilt;
193 }
194 
tilt() const195 double QGeoCameraData::tilt() const
196 {
197     return d->m_tilt;
198 }
199 
setRoll(double roll)200 void QGeoCameraData::setRoll(double roll)
201 {
202     d->m_roll = roll;
203 }
204 
roll() const205 double QGeoCameraData::roll() const
206 {
207     return d->m_roll;
208 }
209 
setFieldOfView(double fieldOfView)210 void QGeoCameraData::setFieldOfView(double fieldOfView)
211 {
212     d->m_fieldOfView = fieldOfView;
213 }
214 
fieldOfView() const215 double QGeoCameraData::fieldOfView() const
216 {
217     return d->m_fieldOfView;
218 }
219 
setZoomLevel(double zoomFactor)220 void QGeoCameraData::setZoomLevel(double zoomFactor)
221 {
222     d->m_zoomLevel = zoomFactor;
223 }
224 
zoomLevel() const225 double QGeoCameraData::zoomLevel() const
226 {
227     return d->m_zoomLevel;
228 }
229 
230 QT_END_NAMESPACE
231