Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void PySideAutoQmlBridgePrivate::registerProperties()
- {
- Shiboken::AutoDecRef klass(PyObject_Type(m_backend));
- if (!klass) {
- PyErr_Clear();
- return;
- }
- // fetch all the class attributes
- Shiboken::AutoDecRef dirList(PyObject_Dir(klass.object()));
- if (!dirList) {
- PyErr_Clear();
- return;
- }
- const Py_ssize_t count = PyList_Size(dirList.object());
- // find all the properties
- for (Py_ssize_t i = 0; i < count; ++i) {
- PyObject *nameObj = PyList_GetItem(dirList.object(), i);
- if (!PyUnicode_Check(nameObj))
- continue;
- QByteArray attrName = Shiboken::String::toCString(nameObj);
- Shiboken::AutoDecRef classDescriptor(PyObject_GetAttrString(klass.object(), attrName.constData()));
- if (!classDescriptor)
- continue;
- // create a PySideProperty for the property and assign its getter, setter and notify signalb
- // fields
- if (PyObject_TypeCheck(classDescriptor.object(), &PyProperty_Type)) {
- Shiboken::AutoDecRef kwds(PyDict_New());
- if (!kwds) {
- PyErr_Print();
- continue;
- }
- Shiboken::AutoDecRef getter(PyObject_GetAttrString(classDescriptor.object(), "fget"));
- Shiboken::AutoDecRef setter(PyObject_GetAttrString(classDescriptor.object(), "fset"));
- // Create the bound method by binding getter/setter to the instance
- if (!getter.isNull()) {
- Shiboken::AutoDecRef boundGetter(PyObject_CallMethod(
- reinterpret_cast<PyObject *>(getter.object()), "__get__", "OO", m_backend, Py_None));
- PyDict_SetItemString(kwds.object(), "fget", boundGetter.object());
- }
- // only add setter if it is implemented
- if (!setter.isNull() && setter.object() != Py_None) {
- Shiboken::AutoDecRef boundSetter(PyObject_CallMethod(
- reinterpret_cast<PyObject *>(setter.object()), "__get__", "OO", m_backend, Py_None));
- PyDict_SetItemString(kwds.object(), "fset", boundSetter.object());
- }
- const char* type_name = "QVariant";
- Shiboken::AutoDecRef pyPropertyType(PyUnicode_FromString(type_name));
- PyDict_SetItemString(kwds.object(), "type", pyPropertyType.object());
- // TODO: Handle type annotations conversion from Python type to Qt type
- // if (!getter.isNull()) {
- // Shiboken::AutoDecRef annotations(PyObject_GetAttrString(getter.object(),
- // "__annotations__"));
- // if (!annotations.isNull()) {
- // Shiboken::AutoDecRef return_type(PyDict_GetItemString(annotations.object(),
- // "return"));
- // if (!return_type.isNull() && PyType_Check(return_type.object())) {
- // type_name = ((PyTypeObject*)return_type.object())->tp_name;
- // Shiboken::AutoDecRef pyPropertyType(PyUnicode_FromString(type_name));
- // PyDict_SetItemString(kwds.object(), "type", pyPropertyType.object());
- // }
- // }
- // }
- QByteArray signalName = attrName + "Changed";
- int signalId = m_metaObjectBuilder->addSignal(signalName + "()");
- if (signalId >= 0) {
- QMetaMethod signal = m_metaObjectBuilder->update()->method(signalId);
- const char *signalSignature = signal.methodSignature().constData();
- Shiboken::AutoDecRef notifySignal(PyUnicode_FromString(signalSignature));
- PyDict_SetItemString(kwds.object(), "notify", notifySignal.object());
- }
- PyObject *args = PyTuple_New(0);
- Shiboken::AutoDecRef pysidePropObj(
- PyObject_Call(reinterpret_cast<PyObject *>(PySideProperty_TypeF()), args, kwds.object()));
- Py_DECREF(args);
- if (!pysidePropObj) {
- if (PyErr_Occurred()) {
- PyErr_Print();
- }
- qDebug() << "Failed to create PySideProperty for" << attrName;
- PyErr_Clear();
- continue;
- }
- // Get the property
- auto *property = reinterpret_cast<PySideProperty *>(pysidePropObj.object());
- Py_INCREF(property); // Increase ref count since we're storing it
- property->d->typeName = type_name;
- if (!getter.isNull()) {
- property->d->fget = getter.object();
- Py_INCREF(getter.object()); // Increase ref count for getter
- }
- if (!setter.isNull()) {
- property->d->fset = setter.object();
- Py_INCREF(setter.object()); // Increase ref count for setter
- }
- // Initialize the private data
- if (!property || !property->d) {
- qDebug() << "Invalid PySideProperty or uninitialized private data for" << attrName;
- Py_DECREF(property);
- continue;
- }
- int propertyIndex = m_metaObjectBuilder->addProperty(attrName.constData(),
- pysidePropObj.object());
- m_model->addProperty(propertyIndex, property);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement