Grantlee  5.1.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 = 0);
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:
123  void streamValueInContext(OutputStream *stream, const QVariant &input,
124  Grantlee::Context *c) const;
125 
129  TemplateImpl *containerTemplate() const;
130 
131 private:
132  Q_DECLARE_PRIVATE(Node)
133  NodePrivate *const d_ptr;
134 };
135 
137 
154 class GRANTLEE_TEMPLATES_EXPORT NodeList : public QList<Grantlee::Node *>
155 {
156 public:
160  NodeList();
161 
165  NodeList(const NodeList &list);
166 
167  NodeList &operator=(const NodeList &list);
168 
172  /* implicit */ NodeList(const QList<Grantlee::Node *> &list);
173 
177  ~NodeList();
178 
182  void append(Grantlee::Node *node);
183 
187  void append(QList<Grantlee::Node *> nodeList);
188 
192  bool containsNonText() const;
193 
197  template <typename T> QList<T> findChildren()
198  {
199  QList<T> children;
201  const QList<Grantlee::Node *>::const_iterator first = constBegin();
202  const QList<Grantlee::Node *>::const_iterator last = constEnd();
203  for (it = first; it != last; ++it) {
204  T object = qobject_cast<T>(*it);
205  if (object) {
206  children << object;
207  }
208  children << (*it)->findChildren<T>();
209  }
210  return children;
211  }
212 
216  void render(OutputStream *stream, Context *c) const;
217 
218 private:
219  bool m_containsNonText;
220 };
221 
222 class AbstractNodeFactoryPrivate;
223 
225 
306 class GRANTLEE_TEMPLATES_EXPORT AbstractNodeFactory : public QObject
307 {
308  Q_OBJECT
309 public:
315  explicit AbstractNodeFactory(QObject *parent = 0);
316 
320  ~AbstractNodeFactory() override;
321 
337  virtual Node *getNode(const QString &tagContent, Parser *p) const = 0;
338 
339 #ifndef Q_QDOC
340 
346  virtual void setEngine(Engine *) {}
347 #endif
348 
349 protected:
365  Q_INVOKABLE QStringList smartSplit(const QString &str) const;
366 
367 protected:
374  QList<FilterExpression> getFilterExpressionList(const QStringList &list,
375  Parser *p) const;
376 
377 private:
378  Q_DECLARE_PRIVATE(AbstractNodeFactory)
379  AbstractNodeFactoryPrivate *const d_ptr;
380 };
381 }
382 
383 #endif
The Context class holds the context to render a template with.
Definition: context.h:117
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
Base class for all nodes.
Definition: node.h:82
A list of Nodes with some convenience API for rendering them.
Definition: node.h:154
QList< T > findChildren()
Definition: node.h:197
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:82
Base class for all NodeFactories.
Definition: node.h:306
complex< _Tp > & operator=(const _Tp &)
The Grantlee namespace holds all public Grantlee API.
Definition: Mainpage.dox:7
Grantlee::Engine is the main entry point for creating Grantlee Templates.
Definition: engine.h:123