QGCMapEngine.cpp 19.3 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9 10 11 12 13 14 15 16 17


/**
 * @file
 *   @brief Map Tile Cache
 *
 *   @author Gus Grubba <mavlink@grubba.com>
 *
 */
18 19 20
#include "QGCApplication.h"
#include "AppSettings.h"
#include "SettingsManager.h"
dogmaphobic's avatar
dogmaphobic committed
21 22 23 24 25

#include <math.h>
#include <QSettings>
#include <QStandardPaths>
#include <QDir>
Gus Grubba's avatar
Gus Grubba committed
26
#include <stdio.h>
dogmaphobic's avatar
dogmaphobic committed
27 28 29 30 31 32 33 34 35 36 37

#include "QGCMapEngine.h"
#include "QGCMapTileSet.h"

Q_DECLARE_METATYPE(QGCMapTask::TaskType)
Q_DECLARE_METATYPE(QGCTile)
Q_DECLARE_METATYPE(QList<QGCTile*>)

static const char* kDbFileName = "qgcMapCache.db";
static QLocale kLocale;

dogmaphobic's avatar
dogmaphobic committed
38
#define CACHE_PATH_VERSION  "300"
39

dogmaphobic's avatar
dogmaphobic committed
40 41 42 43 44 45 46 47
struct stQGeoTileCacheQGCMapTypes {
    const char* name;
    UrlFactory::MapType type;
};

//-- IMPORTANT
//   Changes here must reflect those in QGeoTiledMappingManagerEngineQGC.cpp

48
static stQGeoTileCacheQGCMapTypes kMapTypes[] = {
49
#ifndef QGC_LIMITED_MAPS
dogmaphobic's avatar
dogmaphobic committed
50 51 52
    {"Google Street Map",       UrlFactory::GoogleMap},
    {"Google Satellite Map",    UrlFactory::GoogleSatellite},
    {"Google Terrain Map",      UrlFactory::GoogleTerrain},
Phisten's avatar
Phisten committed
53
    {"Google Hybrid Map",       UrlFactory::GoogleHybrid},
54
#endif
dogmaphobic's avatar
dogmaphobic committed
55 56 57
    {"Bing Street Map",         UrlFactory::BingMap},
    {"Bing Satellite Map",      UrlFactory::BingSatellite},
    {"Bing Hybrid Map",         UrlFactory::BingHybrid},
58
    {"Statkart Terrain Map",    UrlFactory::StatkartTopo},
stmoon's avatar
stmoon committed
59 60 61 62 63
    {"ENIRO Terrain Map",       UrlFactory::EniroTopo},

    {"VWorld Satellite Map",     UrlFactory::VWorldSatellite},
    {"VWorld Street Map",        UrlFactory::VWorldStreet}

Gus Grubba's avatar
Gus Grubba committed
64
    /*
dogmaphobic's avatar
dogmaphobic committed
65 66 67 68 69 70 71 72
    {"MapQuest Street Map",     UrlFactory::MapQuestMap},
    {"MapQuest Satellite Map",  UrlFactory::MapQuestSat}
    {"Open Street Map",         UrlFactory::OpenStreetMap}
     */
};

#define NUM_MAPS (sizeof(kMapTypes) / sizeof(stQGeoTileCacheQGCMapTypes))

73
static stQGeoTileCacheQGCMapTypes kMapboxTypes[] = {
Gus Grubba's avatar
Gus Grubba committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    {"Mapbox Street Map",       UrlFactory::MapboxStreets},
    {"Mapbox Satellite Map",    UrlFactory::MapboxSatellite},
    {"Mapbox High Contrast Map",UrlFactory::MapboxHighContrast},
    {"Mapbox Light Map",        UrlFactory::MapboxLight},
    {"Mapbox Dark Map",         UrlFactory::MapboxDark},
    {"Mapbox Hybrid Map",       UrlFactory::MapboxHybrid},
    {"Mapbox Wheat Paste Map",  UrlFactory::MapboxWheatPaste},
    {"Mapbox Streets Basic Map",UrlFactory::MapboxStreetsBasic},
    {"Mapbox Comic Map",        UrlFactory::MapboxComic},
    {"Mapbox Outdoors Map",     UrlFactory::MapboxOutdoors},
    {"Mapbox Run, Byke and Hike Map",   UrlFactory::MapboxRunBikeHike},
    {"Mapbox Pencil Map",       UrlFactory::MapboxPencil},
    {"Mapbox Pirates Map",      UrlFactory::MapboxPirates},
    {"Mapbox Emerald Map",      UrlFactory::MapboxEmerald}
dogmaphobic's avatar
dogmaphobic committed
88 89
};

Gus Grubba's avatar
Gus Grubba committed
90
#define NUM_MAPBOXMAPS (sizeof(kMapboxTypes) / sizeof(stQGeoTileCacheQGCMapTypes))
dogmaphobic's avatar
dogmaphobic committed
91

92
static stQGeoTileCacheQGCMapTypes kEsriTypes[] = {
Gus Grubba's avatar
Gus Grubba committed
93 94 95 96 97 98 99
    {"Esri Street Map",       UrlFactory::EsriWorldStreet},
    {"Esri Satellite Map",    UrlFactory::EsriWorldSatellite},
    {"Esri Terrain Map",      UrlFactory::EsriTerrain}
};

#define NUM_ESRIMAPS (sizeof(kEsriTypes) / sizeof(stQGeoTileCacheQGCMapTypes))

100
static stQGeoTileCacheQGCMapTypes kElevationTypes[] = {
101 102 103 104 105
    {"Airmap Elevation Data", UrlFactory::AirmapElevation}
};

#define NUM_ELEVMAPS (sizeof(kElevationTypes) / sizeof(stQGeoTileCacheQGCMapTypes))

dogmaphobic's avatar
dogmaphobic committed
106 107 108 109 110
static const char* kMaxDiskCacheKey = "MaxDiskCache";
static const char* kMaxMemCacheKey  = "MaxMemoryCache";

//-----------------------------------------------------------------------------
// Singleton
111
static QGCMapEngine* kMapEngine = nullptr;
dogmaphobic's avatar
dogmaphobic committed
112 113 114 115 116 117 118 119
QGCMapEngine*
getQGCMapEngine()
{
    if(!kMapEngine)
        kMapEngine = new QGCMapEngine();
    return kMapEngine;
}

120
//-----------------------------------------------------------------------------
121
const double QGCMapEngine::srtm1TileSize = 0.01;
122

dogmaphobic's avatar
dogmaphobic committed
123 124 125 126 127 128
//-----------------------------------------------------------------------------
void
destroyMapEngine()
{
    if(kMapEngine) {
        delete kMapEngine;
129
        kMapEngine = nullptr;
dogmaphobic's avatar
dogmaphobic committed
130 131 132 133 134
    }
}

//-----------------------------------------------------------------------------
QGCMapEngine::QGCMapEngine()
135
    : _urlFactory(new UrlFactory())
dogmaphobic's avatar
dogmaphobic committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#ifdef WE_ARE_KOSHER
    //-- TODO: Get proper version
    #if defined Q_OS_MAC
        , _userAgent("QGroundControl (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/2.9.0")
    #elif defined Q_OS_WIN32
        , _userAgent("QGroundControl (Windows; Windows NT 6.0) (KHTML, like Gecko) Version/2.9.0")
    #else
        , _userAgent("QGroundControl (X11; Ubuntu; Linux x86_64) (KHTML, like Gecko) Version/2.9.0")
    #endif
#else
    #if defined Q_OS_MAC
        , _userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefox/25.0")
    #elif defined Q_OS_WIN32
        , _userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0")
    #else
        , _userAgent("Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0")
    #endif
#endif
    , _maxDiskCache(0)
    , _maxMemCache(0)
    , _prunning(false)
dogmaphobic's avatar
dogmaphobic committed
157
    , _cacheWasReset(false)
158
    , _isInternetActive(false)
dogmaphobic's avatar
dogmaphobic committed
159 160 161 162
{
    qRegisterMetaType<QGCMapTask::TaskType>();
    qRegisterMetaType<QGCTile>();
    qRegisterMetaType<QList<QGCTile*>>();
163 164
    connect(&_worker, &QGCCacheWorker::updateTotals,   this, &QGCMapEngine::_updateTotals);
    connect(&_worker, &QGCCacheWorker::internetStatus, this, &QGCMapEngine::_internetStatus);
dogmaphobic's avatar
dogmaphobic committed
165 166 167 168 169 170 171 172 173 174 175
}

//-----------------------------------------------------------------------------
QGCMapEngine::~QGCMapEngine()
{
    _worker.quit();
    _worker.wait();
    if(_urlFactory)
        delete _urlFactory;
}

dogmaphobic's avatar
dogmaphobic committed
176 177 178 179 180 181 182 183 184 185 186
//-----------------------------------------------------------------------------
void
QGCMapEngine::_checkWipeDirectory(const QString& dirPath)
{
    QDir dir(dirPath);
    if (dir.exists(dirPath)) {
        _cacheWasReset = true;
        _wipeDirectory(dirPath);
    }
}

dogmaphobic's avatar
dogmaphobic committed
187 188
//-----------------------------------------------------------------------------
void
dogmaphobic's avatar
dogmaphobic committed
189
QGCMapEngine::_wipeOldCaches()
dogmaphobic's avatar
dogmaphobic committed
190
{
dogmaphobic's avatar
dogmaphobic committed
191 192 193 194
    QString oldCacheDir;
#ifdef __mobile__
    oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)      + QLatin1String("/QGCMapCache55");
#else
195
    oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/QGCMapCache55");
dogmaphobic's avatar
dogmaphobic committed
196
#endif
dogmaphobic's avatar
dogmaphobic committed
197
    _checkWipeDirectory(oldCacheDir);
dogmaphobic's avatar
dogmaphobic committed
198
#ifdef __mobile__
dogmaphobic's avatar
dogmaphobic committed
199
    oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)      + QLatin1String("/QGCMapCache100");
dogmaphobic's avatar
dogmaphobic committed
200
#else
201
    oldCacheDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/QGCMapCache100");
202
#endif
dogmaphobic's avatar
dogmaphobic committed
203
    _checkWipeDirectory(oldCacheDir);
dogmaphobic's avatar
dogmaphobic committed
204 205 206 207 208 209 210 211
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::init()
{
    //-- Delete old style caches (if present)
    _wipeOldCaches();
212 213 214 215
    //-- Figure out cache path
#ifdef __mobile__
    QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)      + QLatin1String("/QGCMapCache" CACHE_PATH_VERSION);
#else
216
    QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/QGCMapCache" CACHE_PATH_VERSION);
dogmaphobic's avatar
dogmaphobic committed
217 218 219
#endif
    if(!QDir::root().mkpath(cacheDir)) {
        qWarning() << "Could not create mapping disk cache directory: " << cacheDir;
220
        cacheDir = QDir::homePath() + QStringLiteral("/.qgcmapscache/");
dogmaphobic's avatar
dogmaphobic committed
221 222 223 224 225 226 227 228 229
        if(!QDir::root().mkpath(cacheDir)) {
            qWarning() << "Could not create mapping disk cache directory: " << cacheDir;
            cacheDir.clear();
        }
    }
    _cachePath = cacheDir;
    if(!_cachePath.isEmpty()) {
        _cacheFile = kDbFileName;
        _worker.setDatabaseFile(_cachePath + "/" + _cacheFile);
230
        qDebug() << "Map Cache in:" << _cachePath << "/" << _cacheFile;
dogmaphobic's avatar
dogmaphobic committed
231 232 233 234 235 236 237
    } else {
        qCritical() << "Could not find suitable map cache directory.";
    }
    QGCMapTask* task = new QGCMapTask(QGCMapTask::taskInit);
    _worker.enqueueTask(task);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
//-----------------------------------------------------------------------------
bool
QGCMapEngine::_wipeDirectory(const QString& dirPath)
{
    bool result = true;
    QDir dir(dirPath);
    if (dir.exists(dirPath)) {
        Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
            if (info.isDir()) {
                result = _wipeDirectory(info.absoluteFilePath());
            } else {
                result = QFile::remove(info.absoluteFilePath());
            }
            if (!result) {
                return result;
            }
        }
        result = dir.rmdir(dirPath);
    }
    return result;
}

dogmaphobic's avatar
dogmaphobic committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
//-----------------------------------------------------------------------------
void
QGCMapEngine::addTask(QGCMapTask* task)
{
    _worker.enqueueTask(task);
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::cacheTile(UrlFactory::MapType type, int x, int y, int z, const QByteArray& image, const QString &format, qulonglong set)
{
    QString hash = getTileHash(type, x, y, z);
    cacheTile(type, hash, image, format, set);
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::cacheTile(UrlFactory::MapType type, const QString& hash, const QByteArray& image, const QString& format, qulonglong set)
{
279 280 281 282 283 284
    AppSettings* appSettings = qgcApp()->toolbox()->settingsManager()->appSettings();
    //-- If we are allowed to persist data, save tile to cache
    if(!appSettings->disableAllPersistence()->rawValue().toBool()) {
        QGCSaveTileTask* task = new QGCSaveTileTask(new QGCCacheTile(hash, image, format, type, set));
        _worker.enqueueTask(task);
    }
dogmaphobic's avatar
dogmaphobic committed
285 286 287 288 289 290
}

//-----------------------------------------------------------------------------
QString
QGCMapEngine::getTileHash(UrlFactory::MapType type, int x, int y, int z)
{
291
    return QString().sprintf("%04d%08d%08d%03d", static_cast<int>(type), x, y, z);
dogmaphobic's avatar
dogmaphobic committed
292 293 294 295 296 297 298
}

//-----------------------------------------------------------------------------
UrlFactory::MapType
QGCMapEngine::hashToType(const QString& hash)
{
    QString type = hash.mid(0,4);
299
    return static_cast<UrlFactory::MapType>(type.toInt());
dogmaphobic's avatar
dogmaphobic committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

//-----------------------------------------------------------------------------
QGCFetchTileTask*
QGCMapEngine::createFetchTileTask(UrlFactory::MapType type, int x, int y, int z)
{
    QString hash = getTileHash(type, x, y, z);
    QGCFetchTileTask* task = new QGCFetchTileTask(hash);
    return task;
}

//-----------------------------------------------------------------------------
QGCTileSet
QGCMapEngine::getTileCount(int zoom, double topleftLon, double topleftLat, double bottomRightLon, double bottomRightLat, UrlFactory::MapType mapType)
{
    if(zoom <  1) zoom = 1;
316
    if(zoom > MAX_MAP_ZOOM) zoom = MAX_MAP_ZOOM;
dogmaphobic's avatar
dogmaphobic committed
317
    QGCTileSet set;
318 319 320 321 322 323 324 325 326 327 328
    if (mapType != UrlFactory::AirmapElevation) {
        set.tileX0 = long2tileX(topleftLon,     zoom);
        set.tileY0 = lat2tileY(topleftLat,      zoom);
        set.tileX1 = long2tileX(bottomRightLon, zoom);
        set.tileY1 = lat2tileY(bottomRightLat,  zoom);
    } else {
        set.tileX0 = long2elevationTileX(topleftLon,        zoom);
        set.tileY0 = lat2elevationTileY(bottomRightLat,     zoom);
        set.tileX1 = long2elevationTileX(bottomRightLon,    zoom);
        set.tileY1 = lat2elevationTileY(topleftLat,         zoom);
    }
329
    set.tileCount = (static_cast<quint64>(set.tileX1) - static_cast<quint64>(set.tileX0) + 1) * (static_cast<quint64>(set.tileY1) - static_cast<quint64>(set.tileY0) + 1);
dogmaphobic's avatar
dogmaphobic committed
330 331 332 333 334 335 336 337
    set.tileSize  = UrlFactory::averageSizeForType(mapType) * set.tileCount;
    return set;
}

//-----------------------------------------------------------------------------
int
QGCMapEngine::long2tileX(double lon, int z)
{
338
    return static_cast<int>(floor((lon + 180.0) / 360.0 * pow(2.0, z)));
dogmaphobic's avatar
dogmaphobic committed
339 340 341 342 343 344
}

//-----------------------------------------------------------------------------
int
QGCMapEngine::lat2tileY(double lat, int z)
{
345
    return static_cast<int>(floor((1.0 - log( tan(lat * M_PI/180.0) + 1.0 / cos(lat * M_PI/180.0)) / M_PI) / 2.0 * pow(2.0, z)));
dogmaphobic's avatar
dogmaphobic committed
346 347
}

348 349 350 351 352
//-----------------------------------------------------------------------------
int
QGCMapEngine::long2elevationTileX(double lon, int z)
{
    Q_UNUSED(z);
353
    return static_cast<int>(floor((lon + 180.0) / srtm1TileSize));
354 355 356 357 358 359 360
}

//-----------------------------------------------------------------------------
int
QGCMapEngine::lat2elevationTileY(double lat, int z)
{
    Q_UNUSED(z);
361
    return static_cast<int>(floor((lat + 90.0) / srtm1TileSize));
362 363
}

dogmaphobic's avatar
dogmaphobic committed
364 365 366 367 368 369 370 371 372 373
//-----------------------------------------------------------------------------
UrlFactory::MapType
QGCMapEngine::getTypeFromName(const QString& name)
{
    size_t i;
    for(i = 0; i < NUM_MAPS; i++) {
        if(name.compare(kMapTypes[i].name, Qt::CaseInsensitive) == 0)
            return kMapTypes[i].type;
    }
    for(i = 0; i < NUM_MAPBOXMAPS; i++) {
Gus Grubba's avatar
Gus Grubba committed
374 375
        if(name.compare(kMapboxTypes[i].name, Qt::CaseInsensitive) == 0)
            return kMapboxTypes[i].type;
dogmaphobic's avatar
dogmaphobic committed
376
    }
Gus Grubba's avatar
Gus Grubba committed
377 378 379 380
    for(i = 0; i < NUM_ESRIMAPS; i++) {
        if(name.compare(kEsriTypes[i].name, Qt::CaseInsensitive) == 0)
            return kEsriTypes[i].type;
    }
381 382 383 384
    for(i = 0; i < NUM_ELEVMAPS; i++) {
        if(name.compare(kElevationTypes[i].name, Qt::CaseInsensitive) == 0)
            return kElevationTypes[i].type;
    }
dogmaphobic's avatar
dogmaphobic committed
385 386 387 388 389 390 391 392 393 394 395
    return UrlFactory::Invalid;
}

//-----------------------------------------------------------------------------
QStringList
QGCMapEngine::getMapNameList()
{
    QStringList mapList;
    for(size_t i = 0; i < NUM_MAPS; i++) {
        mapList << kMapTypes[i].name;
    }
396
    if(!qgcApp()->toolbox()->settingsManager()->appSettings()->mapboxToken()->rawValue().toString().isEmpty()) {
dogmaphobic's avatar
dogmaphobic committed
397
        for(size_t i = 0; i < NUM_MAPBOXMAPS; i++) {
Gus Grubba's avatar
Gus Grubba committed
398
            mapList << kMapboxTypes[i].name;
dogmaphobic's avatar
dogmaphobic committed
399 400
        }
    }
401
    if(!qgcApp()->toolbox()->settingsManager()->appSettings()->esriToken()->rawValue().toString().isEmpty()) {
Gus Grubba's avatar
Gus Grubba committed
402 403 404 405
        for(size_t i = 0; i < NUM_ESRIMAPS; i++) {
            mapList << kEsriTypes[i].name;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    return mapList;
}

//-----------------------------------------------------------------------------
quint32
QGCMapEngine::getMaxDiskCache()
{
    if(!_maxDiskCache) {
        QSettings settings;
        _maxDiskCache = settings.value(kMaxDiskCacheKey, 1024).toUInt();
    }
    return _maxDiskCache;
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::setMaxDiskCache(quint32 size)
{
    QSettings settings;
    settings.setValue(kMaxDiskCacheKey, size);
    _maxDiskCache = size;
}

//-----------------------------------------------------------------------------
quint32
QGCMapEngine::getMaxMemCache()
{
    if(!_maxMemCache) {
        QSettings settings;
#ifdef __mobile__
        _maxMemCache = settings.value(kMaxMemCacheKey, 16).toUInt();
#else
        _maxMemCache = settings.value(kMaxMemCacheKey, 128).toUInt();
#endif
    }
441 442 443
    //-- Size in MB
    if(_maxMemCache > 1024)
        _maxMemCache = 1024;
dogmaphobic's avatar
dogmaphobic committed
444 445 446 447 448 449 450
    return _maxMemCache;
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::setMaxMemCache(quint32 size)
{
451 452 453
    //-- Size in MB
    if(size > 1024)
        size = 1024;
dogmaphobic's avatar
dogmaphobic committed
454 455 456 457 458 459 460 461 462 463 464 465
    QSettings settings;
    settings.setValue(kMaxMemCacheKey, size);
    _maxMemCache = size;
}

//-----------------------------------------------------------------------------
QString
QGCMapEngine::bigSizeToString(quint64 size)
{
    if(size < 1024)
        return kLocale.toString(size);
    else if(size < 1024 * 1024)
466
        return kLocale.toString(static_cast<double>(size) / 1024.0, 'f', 1) + "kB";
dogmaphobic's avatar
dogmaphobic committed
467
    else if(size < 1024 * 1024 * 1024)
468
        return kLocale.toString(static_cast<double>(size) / (1024.0 * 1024.0), 'f', 1) + "MB";
dogmaphobic's avatar
dogmaphobic committed
469
    else if(size < 1024.0 * 1024.0 * 1024.0 * 1024.0)
470
        return kLocale.toString(static_cast<double>(size) / (1024.0 * 1024.0 * 1024.0), 'f', 1) + "GB";
dogmaphobic's avatar
dogmaphobic committed
471
    else
472
        return kLocale.toString(static_cast<double>(size) / (1024.0 * 1024.0 * 1024.0 * 1024), 'f', 1) + "TB";
dogmaphobic's avatar
dogmaphobic committed
473 474
}

475 476 477 478 479 480 481 482 483 484 485 486
//-----------------------------------------------------------------------------
QString
QGCMapEngine::storageFreeSizeToString(quint64 size_MB)
{
    if(size_MB < 1024)
        return kLocale.toString(static_cast<double>(size_MB) , 'f', 0) + " MB";
    else if(size_MB < 1024.0 * 1024.0)
        return kLocale.toString(static_cast<double>(size_MB) / (1024.0), 'f', 2) + " GB";
    else
        return kLocale.toString(static_cast<double>(size_MB) / (1024.0 * 1024), 'f', 2) + " TB";
}

dogmaphobic's avatar
dogmaphobic committed
487 488
//-----------------------------------------------------------------------------
QString
dogmaphobic's avatar
dogmaphobic committed
489
QGCMapEngine::numberToString(quint64 number)
dogmaphobic's avatar
dogmaphobic committed
490 491 492 493 494 495 496 497 498
{
    return kLocale.toString(number);
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::_updateTotals(quint32 totaltiles, quint64 totalsize, quint32 defaulttiles, quint64 defaultsize)
{
    emit updateTotals(totaltiles, totalsize, defaulttiles, defaultsize);
499
    quint64 maxSize = static_cast<quint64>(getMaxDiskCache()) * 1024L * 1024L;
dogmaphobic's avatar
dogmaphobic committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    if(!_prunning && defaultsize > maxSize) {
        //-- Prune Disk Cache
        _prunning = true;
        QGCPruneCacheTask* task = new QGCPruneCacheTask(defaultsize - maxSize);
        connect(task, &QGCPruneCacheTask::pruned, this, &QGCMapEngine::_pruned);
        getQGCMapEngine()->addTask(task);
    }
}
//-----------------------------------------------------------------------------
void
QGCMapEngine::_pruned()
{
    _prunning = false;
}

//-----------------------------------------------------------------------------
int
QGCMapEngine::concurrentDownloads(UrlFactory::MapType type)
{
    switch(type) {
    case UrlFactory::GoogleMap:
    case UrlFactory::GoogleSatellite:
    case UrlFactory::GoogleTerrain:
    case UrlFactory::BingMap:
    case UrlFactory::BingSatellite:
    case UrlFactory::BingHybrid:
526
    case UrlFactory::StatkartTopo:
527
    case UrlFactory::EniroTopo:
Gus Grubba's avatar
Gus Grubba committed
528 529 530
    case UrlFactory::EsriWorldStreet:
    case UrlFactory::EsriWorldSatellite:
    case UrlFactory::EsriTerrain:
531
    case UrlFactory::AirmapElevation:
stmoon's avatar
stmoon committed
532 533 534
    case UrlFactory::VWorldMap:
    case UrlFactory::VWorldSatellite:
    case UrlFactory::VWorldStreet:
dogmaphobic's avatar
dogmaphobic committed
535
        return 12;
Gus Grubba's avatar
Gus Grubba committed
536
    /*
dogmaphobic's avatar
dogmaphobic committed
537 538 539
    case UrlFactory::MapQuestMap:
    case UrlFactory::MapQuestSat:
        return 8;
Gus Grubba's avatar
Gus Grubba committed
540
    */
dogmaphobic's avatar
dogmaphobic committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554
    default:
        break;
    }
    return 6;
}

//-----------------------------------------------------------------------------
QGCCreateTileSetTask::~QGCCreateTileSetTask()
{
    //-- If not sent out, delete it
    if(!_saved && _tileSet)
        delete _tileSet;
}

555 556 557 558
//-----------------------------------------------------------------------------
void
QGCMapEngine::testInternet()
{
559 560 561 562
    if(qgcApp()->toolbox()->settingsManager()->appSettings()->checkInternet()->rawValue().toBool())
        getQGCMapEngine()->addTask(new QGCTestInternetTask());
    else
        _internetStatus(true);
563 564 565 566 567 568
}

//-----------------------------------------------------------------------------
void
QGCMapEngine::_internetStatus(bool active)
{
569 570 571 572
    if(_isInternetActive != active) {
        _isInternetActive = active;
        emit internetUpdated();
    }
573 574
}

dogmaphobic's avatar
dogmaphobic committed
575 576
// Resolution math: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale