Cutelee  6.1.0
scriptablefilterexpression.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 "scriptablefilterexpression.h"
22 
23 #include <QtQml/QJSEngine>
24 
25 #include "parser.h"
26 #include "scriptablesafestring.h"
27 #include "util.h"
28 
29 using namespace Cutelee;
30 
31 ScriptableFilterExpression::ScriptableFilterExpression(QObject *parent)
32  : QObject(parent), m_engine(0)
33 {
34 }
35 
36 ScriptableFilterExpression::ScriptableFilterExpression(QJSEngine *engine,
37  QObject *parent)
38  : QObject(parent), m_engine(engine)
39 {
40 }
41 
42 void ScriptableFilterExpression::init(const QString &content,
43  Cutelee::Parser *parser)
44 {
45  m_filterExpression = FilterExpression(content, parser);
46 }
47 
48 QVariant ScriptableFilterExpression::resolve(ScriptableContext *c)
49 {
50  auto var = m_filterExpression.resolve(c->context());
51 
52  if (Cutelee::isSafeString(var)) {
53  auto ssObj = new ScriptableSafeString(m_engine);
54  ssObj->setContent(getSafeString(var));
55  return m_engine->newQObject(ssObj).toVariant();
56  }
57  return var;
58 }
59 
60 bool ScriptableFilterExpression::isTrue(ScriptableContext *c)
61 {
62  return m_filterExpression.isTrue(c->context());
63 }
64 
65 bool ScriptableFilterExpression::equals(ScriptableFilterExpression *other,
66  ScriptableContext *scriptableC)
67 {
68  auto c = scriptableC->context();
69  return Cutelee::equals(m_filterExpression.resolve(c),
70  other->m_filterExpression.resolve(c));
71 }
A FilterExpression object represents a filter expression in a template.
bool isTrue(Context *c) const
QVariant resolve(OutputStream *stream, Context *c) const
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8
bool equals(const QVariant &lhs, const QVariant &rhs)
Definition: util.cpp:140
bool isSafeString(const QVariant &input)
Definition: util.cpp:117
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.