Commit 57972fb6 authored by Jan Möbius's avatar Jan Möbius

Added Mapping for UpdateType to Python

parent dca90a1f
......@@ -613,6 +613,10 @@ PYBIND11_EMBEDDED_MODULE(openflipper, m) {
core.def("exitFailure", &Core::exitFailure, QCoreApplication::translate("PythonDocCore","Use this function in unit tests, if you detected a failure. Therefore the test functions will recognize that something went wrong.").toLatin1().data() );
core.def("finishSplash", &Core::finishSplash, QCoreApplication::translate("PythonDocCore","Hide the splash screen").toLatin1().data() );
core.def("objectUpdated", &Core::slotObjectUpdated, QCoreApplication::translate("PythonDocCore","Tell the core that an object has been updated").toLatin1().data(),
py::arg( QCoreApplication::translate("PythonDocCore","ID of the updated object").toLatin1().data() ),
py::arg( QCoreApplication::translate("PythonDocCore","What has been updated? String list separated by ; . Possible update types include: All,Visibility,Geometry,Topology,Selection,VertexSelection,EdgeSelection,HalfedgeSelection,FaceSelection,KnotSelection,Color,Texture,State ").toLatin1().data() ) = UPDATE_ALL );
// //load slots
// emit setSlotDescription("createWidget(QString,QString)", tr("Create a widget from an ui file"),
......
......@@ -41,7 +41,7 @@
#include <pybind11/include/pybind11/pybind11.h>
#include <pybind11/include/pybind11/numpy.h>
#include <OpenFlipper/common/UpdateType.hh>
#include <QString>
namespace py = pybind11;
......@@ -467,3 +467,106 @@ namespace pybind11 { namespace detail {
};
}} // namespace pybind11::detail
/** \page python_scripting_UpdateType_type UpdateType data type used for python scripting
*
* The list type UpdateType is mapped to python.
*
* The integrated conversion can map the C++ UpdateType type to and from a String.
* The string has to be a list separated by ';' if it should be a combination of update types.
*
* \code
* core.objectUpdated(5,"Selection;Geometry")
* \endcode
*
* Update types implemented in Core are:
* - All
* - Visibility
* - Geometry
* - Topology
* - Selection
* - VertexSelection
* - EdgeSelection
* - HalfedgeSelection
* - FaceSelection
* - KnotSelection
* - Color
* - Texture
* - State
*
* Other plugins might have implemented additional update types, which will be automatically mapped.
*/
namespace pybind11 { namespace detail {
template <> struct type_caster<UpdateType> {
public:
/**
* This macro establishes the name 'UpdateType' in
* function signatures and declares a local variable
* 'value' of type UpdateType
*/
PYBIND11_TYPE_CASTER(UpdateType, _("UpdateType"));
/**
* Conversion part 1 (Python->C++): convert a PyObject into a UpdateType
* instance or return false upon failure. The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool ) {
/* Extract PyObject from handle */
PyObject *source = src.ptr();
if (!PyUnicode_Check(source))
return false;
Py_ssize_t size;
const char *ptr = PyUnicode_AsUTF8AndSize(source, &size);
if (!ptr) {
return NULL;
}
QString updateString = QString::fromUtf8(ptr, size);
if ( updateString.contains("UPDATE_ALL") ) {
std::cerr << "Update_ALL" << std::endl;
value = UPDATE_ALL;
return true;
}
if ( updateString.contains("All") ) {
std::cerr << "Update_ALL" << std::endl;
value = UPDATE_ALL;
return true;
}
UpdateType type;
QStringList updateList = updateString.split(";");
for ( auto i = 0 ; i < updateList.size() ; ++i ) {
type |= updateType(updateList[i]);
std::cerr << "Update " << updateList[i].toStdString() << std::endl;
}
value = type;
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return ( !PyErr_Occurred() );
}
/**
* Conversion part 2 (C++ -> Python): convert an UpdateType instance into
* a Python object. The second and third arguments are used to
* indicate the return value policy and parent object (for
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(UpdateType src, return_value_policy /* policy */, handle /* parent */) {
return (PyUnicode_FromString( updateTypeName(src).toUtf8().data()) );
}
};
}} // namespace pybind11::detail
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment