20 qCDebug(QGCCachedTileSetLog) <<
this;
23QGCCachedTileSet::~QGCCachedTileSet()
25 qCDebug(QGCCachedTileSetLog) <<
this;
28QString QGCCachedTileSet::downloadStatus()
const
31 return totalTilesSizeStr();
34 if (_totalTileCount <= _savedTileCount) {
35 return savedTileSizeStr();
38 return (savedTileSizeStr() +
" / " + totalTilesSizeStr());
41void QGCCachedTileSet::createDownloadTask()
44 setDownloading(
false);
66 _batchRequested =
true;
69void QGCCachedTileSet::resumeDownloadTask()
71 _cancelPending =
false;
81void QGCCachedTileSet::cancelDownloadTask()
83 _cancelPending =
true;
86void QGCCachedTileSet::_tileListFetched(
const QQueue<QGCTile*> &tiles)
88 _batchRequested =
false;
89 if (tiles.size() < kTileBatchSize) {
93 if (tiles.isEmpty()) {
98 if (!_networkManager) {
99 _networkManager =
new QNetworkAccessManager(
this);
103 _tilesToDownload.append(tiles);
107void QGCCachedTileSet::_doneWithDownload()
109 if (_errorCount == 0) {
110 setTotalTileCount(_savedTileCount);
111 setTotalTileSize(_savedTileSize);
114 if (_savedTileCount != 0) {
115 avg = _savedTileSize / _savedTileCount;
117 qCWarning(QGCCachedTileSetLog) <<
"_savedTileCount=0";
120 setUniqueTileSize(_uniqueTileCount * avg);
123 setDownloading(
false);
128void QGCCachedTileSet::_prepareDownload()
130 if (_tilesToDownload.isEmpty()) {
133 }
else if (!_batchRequested) {
134 createDownloadTask();
140 if (_tilesToDownload.isEmpty()) {
144 QGCTile*
const tile = _tilesToDownload.dequeue();
146 if (!request.url().isValid()) {
147 qCWarning(QGCCachedTileSetLog) <<
"Invalid URL for tile" << tile->
hash <<
"- skipping";
148 setErrorCount(_errorCount + 1);
152 request.setOriginatingObject(
this);
153 request.setAttribute(QNetworkRequest::User, tile->
hash);
155 QNetworkReply*
const reply = _networkManager->get(request);
156 reply->setParent(
this);
158 (void) connect(reply, &QNetworkReply::finished,
this, &QGCCachedTileSet::_networkReplyFinished);
159 (void) connect(reply, &QNetworkReply::errorOccurred,
this, &QGCCachedTileSet::_networkReplyError);
161 QMutexLocker lock(&_repliesMutex);
162 (void) _replies.insert(tile->
hash, reply);
167 createDownloadTask();
172void QGCCachedTileSet::_networkReplyFinished()
174 QNetworkReply*
const reply = qobject_cast<QNetworkReply*>(QObject::sender());
176 qCWarning(QGCCachedTileSetLog) <<
"NULL Reply";
179 reply->deleteLater();
181 if (reply->error() != QNetworkReply::NoError) {
185 if (!reply->isOpen()) {
186 qCWarning(QGCCachedTileSetLog) <<
"Empty Reply";
190 const QString hash = reply->request().attribute(QNetworkRequest::User).toString();
191 if (hash.isEmpty()) {
192 qCWarning(QGCCachedTileSetLog) <<
"Empty Hash";
197 QMutexLocker lock(&_repliesMutex);
198 if (_replies.contains(hash)) {
199 (void) _replies.remove(hash);
201 qCWarning(QGCCachedTileSetLog) <<
"Reply not in list: " << hash;
204 qCDebug(QGCCachedTileSetLog) <<
"Tile fetched:" << hash;
206 QByteArray image = reply->readAll();
207 if (image.isEmpty()) {
208 qCWarning(QGCCachedTileSetLog) <<
"Empty Image";
215 qCWarning(QGCCachedTileSetLog) <<
"Invalid map provider for type:" << type;
219 if (mapProvider->isElevationProvider()) {
220 const SharedElevationProvider elevationProvider = std::dynamic_pointer_cast<const ElevationProvider>(mapProvider);
221 image = elevationProvider->serialize(image);
222 if (image.isEmpty()) {
223 qCWarning(QGCCachedTileSetLog) <<
"Failed to Serialize Terrain Tile";
228 const QString format = mapProvider->getImageFormat(image);
229 if (format.isEmpty()) {
230 qCWarning(QGCCachedTileSetLog) <<
"Empty Format";
241 setSavedTileSize(_savedTileSize + image.size());
242 setSavedTileCount(_savedTileCount + 1);
244 if (_savedTileCount % 10 == 0) {
245 const quint32 avg = _savedTileSize / _savedTileCount;
246 setTotalTileSize(avg * _totalTileCount);
247 setUniqueTileSize(avg * _uniqueTileCount);
253void QGCCachedTileSet::_networkReplyError(QNetworkReply::NetworkError
error)
255 QNetworkReply*
const reply = qobject_cast<QNetworkReply*>(QObject::sender());
259 qCDebug(QGCCachedTileSetLog) <<
"Error fetching tile" << reply->errorString();
261 setErrorCount(_errorCount + 1);
263 const QString hash = reply->request().attribute(QNetworkRequest::User).toString();
264 if (hash.isEmpty()) {
265 qCWarning(QGCCachedTileSetLog) <<
"Empty Hash";
270 QMutexLocker lock(&_repliesMutex);
271 if (_replies.contains(hash)) {
272 (void) _replies.remove(hash);
274 qCWarning(QGCCachedTileSetLog) <<
"Reply not in list:" << hash;
278 if (
error != QNetworkReply::OperationCanceledError) {
279 qCWarning(QGCCachedTileSetLog) <<
"Error:" << reply->errorString();
290void QGCCachedTileSet::setSelected(
bool sel)
292 if (sel != _selected) {
301QString QGCCachedTileSet::errorCountStr()
const
303 return qgcApp()->numberToString(_errorCount);
306QString QGCCachedTileSet::totalTileCountStr()
const
308 return qgcApp()->numberToString(_totalTileCount);
311QString QGCCachedTileSet::totalTilesSizeStr()
const
313 return qgcApp()->bigSizeToString(_totalTileSize);
316QString QGCCachedTileSet::uniqueTileSizeStr()
const
318 return qgcApp()->bigSizeToString(_uniqueTileSize);
321QString QGCCachedTileSet::uniqueTileCountStr()
const
323 return qgcApp()->numberToString(_uniqueTileCount);
326QString QGCCachedTileSet::savedTileCountStr()
const
328 return qgcApp()->numberToString(_savedTileCount);
331QString QGCCachedTileSet::savedTileSizeStr()
const
333 return qgcApp()->bigSizeToString(_savedTileSize);
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QGCMapEngine * getQGCMapEngine()
std::shared_ptr< const ElevationProvider > SharedElevationProvider
std::shared_ptr< const MapProvider > SharedMapProvider
void totalTileCountChanged()
void totalTilesSizeChanged()
void tileListFetched(QQueue< QGCTile * > tiles)
void taskError(QGCMapTask::TaskType type, const QString &error)
void selectedCountChanged()
bool addTask(QGCMapTask *task)
void error(QGCMapTask::TaskType type, const QString &errorString)
static void cacheTile(const QString &type, int x, int y, int z, const QByteArray &image, const QString &format, qulonglong set=UINT64_MAX)
static uint32_t concurrentDownloads(const QString &type)
static QNetworkRequest getNetworkRequest(int mapId, int x, int y, int zoom)
static QString tileHashToType(QStringView tileHash)
static std::shared_ptr< const MapProvider > getMapProviderFromProviderType(QStringView type)
void configureProxy(QNetworkAccessManager *manager)
Set up default proxy configuration on a network manager.
void ignoreSslErrorsIfNeeded(QNetworkReply *reply)