KTextTemplate

util.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "util.h"
11
12#include "metaenumvariable_p.h"
13
14#include <QStringList>
15
17{
18 return input.mid(1, input.size() - 2)
19 .replace(QStringLiteral("\\\'"), QChar::fromLatin1('\''))
20 .replace(QStringLiteral("\\\""), QChar::fromLatin1('"'))
21 .replace(QStringLiteral("\\\\"), QChar::fromLatin1('\\'));
22}
23
25{
26 if (!variant.isValid())
27 return false;
28 switch (variant.userType()) {
29 case QMetaType::Bool: {
30 return variant.value<bool>();
31 }
32 case QMetaType::Int: {
33 return variant.value<int>() > 0;
34 }
35 case QMetaType::UInt: {
36 return variant.value<uint>() > 0;
37 }
39 return variant.value<qlonglong>() > 0;
40 }
42 return variant.value<qulonglong>() > 0;
43 }
44 case QMetaType::Double: {
45 return variant.value<double>() > 0;
46 }
47 case QMetaType::Float: {
48 return variant.value<float>() > 0;
49 }
50 case QMetaType::Char: {
51 return variant.value<char>() > 0;
52 }
54 auto obj = variant.value<QObject *>();
55 if (!obj)
56 return false;
57
58 if (obj->property("__true__").isValid()) {
59 return obj->property("__true__").value<bool>();
60 }
61 return true;
62 }
64 return !variant.value<QVariantList>().isEmpty();
65 }
67 return !variant.value<QVariantHash>().isEmpty();
68 }
69 }
70
71 return !getSafeString(variant).get().isEmpty();
72}
73
75{
76 auto sret = input;
78 return sret;
79}
80
82{
83 auto temp = input;
84 if (input.isSafe() || input.needsEscape())
85 return input;
86
87 temp.setNeedsEscape(true);
88 return temp;
89}
90
92{
93 if (input.userType() == qMetaTypeId<KTextTemplate::SafeString>()) {
94 return input.value<KTextTemplate::SafeString>();
95 }
96 return input.value<QString>();
97}
98
100{
101 const auto type = input.userType();
102 return ((type == qMetaTypeId<KTextTemplate::SafeString>()) || type == QMetaType::QString);
103}
104
105static QList<int> getPrimitives()
106{
107 QList<int> primitives;
108 primitives << qMetaTypeId<KTextTemplate::SafeString>() << QMetaType::QString << QMetaType::Bool << QMetaType::Int << QMetaType::Double << QMetaType::Float
110 return primitives;
111}
112
114{
115 static const auto primitives = getPrimitives();
116 return primitives.contains(input.userType());
117}
118
119bool KTextTemplate::equals(const QVariant &lhs, const QVariant &rhs)
120{
121 // TODO: Redesign...
122
123 // QVariant doesn't use operator== to compare its held data, so we do it
124 // manually instead for SafeString.
125 auto equal = false;
126 if (lhs.userType() == qMetaTypeId<KTextTemplate::SafeString>()) {
127 if (rhs.userType() == qMetaTypeId<KTextTemplate::SafeString>()) {
129 } else if (rhs.userType() == QMetaType::QString) {
130 equal = (lhs.value<KTextTemplate::SafeString>() == rhs.value<QString>());
131 }
132 } else if (rhs.userType() == qMetaTypeId<KTextTemplate::SafeString>() && lhs.userType() == QMetaType::QString) {
133 equal = (rhs.value<KTextTemplate::SafeString>() == lhs.value<QString>());
134 } else if (rhs.userType() == qMetaTypeId<MetaEnumVariable>()) {
135 if (lhs.userType() == qMetaTypeId<MetaEnumVariable>()) {
136 equal = (rhs.value<MetaEnumVariable>() == lhs.value<MetaEnumVariable>());
137 } else if (lhs.userType() == qMetaTypeId<int>()) {
138 equal = (rhs.value<MetaEnumVariable>() == lhs.value<int>());
139 }
140 } else if (lhs.userType() == qMetaTypeId<MetaEnumVariable>()) {
141 if (rhs.userType() == qMetaTypeId<int>()) {
142 equal = (lhs.value<MetaEnumVariable>() == rhs.value<int>());
143 }
144 } else {
145 equal = (lhs == rhs);
146 }
147 return equal;
148}
149
150std::pair<qreal, QString> KTextTemplate::calcFileSize(qreal size, int unitSystem, qreal multiplier)
151{
152 std::pair<qreal, QString> ret;
153
154 int _unitSystem = unitSystem;
155
156 if ((_unitSystem != 2) && (_unitSystem != 10)) {
157 qWarning("%s",
158 "Unrecognized file size unit system. Falling back to "
159 "decimal unit system.");
160 _unitSystem = 10;
161 }
162
163 if (size == 0.0) {
164 ret.first = 0.0;
165 ret.second = QStringLiteral("bytes");
166 return ret;
167 }
168 if ((size == 1.0) || (size == -1.0)) {
169 ret.first = 1.0;
170 ret.second = QStringLiteral("byte");
171 return ret;
172 }
173
174 qreal _size = size * multiplier;
175
176 const bool positiveValue = (_size > 0);
177
178 if (!positiveValue) {
179 _size *= -1;
180 }
181
182 static const QStringList binaryUnits({QStringLiteral("bytes"),
183 QStringLiteral("KiB"),
184 QStringLiteral("MiB"),
185 QStringLiteral("GiB"),
186 QStringLiteral("TiB"),
187 QStringLiteral("PiB"),
188 QStringLiteral("EiB"),
189 QStringLiteral("ZiB"),
190 QStringLiteral("YiB")});
191
192 static const QStringList decimalUnits({QStringLiteral("bytes"),
193 QStringLiteral("KB"),
194 QStringLiteral("MB"),
195 QStringLiteral("GB"),
196 QStringLiteral("TB"),
197 QStringLiteral("PB"),
198 QStringLiteral("EB"),
199 QStringLiteral("ZB"),
200 QStringLiteral("YB")});
201
202 bool found = false;
203 int count = 0;
204 const qreal baseVal = (_unitSystem == 10) ? 1000.0F : 1024.0F;
205 qreal current = 1.0F;
206 int units = decimalUnits.size();
207 while (!found && (count < units)) {
208 current *= baseVal;
209 if (_size < current) {
210 found = true;
211 break;
212 }
213 count++;
214 }
215
216 if (count >= units) {
217 count = (units - 1);
218 }
219
220 qreal devider = current / baseVal;
221 _size = _size / devider;
222
223 if (!positiveValue) {
224 _size *= -1.0;
225 }
226
227 ret.first = _size;
228 ret.second = (_unitSystem == 10) ? decimalUnits.at(count) : binaryUnits.at(count);
229
230 return ret;
231}
232
233KTextTemplate::SafeString KTextTemplate::toString(const QVariantList &list)
234{
235 QString output(QLatin1Char('['));
236 auto it = list.constBegin();
237 const auto end = list.constEnd();
238 while (it != end) {
239 const auto item = *it;
240 if (isSafeString(item)) {
241 output += QStringLiteral("u\'") + static_cast<QString>(getSafeString(item).get()) + QLatin1Char('\'');
242 }
243 if ((item.userType() == qMetaTypeId<int>()) || (item.userType() == qMetaTypeId<uint>()) || (item.userType() == qMetaTypeId<double>())
244 || (item.userType() == qMetaTypeId<float>()) || (item.userType() == qMetaTypeId<long long>())
245 || (item.userType() == qMetaTypeId<unsigned long long>())) {
246 output += item.value<QString>();
247 }
248 if (item.userType() == qMetaTypeId<QVariantList>()) {
249 output += static_cast<QString>(toString(item.value<QVariantList>()).get());
250 }
251 if ((it + 1) != end)
252 output += QStringLiteral(", ");
253 ++it;
254 }
255
256 return output.append(QLatin1Char(']'));
257}
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:81
bool needsEscape() const
Whether the string needs to be escaped.
bool isSafe() const
Whether the string is safe.
@ IsSafe
The string is safe and requires no further escaping.
Definition safestring.h:87
const NestedString & get() const
Returns the String held by this SafeString
Definition safestring.h:283
char * toString(const EngineQuery &query)
const QList< QKeySequence > & end()
std::pair< qreal, QString > calcFileSize(qreal size, int unitSystem=10, qreal multiplier=1.0)
Converts size into the nearest file size unit like MB or MiB, based on the unitSystem value.
Definition util.cpp:150
KTextTemplate::SafeString markForEscaping(const KTextTemplate::SafeString &input)
Marks the input as requiring escaping.
Definition util.cpp:81
KTextTemplate::SafeString getSafeString(const QVariant &input)
Retrieves and returns a SafeString from the input.
Definition util.cpp:91
bool isSafeString(const QVariant &input)
Returns whether input contains a SafeString.
Definition util.cpp:99
bool variantIsTrue(const QVariant &variant)
Returns whether the variant is evaluated to true.
Definition util.cpp:24
QString unescapeStringLiteral(const QString &input)
Converts the input into its unescaped form.
Definition util.cpp:16
KTextTemplate::SafeString markSafe(const KTextTemplate::SafeString &input)
Marks the input as safe.
Definition util.cpp:74
bool equals(const QVariant &lhs, const QVariant &rhs)
Compares lhs and rhs for equality.
Definition util.cpp:119
bool supportedOutputType(const QVariant &input)
Returns true if the type of input can be inserted into a rendered template directly.
Definition util.cpp:113
QChar fromLatin1(char c)
const_iterator constBegin() const const
const_iterator constEnd() const const
QVariant property(const char *name) const const
QString & append(QChar ch)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
bool isValid() const const
int userType() const const
T value() const const
Utility functions used throughout KTextTemplate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:59:16 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.