Changeset View
Changeset View
Standalone View
Standalone View
templates/scriptabletags/scriptabletemplate.cpp
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | 15 | | |||
16 | You should have received a copy of the GNU Lesser General Public | 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/>. | 17 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
18 | 18 | | |||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "scriptabletemplate.h" | 21 | #include "scriptabletemplate.h" | ||
22 | 22 | | |||
23 | #include <QtScript/QScriptContext> | | |||
24 | #include <QtScript/QScriptEngine> | | |||
25 | | ||||
26 | #include "context.h" | 23 | #include "context.h" | ||
27 | #include "engine.h" | 24 | #include "engine.h" | ||
28 | #include "node.h" | 25 | #include "node.h" | ||
29 | #include "scriptablecontext.h" | 26 | #include "scriptablecontext.h" | ||
30 | 27 | | |||
31 | QScriptValue ScriptableTemplateConstructor(QScriptContext *context, | | |||
32 | QScriptEngine *engine) | | |||
33 | { | | |||
34 | auto content = context->argument(0).toString(); | | |||
35 | auto name = context->argument(1).toString(); | | |||
36 | auto parent = context->argument(2).toQObject(); | | |||
37 | auto templateEngine = engine->property("templateEngine").value<Engine *>(); | | |||
38 | | ||||
39 | if (!templateEngine) | | |||
40 | return QScriptValue(); | | |||
41 | | ||||
42 | auto t = templateEngine->newTemplate(content, name); | | |||
43 | | ||||
44 | auto object = new ScriptableTemplate(t, parent); | | |||
45 | return engine->newQObject(object); | | |||
46 | } | | |||
47 | | ||||
48 | ScriptableTemplate::ScriptableTemplate(Grantlee::Template t, QObject *parent) | 28 | ScriptableTemplate::ScriptableTemplate(Grantlee::Template t, QObject *parent) | ||
49 | : QObject(parent), m_template(t) | 29 | : QObject(parent), m_template(t) | ||
50 | { | 30 | { | ||
51 | } | 31 | } | ||
52 | 32 | | |||
53 | QString ScriptableTemplate::render(ScriptableContext *c) const | 33 | QString ScriptableTemplate::render(ScriptableContext *c) const | ||
54 | { | 34 | { | ||
55 | return m_template->render(c->context()); | 35 | return m_template->render(c->context()); | ||
56 | } | 36 | } | ||
57 | 37 | | |||
58 | QObjectList ScriptableTemplate::nodeList() const | 38 | QList<QObject *> ScriptableTemplate::nodeList() const | ||
59 | { | 39 | { | ||
60 | auto nodeList = m_template->nodeList(); | 40 | auto nodeList = m_template->nodeList(); | ||
61 | QObjectList objList; | 41 | QObjectList objList; | ||
62 | 42 | | |||
63 | QListIterator<Node *> it(nodeList); | 43 | QListIterator<Node *> it(nodeList); | ||
64 | while (it.hasNext()) { | 44 | while (it.hasNext()) { | ||
65 | objList << it.next(); | 45 | objList << it.next(); | ||
66 | } | 46 | } | ||
67 | return objList; | 47 | return objList; | ||
68 | } | 48 | } | ||
69 | 49 | | |||
70 | void ScriptableTemplate::setNodeList(const QObjectList &list) | 50 | void ScriptableTemplate::setNodeList(const QList<QObject *> &list) | ||
71 | { | 51 | { | ||
72 | NodeList nodeList; | 52 | NodeList nodeList; | ||
73 | 53 | | |||
74 | QListIterator<QObject *> it(list); | 54 | QListIterator<QObject *> it(list); | ||
75 | 55 | | |||
76 | while (it.hasNext()) { | 56 | while (it.hasNext()) { | ||
77 | auto n = qobject_cast<Node *>(it.next()); | 57 | auto n = qobject_cast<Node *>(it.next()); | ||
78 | if (n) { | 58 | if (n) { | ||
79 | nodeList << n; | 59 | nodeList << n; | ||
80 | } | 60 | } | ||
81 | } | 61 | } | ||
82 | m_template->setNodeList(nodeList); | 62 | m_template->setNodeList(nodeList); | ||
83 | } | 63 | } |