2015-11-23 02:33:01 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include "downloader.h"
|
|
|
|
#include "ll.h"
|
2015-11-26 00:21:11 +01:00
|
|
|
#include "config.h"
|
2015-11-23 02:33:01 +01:00
|
|
|
#include "map.h"
|
2015-12-18 22:48:11 +01:00
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
|
2016-02-12 20:12:37 +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()));
|
|
|
|
|
2016-03-24 19:34:46 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
QList<Download> dl;
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); ++i) {
|
|
|
|
Tile &t = list[i];
|
2016-03-24 19:34:46 +01:00
|
|
|
QString file = TILES_DIR + QString("/%1/%2-%3-%4")
|
|
|
|
.arg(_name).arg(t.zoom()).arg(t.xy().rx()).arg(t.xy().ry());
|
2015-11-23 02:33:01 +01:00
|
|
|
QFileInfo fi(file);
|
|
|
|
|
2015-12-03 00:25:53 +01:00
|
|
|
if (fi.exists()) {
|
|
|
|
if (!t.pixmap().load(file))
|
|
|
|
fprintf(stderr, "Error loading map tile: %s\n",
|
|
|
|
qPrintable(file));
|
|
|
|
} else {
|
2015-11-23 02:33:01 +01:00
|
|
|
t.pixmap() = QPixmap(TILE_SIZE, TILE_SIZE);
|
|
|
|
t.pixmap().fill();
|
|
|
|
|
|
|
|
QString url(_url);
|
|
|
|
url.replace("$z", QString::number(t.zoom()));
|
|
|
|
url.replace("$x", QString::number(t.xy().x()));
|
|
|
|
url.replace("$y", QString::number(t.xy().y()));
|
|
|
|
dl.append(Download(url, file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dl.empty())
|
|
|
|
Downloader::instance().get(dl);
|
|
|
|
}
|
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));
|
|
|
|
}
|