Grantlee  0.4.0
templates/lib/node.h
00001 /*
00002   This file is part of the Grantlee template system.
00003 
00004   Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either version
00009   2.1 of the Licence, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #ifndef GRANTLEE_NODE_H
00022 #define GRANTLEE_NODE_H
00023 
00024 // krazy:excludeall=dpointer
00025 
00026 #include "context.h"
00027 #include "filterexpression.h"
00028 #include "grantlee_core_export.h"
00029 #include "outputstream.h"
00030 #include "safestring.h"
00031 
00032 #include <QtCore/QStringList>
00033 
00034 // Need these for inheriting from QList<T> to work
00035 // http://lists.trolltech.com/qt-interest/2008-01/thread00578-0.html
00036 #include <QtCore/QSet>
00037 #include <QtCore/QVector>
00038 
00039 namespace Grantlee
00040 {
00041 
00042 class Engine;
00043 class NodeList;
00044 class TemplateImpl;
00045 
00046 class NodePrivate;
00047 
00049 
00076 class GRANTLEE_CORE_EXPORT Node : public QObject
00077 {
00078   Q_OBJECT
00079 public:
00085   explicit Node( QObject *parent = 0 );
00086 
00090   virtual ~Node();
00091 
00097   // This can't be const because CycleNode needs to change on each render.
00098   virtual void render( OutputStream *stream, Context *c ) = 0;
00099 
00100 #ifndef Q_QDOC
00101 
00104   virtual bool mustBeFirst() { // krazy:exclude:inline
00105     return false;
00106   }
00107 #endif
00108 
00109 protected:
00116   void streamValueInContext( OutputStream *stream, const QVariant &input, Grantlee::Context *c );
00117 
00121   TemplateImpl* containerTemplate() const;
00122 
00123 private:
00124   Q_DECLARE_PRIVATE( Node )
00125   NodePrivate * const d_ptr;
00126 };
00127 
00129 
00142 class GRANTLEE_CORE_EXPORT NodeList : public QList<Grantlee::Node*>
00143 {
00144 public:
00148   NodeList();
00149 
00153   NodeList( const NodeList &list );
00154 
00158   /* implicit */ NodeList( const QList<Grantlee::Node *> &list );
00159 
00163   ~NodeList();
00164 
00168   void append( Grantlee::Node* node );
00169 
00173   void append( QList<Grantlee::Node*> nodeList );
00174 
00178   bool containsNonText() const;
00179 
00183   template <typename T>
00184   QList<T> findChildren() {
00185     QList<T> children;
00186     QList<Grantlee::Node*>::const_iterator it;
00187     const QList<Grantlee::Node*>::const_iterator first = constBegin();
00188     const QList<Grantlee::Node*>::const_iterator last = constEnd();
00189     for ( it = first; it != last; ++it ) {
00190       T object = qobject_cast<T>( *it );
00191       if ( object ) {
00192         children << object;
00193       }
00194       children << ( *it )->findChildren<T>();
00195     }
00196     return children;
00197   }
00198 
00202   void render( OutputStream *stream, Context *c );
00203 
00204 private:
00205   bool m_containsNonText;
00206 };
00207 
00208 class AbstractNodeFactoryPrivate;
00209 
00211 
00282 class GRANTLEE_CORE_EXPORT AbstractNodeFactory : public QObject
00283 {
00284   Q_OBJECT
00285 public:
00291   explicit AbstractNodeFactory( QObject* parent = 0 );
00292 
00296   virtual ~AbstractNodeFactory();
00297 
00308   virtual Node* getNode( const QString &tagContent, Parser *p ) const = 0;
00309 
00310 #ifndef Q_QDOC
00311 
00316   virtual void setEngine( Engine * ) {}
00317 #endif
00318 
00319 protected:
00333   Q_INVOKABLE QStringList smartSplit( const QString &str ) const;
00334 
00335 protected:
00341   QList<FilterExpression> getFilterExpressionList( const QStringList &list, Parser *p ) const;
00342 
00343 private:
00344   Q_DECLARE_PRIVATE( AbstractNodeFactory )
00345   AbstractNodeFactoryPrivate * const d_ptr;
00346 };
00347 
00348 }
00349 
00350 #endif
00351