21 #include "ifchanged.h"
25 #include <QtCore/QDateTime>
27 IfChangedNodeFactory::IfChangedNodeFactory() {}
31 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
32 auto expr = tagContent.split(QLatin1Char(
' '), QString::SkipEmptyParts);
34 auto expr = tagContent.split(QLatin1Char(
' '), Qt::SkipEmptyParts);
41 = p->
parse(n, {QStringLiteral(
"else"), QStringLiteral(
"endifchanged")});
42 n->setTrueList(trueList);
46 falseList = p->
parse(n, QStringLiteral(
"endifchanged"));
47 n->setFalseList(falseList);
54 IfChangedNode::IfChangedNode(
const QList<FilterExpression> &feList,
56 :
Node(parent), m_filterExpressions(feList)
59 m_id = QString::number(
reinterpret_cast<qint64
>(
this));
62 void IfChangedNode::setTrueList(
const NodeList &trueList)
64 m_trueList = trueList;
67 void IfChangedNode::setFalseList(
const NodeList &falseList)
69 m_falseList = falseList;
74 if (c->
lookup(QStringLiteral(
"forloop")).isValid()
75 && (!c->
lookup(QStringLiteral(
"forloop"))
76 .value<QVariantHash>()
79 auto hash = c->
lookup(QStringLiteral(
"forloop")).value<QVariantHash>();
81 c->
insert(QStringLiteral(
"forloop"), hash);
85 QTextStream watchedTextStream(&watchedString);
86 auto watchedStream = stream->
clone(&watchedTextStream);
87 if (m_filterExpressions.isEmpty()) {
88 m_trueList.
render(watchedStream.get(), c);
90 QVariantList watchedVars;
91 for (
auto &i : m_filterExpressions) {
92 auto var = i.resolve(c);
97 watchedVars.append(var);
103 QVariantList lastSeenVarList;
104 if (m_lastSeen.userType() != qMetaTypeId<QString>()) {
105 lastSeenVarList = m_lastSeen.value<QVariantList>();
111 if ((watchedVars != lastSeenVarList)
112 || (!watchedString.isEmpty()
113 && (watchedString != m_lastSeen.value<
QString>()))) {
114 auto firstLoop = !m_lastSeen.isValid();
115 if (!watchedString.isEmpty())
116 m_lastSeen = watchedString;
118 m_lastSeen = watchedVars;
122 hash.insert(QStringLiteral(
"firstloop"), firstLoop);
123 c->
insert(QStringLiteral(
"ifchanged"), hash);
124 m_trueList.
render(stream, c);
126 }
else if (!m_falseList.isEmpty()) {
127 m_falseList.
render(stream, c);
QList< FilterExpression > getFilterExpressionList(const QStringList &list, Parser *p) const
The Context class holds the context to render a Template with.
void insert(const QString &name, QObject *object)
virtual QVariant lookup(const QString &str) const
A list of Nodes with some convenience API for rendering them.
void render(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.
NodeList parse(Node *parent, const QStringList &stopAt={})
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
QString content
The content of this Token.