1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Aaron McCarthy <mccarthy.aaron@gmail.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtLocation 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 "qgeotilefetcherosm.h" |
41 | #include "qgeomapreplyosm.h" |
42 | |
43 | #include <QtNetwork/QNetworkAccessManager> |
44 | #include <QtNetwork/QNetworkRequest> |
45 | #include <QtLocation/private/qgeotilespec_p.h> |
46 | #include <QtLocation/private/qgeotilefetcher_p_p.h> |
47 | |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | static bool providersResolved(const QVector<QGeoTileProviderOsm *> &providers) |
52 | { |
53 | foreach (const QGeoTileProviderOsm *provider, providers) |
54 | if (!provider->isResolved()) |
55 | return false; |
56 | return true; |
57 | } |
58 | |
59 | class QGeoTileFetcherOsmPrivate : public QGeoTileFetcherPrivate |
60 | { |
61 | Q_DECLARE_PUBLIC(QGeoTileFetcherOsm) |
62 | public: |
63 | QGeoTileFetcherOsmPrivate(); |
64 | virtual ~QGeoTileFetcherOsmPrivate(); |
65 | |
66 | private: |
67 | Q_DISABLE_COPY(QGeoTileFetcherOsmPrivate) |
68 | }; |
69 | |
70 | QGeoTileFetcherOsmPrivate::QGeoTileFetcherOsmPrivate() : QGeoTileFetcherPrivate() |
71 | { |
72 | } |
73 | |
74 | QGeoTileFetcherOsmPrivate::~QGeoTileFetcherOsmPrivate() |
75 | { |
76 | } |
77 | |
78 | |
79 | QGeoTileFetcherOsm::QGeoTileFetcherOsm(const QVector<QGeoTileProviderOsm *> &providers, |
80 | QNetworkAccessManager *nm, |
81 | QGeoMappingManagerEngine *parent) |
82 | : QGeoTileFetcher(*new QGeoTileFetcherOsmPrivate(), parent), m_userAgent("Qt Location based application" ), |
83 | m_providers(providers), m_nm(nm), m_ready(true) |
84 | { |
85 | m_nm->setParent(this); |
86 | foreach (QGeoTileProviderOsm *provider, m_providers) { |
87 | if (!provider->isResolved()) { |
88 | m_ready = false; |
89 | connect(sender: provider, signal: &QGeoTileProviderOsm::resolutionFinished, |
90 | receiver: this, slot: &QGeoTileFetcherOsm::onProviderResolutionFinished); |
91 | connect(sender: provider, signal: &QGeoTileProviderOsm::resolutionError, |
92 | receiver: this, slot: &QGeoTileFetcherOsm::onProviderResolutionError); |
93 | connect(sender: provider, signal: &QGeoTileProviderOsm::resolutionRequired, |
94 | receiver: this, slot: &QGeoTileFetcherOsm::restartTimer, type: Qt::QueuedConnection); |
95 | provider->resolveProvider(); |
96 | } |
97 | } |
98 | if (m_ready) |
99 | readyUpdated(); |
100 | } |
101 | |
102 | void QGeoTileFetcherOsm::setUserAgent(const QByteArray &userAgent) |
103 | { |
104 | m_userAgent = userAgent; |
105 | } |
106 | |
107 | bool QGeoTileFetcherOsm::initialized() const |
108 | { |
109 | if (!m_ready) { |
110 | foreach (QGeoTileProviderOsm *provider, m_providers) |
111 | if (!provider->isResolved()) |
112 | provider->resolveProvider(); |
113 | } |
114 | return m_ready; |
115 | } |
116 | |
117 | void QGeoTileFetcherOsm::onProviderResolutionFinished(const QGeoTileProviderOsm *provider) |
118 | { |
119 | if ((m_ready = providersResolved(providers: m_providers))) { |
120 | qWarning(msg: "QGeoTileFetcherOsm: all providers resolved" ); |
121 | readyUpdated(); |
122 | } |
123 | emit providerDataUpdated(provider); |
124 | } |
125 | |
126 | void QGeoTileFetcherOsm::onProviderResolutionError(const QGeoTileProviderOsm *provider) |
127 | { |
128 | if ((m_ready = providersResolved(providers: m_providers))) { |
129 | qWarning(msg: "QGeoTileFetcherOsm: all providers resolved" ); |
130 | readyUpdated(); |
131 | } |
132 | emit providerDataUpdated(provider); |
133 | } |
134 | |
135 | void QGeoTileFetcherOsm::restartTimer() |
136 | { |
137 | Q_D(QGeoTileFetcherOsm); |
138 | |
139 | if (!d->queue_.isEmpty()) |
140 | d->timer_.start(msec: 0, obj: this); |
141 | } |
142 | |
143 | QGeoTiledMapReply *QGeoTileFetcherOsm::getTileImage(const QGeoTileSpec &spec) |
144 | { |
145 | int id = spec.mapId(); |
146 | if (id < 1 || id > m_providers.size()) { |
147 | qWarning(msg: "Unknown map id %d\n" , spec.mapId()); |
148 | if (m_providers.isEmpty()) |
149 | return nullptr; |
150 | else |
151 | id = 1; |
152 | } |
153 | id -= 1; // TODO: make OSM map ids start from 0. |
154 | |
155 | if (spec.zoom() > m_providers[id]->maximumZoomLevel() || spec.zoom() < m_providers[id]->minimumZoomLevel()) |
156 | return nullptr; |
157 | |
158 | const QUrl url = m_providers[id]->tileAddress(x: spec.x(), y: spec.y(), z: spec.zoom()); |
159 | QNetworkRequest request; |
160 | request.setHeader(header: QNetworkRequest::UserAgentHeader, value: m_userAgent); |
161 | request.setUrl(url); |
162 | QNetworkReply *reply = m_nm->get(request); |
163 | return new QGeoMapReplyOsm(reply, spec, m_providers[id]->format()); |
164 | } |
165 | |
166 | void QGeoTileFetcherOsm::readyUpdated() |
167 | { |
168 | updateTileRequests(tilesAdded: QSet<QGeoTileSpec>(), tilesRemoved: QSet<QGeoTileSpec>()); |
169 | } |
170 | |
171 | QT_END_NAMESPACE |
172 | |