Cutelee  6.1.0
l10n_money.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "l10n_money.h"
22 
23 #include "abstractlocalizer.h"
24 #include "engine.h"
25 #include "exception.h"
26 #include "parser.h"
27 #include "template.h"
28 #include "util.h"
29 
30 L10nMoneyNodeFactory::L10nMoneyNodeFactory() {}
31 
32 Node *L10nMoneyNodeFactory::getNode(const QString &tagContent, Parser *p) const
33 {
34  auto expr = smartSplit(tagContent);
35 
36  if (expr.size() < 2)
37  throw Cutelee::Exception(
38  TagSyntaxError,
39  QStringLiteral("Error: l10n_money tag takes at least one argument"));
40 
41  FilterExpression value(expr.at(1), p);
42 
43  FilterExpression currency;
44 
45  if (expr.size() == 3)
46  currency = FilterExpression(expr.at(2), p);
47 
48  return new L10nMoneyNode(value, currency);
49 }
50 
51 L10nMoneyVarNodeFactory::L10nMoneyVarNodeFactory() {}
52 
54  Parser *p) const
55 {
56 
57  auto expr = smartSplit(tagContent);
58 
59  if (expr.size() < 4)
60  throw Cutelee::Exception(
61  TagSyntaxError,
62  QStringLiteral("Error: l10n_money tag takes at least three arguments"));
63 
64  FilterExpression value(expr.at(1), p);
65 
66  FilterExpression currency;
67 
68  if (expr.size() == 3)
69  currency = FilterExpression(expr.at(2), p);
70 
71  auto resultName = expr.last();
72 
73  return new L10nMoneyVarNode(value, currency, resultName);
74 }
75 
76 L10nMoneyNode::L10nMoneyNode(const FilterExpression &value,
77  const FilterExpression &currency, QObject *parent)
78  : Node(parent), m_value(value), m_currency(currency)
79 {
80 }
81 
83 {
84  auto resultString = c->localizer()->localizeMonetaryValue(
85  m_value.resolve(c).value<double>(),
86  getSafeString(m_currency.resolve(c)).get());
87 
88  streamValueInContext(stream, resultString, c);
89 }
90 
91 L10nMoneyVarNode::L10nMoneyVarNode(const FilterExpression &value,
92  const FilterExpression &currency,
93  const QString &resultName, QObject *parent)
94  : Node(parent), m_value(value), m_currency(currency),
95  m_resultName(resultName)
96 {
97 }
98 
100 {
101  Q_UNUSED(stream)
102  auto resultString = c->localizer()->localizeMonetaryValue(
103  m_value.resolve(c).value<double>(),
104  getSafeString(m_currency.resolve(c)).get());
105 
106  c->insert(m_resultName, resultString);
107 }
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition: node.cpp:202
The Context class holds the context to render a Template with.
Definition: context.h:119
std::shared_ptr< AbstractLocalizer > localizer() const
Definition: context.cpp:230
void insert(const QString &name, QObject *object)
Definition: context.cpp:145
An exception for use when implementing template tags.
Definition: exception.h:85
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
Base class for all nodes.
Definition: node.h:78
void streamValueInContext(OutputStream *stream, const QVariant &input, Cutelee::Context *c) const
Definition: node.cpp:88
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
const NestedString & get() const
Definition: safestring.h:340
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: l10n_money.cpp:32
void render(OutputStream *stream, Context *c) const override
Definition: l10n_money.cpp:82
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: l10n_money.cpp:53
void render(OutputStream *stream, Context *c) const override
Definition: l10n_money.cpp:99
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.