File indexing completed on 2024-11-24 04:15:41

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mapcssrule_p.h"
0008 #include "mapcssresult.h"
0009 #include "mapcssstate_p.h"
0010 
0011 #include <QDebug>
0012 #include <QIODevice>
0013 
0014 #include <cmath>
0015 
0016 using namespace KOSMIndoorMap;
0017 
0018 MapCSSRule::MapCSSRule() = default;
0019 MapCSSRule::~MapCSSRule() = default;
0020 
0021 void MapCSSRule::compile(const OSM::DataSet &dataSet)
0022 {
0023     m_selector->compile(dataSet);
0024     for (const auto &decl : m_declarations) {
0025         decl->compile(dataSet);
0026     }
0027 }
0028 
0029 void MapCSSRule::evaluate(const MapCSSState &state, MapCSSResult &result) const
0030 {
0031     // TODO how do we deal with chained selectors here??
0032     m_selector->matches(state, result, m_declarations);
0033 }
0034 
0035 void MapCSSRule::evaluateCanvas(const MapCSSState &state, MapCSSResult &result) const
0036 {
0037     if (!m_selector->matchesCanvas(state)) {
0038         return;
0039     }
0040 
0041     for (const auto &decl : m_declarations) {
0042         if (decl->type() == MapCSSDeclaration::PropertyDeclaration) {
0043             result[{}].addDeclaration(decl.get());
0044         }
0045     }
0046 }
0047 
0048 void MapCSSRule::write(QIODevice *out) const
0049 {
0050     m_selector->write(out);
0051     out->write("\n{\n");
0052     for (const auto &decl : m_declarations) {
0053         decl->write(out);
0054     }
0055     out->write("}\n\n");
0056 }
0057 
0058 void MapCSSRule::setSelector(MapCSSSelector *selector)
0059 {
0060     m_selector.reset(selector);
0061 }
0062 
0063 void MapCSSRule::addDeclaration(MapCSSDeclaration *decl)
0064 {
0065     std::unique_ptr<MapCSSDeclaration> declPtr(decl);
0066     if (declPtr->isValid()) {
0067         m_declarations.push_back(std::move(declPtr));
0068     }
0069 }