Grantlee  5.1.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_templates_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 
00082 class GRANTLEE_TEMPLATES_EXPORT Node : public QObject
00083 {
00084   Q_OBJECT
00085 public:
00091   explicit Node(QObject *parent = 0);
00092 
00096   ~Node() override;
00097 
00103   virtual void render(OutputStream *stream, Context *c) const = 0;
00104 
00105 #ifndef Q_QDOC
00106 
00109   virtual bool mustBeFirst()
00110   { // krazy:exclude:inline
00111     return false;
00112   }
00113 #endif
00114 
00115 protected:
00123   void streamValueInContext(OutputStream *stream, const QVariant &input,
00124                             Grantlee::Context *c) const;
00125 
00129   TemplateImpl *containerTemplate() const;
00130 
00131 private:
00132   Q_DECLARE_PRIVATE(Node)
00133   NodePrivate *const d_ptr;
00134 };
00135 
00137 
00154 class GRANTLEE_TEMPLATES_EXPORT NodeList : public QList<Grantlee::Node *>
00155 {
00156 public:
00160   NodeList();
00161 
00165   NodeList(const NodeList &list);
00166 
00167   NodeList &operator=(const NodeList &list);
00168 
00172   /* implicit */ NodeList(const QList<Grantlee::Node *> &list);
00173 
00177   ~NodeList();
00178 
00182   void append(Grantlee::Node *node);
00183 
00187   void append(QList<Grantlee::Node *> nodeList);
00188 
00192   bool containsNonText() const;
00193 
00197   template <typename T> QList<T> findChildren()
00198   {
00199     QList<T> children;
00200     QList<Grantlee::Node *>::const_iterator it;
00201     const QList<Grantlee::Node *>::const_iterator first = constBegin();
00202     const QList<Grantlee::Node *>::const_iterator last = constEnd();
00203     for (it = first; it != last; ++it) {
00204       T object = qobject_cast<T>(*it);
00205       if (object) {
00206         children << object;
00207       }
00208       children << (*it)->findChildren<T>();
00209     }
00210     return children;
00211   }
00212 
00216   void render(OutputStream *stream, Context *c) const;
00217 
00218 private:
00219   bool m_containsNonText;
00220 };
00221 
00222 class AbstractNodeFactoryPrivate;
00223 
00225 
00306 class GRANTLEE_TEMPLATES_EXPORT AbstractNodeFactory : public QObject
00307 {
00308   Q_OBJECT
00309 public:
00315   explicit AbstractNodeFactory(QObject *parent = 0);
00316 
00320   ~AbstractNodeFactory() override;
00321 
00337   virtual Node *getNode(const QString &tagContent, Parser *p) const = 0;
00338 
00339 #ifndef Q_QDOC
00340 
00346   virtual void setEngine(Engine *) {}
00347 #endif
00348 
00349 protected:
00365   Q_INVOKABLE QStringList smartSplit(const QString &str) const;
00366 
00367 protected:
00374   QList<FilterExpression> getFilterExpressionList(const QStringList &list,
00375                                                   Parser *p) const;
00376 
00377 private:
00378   Q_DECLARE_PRIVATE(AbstractNodeFactory)
00379   AbstractNodeFactoryPrivate *const d_ptr;
00380 };
00381 }
00382 
00383 #endif