mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-24 11:45:53 +01:00
Do not require a directory per map anymore
This commit is contained in:
parent
0c8e864d32
commit
0e45831111
@ -101,17 +101,7 @@ GUI::~GUI()
|
||||
void GUI::loadMaps()
|
||||
{
|
||||
_ml = new MapList(this);
|
||||
|
||||
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()));
|
||||
|
||||
QString offline;
|
||||
|
||||
if (QFile::exists(USER_MAP_DIR))
|
||||
offline = USER_MAP_DIR;
|
||||
@ -119,23 +109,10 @@ void GUI::loadMaps()
|
||||
offline = GLOBAL_MAP_DIR;
|
||||
|
||||
if (!offline.isNull()) {
|
||||
QDir md(offline);
|
||||
QFileInfoList ml = md.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
|
||||
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()),
|
||||
if (!_ml->loadDir(offline)) {
|
||||
qWarning("Error reading map dir: %s",
|
||||
qPrintable(_ml->errorString()));
|
||||
_ml->clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,37 +31,6 @@ static bool yCmp(const OfflineMap *m1, const OfflineMap *m2)
|
||||
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()
|
||||
{
|
||||
qSort(_maps.begin(), _maps.end(), resCmp);
|
||||
@ -111,16 +80,38 @@ void Atlas::computeBounds()
|
||||
|
||||
Atlas::Atlas(const QString &fileName, QObject *parent) : Map(parent)
|
||||
{
|
||||
Tar tar;
|
||||
QFileInfo fi(fileName);
|
||||
QByteArray ba;
|
||||
QString suffix = fi.suffix().toLower();
|
||||
Tar tar;
|
||||
|
||||
_valid = false;
|
||||
_zoom = 0;
|
||||
_name = fi.dir().dirName();
|
||||
_ci = -1; _cz = -1;
|
||||
|
||||
if (!isAtlas(tar, fileName))
|
||||
|
||||
if (suffix == "tar") {
|
||||
if (!tar.load(fileName)) {
|
||||
_errorString = "Error reading tar file";
|
||||
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());
|
||||
QFileInfoList layers = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
@ -336,3 +327,20 @@ void Atlas::unload()
|
||||
for (int i = 0; i < _maps.count(); i++)
|
||||
_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;
|
||||
}
|
||||
|
@ -35,9 +35,10 @@ public:
|
||||
bool isValid() const {return _valid;}
|
||||
const QString &errorString() const {return _errorString;}
|
||||
|
||||
static bool isAtlas(const QString &path);
|
||||
|
||||
private:
|
||||
void draw(QPainter *painter, const QRectF &rect, int mapIndex);
|
||||
bool isAtlas(Tar &tar, const QString &path);
|
||||
void computeZooms();
|
||||
void computeBounds();
|
||||
|
||||
|
@ -70,6 +70,7 @@ bool MapList::loadList(const QString &path)
|
||||
bool MapList::loadMap(const QString &path)
|
||||
{
|
||||
OfflineMap *map = new OfflineMap(path, this);
|
||||
|
||||
if (map->isValid()) {
|
||||
_maps.append(map);
|
||||
return true;
|
||||
@ -83,6 +84,7 @@ bool MapList::loadMap(const QString &path)
|
||||
bool MapList::loadAtlas(const QString &path)
|
||||
{
|
||||
Atlas *atlas = new Atlas(path, this);
|
||||
|
||||
if (atlas->isValid()) {
|
||||
_maps.append(atlas);
|
||||
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->isValid()) {
|
||||
_maps.append(atlas);
|
||||
return true;
|
||||
if (Atlas::isAtlas(path)) {
|
||||
if (atlas)
|
||||
*atlas = true;
|
||||
return loadAtlas(path);
|
||||
} else {
|
||||
_errorString = atlas->errorString();
|
||||
delete atlas;
|
||||
OfflineMap *map = new OfflineMap(path, this);
|
||||
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;
|
||||
}
|
||||
if (atlas)
|
||||
*atlas = false;
|
||||
return loadMap(path);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (suffix == "txt")
|
||||
return loadList(path);
|
||||
else if (suffix == "map" || suffix == "tif" || suffix == "tiff")
|
||||
return loadMap(path);
|
||||
else if (suffix == "tba")
|
||||
return loadAtlas(path);
|
||||
else if (suffix == "tar")
|
||||
return loadTar(path);
|
||||
else {
|
||||
_errorString = "Not a map/atlas file";
|
||||
if (fi.isDir() && fi.fileName() != "set") {
|
||||
if (!loadDir(fi.absoluteFilePath()))
|
||||
return false;
|
||||
} else if (filter().contains("*." + suffix)) {
|
||||
if (!loadFile(fi.absoluteFilePath(), &atlas)) {
|
||||
_errorString.prepend(QString("%1: ")
|
||||
.arg(fi.canonicalFilePath()));
|
||||
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()
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <QString>
|
||||
#include "map.h"
|
||||
|
||||
class Tar;
|
||||
|
||||
class MapList : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -12,7 +14,9 @@ class MapList : public QObject
|
||||
public:
|
||||
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 QString &errorString() const {return _errorString;}
|
||||
@ -26,7 +30,6 @@ private:
|
||||
bool loadList(const QString &path);
|
||||
bool loadMap(const QString &path);
|
||||
bool loadAtlas(const QString &path);
|
||||
bool loadTar(const QString &path);
|
||||
|
||||
QList<Map*> _maps;
|
||||
QString _errorString;
|
||||
|
@ -12,6 +12,7 @@ public:
|
||||
|
||||
QStringList files() const {return _index.keys();}
|
||||
QByteArray file(const QString &name);
|
||||
bool contains(const QString &name) const {return _index.contains(name);}
|
||||
|
||||
QString fileName() const {return _file.fileName();}
|
||||
bool isOpen() const {return _file.isOpen();}
|
||||
|
Loading…
Reference in New Issue
Block a user