KOSMIndoorMap

mapcsscondition.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "mapcsscondition_p.h"
8 #include "mapcssstate_p.h"
9 
10 #include <QDebug>
11 #include <QIODevice>
12 
13 using namespace KOSMIndoorMap;
14 
15 MapCSSCondition::MapCSSCondition() = default;
16 MapCSSCondition::MapCSSCondition(MapCSSCondition &&) = default;
17 MapCSSCondition::~MapCSSCondition() = default;
18 
19 static double toNumber(const QByteArray &val)
20 {
21  bool res = false;
22  const auto n = val.toDouble(&res);
23  return res ? n : NAN;
24 }
25 
26 void MapCSSCondition::compile(const OSM::DataSet &dataSet)
27 {
28  if (m_key == "mx:closed") {
29  m_tagKey = dataSet.tagKey("opening_hours");
30  m_op = (m_op == KeyNotSet ? IsNotClosed : IsClosed);
31  } else {
32  m_tagKey = dataSet.tagKey(m_key.constData());
33  }
34 }
35 
36 bool MapCSSCondition::matches(const MapCSSState &state) const
37 {
38  if (m_tagKey.isNull()) {
39  return false;
40  }
41 
42  const auto v = state.element.tagValue(m_tagKey);
43  switch (m_op) {
44  case KeySet: return !v.isEmpty();
45  case KeyNotSet: return v.isEmpty();
46  case Equal: return std::isnan(m_numericValue) ? v == m_value : toNumber(v) == m_numericValue;
47  case NotEqual: return std::isnan(m_numericValue) ? v != m_value : toNumber(v) != m_numericValue;
48  case LessThan: return toNumber(v) < m_numericValue;
49  case GreaterThan: return toNumber(v) > m_numericValue;
50  case LessOrEqual: return toNumber(v) <= m_numericValue;
51  case GreaterOrEqual: return toNumber(v) >= m_numericValue;
52  case IsClosed:
53  case IsNotClosed:
54  {
55  if (v.isEmpty() || !state.openingHours) {
56  return m_op == IsNotClosed;
57  }
58  const auto closed = state.openingHours->isClosed(state.element, v);
59  return m_op == IsClosed ? closed : !closed;
60  }
61  }
62  return false;
63 }
64 
65 bool MapCSSCondition::matchesCanvas(const MapCSSState &state) const
66 {
67  if (m_key != "level") {
68  return false;
69  }
70 
71  switch (m_op) {
72  case KeySet:
73  case KeyNotSet:
74  case IsClosed:
75  case IsNotClosed:
76  return false;
77  case Equal: return (state.floorLevel/10) == m_numericValue;
78  case NotEqual: return (state.floorLevel/10) != m_numericValue;
79  case LessThan: return (state.floorLevel/10) < m_numericValue;
80  case GreaterThan: return (state.floorLevel/10) > m_numericValue;
81  case LessOrEqual: return (state.floorLevel/10) <= m_numericValue;
82  case GreaterOrEqual: return (state.floorLevel/10) >= m_numericValue;
83  }
84 
85  return false;
86 }
87 
88 void MapCSSCondition::setKey(const char *key, int len)
89 {
90  m_key = QByteArray(key, len);
91 }
92 
93 void MapCSSCondition::setOperation(MapCSSCondition::Operator op)
94 {
95  m_op = op;
96 }
97 
98 void MapCSSCondition::setValue(const char *value, int len)
99 {
100  m_value = QByteArray(value, len);
101 }
102 
103 void MapCSSCondition::setValue(double val)
104 {
105  m_numericValue = val;
106 }
107 
108 void MapCSSCondition::write(QIODevice *out) const
109 {
110  out->write("[");
111  if (m_op == KeyNotSet) { out->write("!"); }
112  out->write(m_key);
113 
114  switch (m_op) {
115  case KeySet:
116  case KeyNotSet:
117  case IsClosed:
118  case IsNotClosed:
119  out->write("]"); return;
120  case Equal: out->write("="); break;
121  case NotEqual: out->write("!="); break;
122  case LessThan: out->write("<"); break;
123  case GreaterThan: out->write(">"); break;
124  case LessOrEqual: out->write("<="); break;
125  case GreaterOrEqual: out->write(">="); break;
126  }
127 
128  if (m_numericValue != NAN && m_value.isEmpty()) {
129  out->write(QByteArray::number(m_numericValue));
130  } else {
131  out->write(m_value);
132  }
133 
134  out->write("]");
135 }
136 
137 
138 void MapCSSConditionHolder::addCondition(MapCSSCondition *condition)
139 {
140  conditions.push_back(std::unique_ptr<MapCSSCondition>(condition));
141 }
OSM-based multi-floor indoor maps for buildings.
double toDouble(bool *ok) const const
QByteArray number(int n, int base)
TagKey tagKey(const char *keyName) const
Look up a tag key for the given tag name, if it exists.
Definition: datatypes.cpp:27
A set of nodes, ways and relations.
Definition: datatypes.h:283
qint64 write(const char *data, qint64 maxSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 23:04:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.