Include default style in resources + use application specific style load path

This commit is contained in:
Martin Tůma 2018-11-01 18:15:05 +01:00
parent a8cbc962f5
commit 0edb1f0f06
5 changed files with 1706 additions and 10 deletions

View File

@ -30,6 +30,7 @@ SOURCES += src/pbfplugin.cpp \
src/function.cpp \ src/function.cpp \
src/textpathitem.cpp \ src/textpathitem.cpp \
src/textitem.cpp src/textitem.cpp
RESOURCES += pbfplugin.qrc
LIBS += -lprotobuf-lite LIBS += -lprotobuf-lite

5
pbfplugin.qrc Normal file
View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>style/style.json</file>
</qresource>
</RCC>

View File

@ -1,20 +1,20 @@
#include <QDir> #include <QStandardPaths>
#include <QDebug>
#include "pbfplugin.h" #include "pbfplugin.h"
#include "pbfhandler.h" #include "pbfhandler.h"
#include "style.h" #include "style.h"
#define GLOBAL_CONFIG "/usr/share/pbf/style.json"
#define USER_CONFIG QDir::homePath() + "/.pbf/style.json"
PBFPlugin::PBFPlugin() PBFPlugin::PBFPlugin()
{ {
_style = new Style(this); _style = new Style(this);
if (!_style->load(USER_CONFIG)) QString style(QStandardPaths::locate(QStandardPaths::AppDataLocation,
if (!_style->load(GLOBAL_CONFIG)) "style/style.json"));
qCritical() << "Map style not found";
if (style.isEmpty() || !_style->load(style)) {
Q_INIT_RESOURCE(pbfplugin);
_style->load(":/style/style.json");
}
} }
QImageIOPlugin::Capabilities PBFPlugin::capabilities(QIODevice *device, QImageIOPlugin::Capabilities PBFPlugin::capabilities(QIODevice *device,

View File

@ -415,12 +415,21 @@ void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
bool Style::load(const QString &fileName) bool Style::load(const QString &fileName)
{ {
QFile file(fileName); QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCritical() << fileName << ": error opening file";
return false; return false;
}
QByteArray ba(file.readAll()); QByteArray ba(file.readAll());
file.close(); file.close();
QJsonObject json(QJsonDocument::fromJson(ba).object()); QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(ba, &error));
if (doc.isNull()) {
qCritical() << fileName << ":" << error.errorString();
return false;
}
QJsonObject json(doc.object());
if (json.contains("layers") && json["layers"].isArray()) { if (json.contains("layers") && json["layers"].isArray()) {
QJsonArray layers = json["layers"].toArray(); QJsonArray layers = json["layers"].toArray();
for (int i = 0; i < layers.size(); i++) for (int i = 0; i < layers.size(); i++)

1681
style/style.json Normal file

File diff suppressed because it is too large Load Diff