Skip to content

Commit 646a122

Browse files
committed
QtLocationPlugin: Split CacheWorker & CacheDatabase
1 parent f1b8f0f commit 646a122

30 files changed

+2986
-1103
lines changed

src/QGCApplication.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class QGCApplication : public QApplication
101101
/// Although public, these methods are internal and should only be called by UnitTest code
102102
QQmlApplicationEngine *qmlAppEngine() const { return _qmlAppEngine; }
103103

104+
/// Get current language
105+
QLocale getCurrentLanguage() const { return _locale; }
106+
104107
signals:
105108
void languageChanged(const QLocale locale);
106109

@@ -109,9 +112,6 @@ public slots:
109112

110113
void qmlAttemptWindowClose();
111114

112-
/// Get current language
113-
QLocale getCurrentLanguage() const { return _locale; }
114-
115115
/// Show non-modal vehicle message to the user
116116
void showCriticalVehicleMessage(const QString &message);
117117

src/QtLocationPlugin/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ qt_add_plugin(QGCLocation
2727
QGCMapUrlEngine.cpp
2828
QGCMapUrlEngine.h
2929
QGCTile.h
30+
QGCTileCacheDatabase.cpp
31+
QGCTileCacheDatabase.h
3032
QGCTileCacheWorker.cpp
3133
QGCTileCacheWorker.h
3234
QGCTileSet.h

src/QtLocationPlugin/QGCCacheTile.h

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,29 @@
99

1010
#pragma once
1111

12-
#include <QtCore/QString>
1312
#include <QtCore/QByteArray>
1413
#include <QtCore/QMetaType>
14+
#include <QtCore/QString>
1515

16-
class QGCCacheTile
16+
struct QGCCacheTile
1717
{
18-
public:
19-
QGCCacheTile(const QString &hash, const QByteArray &img, const QString &format, const QString &type, quint64 tileSet = UINT64_MAX)
20-
: m_tileSet(tileSet)
21-
, m_hash(hash)
22-
, m_img(img)
23-
, m_format(format)
24-
, m_type(type)
18+
QGCCacheTile(const QString &hash_, const QByteArray &img_, const QString &format_, const QString &type_, quint64 tileSet_ = UINT64_MAX)
19+
: tileSet(tileSet_)
20+
, hash(hash_)
21+
, img(img_)
22+
, format(format_)
23+
, type(type_)
2524
{}
26-
QGCCacheTile(const QString &hash, quint64 tileSet)
27-
: m_tileSet(tileSet)
28-
, m_hash(hash)
25+
QGCCacheTile(const QString &hash_, quint64 tileSet_)
26+
: tileSet(tileSet_)
27+
, hash(hash_)
2928
{}
30-
~QGCCacheTile() = default;
31-
32-
quint64 tileSet() const { return m_tileSet; }
33-
const QString &hash() const { return m_hash; }
34-
const QByteArray &img() const { return m_img; }
35-
const QString &format() const { return m_format; }
36-
const QString &type() const { return m_type; }
3729

38-
private:
39-
const quint64 m_tileSet = 0;
40-
const QString m_hash;
41-
const QByteArray m_img;
42-
const QString m_format;
43-
const QString m_type;
30+
const quint64 tileSet = 0;
31+
const QString hash;
32+
const QByteArray img;
33+
const QString format;
34+
const QString type;
4435
};
4536
Q_DECLARE_METATYPE(QGCCacheTile)
37+
Q_DECLARE_METATYPE(QGCCacheTile*)

src/QtLocationPlugin/QGCCachedTileSet.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,34 @@
77
*
88
****************************************************************************/
99

10-
/**
11-
* @file
12-
* @brief Map Tile Set
13-
*
14-
* @author Gus Grubba <[email protected]>
15-
*
16-
*/
17-
1810
#include "QGCCachedTileSet.h"
1911

12+
#include <QtNetwork/QNetworkProxy>
13+
2014
#include "ElevationMapProvider.h"
15+
#include <QGCApplication.h>
16+
#include <QGCFileDownload.h>
17+
#include <QGCLoggingCategory.h>
2118
#include "QGCMapEngine.h"
2219
#include "QGCMapEngineManager.h"
2320
#include "QGCMapTasks.h"
2421
#include "QGCMapUrlEngine.h"
2522
#include "QGeoFileTileCacheQGC.h"
2623
#include "QGeoTileFetcherQGC.h"
2724

28-
#include <QGCApplication.h>
29-
#include <QGCFileDownload.h>
30-
#include <QGCLoggingCategory.h>
31-
32-
#include <QtNetwork/QNetworkProxy>
3325

3426
QGC_LOGGING_CATEGORY(QGCCachedTileSetLog, "qgc.qtlocation.qgccachedtileset")
3527

3628
QGCCachedTileSet::QGCCachedTileSet(const QString &name, QObject *parent)
3729
: QObject(parent)
3830
, _name(name)
3931
{
40-
// qCDebug(QGCCachedTileSetLog) << Q_FUNC_INFO << this;
32+
qCDebug(QGCCachedTileSetLog) << this;
4133
}
4234

4335
QGCCachedTileSet::~QGCCachedTileSet()
4436
{
45-
// qCDebug(QGCCachedTileSetLog) << Q_FUNC_INFO << this;
37+
qCDebug(QGCCachedTileSetLog) << this;
4638
}
4739

4840
QString QGCCachedTileSet::downloadStatus() const
@@ -118,7 +110,7 @@ void QGCCachedTileSet::_tileListFetched(const QQueue<QGCTile*> &tiles)
118110
#endif
119111
}
120112

121-
(void) _tilesToDownload.append(tiles);
113+
_tilesToDownload.append(tiles);
122114
_prepareDownload();
123115
}
124116

@@ -132,7 +124,7 @@ void QGCCachedTileSet::_doneWithDownload()
132124
if (_savedTileSize != 0) {
133125
avg = _savedTileSize / _savedTileCount;
134126
} else {
135-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "_savedTileSize=0";
127+
qCWarning(QGCCachedTileSetLog) << "_savedTileSize=0";
136128
}
137129

138130
setUniqueTileSize(_uniqueTileCount * avg);
@@ -160,17 +152,17 @@ void QGCCachedTileSet::_prepareDownload()
160152
}
161153

162154
QGCTile* const tile = _tilesToDownload.dequeue();
163-
const int mapId = UrlFactory::getQtMapIdFromProviderType(tile->type());
164-
QNetworkRequest request = QGeoTileFetcherQGC::getNetworkRequest(mapId, tile->x(), tile->y(), tile->z());
155+
const int mapId = UrlFactory::getQtMapIdFromProviderType(tile->type);
156+
QNetworkRequest request = QGeoTileFetcherQGC::getNetworkRequest(mapId, tile->x, tile->y, tile->z);
165157
request.setOriginatingObject(this);
166-
request.setAttribute(QNetworkRequest::User, tile->hash());
158+
request.setAttribute(QNetworkRequest::User, tile->hash);
167159

168160
QNetworkReply* const reply = _networkManager->get(request);
169161
reply->setParent(this);
170162
QGCFileDownload::setIgnoreSSLErrorsIfNeeded(*reply);
171163
(void) connect(reply, &QNetworkReply::finished, this, &QGCCachedTileSet::_networkReplyFinished);
172164
(void) connect(reply, &QNetworkReply::errorOccurred, this, &QGCCachedTileSet::_networkReplyError);
173-
(void) _replies.insert(tile->hash(), reply);
165+
(void) _replies.insert(tile->hash, reply);
174166

175167
delete tile;
176168
if (!_batchRequested && !_noMoreTiles && (_tilesToDownload.count() < (QGeoTileFetcherQGC::concurrentDownloads(_type) * 10))) {
@@ -183,7 +175,7 @@ void QGCCachedTileSet::_networkReplyFinished()
183175
{
184176
QNetworkReply* const reply = qobject_cast<QNetworkReply*>(QObject::sender());
185177
if (!reply) {
186-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "NULL Reply";
178+
qCWarning(QGCCachedTileSetLog) << "NULL Reply";
187179
return;
188180
}
189181
reply->deleteLater();
@@ -193,26 +185,26 @@ void QGCCachedTileSet::_networkReplyFinished()
193185
}
194186

195187
if (!reply->isOpen()) {
196-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Empty Reply";
188+
qCWarning(QGCCachedTileSetLog) << "Empty Reply";
197189
return;
198190
}
199191

200192
const QString hash = reply->request().attribute(QNetworkRequest::User).toString();
201193
if (hash.isEmpty()) {
202-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Empty Hash";
194+
qCWarning(QGCCachedTileSetLog) << "Empty Hash";
203195
return;
204196
}
205197

206198
if (_replies.contains(hash)) {
207199
(void) _replies.remove(hash);
208200
} else {
209-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Reply not in list: " << hash;
201+
qCWarning(QGCCachedTileSetLog) << "Reply not in list: " << hash;
210202
}
211203
qCDebug(QGCCachedTileSetLog) << "Tile fetched:" << hash;
212204

213205
QByteArray image = reply->readAll();
214206
if (image.isEmpty()) {
215-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Empty Image";
207+
qCWarning(QGCCachedTileSetLog) << "Empty Image";
216208
return;
217209
}
218210

@@ -224,14 +216,14 @@ void QGCCachedTileSet::_networkReplyFinished()
224216
const SharedElevationProvider elevationProvider = std::dynamic_pointer_cast<const ElevationProvider>(mapProvider);
225217
image = elevationProvider->serialize(image);
226218
if (image.isEmpty()) {
227-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Failed to Serialize Terrain Tile";
219+
qCWarning(QGCCachedTileSetLog) << "Failed to Serialize Terrain Tile";
228220
return;
229221
}
230222
}
231223

232224
const QString format = mapProvider->getImageFormat(image);
233225
if (format.isEmpty()) {
234-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Empty Format";
226+
qCWarning(QGCCachedTileSetLog) << "Empty Format";
235227
return;
236228
}
237229

@@ -258,24 +250,24 @@ void QGCCachedTileSet::_networkReplyError(QNetworkReply::NetworkError error)
258250
if (!reply) {
259251
return;
260252
}
261-
qCDebug(QGCCachedTileSetLog) << Q_FUNC_INFO << "Error fetching tile" << reply->errorString();
253+
qCDebug(QGCCachedTileSetLog) << "Error fetching tile" << reply->errorString();
262254

263255
setErrorCount(_errorCount + 1);
264256

265257
const QString hash = reply->request().attribute(QNetworkRequest::User).toString();
266258
if (hash.isEmpty()) {
267-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Empty Hash";
259+
qCWarning(QGCCachedTileSetLog) << "Empty Hash";
268260
return;
269261
}
270262

271263
if (_replies.contains(hash)) {
272264
(void) _replies.remove(hash);
273265
} else {
274-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Reply not in list:" << hash;
266+
qCWarning(QGCCachedTileSetLog) << "Reply not in list:" << hash;
275267
}
276268

277269
if (error != QNetworkReply::OperationCanceledError) {
278-
qCWarning(QGCCachedTileSetLog) << Q_FUNC_INFO << "Error:" << reply->errorString();
270+
qCWarning(QGCCachedTileSetLog) << "Error:" << reply->errorString();
279271
}
280272

281273
QGCUpdateTileDownloadStateTask* const task = new QGCUpdateTileDownloadStateTask(_id, QGCTile::StateError, hash);

src/QtLocationPlugin/QGCCachedTileSet.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
*
88
****************************************************************************/
99

10-
11-
/**
12-
* @file
13-
* @brief Map Tile Set
14-
*
15-
* @author Gus Grubba <[email protected]>
16-
*
17-
*/
18-
1910
#pragma once
2011

2112
#include <QtCore/QDateTime>

src/QtLocationPlugin/QGCMapEngine.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,21 @@
77
*
88
****************************************************************************/
99

10+
#include "QGCMapEngine.h"
1011

11-
/**
12-
* @file
13-
* @brief Map Tile Cache
14-
*
15-
* @author Gus Grubba <[email protected]>
16-
*
17-
*/
12+
#include <QtCore/QApplicationStatic>
1813

19-
#include "QGCMapEngine.h"
2014
#include "QGCCachedTileSet.h"
21-
#include "QGCTileCacheWorker.h"
22-
#include "QGeoFileTileCacheQGC.h"
15+
#include "QGCCacheTile.h"
16+
#include "QGCLoggingCategory.h"
2317
#include "QGCMapTasks.h"
24-
#include "QGCTileSet.h"
2518
#include "QGCTile.h"
26-
#include "QGCCacheTile.h"
27-
#include <QGCLoggingCategory.h>
28-
29-
#include <QtCore/QApplicationStatic>
19+
#include "QGCTileCacheWorker.h"
20+
#include "QGCTileSet.h"
21+
#include "QGeoFileTileCacheQGC.h"
3022

3123
QGC_LOGGING_CATEGORY(QGCMapEngineLog, "qgc.qtlocationplugin.qgcmapengine")
3224

33-
Q_DECLARE_METATYPE(QList<QGCTile*>)
34-
3525
Q_APPLICATION_STATIC(QGCMapEngine, _mapEngine);
3626

3727
QGCMapEngine *getQGCMapEngine()
@@ -41,26 +31,26 @@ QGCMapEngine *getQGCMapEngine()
4131

4232
QGCMapEngine::QGCMapEngine(QObject *parent)
4333
: QObject(parent)
44-
, m_worker(new QGCCacheWorker(this))
4534
{
46-
// qCDebug(QGCMapEngineLog) << Q_FUNC_INFO << this;
35+
qCDebug(QGCMapEngineLog) << this;
4736

48-
(void) qRegisterMetaType<QGCMapTask::TaskType>("TaskType");
37+
(void) qRegisterMetaType<QGCMapTask::TaskType>("QGCMapTask::TaskType");
4938
(void) qRegisterMetaType<QGCTile>("QGCTile");
39+
(void) qRegisterMetaType<QGCTile*>("QGCTile*");
5040
(void) qRegisterMetaType<QList<QGCTile*>>("QList<QGCTile*>");
5141
(void) qRegisterMetaType<QGCTileSet>("QGCTileSet");
42+
(void) qRegisterMetaType<QGCTileSet*>("QGCTileSet*");
5243
(void) qRegisterMetaType<QGCCacheTile>("QGCCacheTile");
53-
54-
(void) connect(m_worker, &QGCCacheWorker::updateTotals, this, &QGCMapEngine::_updateTotals);
44+
(void) qRegisterMetaType<QGCCacheTile*>("QGCCacheTile*");
5545
}
5646

5747
QGCMapEngine::~QGCMapEngine()
5848
{
5949
(void) disconnect(m_worker);
6050
m_worker->stop();
61-
m_worker->wait();
51+
(void) m_worker->wait();
6252

63-
// qCDebug(QGCMapEngineLog) << Q_FUNC_INFO << this;
53+
qCDebug(QGCMapEngineLog) << this;
6454
}
6555

6656
QGCMapEngine *QGCMapEngine::instance()
@@ -70,10 +60,18 @@ QGCMapEngine *QGCMapEngine::instance()
7060

7161
void QGCMapEngine::init(const QString &databasePath)
7262
{
63+
if (m_initialized) {
64+
return;
65+
}
66+
67+
m_worker = new QGCCacheWorker(this);
7368
m_worker->setDatabaseFile(databasePath);
69+
(void) connect(m_worker, &QGCCacheWorker::updateTotals, this, &QGCMapEngine::_updateTotals);
7470

75-
QGCMapTask* const task = new QGCMapTask(QGCMapTask::taskInit);
71+
QGCMapTask *task = new QGCMapTask(QGCMapTask::TaskType::taskInit);
7672
(void) addTask(task);
73+
74+
m_initialized = true;
7775
}
7876

7977
bool QGCMapEngine::addTask(QGCMapTask *task)
@@ -85,13 +83,16 @@ void QGCMapEngine::_updateTotals(quint32 totaltiles, quint64 totalsize, quint32
8583
{
8684
emit updateTotals(totaltiles, totalsize, defaulttiles, defaultsize);
8785

88-
const quint64 maxSize = static_cast<quint64>(QGeoFileTileCacheQGC::getMaxDiskCacheSetting()) * pow(1024, 2);
89-
if (!m_prunning && (defaultsize > maxSize)) {
90-
m_prunning = true;
86+
const quint64 maxSize = static_cast<quint64>(QGeoFileTileCacheQGC::getMaxDiskCacheSetting()) * qPow(1024, 2);
87+
if (!m_pruning && (defaultsize > maxSize)) {
88+
m_pruning = true;
9189

9290
const quint64 amountToPrune = defaultsize - maxSize;
93-
QGCPruneCacheTask* const task = new QGCPruneCacheTask(amountToPrune);
91+
QGCPruneCacheTask *task = new QGCPruneCacheTask(amountToPrune);
9492
(void) connect(task, &QGCPruneCacheTask::pruned, this, &QGCMapEngine::_pruned);
95-
(void) addTask(task);
93+
if (!addTask(task)) {
94+
task->deleteLater();
95+
m_pruning = false;
96+
}
9697
}
9798
}

0 commit comments

Comments
 (0)