Commit 852c18a7 authored by Volker Krause's avatar Volker Krause
Browse files

Prepare MapCSS selector matching to support layer selectors

parent 8251eaad
......@@ -27,24 +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, result)) {
return;
}
for (const auto &decl : m_declarations) {
switch (decl->type()) {
case MapCSSDeclaration::PropertyDeclaration:
result[{}].addDeclaration(decl.get());
break;
case MapCSSDeclaration::ClassDeclaration:
result[{}].addClass(decl->classSelectorKey());
break;
case MapCSSDeclaration::TagDeclaration:
// TODO
qDebug() << "MapCSS tag declaration not implemented yet.";
break;
m_selector->matches(state, result, [this](auto &result, auto layer) {
for (const auto &decl : m_declarations) {
switch (decl->type()) {
case MapCSSDeclaration::PropertyDeclaration:
result[layer].addDeclaration(decl.get());
break;
case MapCSSDeclaration::ClassDeclaration:
result[layer].addClass(decl->classSelectorKey());
break;
case MapCSSDeclaration::TagDeclaration:
// TODO
qDebug() << "MapCSS tag declaration not implemented yet.";
break;
}
}
}
});
}
void MapCSSRule::evaluateCanvas(const MapCSSState &state, MapCSSResult &result) const
......
......@@ -32,7 +32,7 @@ void MapCSSBasicSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSBasicSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
bool MapCSSBasicSelector::matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) 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) {
......@@ -76,7 +76,11 @@ bool MapCSSBasicSelector::matches(const MapCSSState &state, const MapCSSResult &
return false;
}
return std::all_of(conditions.begin(), conditions.end(), [&state](const auto &cond) { return cond->matches(state); });
if (std::all_of(conditions.begin(), conditions.end(), [&state](const auto &cond) { return cond->matches(state); })) {
matchCallback(result, m_layer);
return true;
}
return false;
}
bool MapCSSBasicSelector::matchesCanvas(const MapCSSState &state) const
......@@ -190,10 +194,10 @@ void MapCSSChainedSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSChainedSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
bool MapCSSChainedSelector::matches([[maybe_unused]] const MapCSSState &state, [[maybe_unused]] MapCSSResult &result,
[[maybe_unused]] const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) const
{
Q_UNUSED(state); // TODO
Q_UNUSED(result);
// TODO
return false;
}
......@@ -224,9 +228,9 @@ void MapCSSUnionSelector::compile(const OSM::DataSet &dataSet)
}
}
bool MapCSSUnionSelector::matches(const MapCSSState &state, const MapCSSResult &result) const
bool MapCSSUnionSelector::matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) const
{
return std::any_of(selectors.begin(), selectors.end(), [&state, &result](const auto &selector) { return selector->matches(state, result); });
return std::any_of(selectors.begin(), selectors.end(), [&state, &result, &matchCallback](const auto &selector) { return selector->matches(state, result, matchCallback); });
}
bool MapCSSUnionSelector::matchesCanvas(const MapCSSState &state) const
......
......@@ -11,6 +11,7 @@
#include <osm/datatypes.h>
#include <functional>
#include <memory>
#include <vector>
......@@ -32,7 +33,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 MapCSSResult &result) const = 0;
virtual bool matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) const = 0;
/** Selector matches the canvas element. */
virtual bool matchesCanvas(const MapCSSState &state) const = 0;
......@@ -61,7 +62,7 @@ public:
ObjectType objectType;
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) const override;
bool matchesCanvas(const MapCSSState &state) const override;
void write(QIODevice* out) const override;
......@@ -86,7 +87,7 @@ class MapCSSChainedSelector : public MapCSSSelector
{
public:
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) const override;
bool matchesCanvas(const MapCSSState &state) const override;
void write(QIODevice* out) const override;
std::vector<std::unique_ptr<MapCSSBasicSelector>> selectors;
......@@ -100,7 +101,7 @@ public:
~MapCSSUnionSelector();
void compile(const OSM::DataSet &dataSet) override;
bool matches(const MapCSSState &state, const MapCSSResult &result) const override;
bool matches(const MapCSSState &state, MapCSSResult &result, const std::function<void(MapCSSResult&, LayerSelectorKey)> &matchCallback) 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