Cutelee  6.2.0
ifchanged.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2009,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.1 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  Lesser 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 "ifchanged.h"
22 
23 #include "parser.h"
24 
25 #include <QtCore/QDateTime>
26 
27 IfChangedNodeFactory::IfChangedNodeFactory() {}
28 
29 Node *IfChangedNodeFactory::getNode(const QString &tagContent, Parser *p) const
30 {
31  auto expr = tagContent.split(QLatin1Char(' '), Qt::SkipEmptyParts);
32 
33  expr.takeAt(0);
34  auto n = new IfChangedNode(getFilterExpressionList(expr, p), p);
35 
36  auto trueList
37  = p->parse(n, {QStringLiteral("else"), QStringLiteral("endifchanged")});
38  n->setTrueList(trueList);
39  NodeList falseList;
40 
41  if (p->takeNextToken().content == QStringLiteral("else")) {
42  falseList = p->parse(n, QStringLiteral("endifchanged"));
43  n->setFalseList(falseList);
44  p->removeNextToken();
45  }
46 
47  return n;
48 }
49 
50 IfChangedNode::IfChangedNode(const QList<FilterExpression> &feList,
51  QObject *parent)
52  : Node(parent), m_filterExpressions(feList)
53 {
54  m_lastSeen = QVariant();
55  m_id = QString::number(reinterpret_cast<qint64>(this));
56 }
57 
58 void IfChangedNode::setTrueList(const NodeList &trueList)
59 {
60  m_trueList = trueList;
61 }
62 
63 void IfChangedNode::setFalseList(const NodeList &falseList)
64 {
65  m_falseList = falseList;
66 }
67 
69 {
70  if (c->lookup(QStringLiteral("forloop")).isValid()
71  && (!c->lookup(QStringLiteral("forloop"))
72  .value<QVariantHash>()
73  .contains(m_id))) {
74  m_lastSeen = QVariant();
75  auto hash = c->lookup(QStringLiteral("forloop")).value<QVariantHash>();
76  hash.insert(m_id, 1);
77  c->insert(QStringLiteral("forloop"), hash);
78  }
79 
80  QString watchedString;
81  QTextStream watchedTextStream(&watchedString);
82  auto watchedStream = stream->clone(&watchedTextStream);
83  if (m_filterExpressions.isEmpty()) {
84  m_trueList.render(watchedStream.get(), c);
85  }
86  QVariantList watchedVars;
87  for (auto &i : m_filterExpressions) {
88  auto var = i.resolve(c);
89  if (!var.isValid()) {
90  // silent error
91  return;
92  }
93  watchedVars.append(var);
94  }
95 
96  // In Qt6, QVariant::value<QVariantList>() converts a QString
97  // to a QList(QVariant(QChar, c)...).
98  // Avoid that conversion
99  QVariantList lastSeenVarList;
100  if (m_lastSeen.userType() != qMetaTypeId<QString>()) {
101  lastSeenVarList = m_lastSeen.value<QVariantList>();
102  }
103 
104  // At first glance it looks like m_last_seen will always be invalid,
105  // But it will change because render is called multiple times by the parent
106  // {% for %} loop in the template.
107  if ((watchedVars != lastSeenVarList)
108  || (!watchedString.isEmpty()
109  && (watchedString != m_lastSeen.value<QString>()))) {
110  auto firstLoop = !m_lastSeen.isValid();
111  if (!watchedString.isEmpty())
112  m_lastSeen = watchedString;
113  else
114  m_lastSeen = watchedVars;
115  c->push();
116  QVariantHash hash;
117  // TODO: Document this.
118  hash.insert(QStringLiteral("firstloop"), firstLoop);
119  c->insert(QStringLiteral("ifchanged"), hash);
120  m_trueList.render(stream, c);
121  c->pop();
122  } else if (!m_falseList.isEmpty()) {
123  m_falseList.render(stream, c);
124  }
125 }
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition: parser.cpp:180
The Context class holds the context to render a Template with.
Definition: context.h:118
QList< FilterExpression > getFilterExpressionList(const QStringList &list, Parser *p) const
Definition: node.cpp:192
T value() const const
T takeAt(qsizetype i)
QString content
The content of this Token.
Definition: token.h:51
QString number(double n, char format, int precision)
bool isEmpty() const const
bool isEmpty() const const
Base class for all nodes.
Definition: node.h:77
Token takeNextToken()
Definition: parser.cpp:291
int userType() const const
SkipEmptyParts
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:80
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:48
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: ifchanged.cpp:29
A list of Nodes with some convenience API for rendering them.
Definition: node.h:147
bool isValid() const const
virtual std::shared_ptr< OutputStream > clone(QTextStream *stream) const
virtual QVariant lookup(const QString &str) const
Definition: context.cpp:100
void render(OutputStream *stream, Context *c) const override
Definition: ifchanged.cpp:68
void removeNextToken()
Definition: parser.cpp:297
void insert(const QString &name, QObject *object)
Definition: context.cpp:145
void render(OutputStream *stream, Context *c) const
Definition: node.cpp:177