Grantlee  5.0.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 
76 class GRANTLEE_TEMPLATES_EXPORT Node : public QObject
77 {
78  Q_OBJECT
79 public:
85  explicit Node( QObject *parent = 0 );
86 
90  virtual ~Node();
91 
97  virtual void render( OutputStream *stream, Context *c ) const = 0;
98 
99 #ifndef Q_QDOC
100 
103  virtual bool mustBeFirst() { // krazy:exclude:inline
104  return false;
105  }
106 #endif
107 
108 protected:
115  void streamValueInContext( OutputStream *stream, const QVariant &input, Grantlee::Context *c ) const;
116 
120  TemplateImpl* containerTemplate() const;
121 
122 private:
123  Q_DECLARE_PRIVATE( Node )
124  NodePrivate * const d_ptr;
125 };
126 
128 
141 class GRANTLEE_TEMPLATES_EXPORT NodeList : public QList<Grantlee::Node*>
142 {
143 public:
147  NodeList();
148 
152  NodeList( const NodeList &list );
153 
157  /* implicit */ NodeList( const QList<Grantlee::Node *> &list );
158 
162  ~NodeList();
163 
167  void append( Grantlee::Node* node );
168 
172  void append( QList<Grantlee::Node*> nodeList );
173 
177  bool containsNonText() const;
178 
182  template <typename T>
183  QList<T> findChildren() {
184  QList<T> children;
185  QList<Grantlee::Node*>::const_iterator it;
186  const QList<Grantlee::Node*>::const_iterator first = constBegin();
187  const QList<Grantlee::Node*>::const_iterator last = constEnd();
188  for ( it = first; it != last; ++it ) {
189  T object = qobject_cast<T>( *it );
190  if ( object ) {
191  children << object;
192  }
193  children << ( *it )->findChildren<T>();
194  }
195  return children;
196  }
197 
201  void render( OutputStream *stream, Context *c ) const;
202 
203 private:
204  bool m_containsNonText;
205 };
206 
207 class AbstractNodeFactoryPrivate;
208 
210 
281 class GRANTLEE_TEMPLATES_EXPORT AbstractNodeFactory : public QObject
282 {
283  Q_OBJECT
284 public:
290  explicit AbstractNodeFactory( QObject* parent = 0 );
291 
295  virtual ~AbstractNodeFactory();
296 
307  virtual Node* getNode( const QString &tagContent, Parser *p ) const = 0;
308 
309 #ifndef Q_QDOC
310 
315  virtual void setEngine( Engine * ) {}
316 #endif
317 
318 protected:
332  Q_INVOKABLE QStringList smartSplit( const QString &str ) const;
333 
334 protected:
340  QList<FilterExpression> getFilterExpressionList( const QStringList &list, Parser *p ) const;
341 
342 private:
343  Q_DECLARE_PRIVATE( AbstractNodeFactory )
344  AbstractNodeFactoryPrivate * const d_ptr;
345 };
346 
347 }
348 
349 #endif
The Context class holds the context to render a template with.
Definition: context.h:109
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:48
Base class for all nodes.
Definition: node.h:76
A list of Nodes with some convenience API for rendering them.
Definition: node.h:141
QList< T > findChildren()
Definition: node.h:183
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:76
Base class for all NodeFactories.
Definition: node.h:281
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:110