20 #include "l10n_filesize.h" 22 #include "abstractlocalizer.h" 24 #include "exception.h" 31 L10nFileSizeNodeFactory::L10nFileSizeNodeFactory() {}
38 throw Exception(TagSyntaxError, QStringLiteral(
"Error: l10n_filesize requires at least the file size as first parameter"));
44 if (parts.
size() > 1) {
49 if (parts.
size() > 2) {
54 if (parts.
size() > 3) {
61 L10nFileSizeVarNodeFactory::L10nFileSizeVarNodeFactory() {}
67 if (parts.
size() < 2) {
68 throw Exception(TagSyntaxError, QStringLiteral(
"Error: l10n_filesize_var tag takes at least 2 arguments, the file size and the variable name"));
74 if (parts.
size() > 2) {
79 if (parts.
size() > 3) {
84 if (parts.
size() > 4) {
88 auto resultName = parts.
last();
98 :
Node(parent), m_size(size), m_unitSystem(unitSystem), m_precision(precision), m_multiplier(multiplier)
104 bool convertNumbers =
true;
112 if (!convertNumbers) {
113 qWarning(
"%s",
"Failed to convert input file size into a floating point number.");
117 int unitSystem = m_unitSystem.
isValid() ? m_unitSystem.
resolve(c).
toInt(&convertNumbers) : 10;
118 if (!convertNumbers) {
119 qWarning(
"%s",
"Failed to convert unit system for file size into integer value. Using default decimal system as default.");
124 if (!convertNumbers) {
125 qWarning(
"%s",
"Failed to convert decimal precision for file size into an integer value. Using default value 2.");
129 qreal multiplier = m_multiplier.
isValid() ? m_multiplier.
resolve(c).
toReal(&convertNumbers) : 1.0f;
130 if (!convertNumbers) {
131 qWarning(
"%s",
"Failed to convert multiplier file size into a floating point number. Using default value 1.0.");
135 if (multiplier == 0.0f) {
136 qWarning(
"%s",
"It makes no sense to multiply the file size by zero. Using default value 1.0.");
140 const qreal sizeMult = size * multiplier;
142 if (unitSystem == 10) {
143 if ((sizeMult > -1000) && (sizeMult < 1000)) {
146 }
else if (unitSystem == 2) {
147 if ((sizeMult > - 1024) && (sizeMult < 1024)) {
154 if (sizeMult > static_cast<qreal>(std::numeric_limits<qint64>::min()) && sizeMult < static_cast<qreal>(std::numeric_limits<qint64>::max())) {
159 resultString = l.formattedDataSize(static_cast<qint64>(sizeMult), precision, format);
162 const std::pair<qreal,QString> fspair =
calcFileSize(size, unitSystem, multiplier);
164 if (precision == 2) {
168 resultString = l.toString(fspair.first,
'f', precision) +
QChar(
QChar::Space) + fspair.second;
180 :
Node(parent), m_size(size), m_unitSystem(unitSystem), m_precision(precision), m_multiplier(multiplier),
181 m_resultName(resultName)
188 bool convertNumbers =
true;
196 if (!convertNumbers) {
197 qWarning(
"%s",
"Failed to convert input file size into a floating point number.");
201 int unitSystem = m_unitSystem.
isValid() ? m_unitSystem.
resolve(c).
toInt(&convertNumbers) : 10;
202 if (!convertNumbers) {
203 qWarning(
"%s",
"Failed to convert unit system for file size into integer value. Using default decimal system.");
208 if (!convertNumbers) {
209 qWarning(
"%s",
"Failed to convert decimal precision for file size into an integer value. Using default value 2.");
213 qreal multiplier = m_multiplier.
isValid() ? m_multiplier.
resolve(c).
toReal(&convertNumbers) : 1.0f;
214 if (!convertNumbers) {
215 qWarning(
"%s",
"Failed to convert multiplier file size into a floating point number. Using default value 1.0.");
219 if (multiplier == 0.0f) {
220 qWarning(
"%s",
"It makes no sense to mulitply the file size by zero. Using default value 1.0.");
224 const double sizeMult = size * multiplier;
226 if (unitSystem == 10) {
227 if ((sizeMult > -1000) && (sizeMult < 1000)) {
230 }
else if (unitSystem == 2) {
231 if ((sizeMult > - 1024) && (sizeMult < 1024)) {
238 if (sizeMult > static_cast<qreal>(std::numeric_limits<qint64>::min()) && sizeMult < static_cast<qreal>(std::numeric_limits<qint64>::max())) {
243 resultString = l.formattedDataSize(static_cast<qint64>(sizeMult), precision, format);
246 const std::pair<qreal,QString> fspair =
calcFileSize(size, unitSystem, multiplier);
248 if (precision == 2) {
252 resultString = l.toString(fspair.first,
'f', precision) +
QChar(
QChar::Space) + fspair.second;
256 c->
insert(m_resultName, resultString);
The Context class holds the context to render a Template with.
Node * getNode(const QString &tagContent, Parser *p) const override
Node * getNode(const QString &tagContent, Parser *p) const override
const_reference at(qsizetype i) const const
void render(OutputStream *stream, Context *c) const override
qsizetype size() const const
bool canConvert() const const
int toInt(bool *ok) const const
bool isEmpty() const const
Base class for all nodes.
Utility functions used throughout Cutelee.
The OutputStream class is used to render templates to a QTextStream.
QVariant resolve(OutputStream *stream, Context *c) const
std::shared_ptr< AbstractLocalizer > localizer() const
The Parser class processes a string template into a tree of nodes.
void render(OutputStream *stream, Context *c) const override
std::pair< qreal, QString > calcFileSize(qreal size, int unitSystem=10, qreal multiplier=1.0)
A FilterExpression object represents a filter expression in a template.
Q_INVOKABLE QStringList smartSplit(const QString &str) const
qreal toReal(bool *ok) const const
void streamValueInContext(OutputStream *stream, const QVariant &input, Cutelee::Context *c) const
void insert(const QString &name, QObject *object)
Cutelee::SafeString getSafeString(const QVariant &input)
An exception for use when implementing template tags.