Skip to content

Commit

Permalink
Gui: PythonWrapper: Consolidate typeName handling
Browse files Browse the repository at this point in the history
Use typeName consistently for both PySide and Python interface
code paths.
  • Loading branch information
3x380V committed Mar 2, 2024
1 parent 28139ad commit 5f75a67
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions src/Gui/PythonWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ QImage *PythonWrapper::toQImage(PyObject *pyobj)
Py::Object PythonWrapper::fromQIcon(const QIcon* icon)
{
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
const char* typeName = typeid(*const_cast<QIcon*>(icon)).name();
auto type = getPyTypeObjectForTypeName<QIcon>();
if (type) {
const char* typeName = typeid(*const_cast<QIcon*>(icon)).name();
PyObject* pyobj = Shiboken::Object::newObject(type, const_cast<QIcon*>(icon), true, false, typeName);
return Py::asObject(pyobj);
}
Expand All @@ -634,14 +634,18 @@ QIcon *PythonWrapper::toQIcon(PyObject *pyobj)
Py::Object PythonWrapper::fromQDir(const QDir& dir)
{
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
const char* typeName = typeid(dir).name();
auto type = getPyTypeObjectForTypeName<QDir>();
if (type) {
const char* typeName = typeid(dir).name();
PyObject* pyobj = Shiboken::Object::newObject(type, const_cast<QDir*>(&dir), false, false, typeName);
return Py::asObject(pyobj);
}
#else
Q_UNUSED(dir)
// Access shiboken/PySide via Python
Py::Object obj = qt_wrapInstance<const QDir*>(&dir, "QDir", "QtGui");
if (!obj.isNull()) {
return obj;
}
#endif
throw Py::RuntimeError("Failed to wrap directory");
}
Expand Down Expand Up @@ -706,32 +710,23 @@ Py::Object PythonWrapper::fromQObject(QObject* object, const char* className)
if (!object) {
return Py::None();
}
const char* typeName;
if (className) {
typeName = className;
}
else {
typeName = object->metaObject()->className();
}
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
// Access shiboken/PySide via C++
auto type = getPyTypeObjectForTypeName<QObject>();
if (type) {
std::string typeName;
if (className) {
typeName = className;
}
else {
typeName = object->metaObject()->className();
}

PyObject* pyobj = Shiboken::Object::newObject(type, object, false, false, typeName.c_str());
PyObject* pyobj = Shiboken::Object::newObject(type, object, false, false, typeName);
WrapperManager::instance().addQObject(object, pyobj);
return Py::asObject(pyobj);
}
#else
// Access shiboken/PySide via Python
std::string typeName;
if (className) {
typeName = className;
}
else {
typeName = object->metaObject()->className();
}

Py::Object obj = qt_wrapInstance<QObject*>(object, typeName, "QtCore");
if (!obj.isNull()) {
return obj;
Expand All @@ -742,32 +737,23 @@ Py::Object PythonWrapper::fromQObject(QObject* object, const char* className)

Py::Object PythonWrapper::fromQWidget(QWidget* widget, const char* className)
{
const char* typeName;
if (className) {
typeName = className;
}
else {
typeName = widget->metaObject()->className();
}
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
// Access shiboken/PySide via C++
auto type = getPyTypeObjectForTypeName<QWidget>();
if (type) {
std::string typeName;
if (className) {
typeName = className;
}
else {
typeName = widget->metaObject()->className();
}

PyObject* pyobj = Shiboken::Object::newObject(type, widget, false, false, typeName.c_str());
PyObject* pyobj = Shiboken::Object::newObject(type, widget, false, false, typeName);
WrapperManager::instance().addQObject(widget, pyobj);
return Py::asObject(pyobj);
}
#else
// Access shiboken/PySide via Python
std::string typeName;
if (className) {
typeName = className;
}
else {
typeName = widget->metaObject()->className();
}

Py::Object obj = qt_wrapInstance<QWidget*>(widget, typeName, "QtWidgets");
if (!obj.isNull()) {
return obj;
Expand Down

0 comments on commit 5f75a67

Please sign in to comment.