From 36f776aab8fa12d78076a1cf38bcce8d64089de6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 24 Oct 2018 22:27:59 +0200 Subject: [PATCH] restore shiboken wrapper for Base::Quantity --- src/Gui/WidgetFactory.cpp | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/Gui/WidgetFactory.cpp b/src/Gui/WidgetFactory.cpp index b77333c502..85262a12a4 100644 --- a/src/Gui/WidgetFactory.cpp +++ b/src/Gui/WidgetFactory.cpp @@ -114,6 +114,91 @@ PyTypeObject** SbkPySide2_QtWidgetsTypes=NULL; using namespace Gui; +#if defined (HAVE_SHIBOKEN) + +/** + Example: + \code + ui = FreeCADGui.UiLoader() + w = ui.createWidget("Gui::InputField") + w.show() + w.property("quantity") + \endcode + */ + +PyObject* toPythonFuncQuantityTyped(Base::Quantity cpx) { + return new Base::QuantityPy(new Base::Quantity(cpx)); +} + +PyObject* toPythonFuncQuantity(const void* cpp) +{ + return toPythonFuncQuantityTyped(*reinterpret_cast(cpp)); +} + +void toCppPointerConvFuncQuantity(PyObject* pyobj,void* cpp) +{ + *((Base::Quantity*)cpp) = *static_cast(pyobj)->getQuantityPtr(); +} + +PythonToCppFunc toCppPointerCheckFuncQuantity(PyObject* obj) +{ + if (PyObject_TypeCheck(obj, &(Base::QuantityPy::Type))) + return toCppPointerConvFuncQuantity; + else + return 0; +} + +void BaseQuantity_PythonToCpp_QVariant(PyObject* pyIn, void* cppOut) +{ + Base::Quantity* q = static_cast(pyIn)->getQuantityPtr(); + *((QVariant*)cppOut) = QVariant::fromValue(*q); +} + +PythonToCppFunc isBaseQuantity_PythonToCpp_QVariantConvertible(PyObject* obj) +{ + if (PyObject_TypeCheck(obj, &(Base::QuantityPy::Type))) + return BaseQuantity_PythonToCpp_QVariant; + return 0; +} + +#if QT_VERSION >= 0x050200 +Base::Quantity convertWrapperToQuantity(const PySide::PyObjectWrapper &w) +{ + PyObject* pyIn = static_cast(w); + if (PyObject_TypeCheck(pyIn, &(Base::QuantityPy::Type))) { + return *static_cast(pyIn)->getQuantityPtr(); + } + + return Base::Quantity(std::numeric_limits::quiet_NaN()); +} +#endif + +void registerTypes() +{ + SbkConverter* convert = Shiboken::Conversions::createConverter(&Base::QuantityPy::Type, + toPythonFuncQuantity); + Shiboken::Conversions::setPythonToCppPointerFunctions(convert, + toCppPointerConvFuncQuantity, + toCppPointerCheckFuncQuantity); + Shiboken::Conversions::registerConverterName(convert, "Base::Quantity"); + + SbkConverter* qvariant_conv = Shiboken::Conversions::getConverter("QVariant"); + if (qvariant_conv) { + // The type QVariant already has a converter from PyBaseObject_Type which will + // come before our own converter. + Shiboken::Conversions::addPythonToCppValueConversion(qvariant_conv, + BaseQuantity_PythonToCpp_QVariant, + isBaseQuantity_PythonToCpp_QVariantConvertible); + } + +#if QT_VERSION >= 0x050200 + QMetaType::registerConverter(&convertWrapperToQuantity); +#endif +} +#endif + +// -------------------------------------------------------- + namespace Gui { template Py::Object qt_wrapInstance(qttype object, const char* className, @@ -173,6 +258,13 @@ void* qt_getCppPointer(const Py::Object& pyobject, const char* shiboken, const c PythonWrapper::PythonWrapper() { +#if defined (HAVE_SHIBOKEN) + static bool init = false; + if (!init) { + init = true; + registerTypes(); + } +#endif } bool PythonWrapper::toCString(const Py::Object& pyobject, std::string& str)