Qt 6.x
The Qt SDK
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
24
26 void tileError(const QGeoTileSpec &tile, const QString &errorString);
27
31
32 void tileFetched(const QGeoTileSpec &spec);
33};
34
37{
38
39}
40
42{
43
44}
45
47{
48 return d_ptr->requestTiles(tiles);
49}
50
52{
53 d_ptr->tileFetched(spec);
54}
55
57{
58 if (d_ptr->m_engine)
59 return d_ptr->m_engine->getTileTexture(spec);
60 else
62}
63
64void QGeoTileRequestManager::tileError(const QGeoTileSpec &tile, const QString &errorString)
65{
66 d_ptr->tileError(tile, errorString);
67}
68
70 : m_map(map),
71 m_engine(engine)
72{
73}
74
76{
77}
78
80{
81 QSet<QGeoTileSpec> cancelTiles = m_requested - tiles;
83 QSet<QGeoTileSpec> cached;
84// int tileSize = tiles.size();
85// int newTiles = requestTiles.size();
86
88
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;
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);
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;
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
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)
int x() const
int zoom() const
void setZoom(int zoom)
void setY(int y)
int y() const
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)
\inmodule QtCore
Definition qhash.h:818
bool remove(const Key &key)
Removes the item that has the key from the hash.
Definition qhash.h:956
T value(const Key &key) const noexcept
Definition qhash.h:1044
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
Definition qmap.h:186
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
\inmodule QtCore
Definition qpointer.h:18
bool isNull() const
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwi...
Definition qpointer.h:67
Definition qset.h:18
bool remove(const T &value)
Definition qset.h:63
bool isEmpty() const
Definition qset.h:52
const_iterator constBegin() const noexcept
Definition qset.h:139
const_iterator constEnd() const noexcept
Definition qset.h:143
bool contains(const T &value) const
Definition qset.h:71
iterator insert(const T &value)
Definition qset.h:155
\inmodule QtCore
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
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:162
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:1391
#define Q_OBJECT
#define Q_SLOTS
QFuture< void > future
[5]
QJSEngine engine
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent