~ubuntu-branches/ubuntu/trusty/kalgebra/trusty-updates

« back to all changes in this revision

Viewing changes to analitza/importqobjectmetatype.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 20:10:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110708201034-0cqpagx7uz4pu82n
Tags: upstream-4.6.90+repack1
Import upstream version 4.6.90+repack1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************
 
2
 *  Copyright (C) 2011 by Aleix Pol <aleixpol@kde.org>                               *
 
3
 *                                                                                   *
 
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.                           *
 
8
 *                                                                                   *
 
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.                                     *
 
13
 *                                                                                   *
 
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
 *************************************************************************************/
 
18
 
 
19
#include "importqobjectmetatype.h"
 
20
#include "analyzer.h"
 
21
#include <qmetaobject.h>
 
22
#include "analitzautils.h"
 
23
#include "value.h"
 
24
 
 
25
using namespace Analitza;
 
26
 
 
27
ExpressionType toExpressionType(QVariant::Type t, const QString& type_name)
 
28
{
 
29
        switch(t)
 
30
        {
 
31
                case QVariant::Int:
 
32
                case QVariant::Double:
 
33
                        return ExpressionType(ExpressionType::Value);
 
34
                case QVariant::String:
 
35
                        return ExpressionType(ExpressionType::List, ExpressionType(ExpressionType::Char));
 
36
                default:
 
37
                        return ExpressionType(type_name);
 
38
        }
 
39
}
 
40
 
 
41
class QObjectGet : public Analitza::FunctionDefinition
 
42
{
 
43
        public:
 
44
                QObjectGet(const QByteArray& name) : m_name(name) {}
 
45
        
 
46
                virtual Expression operator()(const QList< Expression >& args)
 
47
                {
 
48
                        QObject* o=args.first().customObjectValue().value<QObject*>();
 
49
                        QVariant v=o->property(m_name);
 
50
                        
 
51
                        return AnalitzaUtils::variantToExpression(v);
 
52
                }
 
53
                
 
54
                ExpressionType type(const QByteArray& name, const QMetaProperty& prop) const
 
55
                {
 
56
                        ExpressionType typeGet(ExpressionType::Lambda);
 
57
                        typeGet.addParameter(ExpressionType(name))
 
58
                                .addParameter(toExpressionType(prop.type(), prop.typeName()));
 
59
                        
 
60
                        return typeGet;
 
61
                }
 
62
                
 
63
        private:
 
64
                QByteArray m_name;
 
65
};
 
66
 
 
67
class QObjectSet : public Analitza::FunctionDefinition
 
68
{
 
69
        public:
 
70
                QObjectSet(const QByteArray& name) : m_name(name) {}
 
71
        
 
72
                virtual Expression operator()(const QList< Expression >& args)
 
73
                {
 
74
                        QObject* o=args.first().customObjectValue().value<QObject*>();
 
75
                        QVariant value=AnalitzaUtils::expressionToVariant(args.last());
 
76
                        
 
77
                        return Expression(Cn(o->setProperty(m_name, value)));
 
78
                }
 
79
                
 
80
                ExpressionType type(const QByteArray& name, const QMetaProperty& prop) const
 
81
                {
 
82
                        ExpressionType typeSet(ExpressionType::Lambda);
 
83
                        typeSet.addParameter(ExpressionType(name))
 
84
                                .addParameter(toExpressionType(prop.type(), prop.typeName()))
 
85
                                .addParameter(ExpressionType(ExpressionType::Bool));
 
86
                        
 
87
                        return typeSet;
 
88
                }
 
89
                
 
90
        private:
 
91
                QByteArray m_name;
 
92
};
 
93
 
 
94
ImportQMetaObject::ImportQMetaObject(Analitza::Analyzer* a)
 
95
        : m_a(a)
 
96
{}
 
97
 
 
98
void ImportQMetaObject::import(const QMetaObject& t)
 
99
{
 
100
        Analitza::BuiltinMethods* b=m_a->builtinMethods();
 
101
        
 
102
        for(int p=0; p<t.propertyCount(); p++) {
 
103
                QMetaProperty prop=t.property(p);
 
104
                QByteArray name(prop.name());
 
105
                
 
106
                if(prop.isReadable()) {
 
107
                        QObjectGet* getter=new QObjectGet(name);
 
108
                        b->insertFunction(name, getter->type(name, prop), getter);
 
109
                }
 
110
                
 
111
                if(prop.isWritable()) {
 
112
                        QObjectSet* setter=new QObjectSet(name);
 
113
                        b->insertFunction("get_"+name, setter->type(name, prop), setter);
 
114
                }
 
115
        }
 
116
}
 
117