Commit ea0779c4 authored by Volker Krause's avatar Volker Krause
Browse files

Parse values for MapCSS tag and class declarations

parent 3b6abba7
area[railway=platform]
{
color: #ff550022;
}
area[railway=platform],
relation[type=multipolygon][railway=platform]
{
color: #ff550022;
fill-color: #80f0e0d0;
}
area[railway=platform] node[sign]
{
fill-color: #ffff0000;
opacity: 0.5;
}
node[building:part][building:part=elevator]
{
opacity: 1;
color: building:color;
}
*
{
text: ref;
text-color: #ffff00ff;
}
line
{
dashes: 3, 5, ;
linecap: round;
linejoin: bevel;
casing-width: 1;
casing-color: #ff444444;
casing-dashes: 1, 1, ;
}
node|z12-13
{
}
node|z10
{
}
node|z-10
{
}
node|z10-
{
}
node|z12-13[name]
{
}
node|z14-[name]
{
}
*
{
font-family: Arial;
font-size: 16;
font-weight: bold;
font-style: italic;
text-decoration: underline;
}
*[layer>1],
*[layer<2],
*[layer>=3],
*[layer<=4]
{
}
area[indoor=area]
{
text-position: line;
}
*
{
font-size: 16pt;
width: 42px;
casing-width: 2m;
}
*
{
width: -10.42;
}
*
{
text-color: #ffaabbcc;
fill-color: #80ccbbaa;
casing-color: #ff112233;
shield-color: #ffff7f3f;
shield-casing-color: #7fbfff3f;
color: "red";
}
area[!shop]
{
opacity: 0;
}
node[shop=bakery]
{
set mx:important;
}
area[shop=bakery]
{
set mx:importance = "high";
set mx:importance = 20;
}
node[shop=bakery],
node[shop=pastry]
{
set .importantShops;
}
......@@ -8,6 +8,7 @@
#include <map/style/mapcssstyle.h>
#include <QFile>
#include <QProcess>
#include <QTest>
using namespace KOSMIndoorMap;
......@@ -22,9 +23,24 @@ private Q_SLOTS:
const auto style = p.parse(QStringLiteral(SOURCE_DIR "/data/mapcss/parser-test.mapcss"));
QVERIFY(!p.hasError());
QFile outFile(QStringLiteral(SOURCE_DIR "/data/mapcss/parser-test.mapcss.out"));
QFile outFile(QStringLiteral("parser-test.mapcss.out"));
QVERIFY(outFile.open(QFile::WriteOnly));
style.write(&outFile);
outFile.close();
QVERIFY(outFile.open(QFile::ReadOnly));
const auto b1 = outFile.readAll();
QFile refFile(QStringLiteral(SOURCE_DIR "/data/mapcss/parser-test.mapcss.ref"));
QVERIFY(refFile.open(QFile::ReadOnly));
const auto b2 = refFile.readAll();
if (b1 != b2) {
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), outFile.fileName(), refFile.fileName()});
proc.waitForFinished();
}
QVERIFY(b1 == b2);
}
void testBuiltInStyles_data()
......
......@@ -116,6 +116,20 @@ MapCSSDeclaration::MapCSSDeclaration(Type type)
MapCSSDeclaration::~MapCSSDeclaration() = default;
bool MapCSSDeclaration::isValid() const
{
switch (m_type) {
case PropertyDeclaration:
return property() != Unknown;
case TagDeclaration:
case ClassDeclaration:
return !m_identValue.isEmpty();
}
Q_UNREACHABLE();
return false;
}
MapCSSDeclaration::Type MapCSSDeclaration::type() const
{
return m_type;
......@@ -285,36 +299,56 @@ void MapCSSDeclaration::write(QIODevice *out) const
{
out->write(" ");
for (const auto &p : property_types) {
if (p.property == m_property) {
out->write(p.name);
switch (m_type) {
case PropertyDeclaration:
for (const auto &p : property_types) {
if (p.property == m_property) {
out->write(p.name);
break;
}
}
out->write(": ");
if (!std::isnan(m_doubleValue)) {
out->write(QByteArray::number(m_doubleValue));
} else if (m_colorValue.isValid()) {
out->write(m_colorValue.name(QColor::HexArgb).toUtf8());
} else if (!m_dashValue.isEmpty()) {
for (const auto &d : m_dashValue) {
out->write(QByteArray::number(d));
out->write(", ");
}
} else if (!m_stringValue.isEmpty()) {
out->write("\"");
out->write(m_stringValue.toUtf8()); // this would need to be quoted...
out->write("\"");
} else {
out->write(m_identValue);
}
for (const auto &u : unit_map) {
if (u.unit == m_unit) {
out->write(u.name);
break;
}
}
break;
}
}
out->write(": ");
if (!std::isnan(m_doubleValue)) {
out->write(QByteArray::number(m_doubleValue));
} else if (m_colorValue.isValid()) {
out->write(m_colorValue.name(QColor::HexArgb).toUtf8());
} else if (!m_dashValue.isEmpty()) {
for (const auto &d : m_dashValue) {
out->write(QByteArray::number(d));
out->write(", ");
}
} else if (!m_stringValue.isEmpty()) {
out->write("\"");
out->write(m_stringValue.toUtf8()); // this would need to be quoted...
out->write("\"");
} else {
out->write(m_identValue);
}
for (const auto &u : unit_map) {
if (u.unit == m_unit) {
out->write(u.name);
case TagDeclaration:
out->write("set ");
out->write(m_identValue);
if (!std::isnan(m_doubleValue)) {
out->write(" = ");
out->write(QByteArray::number(m_doubleValue));
} else if (!m_stringValue.isEmpty()) {
out->write(" = \"");
out->write(m_stringValue.toUtf8()); // this would need to be quoted...
out->write("\"");
}
break;
case ClassDeclaration:
out->write("set .");
out->write(m_identValue);
break;
}
}
out->write(";\n");
......
......@@ -109,6 +109,9 @@ public:
ShieldShape, /// @p rounded or @p rectangular
};
/** Checks if this is a meaningful declaration. */
bool isValid() const;
Type type() const;
Property property() const;
......
......@@ -240,10 +240,24 @@ Declarations:
Declaration:
PropertyName T_COLON PropertyValue T_SEMICOLON { $$ = $3; $$->setPropertyName($1.str, $1.len); }
| T_KEYWORD_SET Key T_EQUALS T_STRING T_SEMICOLON { $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration); }
| T_KEYWORD_SET Key T_EQUALS T_DOUBLE T_SEMICOLON { $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration); }
| T_KEYWORD_SET Key T_SEMICOLON { $$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration); }
| T_KEYWORD_SET T_DOT T_IDENT T_SEMICOLON { $$ = new MapCSSDeclaration(MapCSSDeclaration::ClassDeclaration); }
| T_KEYWORD_SET Key[K] T_EQUALS T_STRING[V] T_SEMICOLON {
$$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
$$->setIdentifierValue($K.str, $K.len);
$$->setStringValue($V);
}
| T_KEYWORD_SET Key[K] T_EQUALS T_DOUBLE[V] T_SEMICOLON {
$$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
$$->setIdentifierValue($K.str, $K.len);
$$->setDoubleValue($V);
}
| T_KEYWORD_SET Key[K] T_SEMICOLON {
$$ = new MapCSSDeclaration(MapCSSDeclaration::TagDeclaration);
$$->setIdentifierValue($K.str, $K.len);
}
| T_KEYWORD_SET T_DOT T_IDENT[C] T_SEMICOLON {
$$ = new MapCSSDeclaration(MapCSSDeclaration::ClassDeclaration);
$$->setIdentifierValue($C.str, $C.len);
}
;
PropertyName:
......
......@@ -65,7 +65,7 @@ void MapCSSRule::setSelector(MapCSSSelector *selector)
void MapCSSRule::addDeclaration(MapCSSDeclaration *decl)
{
std::unique_ptr<MapCSSDeclaration> declPtr(decl);
if (declPtr->property() != MapCSSDeclaration::Unknown) {
if (declPtr->isValid()) {
m_declarations.push_back(std::move(declPtr));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment