Grantlee: templates/lib/node.h Source File

Grantlee 5.2.0

node.h
1 /*
2 This file is part of the Grantlee 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 #ifndef GRANTLEE_NODE_H
22 #define GRANTLEE_NODE_H
23
24 // krazy:excludeall=dpointer
25
26 #include "context.h"
27 #include "filterexpression.h"
28 #include "grantlee_templates_export.h"
29 #include "outputstream.h"
30 #include "safestring.h"
31
32 #include <QtCore/QStringList>
33
34 // Need these for inheriting from QList<T> to work
35 // http://lists.trolltech.com/qt-interest/2008-01/thread00578-0.html
36 #include <QtCore/QSet>
37 #include <QtCore/QVector>
38
39 namespace Grantlee
40 {
41
42 class Engine;
43 class NodeList;
44 class TemplateImpl;
45
46 class NodePrivate;
47
49
82 class GRANTLEE_TEMPLATES_EXPORT Node : public QObject
83 {
84 Q_OBJECT
85 public:
91 explicit Node(QObject *parent = {});
92
96 ~Node() override;
97
103 virtual void render(OutputStream *stream, Context *c) const = 0;
104
105 #ifndef Q_QDOC
106
109 virtual bool mustBeFirst()
110 { // krazy:exclude:inline
111 return false;
112 }
113 #endif
114
115 protected:
122 void streamValueInContext(OutputStream *stream, const QVariant &input,
123 Grantlee::Context *c) const;
124
128 TemplateImpl *containerTemplate() const;
129
130 private:
131 Q_DECLARE_PRIVATE(Node)
132 NodePrivate *const d_ptr;
133 };
134
136
152 class GRANTLEE_TEMPLATES_EXPORT NodeList : public QList<Grantlee::Node *>
153 {
154 public:
158 NodeList();
159
163 NodeList(const NodeList &list);
164
165 NodeList &operator=(const NodeList &list);
166
170 /* implicit */ NodeList(const QList<Grantlee::Node *> &list);
171
175 ~NodeList();
176
180 void append(Grantlee::Node *node);
181
185 void append(QList<Grantlee::Node *> nodeList);
186
190 bool containsNonText() const;
191
195 template <typename T> QList<T> findChildren()
196 {
197 QList<T> children;
198 QList<Grantlee::Node *>::const_iterator it;
199 const QList<Grantlee::Node *>::const_iterator first = constBegin();
200 const QList<Grantlee::Node *>::const_iterator last = constEnd();
201 for (it = first; it != last; ++it) {
202 T object = qobject_cast<T>(*it);
203 if (object) {
204 children << object;
205 }
206 children << (*it)->findChildren<T>();
207 }
208 return children;
209 }
210
214 void render(OutputStream *stream, Context *c) const;
215
216 private:
217 bool m_containsNonText;
218 };
219
220 class AbstractNodeFactoryPrivate;
221
223
304 class GRANTLEE_TEMPLATES_EXPORT AbstractNodeFactory : public QObject
305 {
306 Q_OBJECT
307 public:
313 explicit AbstractNodeFactory(QObject *parent = {});
314
318 ~AbstractNodeFactory() override;
319
334 virtual Node *getNode(const QString &tagContent, Parser *p) const = 0;
335
336 #ifndef Q_QDOC
337
343 virtual void setEngine(Engine *) {}
344 #endif
345
346 protected:
362 Q_INVOKABLE QStringList smartSplit(const QString &str) const;
363
364 protected:
371 QList<FilterExpression> getFilterExpressionList(const QStringList &list,
372 Parser *p) const;
373
374 private:
375 Q_DECLARE_PRIVATE(AbstractNodeFactory)
376 AbstractNodeFactoryPrivate *const d_ptr;
377 };
378 }
379
380 #endif
Grantlee::Context
The Context class holds the context to render a Template with.
Definition: context.h:117
Grantlee::Parser
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:48
Grantlee::Node
Base class for all nodes.
Definition: node.h:82
Grantlee::NodeList
A list of Nodes with some convenience API for rendering them.
Definition: node.h:152
Grantlee::NodeList::findChildren
QList< T > findChildren()
Definition: node.h:195
Grantlee::OutputStream
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:80
Grantlee::AbstractNodeFactory
Base class for all NodeFactories.
Definition: node.h:304
Grantlee
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7
Grantlee::Engine
Grantlee::Engine is the main entry point for creating Grantlee Templates.
Definition: engine.h:120