1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/map/tileloader.cpp

127 lines
2.7 KiB
C++
Raw Normal View History

2018-02-20 23:37:19 +01:00
#include <QDir>
#include <QFileInfo>
#include <QEventLoop>
#include "tileloader.h"
static bool loadTileFile(Tile &tile, const QString &file)
{
if (!tile.pixmap().load(file)) {
2018-09-24 23:07:11 +02:00
qWarning("%s: error loading tile file", qPrintable(file));
2018-02-20 23:37:19 +01:00
return false;
}
return true;
}
2018-09-24 22:49:10 +02:00
TileLoader::TileLoader(const QString &dir, QObject *parent)
: QObject(parent), _dir(dir)
{
2018-09-24 22:49:10 +02:00
if (!QDir().mkpath(_dir))
2018-09-24 23:07:11 +02:00
qWarning("%s: %s", qPrintable(_dir), "Error creating tiles directory");
2018-09-24 22:49:10 +02:00
_downloader = new Downloader(this);
connect(_downloader, SIGNAL(finished()), this, SIGNAL(finished()));
}
2018-02-20 23:37:19 +01:00
2018-10-05 07:10:49 +02:00
void TileLoader::loadTilesAsync(QVector<Tile> &list)
2018-02-20 23:37:19 +01:00
{
QList<Download> dl;
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
2018-09-22 12:42:49 +02:00
QString file(tileFile(t));
2018-02-20 23:37:19 +01:00
QFileInfo fi(file);
2018-10-04 23:02:43 +02:00
if (fi.exists())
2018-02-20 23:37:19 +01:00
loadTileFile(t, file);
2018-10-04 23:02:43 +02:00
else {
QUrl url(tileUrl(t));
if (url.isLocalFile())
loadTileFile(t, url.toLocalFile());
else
dl.append(Download(url, file));
}
2018-02-20 23:37:19 +01:00
}
if (!dl.empty())
_downloader->get(dl, _authorization);
2018-02-20 23:37:19 +01:00
}
2018-10-05 07:10:49 +02:00
void TileLoader::loadTilesSync(QVector<Tile> &list)
2018-02-20 23:37:19 +01:00
{
QList<Download> dl;
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
2018-09-22 12:42:49 +02:00
QString file(tileFile(t));
2018-02-20 23:37:19 +01:00
QFileInfo fi(file);
2018-10-04 23:02:43 +02:00
if (fi.exists())
2018-02-20 23:37:19 +01:00
loadTileFile(t, file);
2018-10-04 23:02:43 +02:00
else {
QUrl url(tileUrl(t));
if (url.isLocalFile())
loadTileFile(t, url.toLocalFile());
else
dl.append(Download(url, file));
}
2018-02-20 23:37:19 +01:00
}
if (dl.empty())
return;
QEventLoop wait;
QObject::connect(_downloader, SIGNAL(finished()), &wait, SLOT(quit()));
if (_downloader->get(dl, _authorization))
2018-02-20 23:37:19 +01:00
wait.exec();
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
if (t.pixmap().isNull()) {
QString file = tileFile(t);
2018-04-07 23:04:26 +02:00
if (QFileInfo(file).exists())
2018-02-20 23:37:19 +01:00
loadTileFile(t, file);
}
}
}
void TileLoader::clearCache()
{
QDir dir = QDir(_dir);
QStringList list = dir.entryList();
for (int i = 0; i < list.count(); i++)
dir.remove(list.at(i));
_downloader->clearErrors();
2018-02-20 23:37:19 +01:00
}
2018-09-22 12:42:49 +02:00
QUrl TileLoader::tileUrl(const Tile &tile) const
2018-02-20 23:37:19 +01:00
{
QString url(_url);
if (!tile.bbox().isNull()) {
QString bbox = QString("%1,%2,%3,%4").arg(
QString::number(tile.bbox().left(), 'f', 6),
QString::number(tile.bbox().bottom(), 'f', 6),
QString::number(tile.bbox().right(), 'f', 6),
QString::number(tile.bbox().top(), 'f', 6));
url.replace("$bbox", bbox);
} else {
url.replace("$z", tile.zoom().toString());
url.replace("$x", QString::number(tile.xy().x()));
url.replace("$y", QString::number(tile.xy().y()));
}
2018-02-20 23:37:19 +01:00
2018-09-22 12:42:49 +02:00
return QUrl(url);
2018-02-20 23:37:19 +01:00
}
QString TileLoader::tileFile(const Tile &tile) const
{
2018-10-04 23:02:43 +02:00
return _dir + QLatin1Char('/') + tile.zoom().toString() + QLatin1Char('-')
+ QString::number(tile.xy().x()) + QLatin1Char('-')
+ QString::number(tile.xy().y());
2018-02-20 23:37:19 +01:00
}