Cutelee 6.1.0
scriptablenode.cpp
1/*
2 This file is part of the Cutelee 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#include "scriptablenode.h"
22
23#include <QtQml/QJSEngine>
24
25#include "context.h"
26#include "engine.h"
27#include "exception.h"
28#include "parser.h"
29#include "scriptablecontext.h"
30#include "scriptableparser.h"
31
32ScriptableNode::ScriptableNode(QObject *parent)
33 : Node(parent), m_scriptEngine(0)
34{
35}
36
37void ScriptableNode::setScriptEngine(QJSEngine *engine)
38{
39 m_scriptEngine = engine;
40}
41
42void ScriptableNode::init(const QJSValue &concreteNode,
43 const QJSValue &renderMethod)
44{
45 m_concreteNode = concreteNode;
46 m_renderMethod = renderMethod;
47}
48
50{
52 auto contextObject = m_scriptEngine->newQObject(&sc);
53
54 QJSValueList args;
55 args << contextObject;
56
57 // Call the render method in the context of the concreteNode
58 auto value = const_cast<QJSValue &>(m_renderMethod)
59 .callWithInstance(m_concreteNode, args);
60
61 if (!value.isError() && !value.isUndefined())
62 (*stream) << value.toString();
63}
64
65ScriptableNodeFactory::ScriptableNodeFactory(QObject *parent)
66 : AbstractNodeFactory(parent), m_scriptEngine(0)
67{
68}
69
70void ScriptableNodeFactory::setScriptEngine(QJSEngine *engine)
71{
72 m_scriptEngine = engine;
73}
74
75void ScriptableNodeFactory::setEngine(Engine *engine)
76{
77 m_scriptEngine->setProperty("templateEngine", QVariant::fromValue(engine));
78}
79
80void ScriptableNodeFactory::setFactory(const QJSValue &factoryMethod)
81{
82 m_factoryMethod = factoryMethod;
83}
84
86{
87 auto sp = new ScriptableParser(p, m_scriptEngine);
88 auto parserObject = m_scriptEngine->newQObject(sp);
89
90 QJSValueList args;
91 args << tagContent;
92 args << parserObject;
93
94 auto factory = m_factoryMethod;
95
96 auto scriptNode = factory.callWithInstance(factory, args);
97 if (scriptNode.isError())
98 throw Cutelee::Exception(TagSyntaxError, scriptNode.toString());
99
100 auto node = qjsvalue_cast<Node *>(scriptNode);
101 node->setParent(p);
102 return node;
103}
104
105QJSEngine *ScriptableNode::engine() { return m_scriptEngine; }
106
107void ScriptableNode::setNodeList(const QString &name,
108 const QList<QObject *> &objectList)
109{
110 auto objectListArray = m_scriptEngine->newArray(objectList.size());
111
112 for (auto i = 0; i < objectList.size(); ++i) {
113 objectListArray.setProperty(i,
114 m_scriptEngine->newQObject(objectList.at(i)));
115 }
116 m_concreteNode.setProperty(name, objectListArray);
117}
Base class for all NodeFactories.
Definition node.h:300
The Context class holds the context to render a Template with.
Definition context.h:119
Cutelee::Engine is the main entry point for creating Cutelee Templates.
Definition engine.h:121
An exception for use when implementing template tags.
Definition exception.h:85
Base class for all nodes.
Definition node.h:78
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
Definition parser.h:49
Node * getNode(const QString &tagContent, Parser *p={}) const override
void render(OutputStream *stream, Context *c) const override