From c79b0e3d137be866da16a23a75522c5b452a2cdd 2009-06-23 14:15:24 From: florianlink Date: 2009-06-23 14:15:24 Subject: [PATCH] added findChild and findChildren contributed by David Geldreich git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@115 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/src/PythonQtStdDecorators.cpp b/src/PythonQtStdDecorators.cpp index 2a5fdee..19d12fe 100644 --- a/src/PythonQtStdDecorators.cpp +++ b/src/PythonQtStdDecorators.cpp @@ -154,3 +154,151 @@ QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QB return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), QCoreApplication::CodecForTr, n); } +QObject* PythonQtStdDecorators::findChild(QObject* parent, PyObject* type, const QString& name) +{ + const QMetaObject* meta = NULL; + const char* typeName = NULL; + + if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) { + meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject(); + } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) { + meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject(); + } else if (PyString_Check(type)) { + typeName = PyString_AsString(type); + } + + if (!typeName && !meta) + return NULL; + + return findChild(parent, typeName, meta, name); +} + +QList PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QString& name) +{ + const QMetaObject* meta = NULL; + const char* typeName = NULL; + + if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) { + meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject(); + } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) { + meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject(); + } else if (PyString_Check(type)) { + typeName = PyString_AsString(type); + } + + QList list; + + if (!typeName && !meta) + return list; + + findChildren(parent, typeName, meta, name, list); + + return list; +} + +QList PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp) +{ + const QMetaObject* meta = NULL; + const char* typeName = NULL; + + if (PyObject_TypeCheck(type, &PythonQtClassWrapper_Type)) { + meta = ((PythonQtClassWrapper*)type)->classInfo()->metaObject(); + } else if (PyObject_TypeCheck(type, &PythonQtInstanceWrapper_Type)) { + meta = ((PythonQtInstanceWrapper*)type)->classInfo()->metaObject(); + } else if (PyString_Check(type)) { + typeName = PyString_AsString(type); + } + + QList list; + + if (!typeName && !meta) + return list; + + findChildren(parent, typeName, meta, regExp, list); + + return list; +} + +QObject* PythonQtStdDecorators::findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name) +{ + const QObjectList &children = parent->children(); + + int i; + for (i = 0; i < children.size(); ++i) { + QObject* obj = children.at(i); + + if (!obj) + return NULL; + + // Skip if the name doesn't match. + if (!name.isNull() && obj->objectName() != name) + continue; + + if ((typeName && obj->inherits(typeName)) || + (meta && meta->cast(obj))) + return obj; + } + + for (i = 0; i < children.size(); ++i) { + QObject* obj = findChild(children.at(i), typeName, meta, name); + + if (obj != NULL) + return obj; + } + + return NULL; +} + +int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name, QList& list) +{ + const QObjectList& children = parent->children(); + int i; + + for (i = 0; i < children.size(); ++i) { + QObject* obj = children.at(i); + + if (!obj) + return -1; + + // Skip if the name doesn't match. + if (!name.isNull() && obj->objectName() != name) + continue; + + if ((typeName && obj->inherits(typeName)) || + (meta && meta->cast(obj))) { + list += obj; + } + + if (findChildren(obj, typeName, meta, name, list) < 0) + return -1; + } + + return 0; +} + +int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList& list) +{ + const QObjectList& children = parent->children(); + int i; + + for (i = 0; i < children.size(); ++i) { + QObject* obj = children.at(i); + + if (!obj) + return -1; + + // Skip if the name doesn't match. + if (regExp.indexIn(obj->objectName()) == -1) + continue; + + if ((typeName && obj->inherits(typeName)) || + (meta && meta->cast(obj))) { + list += obj; + } + + if (findChildren(obj, typeName, meta, regExp, list) < 0) + return -1; + } + + return 0; +} diff --git a/src/PythonQtStdDecorators.h b/src/PythonQtStdDecorators.h index 7ca327b..781261c 100644 --- a/src/PythonQtStdDecorators.h +++ b/src/PythonQtStdDecorators.h @@ -71,6 +71,9 @@ public slots: void setParent(QObject* o, QObject* parent); const QObjectList* children(QObject* o); + QObject* findChild(QObject* parent, PyObject* type, const QString& name = QString()); + QList findChildren(QObject* parent, PyObject* type, const QString& name= QString()); + QList findChildren(QObject* parent, PyObject* type, const QRegExp& regExp); double static_Qt_qAbs(double a) { return qAbs(a); } double static_Qt_qBound(double a,double b,double c) { return qBound(a,b,c); } @@ -96,7 +99,10 @@ public slots: QByteArray static_Qt_SIGNAL(const QByteArray& s) { return QByteArray("2") + s; } QByteArray static_Qt_SLOT(const QByteArray& s) { return QByteArray("1") + s; } - //TODO: add findChild/findChildren/children/... +private: + QObject* findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name); + int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name, QList& list); + int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList& list); };