1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-02-07 12:05:14 +01:00
GPXSee/src/map.cpp

135 lines
2.5 KiB
C++
Raw Normal View History

2015-11-23 02:33:01 +01:00
#include <QFileInfo>
#include <QDir>
#include "downloader.h"
2015-11-26 00:21:11 +01:00
#include "config.h"
2015-11-23 02:33:01 +01:00
#include "map.h"
2015-11-23 02:33:01 +01:00
Map::Map(QObject *parent, const QString &name, const QString &url)
2016-02-08 21:08:29 +01:00
: QObject(parent)
2015-11-23 02:33:01 +01:00
{
_name = name;
_url = url;
connect(&Downloader::instance(), SIGNAL(finished()), this,
SLOT(emitLoaded()));
QString path = TILES_DIR + QString("/") + _name;
2016-04-01 19:25:34 +02:00
if (!QDir().mkpath(path))
2015-12-18 22:50:28 +01:00
fprintf(stderr, "Error creating tiles dir: %s\n", qPrintable(path));
2015-11-23 02:33:01 +01:00
}
void Map::emitLoaded()
{
emit loaded();
}
void Map::loadTiles(QList<Tile> &list, bool block)
{
if (block)
loadTilesSync(list);
else
loadTilesAsync(list);
}
void Map::loadTilesAsync(QList<Tile> &list)
2015-11-23 02:33:01 +01:00
{
QList<Download> dl;
for (int i = 0; i < list.size(); i++) {
2015-11-23 02:33:01 +01:00
Tile &t = list[i];
QString file = tileFile(t);
2015-11-23 02:33:01 +01:00
QFileInfo fi(file);
if (!fi.exists()) {
fillTile(t);
dl.append(Download(tileUrl(t), file));
} else
loadTileFile(t, file);
2015-11-23 02:33:01 +01:00
}
if (!dl.empty())
Downloader::instance().get(dl);
}
2016-04-01 19:25:34 +02:00
void Map::loadTilesSync(QList<Tile> &list)
{
QList<Download> dl;
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
QString file = tileFile(t);
QFileInfo fi(file);
if (!fi.exists())
dl.append(Download(tileUrl(t), file));
else
loadTileFile(t, file);
}
if (dl.empty())
return;
QEventLoop wait;
connect(&Downloader::instance(), SIGNAL(finished()), &wait, SLOT(quit()));
if (Downloader::instance().get(dl))
wait.exec();
for (int i = 0; i < list.size(); i++) {
Tile &t = list[i];
if (t.pixmap().isNull()) {
QString file = tileFile(t);
QFileInfo fi(file);
if (!(fi.exists() && loadTileFile(t, file)))
fillTile(t);
}
}
}
void Map::fillTile(Tile &tile)
{
tile.pixmap() = QPixmap(Tile::size(), Tile::size());
tile.pixmap().fill();
}
bool Map::loadTileFile(Tile &tile, const QString &file)
{
if (!tile.pixmap().load(file)) {
fprintf(stderr, "%s: error loading tile file\n", qPrintable(file));
return false;
}
return true;
}
QString Map::tileUrl(const Tile &tile)
{
QString url(_url);
url.replace("$z", QString::number(tile.zoom()));
url.replace("$x", QString::number(tile.xy().x()));
url.replace("$y", QString::number(tile.xy().y()));
return url;
}
QString Map::tileFile(const Tile &tile)
{
QString file = TILES_DIR + QString("/%1/%2-%3-%4").arg(_name)
.arg(tile.zoom()).arg(tile.xy().x()).arg(tile.xy().y());
return file;
}
2016-04-01 19:25:34 +02:00
void Map::clearCache()
{
QString path = TILES_DIR + QString("/") + _name;
QDir dir = QDir(path);
QStringList list = dir.entryList();
for (int i = 0; i < list.count(); i++)
dir.remove(list.at(i));
}