2015-11-26 19:13:59 +01:00
|
|
|
#include <QFileInfo>
|
2017-04-21 21:15:58 +02:00
|
|
|
#include <QDir>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/range.h"
|
2017-04-21 21:15:58 +02:00
|
|
|
#include "atlas.h"
|
|
|
|
#include "offlinemap.h"
|
2017-03-18 01:30:31 +01:00
|
|
|
#include "onlinemap.h"
|
2015-11-23 23:19:57 +01:00
|
|
|
#include "maplist.h"
|
|
|
|
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
#define ZOOM_MAX 18
|
|
|
|
#define ZOOM_MIN 2
|
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
Map *MapList::loadListEntry(const QByteArray &line)
|
2015-11-23 23:19:57 +01:00
|
|
|
{
|
2017-09-09 12:33:43 +02:00
|
|
|
int max;
|
|
|
|
|
2017-04-21 21:15:58 +02:00
|
|
|
QList<QByteArray> list = line.split('\t');
|
2017-09-09 12:33:43 +02:00
|
|
|
if (list.size() < 2)
|
2017-07-27 19:47:46 +02:00
|
|
|
return 0;
|
2015-11-26 19:13:59 +01:00
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
QByteArray ba1 = list.at(0).trimmed();
|
|
|
|
QByteArray ba2 = list.at(1).trimmed();
|
2017-04-21 21:15:58 +02:00
|
|
|
if (ba1.isEmpty() || ba2.isEmpty())
|
2017-07-27 19:47:46 +02:00
|
|
|
return 0;
|
2017-04-21 21:15:58 +02:00
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
if (list.size() == 3) {
|
|
|
|
bool ok;
|
|
|
|
max = QString(list.at(2).trimmed()).toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
max = ZOOM_MAX;
|
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
return new OnlineMap(QString::fromUtf8(ba1.data(), ba1.size()),
|
2017-09-09 12:33:43 +02:00
|
|
|
QString::fromLatin1(ba2.data(), ba2.size()), Range(ZOOM_MIN, max), this);
|
2017-04-21 21:15:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MapList::loadList(const QString &path)
|
|
|
|
{
|
|
|
|
QFile file(path);
|
2017-07-27 19:47:46 +02:00
|
|
|
QList<Map*> maps;
|
2015-11-23 23:19:57 +01:00
|
|
|
|
|
|
|
if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
2017-04-21 21:15:58 +02:00
|
|
|
_errorString = file.errorString();
|
|
|
|
return false;
|
2015-11-23 23:19:57 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 22:31:39 +01:00
|
|
|
int ln = 0;
|
2015-11-23 23:19:57 +01:00
|
|
|
while (!file.atEnd()) {
|
2015-12-01 22:31:39 +01:00
|
|
|
ln++;
|
2015-11-23 23:19:57 +01:00
|
|
|
QByteArray line = file.readLine();
|
2017-07-27 19:47:46 +02:00
|
|
|
Map *map = loadListEntry(line);
|
2017-04-21 21:15:58 +02:00
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
if (map)
|
|
|
|
maps.append(map);
|
|
|
|
else {
|
|
|
|
for (int i = 0; i < maps.count(); i++)
|
|
|
|
delete maps.at(i);
|
2017-04-21 21:15:58 +02:00
|
|
|
_errorString = QString("Invalid map list entry on line %1.")
|
|
|
|
.arg(QString::number(ln));
|
|
|
|
return false;
|
2015-11-23 23:19:57 +01:00
|
|
|
}
|
2017-04-21 21:15:58 +02:00
|
|
|
}
|
2015-11-23 23:19:57 +01:00
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
_maps += maps;
|
|
|
|
|
2017-04-21 21:15:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-23 23:19:57 +01:00
|
|
|
|
2017-04-21 21:15:58 +02:00
|
|
|
bool MapList::loadMap(const QString &path)
|
|
|
|
{
|
2017-07-27 19:47:46 +02:00
|
|
|
OfflineMap *map = new OfflineMap(path, this);
|
|
|
|
if (map->isValid()) {
|
|
|
|
_maps.append(map);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
_errorString = map->errorString();
|
|
|
|
delete map;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 23:19:57 +01:00
|
|
|
|
2017-07-27 19:47:46 +02:00
|
|
|
bool MapList::loadTba(const QString &path)
|
|
|
|
{
|
|
|
|
Atlas *atlas = new Atlas(path, this);
|
|
|
|
if (atlas->isValid()) {
|
|
|
|
_maps.append(atlas);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
_errorString = atlas->errorString();
|
|
|
|
delete atlas;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapList::loadTar(const QString &path)
|
|
|
|
{
|
|
|
|
Atlas *atlas = new Atlas(path, this);
|
|
|
|
if (atlas->isValid()) {
|
|
|
|
_maps.append(atlas);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
_errorString = atlas->errorString();
|
|
|
|
delete atlas;
|
|
|
|
OfflineMap *map = new OfflineMap(path, this);
|
|
|
|
if (map->isValid()) {
|
|
|
|
_maps.append(map);
|
2017-04-21 21:15:58 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2017-07-27 19:47:46 +02:00
|
|
|
qWarning("%s: %s", qPrintable(path), qPrintable(_errorString));
|
|
|
|
qWarning("%s: %s", qPrintable(path),
|
|
|
|
qPrintable(map->errorString()));
|
|
|
|
_errorString = "Not a map/atlas file";
|
|
|
|
delete map;
|
2017-04-21 21:15:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-27 19:47:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MapList::loadFile(const QString &path)
|
|
|
|
{
|
|
|
|
QFileInfo fi(path);
|
|
|
|
QString suffix = fi.suffix().toLower();
|
|
|
|
|
|
|
|
if (suffix == "txt")
|
|
|
|
return loadList(path);
|
|
|
|
else if (suffix == "map")
|
|
|
|
return loadMap(path);
|
|
|
|
else if (suffix == "tba")
|
|
|
|
return loadTba(path);
|
|
|
|
else if (suffix == "tar")
|
|
|
|
return loadTar(path);
|
|
|
|
else {
|
2017-04-21 21:15:58 +02:00
|
|
|
_errorString = "Not a map/atlas file";
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-23 23:19:57 +01:00
|
|
|
}
|
2017-07-27 19:47:46 +02:00
|
|
|
|
|
|
|
QString MapList::formats()
|
|
|
|
{
|
|
|
|
return tr("Map files (*.map *.tba *.tar)") + ";;"
|
|
|
|
+ tr("URL list files (*.txt)");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList MapList::filter()
|
|
|
|
{
|
|
|
|
QStringList filter;
|
|
|
|
filter << "*.map" << "*.tba" << "*.tar" << "*.txt";
|
|
|
|
return filter;
|
|
|
|
}
|