1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00

Do not require a directory per map anymore

This commit is contained in:
Martin Tůma 2018-01-14 23:03:27 +01:00
parent 0c8e864d32
commit 0e45831111
6 changed files with 96 additions and 98 deletions

View File

@ -101,17 +101,7 @@ GUI::~GUI()
void GUI::loadMaps() void GUI::loadMaps()
{ {
_ml = new MapList(this); _ml = new MapList(this);
QString offline;
QString offline, online;
if (QFile::exists(USER_MAP_FILE))
online = USER_MAP_FILE;
else if (QFile::exists(GLOBAL_MAP_FILE))
online = GLOBAL_MAP_FILE;
if (!online.isNull() && !_ml->loadFile(online))
qWarning("%s: %s", qPrintable(online), qPrintable(_ml->errorString()));
if (QFile::exists(USER_MAP_DIR)) if (QFile::exists(USER_MAP_DIR))
offline = USER_MAP_DIR; offline = USER_MAP_DIR;
@ -119,23 +109,10 @@ void GUI::loadMaps()
offline = GLOBAL_MAP_DIR; offline = GLOBAL_MAP_DIR;
if (!offline.isNull()) { if (!offline.isNull()) {
QDir md(offline); if (!_ml->loadDir(offline)) {
QFileInfoList ml = md.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); qWarning("Error reading map dir: %s",
for (int i = 0; i < ml.size(); i++) {
QDir dir(ml.at(i).absoluteFilePath());
QFileInfoList fl = dir.entryInfoList(MapList::filter(), QDir::Files);
if (fl.isEmpty())
qWarning("%s: no map/atlas file found",
qPrintable(ml.at(i).absoluteFilePath()));
else if (fl.size() > 1)
qWarning("%s: ambiguous directory content",
qPrintable(ml.at(i).absoluteFilePath()));
else
if (!_ml->loadFile(fl.first().absoluteFilePath()))
qWarning("%s: %s", qPrintable(fl.first().absoluteFilePath()),
qPrintable(_ml->errorString())); qPrintable(_ml->errorString()));
_ml->clear();
} }
} }

View File

@ -31,37 +31,6 @@ static bool yCmp(const OfflineMap *m1, const OfflineMap *m2)
return TL(m1).y() > TL(m2).y(); return TL(m1).y() > TL(m2).y();
} }
bool Atlas::isAtlas(Tar &tar, const QString &path)
{
QFileInfo fi(path);
QByteArray ba;
QString suffix = fi.suffix().toLower();
if (suffix == "tar") {
if (!tar.load(path)) {
_errorString = "Error reading tar file";
return false;
}
QString tbaFileName = fi.completeBaseName() + ".tba";
ba = tar.file(tbaFileName);
} else if (suffix == "tba") {
QFile tbaFile(path);
if (!tbaFile.open(QIODevice::ReadOnly)) {
_errorString = QString("Error opening tba file: %1")
.arg(tbaFile.errorString());
return false;
}
ba = tbaFile.readAll();
}
if (ba.startsWith("Atlas 1.0"))
return true;
else {
_errorString = "Missing or invalid tba file";
return false;
}
}
void Atlas::computeZooms() void Atlas::computeZooms()
{ {
qSort(_maps.begin(), _maps.end(), resCmp); qSort(_maps.begin(), _maps.end(), resCmp);
@ -111,16 +80,38 @@ void Atlas::computeBounds()
Atlas::Atlas(const QString &fileName, QObject *parent) : Map(parent) Atlas::Atlas(const QString &fileName, QObject *parent) : Map(parent)
{ {
Tar tar;
QFileInfo fi(fileName); QFileInfo fi(fileName);
QByteArray ba;
QString suffix = fi.suffix().toLower();
Tar tar;
_valid = false; _valid = false;
_zoom = 0; _zoom = 0;
_name = fi.dir().dirName(); _name = fi.dir().dirName();
_ci = -1; _cz = -1; _ci = -1; _cz = -1;
if (!isAtlas(tar, fileName))
if (suffix == "tar") {
if (!tar.load(fileName)) {
_errorString = "Error reading tar file";
return; return;
}
QString tbaFileName = fi.completeBaseName() + ".tba";
ba = tar.file(tbaFileName);
} else if (suffix == "tba") {
QFile tbaFile(fileName);
if (!tbaFile.open(QIODevice::ReadOnly)) {
_errorString = QString("Error opening tba file: %1")
.arg(tbaFile.errorString());
return;
}
ba = tbaFile.readAll();
}
if (!ba.startsWith("Atlas 1.0")) {
_errorString = "Missing or invalid tba file";
return;
}
QDir dir(fi.absolutePath()); QDir dir(fi.absolutePath());
QFileInfoList layers = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); QFileInfoList layers = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
@ -336,3 +327,20 @@ void Atlas::unload()
for (int i = 0; i < _maps.count(); i++) for (int i = 0; i < _maps.count(); i++)
_maps.at(i)->unload(); _maps.at(i)->unload();
} }
bool Atlas::isAtlas(const QString &path)
{
QFileInfo fi(path);
QString suffix = fi.suffix().toLower();
Tar tar;
if (suffix == "tar") {
if (!tar.load(path))
return false;
QString tbaFileName = fi.completeBaseName() + ".tba";
return tar.contains(tbaFileName);
} else if (suffix == "tba")
return true;
return false;
}

View File

@ -35,9 +35,10 @@ public:
bool isValid() const {return _valid;} bool isValid() const {return _valid;}
const QString &errorString() const {return _errorString;} const QString &errorString() const {return _errorString;}
static bool isAtlas(const QString &path);
private: private:
void draw(QPainter *painter, const QRectF &rect, int mapIndex); void draw(QPainter *painter, const QRectF &rect, int mapIndex);
bool isAtlas(Tar &tar, const QString &path);
void computeZooms(); void computeZooms();
void computeBounds(); void computeBounds();

View File

@ -70,6 +70,7 @@ bool MapList::loadList(const QString &path)
bool MapList::loadMap(const QString &path) bool MapList::loadMap(const QString &path)
{ {
OfflineMap *map = new OfflineMap(path, this); OfflineMap *map = new OfflineMap(path, this);
if (map->isValid()) { if (map->isValid()) {
_maps.append(map); _maps.append(map);
return true; return true;
@ -83,6 +84,7 @@ bool MapList::loadMap(const QString &path)
bool MapList::loadAtlas(const QString &path) bool MapList::loadAtlas(const QString &path)
{ {
Atlas *atlas = new Atlas(path, this); Atlas *atlas = new Atlas(path, this);
if (atlas->isValid()) { if (atlas->isValid()) {
_maps.append(atlas); _maps.append(atlas);
return true; return true;
@ -93,47 +95,53 @@ bool MapList::loadAtlas(const QString &path)
} }
} }
bool MapList::loadTar(const QString &path) bool MapList::loadFile(const QString &path, bool *atlas)
{ {
Atlas *atlas = new Atlas(path, this); if (Atlas::isAtlas(path)) {
if (atlas->isValid()) { if (atlas)
_maps.append(atlas); *atlas = true;
return true; return loadAtlas(path);
} else { } else {
_errorString = atlas->errorString(); if (atlas)
delete atlas; *atlas = false;
OfflineMap *map = new OfflineMap(path, this); return loadMap(path);
if (map->isValid()) {
_maps.append(map);
return true;
} else {
qWarning("%s: %s", qPrintable(path), qPrintable(_errorString));
qWarning("%s: %s", qPrintable(path),
qPrintable(map->errorString()));
_errorString = "Not a map/atlas file";
delete map;
return false;
}
} }
} }
bool MapList::loadFile(const QString &path) bool MapList::loadDir(const QString &path)
{ {
QFileInfo fi(path); QDir md(path);
md.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
md.setSorting(QDir::DirsLast);
QFileInfoList ml = md.entryInfoList();
bool atlas;
for (int i = 0; i < ml.size(); i++) {
const QFileInfo &fi = ml.at(i);
QString suffix = fi.suffix().toLower(); QString suffix = fi.suffix().toLower();
if (suffix == "txt") if (fi.isDir() && fi.fileName() != "set") {
return loadList(path); if (!loadDir(fi.absoluteFilePath()))
else if (suffix == "map" || suffix == "tif" || suffix == "tiff") return false;
return loadMap(path); } else if (filter().contains("*." + suffix)) {
else if (suffix == "tba") if (!loadFile(fi.absoluteFilePath(), &atlas)) {
return loadAtlas(path); _errorString.prepend(QString("%1: ")
else if (suffix == "tar") .arg(fi.canonicalFilePath()));
return loadTar(path);
else {
_errorString = "Not a map/atlas file";
return false; return false;
} }
if (atlas)
break;
}
}
return true;
}
void MapList::clear()
{
for (int i = 0; i < _maps.count(); i++)
delete _maps.at(i);
_maps.clear();
} }
QString MapList::formats() QString MapList::formats()

View File

@ -5,6 +5,8 @@
#include <QString> #include <QString>
#include "map.h" #include "map.h"
class Tar;
class MapList : public QObject class MapList : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -12,7 +14,9 @@ class MapList : public QObject
public: public:
MapList(QObject *parent = 0) : QObject(parent) {} MapList(QObject *parent = 0) : QObject(parent) {}
bool loadFile(const QString &path); bool loadFile(const QString &path, bool *atlas = 0);
bool loadDir(const QString &path);
void clear();
const QList<Map*> &maps() const {return _maps;} const QList<Map*> &maps() const {return _maps;}
const QString &errorString() const {return _errorString;} const QString &errorString() const {return _errorString;}
@ -26,7 +30,6 @@ private:
bool loadList(const QString &path); bool loadList(const QString &path);
bool loadMap(const QString &path); bool loadMap(const QString &path);
bool loadAtlas(const QString &path); bool loadAtlas(const QString &path);
bool loadTar(const QString &path);
QList<Map*> _maps; QList<Map*> _maps;
QString _errorString; QString _errorString;

View File

@ -12,6 +12,7 @@ public:
QStringList files() const {return _index.keys();} QStringList files() const {return _index.keys();}
QByteArray file(const QString &name); QByteArray file(const QString &name);
bool contains(const QString &name) const {return _index.contains(name);}
QString fileName() const {return _file.fileName();} QString fileName() const {return _file.fileName();}
bool isOpen() const {return _file.isOpen();} bool isOpen() const {return _file.isOpen();}