Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickgeocoordinateanimation.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
7#include <QtQuick/private/qquickanimation_p_p.h>
8#include <QtPositioning/private/qdoublevector2d_p.h>
9#include <QtPositioning/private/qwebmercator_p.h>
10#include <QtPositioning/private/qgeocoordinate_p.h>
11
13
14/*!
15 \qmltype CoordinateAnimation
16 \inherits PropertyAnimation
17 \inqmlmodule QtPositioning
18 \since 5.3
19
20 \brief A PropertyAnimation for geo coordinate properties.
21
22 A specialized \l{PropertyAnimation} that defines an animation
23 between two \l{geoCoordinate}{coordinates}.
24
25 By default, a \l{geoCoordinate::latitude}{latitude} of the
26 \l geoCoordinate is animated in the direction of shortest
27 (geodesic) distance between those coordinates. Since CoordinateAnimation uses Mercator
28 map projection, the \l{geoCoordinate::latitude}{latitude} animation
29 is always between -90 and 90 degrees.
30 The \l{geoCoordinate::longitude}{longitude} animation path is not
31 limited and can go over 180 degrees in both west and east directions.
32
33 The \l{direction} property can be set to specify the direction in which the
34 \l{geoCoordinate::longitude}{longitude} animation should occur.
35
36 Since Qt 6.13, the \l{geoCoordinate::altitudeType}{altitude type} of the
37 coordinates is also taken into account. The altitude type cannot be
38 interpolated, so the animated coordinate uses the altitude type of the
39 \l from coordinate during the first half of the animation, and the
40 altitude type of the \l to coordinate during the second half.
41
42 \note If the altitude types of the \l from and \l to coordinates differ,
43 the \l{geoCoordinate::altitude}{altitude} values are still interpolated
44 without any conversion between the reference surfaces.
45
46 \sa {Animation and Transitions in Qt Quick}
47*/
48
49static QGeoCoordinate::AltitudeType interpolatedAltitudeType(const QGeoCoordinate &from,
50 const QGeoCoordinate &to,
51 qreal progress)
52{
53 return progress < 0.5 ? from.altitudeType() : to.altitudeType();
54}
55
56QVariant q_coordinateInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
57{
58 if (from == to) {
59 if (progress < 0.5) {
60 return QVariant::fromValue(from);
61 } else {
62 return QVariant::fromValue(to);
63 }
64 }
65
66 QGeoCoordinate result = QWebMercator::coordinateInterpolation(from, to, progress);
67 result.setAltitudeType(interpolatedAltitudeType(from, to, progress));
68
69 return QVariant::fromValue(result);
70}
71
72QVariant q_coordinateShortestInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
73{
74 const QGeoMercatorCoordinatePrivate* fromMercator =
75 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&from));
76 const QGeoMercatorCoordinatePrivate* toMercator =
77 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&to));
78
79 double toX = toMercator->m_mercatorX;
80 double toY = toMercator->m_mercatorY;
81 double fromX = fromMercator->m_mercatorX;
82 double fromY = fromMercator->m_mercatorY;
83 double x;
84 if (0.5 < qAbs(toX - fromX)) {
85 // handle dateline crossing
86 double ex = toX;
87 double sx = fromX;
88 if (ex < sx)
89 sx -= 1.0;
90 else if (sx < ex)
91 ex -= 1.0;
92
93 x = sx + (ex - sx) * progress;
94
95 if (x < 0.0)
96 x += 1.0;
97
98 } else {
99 x = fromX + (toX - fromX) * progress;
100 }
101
102 double y = fromY + (toY - fromY) * progress;
103
104 QGeoCoordinate result = QWebMercator::mercatorToCoord(QDoubleVector2D(x, y));
105 result.setAltitude(from.altitude() + (to.altitude() - from.altitude()) * progress);
106 result.setAltitudeType(interpolatedAltitudeType(from, to, progress));
107 return QVariant::fromValue(result);
108}
109
110QVariant q_coordinateEastInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
111{
112 const QGeoMercatorCoordinatePrivate* fromMercator =
113 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&from));
114 const QGeoMercatorCoordinatePrivate* toMercator =
115 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&to));
116
117 double toX = toMercator->m_mercatorX;
118 double toY = toMercator->m_mercatorY;
119 double fromX = fromMercator->m_mercatorX;
120 double fromY = fromMercator->m_mercatorY;
121 double diff = toX - fromX;
122
123 while (diff < 0.0) {
124 toX += 1.0;
125 diff += 1.0;
126 }
127
128 double x = fromX + (toX - fromX) * progress;
129 double y = fromY + (toY - fromY) * progress;
130
131 while (x > 1.0)
132 x -= 1.0;
133
134 QGeoCoordinate result = QWebMercator::mercatorToCoord(QDoubleVector2D(x, y));
135 result.setAltitude(from.altitude() + (to.altitude() - from.altitude()) * progress);
136 result.setAltitudeType(interpolatedAltitudeType(from, to, progress));
137
138 return QVariant::fromValue(result);
139}
140
141QVariant q_coordinateWestInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
142{
143 const QGeoMercatorCoordinatePrivate* fromMercator =
144 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&from));
145 const QGeoMercatorCoordinatePrivate* toMercator =
146 static_cast<const QGeoMercatorCoordinatePrivate*>(QGeoCoordinatePrivate::get(&to));
147
148 double toX = toMercator->m_mercatorX;
149 double toY = toMercator->m_mercatorY;
150 double fromX = fromMercator->m_mercatorX;
151 double fromY = fromMercator->m_mercatorY;
152 double diff = toX - fromX;
153
154 while (diff > 0.0) {
155 toX -= 1.0;
156 diff -= 1.0;
157 }
158
159 double x = fromX + (toX - fromX) * progress;
160 double y = fromY + (toY - fromY) * progress;
161
162 while (x < 0.0)
163 x += 1.0;
164
165 QGeoCoordinate result = QWebMercator::mercatorToCoord(QDoubleVector2D(x, y));
166 result.setAltitude(from.altitude() + (to.altitude() - from.altitude()) * progress);
167 result.setAltitudeType(interpolatedAltitudeType(from, to, progress));
168
169 return QVariant::fromValue(result);
170}
171
172QQuickGeoCoordinateAnimation::QQuickGeoCoordinateAnimation(QObject *parent)
173 : QQuickPropertyAnimation(*(new QQuickGeoCoordinateAnimationPrivate), parent)
174
175{
176 Q_D(QQuickGeoCoordinateAnimation);
177 d->interpolatorType = qMetaTypeId<QGeoCoordinate>();
178 d->defaultToInterpolatorType = true;
179 d->interpolator = QVariantAnimationPrivate::getInterpolator(d->interpolatorType);
180}
181
182QQuickGeoCoordinateAnimation::~QQuickGeoCoordinateAnimation()
183{
184}
185
186/*!
187 \qmlproperty coordinate CoordinateAnimation::from
188 This property holds the coordinate where the animation should begin.
189*/
190QGeoCoordinate QQuickGeoCoordinateAnimation::from() const
191{
192 Q_D(const QQuickGeoCoordinateAnimation);
193 return d->from.value<QGeoCoordinate>();
194}
195
196void QQuickGeoCoordinateAnimation::setFrom(const QGeoCoordinate &f)
197{
198 QGeoMercatorCoordinatePrivate *mercator = new QGeoMercatorCoordinatePrivate();
199 QDoubleVector2D fromVector = QWebMercator::coordToMercator(f);
200 mercator->lat = f.latitude();
201 mercator->lng = f.longitude();
202 mercator->alt = f.altitude();
203 mercator->altType = f.altitudeType();
204 mercator->m_mercatorX = fromVector.x();
205 mercator->m_mercatorY = fromVector.y();
206 QGeoCoordinate from(*mercator);
207 QQuickPropertyAnimation::setFrom(QVariant::fromValue(from));
208}
209
210/*!
211 \qmlproperty coordinate CoordinateAnimation::to
212 This property holds the coordinate where the animation should end.
213*/
214QGeoCoordinate QQuickGeoCoordinateAnimation::to() const
215{
216 Q_D(const QQuickGeoCoordinateAnimation);
217 return d->to.value<QGeoCoordinate>();
218}
219
220void QQuickGeoCoordinateAnimation::setTo(const QGeoCoordinate &t)
221{
222 QGeoMercatorCoordinatePrivate *mercator = new QGeoMercatorCoordinatePrivate();
223 QDoubleVector2D toVector = QWebMercator::coordToMercator(t);
224 mercator->lat = t.latitude();
225 mercator->lng = t.longitude();
226 mercator->alt = t.altitude();
227 mercator->altType = t.altitudeType();
228 mercator->m_mercatorX = toVector.x();
229 mercator->m_mercatorY = toVector.y();
230 QGeoCoordinate to(*mercator);
231 QQuickPropertyAnimation::setTo(QVariant::fromValue(to));
232}
233
234/*!
235 \qmlproperty enumeration CoordinateAnimation::direction
236
237 This property holds the direction of the
238 \l{geoCoordinate::longitude}{longitude} animation of the
239 \l geoCoordinate.
240
241 \value CoordinateAnimation.Shortest
242 The longitude animation goes in the direction
243 that produces the shortest animation path. This is the default.
244 \value CoordinateAnimation.West
245 The longitude animation always goes into western direction
246 and may cross the date line.
247 \value CoordinateAnimation.East
248 The longitude animation always goes into eastern direction
249 and may cross the date line.
250
251 \since 5.5
252*/
253
254
255QQuickGeoCoordinateAnimation::Direction QQuickGeoCoordinateAnimation::direction() const
256{
257 Q_D(const QQuickGeoCoordinateAnimation);
258 return d->m_direction.value();
259}
260
261void QQuickGeoCoordinateAnimation::setDirection(QQuickGeoCoordinateAnimation::Direction direction)
262{
263 Q_D( QQuickGeoCoordinateAnimation);
264 d->m_direction.removeBindingUnlessInWrapper();
265 if (d->m_direction.valueBypassingBindings() == direction)
266 return;
267
268 d->m_direction.setValueBypassingBindings(direction);
269 switch (direction) {
270 case West:
271 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(reinterpret_cast<void *>(&q_coordinateWestInterpolator));
272 break;
273 case East:
274 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(reinterpret_cast<void *>(&q_coordinateEastInterpolator));
275 break;
276 case Shortest:
277 default:
278 d->interpolator = reinterpret_cast<QVariantAnimation::Interpolator>(reinterpret_cast<void *>(&q_coordinateShortestInterpolator));
279 break;
280 }
281 d->m_direction.notify();
282}
283
284QBindable<QQuickGeoCoordinateAnimation::Direction> QQuickGeoCoordinateAnimation::bindableDirection()
285{
286 Q_D(QQuickGeoCoordinateAnimation);
287 return QBindable<Direction>(&d->m_direction);
288}
289
290QT_END_NAMESPACE
291
292#include "moc_qquickgeocoordinateanimation_p.cpp"
Combined button and popup list for selecting options.
QVariant q_coordinateShortestInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
static QT_BEGIN_NAMESPACE QGeoCoordinate::AltitudeType interpolatedAltitudeType(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
\qmltype CoordinateAnimation \inherits PropertyAnimation \inqmlmodule QtPositioning
QVariant q_coordinateWestInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
QVariant q_coordinateEastInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)
QVariant q_coordinateInterpolator(const QGeoCoordinate &from, const QGeoCoordinate &to, qreal progress)