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

Implement MapCSS class declarations and class selectors

Will make maintaining the different color scheme style sheets easier.
parent ae7d98c5
Pipeline #42406 passed with stage
in 24 seconds
......@@ -16,6 +16,7 @@ MapCSSResult::~MapCSSResult() = default;
void MapCSSResult::clear()
{
m_declarations.clear();
m_classes.clear();
m_flags = MapCSSDeclaration::NoFlag;
}
......@@ -63,3 +64,16 @@ void MapCSSResult::addDeclaration(const MapCSSDeclaration *decl)
m_flags |= decl->propertyFlags();
}
void MapCSSResult::addClass(const QByteArray &cls)
{
const auto it = std::lower_bound(m_classes.begin(), m_classes.end(), cls);
if (it == m_classes.end() || (*it) != cls) {
m_classes.insert(it, cls);
}
}
bool MapCSSResult::hasClass(const QByteArray& cls) const
{
return std::binary_search(m_classes.begin(), m_classes.end(), cls);
}
......@@ -36,9 +36,12 @@ public:
/** @internal */
void addDeclaration(const MapCSSDeclaration *decl);
void addClass(const QByteArray &cls);
bool hasClass(const QByteArray &cls) const;
private:
std::vector<const MapCSSDeclaration*> m_declarations;
std::vector<QByteArray> m_classes;
int m_flags = 0;
};
......
......@@ -27,13 +27,22 @@ void MapCSSRule::compile(const OSM::DataSet &dataSet)
void MapCSSRule::evaluate(const MapCSSState &state, MapCSSResult &result) const
{
// TODO how do we deal with chained selectors here??
if (!m_selector->matches(state)) {
if (!m_selector->matches(state, result)) {
return;
}
for (const auto &decl : m_declarations) {
if (decl->type() == MapCSSDeclaration::PropertyDeclaration) {
result.addDeclaration(decl.get());
switch (decl->type()) {
case MapCSSDeclaration::PropertyDeclaration:
result.addDeclaration(decl.get());
break;
case MapCSSDeclaration::ClassDeclaration:
result.addClass(decl->keyValue());
break;
case MapCSSDeclaration::TagDeclaration:
// TODO
qDebug() << "MapCSS tag declaration not implemented yet.";
break;
}
}
}
......
......@@ -6,6 +6,7 @@
#include "mapcssselector_p.h"
#include "mapcsscondition_p.h"
#include "mapcssresult_p.h"
#include "mapcssstate_p.h"
#include <QDebug>
......@@ -31,7 +32,7 @@ void MapCSSBasicSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSBasicSelector::matches(const MapCSSState &state) const
bool MapCSSBasicSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
{
// check zoom conditions first, as this is the cheapest one and can avoid expensive tag lookups we it doesn't match
if (m_zoomLow > 0 && state.zoomLevel < m_zoomLow) {
......@@ -71,6 +72,10 @@ bool MapCSSBasicSelector::matches(const MapCSSState &state) const
case Any: break;
}
if (!m_class.isEmpty() && !result.hasClass(m_class)) {
return false;
}
return std::all_of(conditions.begin(), conditions.end(), [&state](const auto &cond) { return cond->matches(state); });
}
......@@ -177,9 +182,10 @@ void MapCSSChainedSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSChainedSelector::matches(const MapCSSState &state) const
bool MapCSSChainedSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
{
Q_UNUSED(state); // TODO
Q_UNUSED(result);
return false;
}
......@@ -210,9 +216,9 @@ void MapCSSUnionSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSUnionSelector::matches(const MapCSSState &state) const
bool MapCSSUnionSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
{
return std::any_of(selectors.begin(), selectors.end(), [&state](const auto &selector) { return selector->matches(state); });
return std::any_of(selectors.begin(), selectors.end(), [&state, &result](const auto &selector) { return selector->matches(state, result); });
}
bool MapCSSUnionSelector::matchesCanvas(const MapCSSState &state) const
......
......@@ -18,6 +18,7 @@ namespace KOSMIndoorMap {
class MapCSSCondition;
class MapCSSConditionHolder;
class MapCSSResult;
class MapCSSState;
/** Base class for a style selector. */
......@@ -29,7 +30,7 @@ public:
/** Resolve tag keys. */
virtual void compile(const OSM::DataSet &dataSet) = 0;
/** Returns @c true if this selector matches the evaluation state. */
virtual bool matches(const MapCSSState &state) const = 0;
virtual bool matches(const MapCSSState &state, const MapCSSResult &result) const = 0;
/** Selector matches the canvas element. */
virtual bool matchesCanvas(const MapCSSState &state) const = 0;
......@@ -58,7 +59,7 @@ public:
ObjectType objectType;
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state) const override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matchesCanvas(const MapCSSState &state) const override;
void write(QIODevice* out) const override;
......@@ -81,7 +82,7 @@ class MapCSSChainedSelector : public MapCSSSelector
{
public:
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state) const override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matchesCanvas(const MapCSSState &state) const override;
void write(QIODevice* out) const override;
std::vector<std::unique_ptr<MapCSSBasicSelector>> selectors;
......@@ -95,7 +96,7 @@ public:
~MapCSSUnionSelector();
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state) const override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matchesCanvas(const MapCSSState &state) const override;
void write(QIODevice* out) const override;
std::vector<std::unique_ptr<MapCSSSelector>> selectors;
......
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