23 #include "../lib/exception.h"
25 #include "rendercontext.h"
28 static const char _namedCycleNodes[] =
"_namedCycleNodes";
30 CycleNodeFactory::CycleNodeFactory() {}
36 if (expr.size() < 2) {
39 QStringLiteral(
"%1 expects at least one argument").arg(expr.first()));
42 if (expr.at(1).contains(QLatin1Char(
','))) {
43 auto csvlist = expr.at(1).split(QLatin1Char(
','));
45 for (
auto i = 0; i < csvlist.size(); ++i) {
46 expr.insert(i + 1, QChar::fromLatin1(
'"') + csvlist.at(i)
47 + QChar::fromLatin1(
'"'));
51 if (expr.size() == 2) {
53 auto name = expr.at(1);
54 auto cycleNodes = p->property(_namedCycleNodes);
55 if (cycleNodes.userType() != qMetaTypeId<QVariantHash>()) {
58 QStringLiteral(
"No named cycles in template. '%1' is not defined")
61 auto hash = cycleNodes.value<QVariantHash>();
62 if (!hash.contains(name)) {
64 QStringLiteral(
"Node not found: %1").arg(name));
66 auto nodeVariant = hash.value(name);
67 Q_ASSERT(nodeVariant.canConvert<
Node *>());
68 return nodeVariant.value<
Node *>();
71 auto exprSize = expr.size();
72 if (exprSize > 4 && expr.at(exprSize - 2) == QStringLiteral(
"as")) {
74 auto name = expr.at(exprSize - 1);
75 auto list = expr.mid(1, exprSize - 3);
77 auto hashVariant = p->property(_namedCycleNodes);
79 if (hashVariant.userType() == qMetaTypeId<QVariantHash>()) {
80 hash = hashVariant.value<QVariantHash>();
82 hash.insert(name, QVariant::fromValue(node));
83 p->setProperty(_namedCycleNodes,
QVariant(hash));
86 auto list = expr.mid(1, exprSize - 1);
91 CycleNode::CycleNode(
const QList<FilterExpression> &list,
const QString &name,
93 :
Node(parent), m_list(list), m_variableIterator(list), m_name(name)
103 if (variant.isValid())
109 QTextStream textStream(&value);
110 auto temp = stream->
clone(&textStream);
114 variant.setValue(rotator);
116 if (!m_name.isEmpty()) {
QList< FilterExpression > getFilterExpressionList(const QStringList &list, Parser *p) const
Q_INVOKABLE QStringList smartSplit(const QString &str) const
The Context class holds the context to render a Template with.
RenderContext * renderContext() const
void insert(const QString &name, QObject *object)
An exception for use when implementing template tags.
QVariant resolve(OutputStream *stream, Context *c) const
Base class for all nodes.
The OutputStream class is used to render templates to a QTextStream.
virtual std::shared_ptr< OutputStream > clone(QTextStream *stream) const
The Parser class processes a string template into a tree of nodes.
QVariant & data(const Node *const scopeNode)
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
Utility functions used throughout Cutelee.