Cutelee  6.1.0
scriptablevariable.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 "scriptablevariable.h"
22 
23 #include <QtQml/QJSEngine>
24 
25 #include "scriptablesafestring.h"
26 #include "util.h"
27 
28 ScriptableVariable::ScriptableVariable(QObject *parent)
29  : QObject(parent), m_engine(0)
30 {
31 }
32 
33 ScriptableVariable::ScriptableVariable(QJSEngine *engine, QObject *parent)
34  : QObject(parent), m_engine(engine)
35 {
36 }
37 
38 void ScriptableVariable::setContent(const QString &content)
39 {
40  m_variable = Variable(content);
41 }
42 
43 QVariant ScriptableVariable::resolve(ScriptableContext *c)
44 {
45  auto var = m_variable.resolve(c->context());
46 
47  if (Cutelee::isSafeString(var)) {
48  auto ssObj = new ScriptableSafeString(m_engine);
49  ssObj->setContent(getSafeString(var));
50  return m_engine->newQObject(ssObj).toVariant();
51  }
52  return var;
53 }
54 
55 bool ScriptableVariable::isTrue(ScriptableContext *c)
56 {
57  return m_variable.isTrue(c->context());
58 }
A container for static variables defined in Templates.
Definition: variable.h:53
QVariant resolve(Context *c) const
Definition: variable.cpp:175
bool isTrue(Context *c) const
Definition: variable.cpp:145
bool isSafeString(const QVariant &input)
Definition: util.cpp:117
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.