summaryrefslogtreecommitdiffstats
path: root/lib/kross/api/qtobject.cpp
blob: 631c994346688cebc9cc79294a2d8d4f8aba0f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/***************************************************************************
 * qtobject.cpp
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer ([email protected])
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "qtobject.h"
#include "object.h"
#include "variant.h"
#include "event.h"

#include "../main/manager.h"
#include "eventslot.h"
#include "eventsignal.h"

#include <tqobject.h>
#include <tqsignal.h>
//#include <tqglobal.h>
//#include <tqobjectdefs.h>
#include <tqmetaobject.h>
#include <tqucom_p.h> // for the TQt TQUObject API.

using namespace Kross::Api;

QtObject::QtObject(TQObject* object, const TQString& name)
    : Kross::Api::Class<QtObject>(name.isEmpty() ? TQString(object->name()) : name)
    , m_object(object)
{
    // Walk through the signals and slots the TQObject has
    // and attach them as events to this QtObject.

    TQStrList slotnames = m_object->metaObject()->slotNames(false);
    for(char* c = slotnames.first(); c; c = slotnames.next()) {
        TQCString s = c;
        addChild(s, new EventSlot(s, object, s) );
    }

    TQStrList signalnames = m_object->metaObject()->signalNames(false);
    for(char* c = signalnames.first(); c; c = signalnames.next()) {
        TQCString s = c;
        addChild(s, new EventSignal(s, object, s) );
    }

    // Add functions to wrap TQObject methods into callable
    // Kross objects.

    addFunction("propertyNames", &QtObject::propertyNames);
    addFunction("hasProperty", &QtObject::hasProperty);
    addFunction("getProperty", &QtObject::getProperty);
    addFunction("setProperty", &QtObject::setProperty);

    addFunction("slotNames", &QtObject::slotNames);
    addFunction("hasSlot", &QtObject::hasSlot);
    addFunction("slot", &QtObject::callSlot);

    addFunction("signalNames", &QtObject::signalNames);
    addFunction("hasSignal", &QtObject::hasSignal);
    addFunction("signal", &QtObject::emitSignal);

    addFunction("connect", &QtObject::connectSignal);
    addFunction("disconnect", &QtObject::disconnectSignal);
}

QtObject::~QtObject()
{
}

const TQString QtObject::getClassName() const
{
    return "Kross::Api::QtObject";
}

TQObject* QtObject::getObject()
{
    return m_object;
}

TQUObject* QtObject::toTQUObject(const TQString& signature, List::Ptr arguments)
{
    int startpos = signature.find("(");
    int endpos = signature.findRev(")");
    if(startpos < 0 || startpos > endpos)
        throw Exception::Ptr( new Exception(TQString("Invalid TQt signal or slot signature '%1'").arg(signature)) );

    //TQString sig = signature.left(startpos);
    TQString params = signature.mid(startpos + 1, endpos - startpos - 1);
    TQStringList paramlist = TQStringList::split(",", params); // this will fail on something like myslot(TQMap<TQString,TQString> arg), but we don't care jet.
    uint paramcount = paramlist.size();

    // The first item in the TQUObject-array is for the returnvalue
    // while everything >=1 are the passed parameters.
    TQUObject* uo = new TQUObject[ paramcount + 1 ];
    uo[0] = TQUObject(); // empty placeholder for the returnvalue.

//TQString t;
//for(int j=0; j<argcount; j++) t += "'" + Variant::toString(arguments->item(j)) + "' ";
//krossdebug( TQString("1 ---------------------: (%1) %2").arg(argcount).arg(t) );

    // Fill parameters.
    uint argcount = arguments ? arguments->count() : 0;
    for(uint i = 0; i < paramcount; i++) {
        if(paramlist[i].find(TQSTRING_OBJECT_NAME_STRING) >= 0) {
            const TQString s = (argcount > i) ? Variant::toString(arguments->item(i)) : TQString();
            //krossdebug(TQString("EventSlot::toTQUObject s=%1").arg(s));
            static_TQUType_TQString.set( &(uo[i + 1]), s );
        }
        //TODO handle int, long, char*, TQStringList, etc.
        else {
            throw Exception::Ptr( new Exception(TQString("Unknown TQt signal or slot argument '%1' in signature '%2'.").arg(paramlist[i]).arg(signature)) );
        }
    }

    return uo;
}

Kross::Api::Object::Ptr QtObject::propertyNames(Kross::Api::List::Ptr)
{
    return new Kross::Api::Variant(
        TQStringList::fromStrList(m_object->metaObject()->propertyNames(false)));
}

Kross::Api::Object::Ptr QtObject::hasProperty(Kross::Api::List::Ptr args)
{
    return new Kross::Api::Variant(
        m_object->metaObject()->findProperty(Kross::Api::Variant::toString(args->item(0)).latin1(), false));
}

Kross::Api::Object::Ptr QtObject::getProperty(Kross::Api::List::Ptr args)
{
    TQVariant variant = m_object->property(Kross::Api::Variant::toString(args->item(0)).latin1());
    if(variant.type() == TQVariant::Invalid)
        return 0;
    return new Kross::Api::Variant(variant);
}

Kross::Api::Object::Ptr QtObject::setProperty(Kross::Api::List::Ptr args)
{
    return new Kross::Api::Variant(
           m_object->setProperty(
               Kross::Api::Variant::toString(args->item(0)).latin1(),
               Kross::Api::Variant::toVariant(args->item(1))
           ));
}

Kross::Api::Object::Ptr QtObject::slotNames(Kross::Api::List::Ptr)
{
    return new Kross::Api::Variant(
           TQStringList::fromStrList(m_object->metaObject()->slotNames(false)));
}

Kross::Api::Object::Ptr QtObject::hasSlot(Kross::Api::List::Ptr args)
{
    return new Kross::Api::Variant(
           bool(m_object->metaObject()->slotNames(false).find(
               Kross::Api::Variant::toString(args->item(0)).latin1()
           ) != -1));
}

Kross::Api::Object::Ptr QtObject::callSlot(Kross::Api::List::Ptr args)
{
//TODO just call the child event ?!
    TQString name = Kross::Api::Variant::toString(args->item(0));
    int slotid = m_object->metaObject()->findSlot(name.latin1(), false);
    if(slotid < 0)
        throw Exception::Ptr( new Exception(TQString("No such slot '%1'.").arg(name)) );

    TQUObject* uo = QtObject::toTQUObject(name, args);
    m_object->tqt_invoke(slotid, uo);
    delete [] uo;

    return new Variant( TQVariant(true) );
}

Kross::Api::Object::Ptr QtObject::signalNames(Kross::Api::List::Ptr)
{
    return new Kross::Api::Variant(
           TQStringList::fromStrList(m_object->metaObject()->signalNames(false)));
}

Kross::Api::Object::Ptr QtObject::hasSignal(Kross::Api::List::Ptr args)
{
    return new Kross::Api::Variant(
           bool(m_object->metaObject()->signalNames(false).find(
               Kross::Api::Variant::toString(args->item(0)).latin1()
           ) != -1));
}

Kross::Api::Object::Ptr QtObject::emitSignal(Kross::Api::List::Ptr args)
{
    TQString name = Kross::Api::Variant::toString(args->item(0));
    int signalid = m_object->metaObject()->findSignal(name.latin1(), false);
    if(signalid < 0)
        throw Exception::Ptr( new Exception(TQString("No such signal '%1'.").arg(name)) );
    m_object->tqt_invoke(signalid, 0); //TODO convert Kross::Api::List::Ptr => TQUObject*
    return 0;
}

Kross::Api::Object::Ptr QtObject::connectSignal(Kross::Api::List::Ptr args)
{
    TQString signalname = Kross::Api::Variant::toString(args->item(0));
    TQString signalsignatur = TQString("2%1").arg(signalname);
    const char* signalsig = signalsignatur.latin1();

    QtObject* obj = Kross::Api::Object::fromObject<Kross::Api::QtObject>(args->item(1));
    TQObject* o = obj->getObject();
    if(! o)
        throw Exception::Ptr( new Exception(TQString("No such TQObject receiver in '%1'.").arg(obj->getName())) );

    TQString slotname = Kross::Api::Variant::toString(args->item(2));
    TQString slotsignatur = TQString("1%1").arg(slotname);
    const char* slotsig = slotsignatur.latin1();

    return new Kross::Api::Variant(
           TQObject::connect(m_object, signalsig, o, slotsig));
}

Kross::Api::Object::Ptr QtObject::disconnectSignal(Kross::Api::List::Ptr)
{
    //TODO
    return 0;
}