Grantlee  0.4.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_core_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_CORE_EXPORT Node : public QObject
77 {
78  Q_OBJECT
79 public:
85  explicit Node( QObject *parent = 0 );
86 
90  virtual ~Node();
91 
97  // This can't be const because CycleNode needs to change on each render.
98  virtual void render( OutputStream *stream, Context *c ) = 0;
99 
100 #ifndef Q_QDOC
101 
104  virtual bool mustBeFirst() { // krazy:exclude:inline
105  return false;
106  }
107 #endif
108 
109 protected:
116  void streamValueInContext( OutputStream *stream, const QVariant &input, Grantlee::Context *c );
117 
121  TemplateImpl* containerTemplate() const;
122 
123 private:
124  Q_DECLARE_PRIVATE( Node )
125  NodePrivate * const d_ptr;
126 };
127 
129 
142 class GRANTLEE_CORE_EXPORT NodeList : public QList<Grantlee::Node*>
143 {
144 public:
148  NodeList();
149 
153  NodeList( const NodeList &list );
154 
158  /* implicit */ NodeList( const QList<Grantlee::Node *> &list );
159 
163  ~NodeList();
164 
168  void append( Grantlee::Node* node );
169 
173  void append( QList<Grantlee::Node*> nodeList );
174 
178  bool containsNonText() const;
179 
183  template <typename T>
184  QList<T> findChildren() {
185  QList<T> children;
186  QList<Grantlee::Node*>::const_iterator it;
187  const QList<Grantlee::Node*>::const_iterator first = constBegin();
188  const QList<Grantlee::Node*>::const_iterator last = constEnd();
189  for ( it = first; it != last; ++it ) {
190  T object = qobject_cast<T>( *it );
191  if ( object ) {
192  children << object;
193  }
194  children << ( *it )->findChildren<T>();
195  }
196  return children;
197  }
198 
202  void render( OutputStream *stream, Context *c );
203 
204 private:
205  bool m_containsNonText;
206 };
207 
208 class AbstractNodeFactoryPrivate;
209 
211 
282 class GRANTLEE_CORE_EXPORT AbstractNodeFactory : public QObject
283 {
284  Q_OBJECT
285 public:
291  explicit AbstractNodeFactory( QObject* parent = 0 );
292 
296  virtual ~AbstractNodeFactory();
297 
308  virtual Node* getNode( const QString &tagContent, Parser *p ) const = 0;
309 
310 #ifndef Q_QDOC
311 
316  virtual void setEngine( Engine * ) {}
317 #endif
318 
319 protected:
333  Q_INVOKABLE QStringList smartSplit( const QString &str ) const;
334 
335 protected:
341  QList<FilterExpression> getFilterExpressionList( const QStringList &list, Parser *p ) const;
342 
343 private:
344  Q_DECLARE_PRIVATE( AbstractNodeFactory )
345  AbstractNodeFactoryPrivate * const d_ptr;
346 };
347 
348 }
349 
350 #endif
351 
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:142
QList< T > findChildren()
Definition: node.h:184
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:76
Base class for all NodeFactories.
Definition: node.h:282
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