[Kde-bindings] playground/bindings/kimono
Arno Rehn
kde at arnorehn.de
Wed Feb 28 17:03:14 UTC 2007
SVN commit 638043 by arnorehn:
* Made QObject.FindChild() and QObject.FindChildren() work
CCMAIL: kde-bindings at kde.org
M +1 -0 ChangeLog
M +41 -9 core/QObjectExtras.cs
M +90 -101 qyoto.cpp
--- trunk/playground/bindings/kimono/ChangeLog #638042:638043
@@ -8,6 +8,7 @@
* If the return value is postive, it was a C++ slot/signal/property, return
* If the return value is negative, look for C# slots/signals/properties
and call them
+ * Made QObject.FindChild() and QObject.FindChildren() work
2007-02-27 Richard Dale <rdale at foton.es>
--- trunk/playground/bindings/kimono/core/QObjectExtras.cs #638042:638043
@@ -1,27 +1,59 @@
namespace Qyoto {
using System;
+ using System.Runtime.InteropServices;
using System.Collections.Generic;
public partial class QObject : Qt, IDisposable {
- T FindChild<T>(string name) {
- return (T) (object) default(T);
+ private delegate void AddToListFn(IntPtr obj);
+
+ [DllImport("libqyoto", CharSet=CharSet.Ansi)]
+ private static extern IntPtr FindQObjectChild(IntPtr parent, string childName);
+
+ [DllImport("libqyoto", CharSet=CharSet.Ansi)]
+ private static extern void FindQObjectChildren(IntPtr parent, IntPtr regexp,
+ string childName, AddToListFn addFn);
+
+ public T FindChild<T>(string name) {
+ IntPtr child = FindQObjectChild((IntPtr) GCHandle.Alloc(this), name);
+ if (child != IntPtr.Zero) {
+ try {
+ return (T) ((GCHandle) child).Target;
+ } catch (Exception e) {
+ Console.WriteLine("Found child, but an error has occurred: {0}", e.Message);
+ return default(T);
+ }
+ } else {
+ return default(T);
+ }
}
- T FindChild<T>() {
+ public T FindChild<T>() {
return FindChild<T>("");
}
- List<T> FindChildren<T>(string name) {
- return new List<T>();
+ public List<T> FindChildren<T>(string name) {
+ List<T> list = new List<T>();
+ AddToListFn addFn = delegate(IntPtr obj) {
+ T o = (T) ((System.Runtime.InteropServices.GCHandle) obj).Target;
+ list.Add(o);
+ };
+ FindQObjectChildren((IntPtr) GCHandle.Alloc(this), IntPtr.Zero, name, addFn);
+ return list;
}
- List<T> FindChildren<T>() {
- return new List<T>();
+ public List<T> FindChildren<T>() {
+ return FindChildren<T>("");
}
- List<T> FindChildren<T>(QRegExp regExp) {
- return new List<T>();
+ public List<T> FindChildren<T>(QRegExp regExp) {
+ List<T> list = new List<T>();
+ AddToListFn addFn = delegate(IntPtr obj) {
+ T o = (T) ((System.Runtime.InteropServices.GCHandle) obj).Target;
+ list.Add(o);
+ };
+ FindQObjectChildren((IntPtr) GCHandle.Alloc(this), (IntPtr) GCHandle.Alloc(regExp), "", addFn);
+ return list;
}
}
}
\ No newline at end of file
--- trunk/playground/bindings/kimono/qyoto.cpp #638042:638043
@@ -1123,110 +1123,114 @@
return qt_Smoke->methodMaps[meth].method;
}
+QMetaObject* parent_meta_object(void* obj) {
+ smokeqyoto_object* o = value_obj_info(obj);
+ Smoke::Index nameId = o->smoke->idMethodName("metaObject");
+ Smoke::Index meth = o->smoke->findMethod(o->classId, nameId);
+ if (meth <= 0) {
+ // Should never happen..
+ }
+
+ Smoke::Method &methodId = o->smoke->methods[o->smoke->methodMaps[meth].method];
+ Smoke::ClassFn fn = o->smoke->classes[methodId.classId].classFn;
+ Smoke::StackItem i[1];
+ (*fn)(methodId.method, o->ptr, i);
+ return (QMetaObject*) i[0].s_voidp;
+}
+
/* Adapted from the internal function qt_qFindChildren() in qobject.cpp */
-static void
-cs_qFindChildrenHelper(void * parent, const QString &name, void * re,
- const QMetaObject &mo, void * list)
+void
+cs_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
+ const QMetaObject &mo, QList<void*> *list)
{
-/*
- if (parent == Qnil || list == Qnil)
- return;
- VALUE children = rb_funcall(parent, rb_intern("children"), 0);
- VALUE rv = Qnil;
- for (int i = 0; i < RARRAY(children)->len; ++i) {
- rv = RARRAY(children)->ptr[i];
- smokeruby_object *o = value_obj_info(rv);
- QObject * obj = (QObject *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QObject"));
-
- // The original code had 'if (mo.cast(obj))' as a test, but it doesn't work here
- if (obj->qt_metacast(mo.className()) != 0) {
- if (re != Qnil) {
- VALUE re_test = rb_funcall(re, rb_intern("=~"), 1, rb_funcall(rv, rb_intern("objectName"), 0));
- if (re_test != Qnil && re_test != Qfalse) {
- rb_ary_push(list, rv);
- }
- } else {
- if (name.isNull() || obj->objectName() == name) {
- rb_ary_push(list, rv);
- }
- }
- }
- rb_qFindChildren_helper(rv, name, re, mo, list);
- }
-*/
- return;
+ if (!parent || !list)
+ return;
+
+ const QObjectList &children = parent->children();
+ QObject *obj;
+
+ for (int i = 0; i < children.size(); ++i) {
+ obj = children.at(i);
+ if (mo.cast(obj)) {
+ if (re) {
+ if (re->indexIn(obj->objectName()) != -1)
+ list->append(getPointerObject(obj));
+ } else {
+ if (name.isNull() || obj->objectName() == name)
+ list->append(getPointerObject(obj));
+ }
+ }
+ qt_qFindChildren_helper(obj, name, re, mo, list);
+ }
}
/* Should mimic Qt4's QObject::findChildren method with this syntax:
obj.findChildren(Qt::Widget, "Optional Widget Name")
*/
-static void *
-FindQObjectChildren(char * typeName, char * name)
+void
+FindQObjectChildren(void* parent, void* regexp, char* childName, FromIntPtr addFn)
{
-/*
- QString name;
- VALUE re = Qnil;
- if (argc == 2) {
- // If the second arg isn't a String, assume it's a regular expression
- if (TYPE(argv[1]) == T_STRING) {
- name = QString::fromLatin1(StringValuePtr(argv[1]));
- } else {
- re = argv[1];
- }
+ QMetaObject *mo = parent_meta_object(parent);
+
+ smokeqyoto_object *o;
+ o = value_obj_info(parent);
+ QObject* p = (QObject*) o->ptr;
+ delete o;
+
+ QRegExp* re = 0;
+ if (regexp) {
+ o = value_obj_info(regexp);
+ re = (QRegExp*) o->ptr;
+ delete o;
}
-
- VALUE metaObject = rb_funcall(argv[0], rb_intern("staticMetaObject"), 0);
- smokeruby_object *o = value_obj_info(metaObject);
- QMetaObject * mo = (QMetaObject*) o->ptr;
- VALUE result = rb_ary_new();
- rb_qFindChildren_helper(self, name, re, *mo, result);
- return result;
-*/
+
+ QList<void*> *list = new QList<void*>();
+
+ cs_qFindChildren_helper(p, QString::fromUtf8(childName), re, *mo, list);
+
+ for(int i = 0; i < list->size(); ++i) {
+ (*addFn)(list->at(i));
+ }
}
/* Adapted from the internal function qt_qFindChild() in qobject.cpp */
-static void *
+void *
cs_qFindChildHelper(void * parent, const QString &name, const QMetaObject &mo)
{
-/*
- if (parent == Qnil)
- return Qnil;
- VALUE children = rb_funcall(parent, rb_intern("children"), 0);
- VALUE rv;
+ if (!parent)
+ return 0;
+
+ smokeqyoto_object *o = value_obj_info(parent);
+ QObject* p = (QObject*) o->ptr;
+ delete o;
+
+ const QObjectList &children = p->children();
+ QObject* obj;
+ void* monoObject;
+
int i;
- for (i = 0; i < RARRAY(children)->len; ++i) {
- rv = RARRAY(children)->ptr[i];
- smokeruby_object *o = value_obj_info(rv);
- QObject * obj = (QObject *) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QObject"));
- if (obj->qt_metacast(mo.className()) != 0 && (name.isNull() || obj->objectName() == name))
- return rv;
- }
- for (i = 0; i < RARRAY(children)->len; ++i) {
- rv = rb_qFindChild_helper(RARRAY(children)->ptr[i], name, mo);
- if (rv != Qnil)
- return rv;
- }
- return Qnil;
-*/
+ for (i = 0; i < children.size(); ++i) {
+ obj = children.at(i);
+ if (mo.cast(obj) && (name.isNull() || obj->objectName() == name)) {
+ monoObject = getPointerObject(obj);
+ return monoObject;
+ }
+ }
+ for (i = 0; i < children.size(); ++i) {
+ monoObject = qt_qFindChild_helper(children.at(i), name, mo);
+ if (monoObject)
+ return monoObject;
+ }
+ return 0;
}
-static void *
-FindQObjectChild(char * typeName, char * name)
+void *
+FindQObjectChild(void* parent, char * childName)
{
-/*
- if (argc < 1 || argc > 2) rb_raise(rb_eArgError, "Invalid argument list");
- Check_Type(argv[0], T_CLASS);
-
- QString name;
- if (argc == 2) {
- name = QString::fromLatin1(StringValuePtr(argv[1]));
- }
-
- VALUE metaObject = rb_funcall(argv[0], rb_intern("staticMetaObject"), 0);
- smokeruby_object *o = value_obj_info(metaObject);
- QMetaObject * mo = (QMetaObject*) o->ptr;
- return rb_qFindChild_helper(self, name, *mo);
-*/
+ QString name = QString::fromUtf8(childName);
+
+ QMetaObject * mo = parent_meta_object(parent);
+ return cs_qFindChildHelper(parent, name, *mo);
}
void *
@@ -1495,21 +1499,6 @@
return true;
}
-QMetaObject* parent_meta_object(void* obj) {
- smokeqyoto_object* o = value_obj_info(obj);
- Smoke::Index nameId = o->smoke->idMethodName("metaObject");
- Smoke::Index meth = o->smoke->findMethod(o->classId, nameId);
- if (meth <= 0) {
- // Should never happen..
- }
-
- Smoke::Method &methodId = o->smoke->methods[o->smoke->methodMaps[meth].method];
- Smoke::ClassFn fn = o->smoke->classes[methodId.classId].classFn;
- Smoke::StackItem i[1];
- (*fn)(methodId.method, o->ptr, i);
- return (QMetaObject*) i[0].s_voidp;
-}
-
void* make_metaObject( void* obj, void* parentMeta,
const char* stringdata, int stringdata_count,
const uint* data, int data_count )
More information about the Kde-bindings
mailing list