lib

qtobject.cpp
1/***************************************************************************
2 * qtobject.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "qtobject.h"
21#include "object.h"
22#include "variant.h"
23#include "event.h"
24
25#include "../main/manager.h"
26#include "eventslot.h"
27#include "eventsignal.h"
28
29#include <tqobject.h>
30#include <tqsignal.h>
31//#include <tqglobal.h>
32//#include <tqobjectdefs.h>
33#include <tqmetaobject.h>
34#include <tqucom_p.h> // for the TQt TQUObject API.
35
36using namespace Kross::Api;
37
38QtObject::QtObject(TQObject* object, const TQString& name)
39 : Kross::Api::Class<QtObject>(name.isEmpty() ? TQString(object->name()) : name)
40 , m_object(object)
41{
42 // Walk through the signals and slots the TQObject has
43 // and attach them as events to this QtObject.
44
45 TQStrList slotnames = m_object->metaObject()->slotNames(false);
46 for(char* c = slotnames.first(); c; c = slotnames.next()) {
47 TQCString s = c;
48 addChild(s, new EventSlot(s, object, s) );
49 }
50
51 TQStrList signalnames = m_object->metaObject()->signalNames(false);
52 for(char* c = signalnames.first(); c; c = signalnames.next()) {
53 TQCString s = c;
54 addChild(s, new EventSignal(s, object, s) );
55 }
56
57 // Add functions to wrap TQObject methods into callable
58 // Kross objects.
59
60 addFunction("propertyNames", &QtObject::propertyNames);
61 addFunction("hasProperty", &QtObject::hasProperty);
62 addFunction("getProperty", &QtObject::getProperty);
63 addFunction("setProperty", &QtObject::setProperty);
64
65 addFunction("slotNames", &QtObject::slotNames);
66 addFunction("hasSlot", &QtObject::hasSlot);
67 addFunction("slot", &QtObject::callSlot);
68
69 addFunction("signalNames", &QtObject::signalNames);
70 addFunction("hasSignal", &QtObject::hasSignal);
71 addFunction("signal", &QtObject::emitSignal);
72
73 addFunction("connect", &QtObject::connectSignal);
74 addFunction("disconnect", &QtObject::disconnectSignal);
75}
76
80
81const TQString QtObject::getClassName() const
82{
83 return "Kross::Api::QtObject";
84}
85
87{
88 return m_object;
89}
90
91TQUObject* QtObject::toTQUObject(const TQString& signature, List::Ptr arguments)
92{
93 int startpos = signature.find("(");
94 int endpos = signature.findRev(")");
95 if(startpos < 0 || startpos > endpos)
96 throw Exception::Ptr( new Exception(TQString("Invalid TQt signal or slot signature '%1'").arg(signature)) );
97
98 //TQString sig = signature.left(startpos);
99 TQString params = signature.mid(startpos + 1, endpos - startpos - 1);
100 TQStringList paramlist = TQStringList::split(",", params); // this will fail on something like myslot(TQMap<TQString,TQString> arg), but we don't care jet.
101 uint paramcount = paramlist.size();
102
103 // The first item in the TQUObject-array is for the returnvalue
104 // while everything >=1 are the passed parameters.
105 TQUObject* uo = new TQUObject[ paramcount + 1 ];
106 uo[0] = TQUObject(); // empty placeholder for the returnvalue.
107
108//TQString t;
109//for(int j=0; j<argcount; j++) t += "'" + Variant::toString(arguments->item(j)) + "' ";
110//krossdebug( TQString("1 ---------------------: (%1) %2").arg(argcount).arg(t) );
111
112 // Fill parameters.
113 uint argcount = arguments ? arguments->count() : 0;
114 for(uint i = 0; i < paramcount; i++) {
115 if(paramlist[i].find("TQString") >= 0) {
116 const TQString s = (argcount > i) ? Variant::toString(arguments->item(i)) : TQString();
117 //krossdebug(TQString("EventSlot::toTQUObject s=%1").arg(s));
118 static_QUType_TQString.set( &(uo[i + 1]), s );
119 }
120 //TODO handle int, long, char*, TQStringList, etc.
121 else {
122 throw Exception::Ptr( new Exception(TQString("Unknown TQt signal or slot argument '%1' in signature '%2'.").arg(paramlist[i]).arg(signature)) );
123 }
124 }
125
126 return uo;
127}
128
129Kross::Api::Object::Ptr QtObject::propertyNames(Kross::Api::List::Ptr)
130{
131 return new Kross::Api::Variant(
132 TQStringList::fromStrList(m_object->metaObject()->propertyNames(false)));
133}
134
135Kross::Api::Object::Ptr QtObject::hasProperty(Kross::Api::List::Ptr args)
136{
137 return new Kross::Api::Variant(
138 m_object->metaObject()->findProperty(Kross::Api::Variant::toString(args->item(0)).latin1(), false));
139}
140
141Kross::Api::Object::Ptr QtObject::getProperty(Kross::Api::List::Ptr args)
142{
143 TQVariant variant = m_object->property(Kross::Api::Variant::toString(args->item(0)).latin1());
144 if(variant.type() == TQVariant::Invalid)
145 return 0;
146 return new Kross::Api::Variant(variant);
147}
148
149Kross::Api::Object::Ptr QtObject::setProperty(Kross::Api::List::Ptr args)
150{
151 return new Kross::Api::Variant(
152 m_object->setProperty(
153 Kross::Api::Variant::toString(args->item(0)).latin1(),
154 Kross::Api::Variant::toVariant(args->item(1))
155 ));
156}
157
159{
160 return new Kross::Api::Variant(
161 TQStringList::fromStrList(m_object->metaObject()->slotNames(false)));
162}
163
165{
166 return new Kross::Api::Variant(
167 bool(m_object->metaObject()->slotNames(false).find(
168 Kross::Api::Variant::toString(args->item(0)).latin1()
169 ) != -1));
170}
171
172Kross::Api::Object::Ptr QtObject::callSlot(Kross::Api::List::Ptr args)
173{
174//TODO just call the child event ?!
175 TQString name = Kross::Api::Variant::toString(args->item(0));
176 int slotid = m_object->metaObject()->findSlot(name.latin1(), false);
177 if(slotid < 0)
178 throw Exception::Ptr( new Exception(TQString("No such slot '%1'.").arg(name)) );
179
180 TQUObject* uo = QtObject::toTQUObject(name, args);
181 m_object->tqt_invoke(slotid, uo);
182 delete [] uo;
183
184 return new Variant( TQVariant(true) );
185}
186
188{
189 return new Kross::Api::Variant(
190 TQStringList::fromStrList(m_object->metaObject()->signalNames(false)));
191}
192
193Kross::Api::Object::Ptr QtObject::hasSignal(Kross::Api::List::Ptr args)
194{
195 return new Kross::Api::Variant(
196 bool(m_object->metaObject()->signalNames(false).find(
197 Kross::Api::Variant::toString(args->item(0)).latin1()
198 ) != -1));
199}
200
201Kross::Api::Object::Ptr QtObject::emitSignal(Kross::Api::List::Ptr args)
202{
203 TQString name = Kross::Api::Variant::toString(args->item(0));
204 int signalid = m_object->metaObject()->findSignal(name.latin1(), false);
205 if(signalid < 0)
206 throw Exception::Ptr( new Exception(TQString("No such signal '%1'.").arg(name)) );
207 m_object->tqt_invoke(signalid, 0); //TODO convert Kross::Api::List::Ptr => TQUObject*
208 return 0;
209}
210
211Kross::Api::Object::Ptr QtObject::connectSignal(Kross::Api::List::Ptr args)
212{
213 TQString signalname = Kross::Api::Variant::toString(args->item(0));
214 TQString signalsignatur = TQString("2%1").arg(signalname);
215 const char* signalsig = signalsignatur.latin1();
216
218 TQObject* o = obj->getObject();
219 if(! o)
220 throw Exception::Ptr( new Exception(TQString("No such TQObject receiver in '%1'.").arg(obj->getName())) );
221
222 TQString slotname = Kross::Api::Variant::toString(args->item(2));
223 TQString slotsignatur = TQString("1%1").arg(slotname);
224 const char* slotsig = slotsignatur.latin1();
225
226 return new Kross::Api::Variant(
227 TQObject::connect(m_object, signalsig, o, slotsig));
228}
229
230Kross::Api::Object::Ptr QtObject::disconnectSignal(Kross::Api::List::Ptr)
231{
232 //TODO
233 return 0;
234}
235
const TQString getName() const
Definition callable.cpp:37
Class(const TQString &name)
Definition class.h:57
void addFunction(const TQString &name, FunctionPtr function)
Definition event.h:99
Common exception class used for representing exceptions in Kross.
Definition exception.h:39
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:46
static T * fromObject(Object::Ptr object)
Try to convert the Object instance to the template class T.
Definition object.h:140
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:56
virtual const TQString getClassName() const
Definition qtobject.cpp:81
TQObject * getObject()
Return the TQObject instance this class wraps.
Definition qtobject.cpp:86
virtual ~QtObject()
Destructor.
Definition qtobject.cpp:77
QtObject(TQObject *object, const TQString &name=TQString())
Constructor.
Definition qtobject.cpp:38
static TQUObject * toTQUObject(const TQString &signature, List::Ptr arguments)
Build a TQt TQUObject struct out of the TQt signal or slot signature and the passed List arguments.
Definition qtobject.cpp:91
Variant value to wrap a TQVariant into a Kross::Api::Value to enable primitive types like strings or ...
Definition variant.h:39
virtual const TQString toString()
Definition variant.cpp:41
static const TQVariant & toVariant(Object::Ptr object)
Try to convert the given Object into a TQVariant.
Definition variant.cpp:75
const char * name(StdAction id)
The common Kross API used as common codebase.
Definition callable.h:33
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:33