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 
37 #include "qgeomapitemgeometry_p.h"
38 #include "qdeclarativegeomap_p.h"
39 #include "qlocationutils_p.h"
40 #include <QtQuick/QSGGeometry>
41 #include "qdoublevector2d_p.h"
42 #include <QtLocation/private/qgeomap_p.h>
43 
44 QT_BEGIN_NAMESPACE
45 
QGeoMapItemGeometry()46 QGeoMapItemGeometry::QGeoMapItemGeometry()
47 :   sourceDirty_(true), screenDirty_(true), clipToViewport_(true), preserveGeometry_(false)
48 {
49 }
50 
~QGeoMapItemGeometry()51 QGeoMapItemGeometry::~QGeoMapItemGeometry()
52 {
53 
54 }
55 
56 /*!
57     \internal
58 */
translate(const QPointF & offset)59 void QGeoMapItemGeometry::translate(const QPointF &offset)
60 {
61     for (int i = 0; i < screenVertices_.size(); ++i)
62         screenVertices_[i] += offset;
63 
64     firstPointOffset_ += offset;
65     screenOutline_.translate(offset);
66     screenBounds_.translate(offset);
67 }
68 
69 /*!
70     \internal
71 */
allocateAndFill(QSGGeometry * geom) const72 void QGeoMapItemGeometry::allocateAndFill(QSGGeometry *geom) const
73 {
74     const QVector<QPointF> &vx = screenVertices_;
75     const QVector<quint32> &ix = screenIndices_;
76 
77     if (isIndexed()) {
78         geom->allocate(vx.size(), ix.size());
79         if (geom->indexType() == QSGGeometry::UnsignedShortType) {
80             quint16 *its = geom->indexDataAsUShort();
81             for (int i = 0; i < ix.size(); ++i)
82                 its[i] = ix[i];
83         } else if (geom->indexType() == QSGGeometry::UnsignedIntType) {
84             quint32 *its = geom->indexDataAsUInt();
85             for (int i = 0; i < ix.size(); ++i)
86                 its[i] = ix[i];
87         }
88     } else {
89         geom->allocate(vx.size());
90     }
91 
92     QSGGeometry::Point2D *pts = geom->vertexDataAsPoint2D();
93     for (int i = 0; i < vx.size(); ++i)
94         pts[i].set(vx[i].x(), vx[i].y());
95 }
96 
97 /*!
98     \internal
99 */
translateToCommonOrigin(const QList<QGeoMapItemGeometry * > & geoms)100 QRectF QGeoMapItemGeometry::translateToCommonOrigin(const QList<QGeoMapItemGeometry *> &geoms)
101 {
102     QGeoCoordinate origin = geoms.at(0)->origin();
103 
104     QPainterPath brects;
105 
106     // first get max offset
107     QPointF maxOffset = geoms.at(0)->firstPointOffset();
108     foreach (QGeoMapItemGeometry *g, geoms) {
109         QPointF o = g->firstPointOffset();
110         maxOffset.setX(qMax(o.x(), maxOffset.x()));
111         maxOffset.setY(qMax(o.y(), maxOffset.y()));
112     }
113 
114     // then translate everything
115     foreach (QGeoMapItemGeometry *g, geoms) {
116         g->translate(maxOffset - g->firstPointOffset());
117         brects.addRect(g->sourceBoundingBox());
118     }
119 
120     return brects.boundingRect();
121 }
122 
123 /*!
124     \internal
125 */
geoDistanceToScreenWidth(const QGeoMap & map,const QGeoCoordinate & fromCoord,const QGeoCoordinate & toCoord)126 double QGeoMapItemGeometry::geoDistanceToScreenWidth(const QGeoMap &map,
127                                                      const QGeoCoordinate &fromCoord,
128                                                      const QGeoCoordinate &toCoord)
129 {
130     // Do not wrap around half the globe
131     Q_ASSERT(!qFuzzyCompare(fromCoord.longitude(), toCoord.longitude()));
132 
133     QGeoCoordinate mapMid = map.geoProjection().itemPositionToCoordinate(QDoubleVector2D(map.viewportWidth()/2.0, 0));
134     double halfGeoDist = toCoord.longitude() - fromCoord.longitude();
135     if (toCoord.longitude() < fromCoord.longitude())
136         halfGeoDist += 360;
137     halfGeoDist /= 2.0;
138     QGeoCoordinate geoDelta =  QGeoCoordinate(0,
139                     QLocationUtils::wrapLong(mapMid.longitude() + halfGeoDist));
140     QDoubleVector2D halfScreenDist = map.geoProjection().coordinateToItemPosition(geoDelta, false)
141                                 - QDoubleVector2D(map.viewportWidth()/2.0, 0);
142     return halfScreenDist.x() * 2.0;
143 }
144 
145 QT_END_NAMESPACE
146