KOSMIndoorMap

mapcssvalue.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "mapcssvalue_p.h"
7
8#include <QIODevice>
9
10#include <cmath>
11
12using namespace KOSMIndoorMap;
13
14MapCSSValue::MapCSSValue() = default;
15
16MapCSSValue::MapCSSValue(const QByteArray &str)
17 : m_value(str)
18{
19}
20
21MapCSSValue::MapCSSValue(double num)
22 : m_value(num)
23{}
24
25MapCSSValue::MapCSSValue(bool b)
26 : m_value(b)
27{}
28
29MapCSSValue::~MapCSSValue() = default;
30
31bool MapCSSValue::isNone() const
32{
33 switch (m_value.typeId()) {
35 return m_value.toByteArray().isEmpty();
37 return m_value.toDouble() == 0.0 || std::isnan(m_value.toDouble());
38 }
39 return m_value.isNull();
40}
41
42QByteArray MapCSSValue::asString() const
43{
44 switch (m_value.typeId()) {
46 return m_value.toByteArray();
48 return QByteArray::number(m_value.toDouble());
49 case QMetaType::Bool:
50 return m_value.toBool() ? "true" : "false";
51 }
52 return {};
53}
54
55double MapCSSValue::asNumber() const
56{
57 switch (m_value.typeId()) {
59 const auto b = m_value.toByteArray();
60 if (b.isEmpty()) {
61 return 0.0;
62 }
63 bool ok = false;
64 auto n = b.toDouble(&ok);
65 return ok ? n : NAN;
66 }
68 return m_value.toDouble();
69 }
70
71 return NAN;
72}
73
74bool MapCSSValue::asBoolean() const
75{
76 switch (m_value.typeId()) {
78 {
79 const auto b = m_value.toByteArray();
80 return b != "false" && b != "0" && b != "no" && !b.isEmpty();
81 }
83 return m_value.toDouble() != 0.0;
84 case QMetaType::Bool:
85 return m_value.toBool();
86 }
87
88 return false;
89}
90
91bool MapCSSValue::compareEqual(const MapCSSValue &other) const
92{
93 if (m_value.typeId() != other.m_value.typeId()) {
94 return asString() == other.asString();
95 }
96
97 switch (m_value.typeId()) {
99 return m_value.toByteArray() == other.m_value.toByteArray();
101 return m_value.toDouble() == other.m_value.toDouble();
102 case QMetaType::Bool:
103 return m_value.toBool() == other.m_value.toBool();
104 }
105
106 return false;
107}
108
109void MapCSSValue::write(QIODevice *out) const
110{
111 switch (m_value.typeId()) {
113 out->write(m_value.toByteArray());
114 break;
116 out->write(QByteArray::number(m_value.toDouble()));
117 break;
118 }
119}
OSM-based multi-floor indoor maps for buildings.
QByteArray number(double n, char format, int precision)
qint64 write(const QByteArray &data)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.