1
/*************************************************************************************
2
* Copyright (C) 2011 by Aleix Pol <aleixpol@kde.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License *
6
* as published by the Free Software Foundation; either version 2 *
7
* of the License, or (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the Free Software *
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17
*************************************************************************************/
19
#include "importqobjectmetatype.h"
21
#include <qmetaobject.h>
22
#include "analitzautils.h"
25
using namespace Analitza;
27
ExpressionType toExpressionType(QVariant::Type t, const QString& type_name)
32
case QVariant::Double:
33
return ExpressionType(ExpressionType::Value);
34
case QVariant::String:
35
return ExpressionType(ExpressionType::List, ExpressionType(ExpressionType::Char));
37
return ExpressionType(type_name);
41
class QObjectGet : public Analitza::FunctionDefinition
44
QObjectGet(const QByteArray& name) : m_name(name) {}
46
virtual Expression operator()(const QList< Expression >& args)
48
QObject* o=args.first().customObjectValue().value<QObject*>();
49
QVariant v=o->property(m_name);
51
return AnalitzaUtils::variantToExpression(v);
54
ExpressionType type(const QByteArray& name, const QMetaProperty& prop) const
56
ExpressionType typeGet(ExpressionType::Lambda);
57
typeGet.addParameter(ExpressionType(name))
58
.addParameter(toExpressionType(prop.type(), prop.typeName()));
67
class QObjectSet : public Analitza::FunctionDefinition
70
QObjectSet(const QByteArray& name) : m_name(name) {}
72
virtual Expression operator()(const QList< Expression >& args)
74
QObject* o=args.first().customObjectValue().value<QObject*>();
75
QVariant value=AnalitzaUtils::expressionToVariant(args.last());
77
return Expression(Cn(o->setProperty(m_name, value)));
80
ExpressionType type(const QByteArray& name, const QMetaProperty& prop) const
82
ExpressionType typeSet(ExpressionType::Lambda);
83
typeSet.addParameter(ExpressionType(name))
84
.addParameter(toExpressionType(prop.type(), prop.typeName()))
85
.addParameter(ExpressionType(ExpressionType::Bool));
94
ImportQMetaObject::ImportQMetaObject(Analitza::Analyzer* a)
98
void ImportQMetaObject::import(const QMetaObject& t)
100
Analitza::BuiltinMethods* b=m_a->builtinMethods();
102
for(int p=0; p<t.propertyCount(); p++) {
103
QMetaProperty prop=t.property(p);
104
QByteArray name(prop.name());
106
if(prop.isReadable()) {
107
QObjectGet* getter=new QObjectGet(name);
108
b->insertFunction(name, getter->type(name, prop), getter);
111
if(prop.isWritable()) {
112
QObjectSet* setter=new QObjectSet(name);
113
b->insertFunction("get_"+name, setter->type(name, prop), setter);