Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgeotilerequestmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 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
4#include "qgeotilespec_p.h"
5#include "qgeotiledmap_p.h"
8
9#include <QtCore/QPointer>
10#include <QtCore/QTimer>
11
13
14class RetryFuture;
15
17{
18public:
21
23 QPointer<QGeoTiledMappingManagerEngine> m_engine;
24
25 QMap<QGeoTileSpec, QSharedPointer<QGeoTileTexture> > requestTiles(const QSet<QGeoTileSpec> &tiles);
26 void tileError(const QGeoTileSpec &tile, const QString &errorString);
27
28 QHash<QGeoTileSpec, int> m_retries;
29 QHash<QGeoTileSpec, QSharedPointer<RetryFuture> > m_futures;
30 QSet<QGeoTileSpec> m_requested;
31
32 void tileFetched(const QGeoTileSpec &spec);
33};
34
40
45
46QMap<QGeoTileSpec, QSharedPointer<QGeoTileTexture> > QGeoTileRequestManager::requestTiles(const QSet<QGeoTileSpec> &tiles)
47{
48 return d_ptr->requestTiles(tiles);
49}
50
52{
53 d_ptr->tileFetched(spec);
54}
55
56QSharedPointer<QGeoTileTexture> QGeoTileRequestManager::tileTexture(const QGeoTileSpec &spec)
57{
58 if (d_ptr->m_engine)
59 return d_ptr->m_engine->getTileTexture(spec);
60 else
61 return QSharedPointer<QGeoTileTexture>();
62}
63
64void QGeoTileRequestManager::tileError(const QGeoTileSpec &tile, const QString &errorString)
65{
66 d_ptr->tileError(tile, errorString);
67}
68
74
78
79QMap<QGeoTileSpec, QSharedPointer<QGeoTileTexture> > QGeoTileRequestManagerPrivate::requestTiles(const QSet<QGeoTileSpec> &tiles)
80{
81 QSet<QGeoTileSpec> cancelTiles = m_requested - tiles;
82 QSet<QGeoTileSpec> requestTiles = tiles - m_requested;
83 QSet<QGeoTileSpec> cached;
84// int tileSize = tiles.size();
85// int newTiles = requestTiles.size();
86
88
89 QMap<QGeoTileSpec, QSharedPointer<QGeoTileTexture> > cachedTex;
90
91 // remove tiles in cache from request tiles
92 if (!m_engine.isNull()) {
93 iter i = requestTiles.constBegin();
94 iter end = requestTiles.constEnd();
95 for (; i != end; ++i) {
96 QGeoTileSpec tile = *i;
97 QSharedPointer<QGeoTileTexture> tex = m_engine->getTileTexture(tile);
98 if (tex) {
99 if (!tex->image.isNull())
100 cachedTex.insert(tile, tex);
101 cached.insert(tile);
102 } else {
103 // Try to use textures from lower zoom levels, but still request the proper tile
104 QGeoTileSpec spec = tile;
105 const int endRange = qMax(0, tile.zoom() - 4); // Using up to 4 zoom levels up. 4 is arbitrary.
106 for (int z = tile.zoom() - 1; z >= endRange; z--) {
107 int denominator = 1 << (tile.zoom() - z);
108 spec.setZoom(z);
109 spec.setX(tile.x() / denominator);
110 spec.setY(tile.y() / denominator);
111 QSharedPointer<QGeoTileTexture> t = m_engine->getTileTexture(spec);
112 if (t && !t->image.isNull()) {
113 cachedTex.insert(tile, t);
114 break;
115 }
116 }
117 }
118 }
119 }
120
121 requestTiles -= cached;
122
123 m_requested -= cancelTiles;
125
126// qDebug() << "required # tiles: " << tileSize << ", new tiles: " << newTiles << ", total server requests: " << requested_.size();
127
128 if (!requestTiles.isEmpty() || !cancelTiles.isEmpty()) {
129 if (!m_engine.isNull()) {
130// qDebug() << "new server requests: " << requestTiles.size() << ", server cancels: " << cancelTiles.size();
132
133 // Remove any cancelled tiles from the error retry hash to avoid
134 // re-using the numbers for a totally different request cycle.
135 iter i = cancelTiles.constBegin();
136 iter end = cancelTiles.constEnd();
137 for (; i != end; ++i) {
140 }
141 }
142 }
143
144 return cachedTex;
145}
146
148{
149 m_map->updateTile(spec);
150 m_requested.remove(spec);
151 m_retries.remove(spec);
152 m_futures.remove(spec);
153}
154
155// Represents a tile that needs to be retried after a certain period of time
156class RetryFuture : public QObject
157{
159public:
161
162public Q_SLOTS:
163 void retry();
164
165private:
166 QGeoTileSpec m_tile;
167 QGeoTiledMap *m_map;
168 QPointer<QGeoTiledMappingManagerEngine> m_engine;
169};
170
172 : QObject(parent), m_tile(tile), m_map(map), m_engine(engine)
173{}
174
176{
177 QSet<QGeoTileSpec> requestTiles;
178 QSet<QGeoTileSpec> cancelTiles;
179 requestTiles.insert(m_tile);
180 if (!m_engine.isNull())
181 m_engine->updateTileRequests(m_map, requestTiles, cancelTiles);
182}
183
185{
186 if (m_requested.contains(tile)) {
187 int count = m_retries.value(tile, 0);
188 m_retries.insert(tile, count + 1);
189
190 if (count >= 5) {
191 qWarning("QGeoTileRequestManager: Failed to fetch tile (%d,%d,%d) 5 times, giving up. "
192 "Last error message was: '%s'",
193 tile.x(), tile.y(), tile.zoom(), qPrintable(errorString));
194 m_requested.remove(tile);
195 m_retries.remove(tile);
196 m_futures.remove(tile);
197
198 } else {
199 // Exponential time backoff when retrying
200 int delay = (1 << count) * 500;
201
202 QSharedPointer<RetryFuture> future(new RetryFuture(tile,m_map,m_engine));
203 m_futures.insert(tile, future);
204
206 // Passing .data() to singleShot is ok -- Qt will clean up the
207 // connection if the target qobject is deleted
208 }
209 }
210}
211
213
214#include "qgeotilerequestmanager.moc"
QMap< QGeoTileSpec, QSharedPointer< QGeoTileTexture > > requestTiles(const QSet< QGeoTileSpec > &tiles)
void tileError(const QGeoTileSpec &tile, const QString &errorString)
QPointer< QGeoTiledMappingManagerEngine > m_engine
void tileFetched(const QGeoTileSpec &spec)
QGeoTileRequestManagerPrivate(QGeoTiledMap *map, QGeoTiledMappingManagerEngine *engine)
QHash< QGeoTileSpec, QSharedPointer< RetryFuture > > m_futures
QHash< QGeoTileSpec, int > m_retries
QGeoTileRequestManager(QGeoTiledMap *map, QGeoTiledMappingManagerEngine *engine)
void tileFetched(const QGeoTileSpec &spec)
void tileError(const QGeoTileSpec &tile, const QString &errorString)
QMap< QGeoTileSpec, QSharedPointer< QGeoTileTexture > > requestTiles(const QSet< QGeoTileSpec > &tiles)
QSharedPointer< QGeoTileTexture > tileTexture(const QGeoTileSpec &spec)
void setZoom(int zoom)
void setY(int y)
void setX(int x)
void updateTile(const QGeoTileSpec &spec)
virtual void updateTileRequests(QGeoTiledMap *map, const QSet< QGeoTileSpec > &tilesAdded, const QSet< QGeoTileSpec > &tilesRemoved)
virtual QSharedPointer< QGeoTileTexture > getTileTexture(const QGeoTileSpec &spec)
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:958
T value(const Key &key) const noexcept
Definition qhash.h:1054
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
bool isNull() const noexcept
Definition qpointer.h:84
bool remove(const T &value)
Definition qset.h:63
bool contains(const T &value) const
Definition qset.h:71
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
RetryFuture(const QGeoTileSpec &tile, QGeoTiledMap *map, QGeoTiledMappingManagerEngine *engine, QObject *parent=nullptr)
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
#define qWarning
Definition qlogging.h:166
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLuint GLuint end
GLenum GLenum GLsizei count
GLdouble GLdouble t
Definition qopenglext.h:243
#define qPrintable(string)
Definition qstring.h:1531
#define Q_OBJECT
#define Q_SLOTS
QFuture< void > future
[5]
QJSEngine engine
[0]