From 07f6b103b979f1e147d270458063c6436a9cfe93 Mon Sep 17 00:00:00 2001 From: Ajinkya Dahale Date: Fri, 18 Aug 2023 20:33:00 +0530 Subject: [PATCH] [App] Use keyword arguments when adding properties to Document --- src/App/DocumentPy.xml | 6 ++---- src/App/DocumentPyImp.cpp | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/App/DocumentPy.xml b/src/App/DocumentPy.xml index 5be462037f..3c9c1c08f8 100644 --- a/src/App/DocumentPy.xml +++ b/src/App/DocumentPy.xml @@ -104,12 +104,10 @@ attach (Boolean): if True, then bind the document object first before adding to viewType (String): override the view provider type directly, only effective when attach is False. - + - addProperty(string, string) -- Add a generic property. - The first argument specifies the type, the second the - name of the property. + addProperty(type: string, name: string, group="", doc="", attr=0, read_only=False, hidden=False) -- Add a generic property. diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index c5da0e6a3f..359833017c 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -40,14 +40,17 @@ using namespace App; -PyObject* DocumentPy::addProperty(PyObject *args) +PyObject* DocumentPy::addProperty(PyObject *args, PyObject *kwd) { char *sType,*sName=nullptr,*sGroup=nullptr,*sDoc=nullptr; short attr=0; std::string sDocStr; PyObject *ro = Py_False, *hd = Py_False; - if (!PyArg_ParseTuple(args, "s|ssethO!O!", &sType,&sName,&sGroup,"utf-8",&sDoc,&attr, - &PyBool_Type, &ro, &PyBool_Type, &hd)) + PyObject* enumVals = nullptr; + static char *kwlist[] = {"type","name","group","doc","attr","read_only","hidden","enum_vals",nullptr}; + if (!PyArg_ParseTupleAndKeywords( + args, kwd, "ss|sethO!O!O", kwlist, &sType, &sName, &sGroup, "utf-8", + &sDoc, &attr, &PyBool_Type, &ro, &PyBool_Type, &hd, &enumVals)) return nullptr; if (sDoc) { @@ -55,8 +58,21 @@ PyObject* DocumentPy::addProperty(PyObject *args) PyMem_Free(sDoc); } - getDocumentPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr, - Base::asBoolean(ro), Base::asBoolean(hd)); + Property *prop = getDocumentPtr()-> + addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr, + Base::asBoolean(ro), Base::asBoolean(hd)); + + // enum support + auto* propEnum = dynamic_cast(prop); + if (propEnum) { + if (PySequence_Check(enumVals)) { + std::vector enumValsAsVector; + for (Py_ssize_t i = 0; i < PySequence_Length(enumVals); ++i) { + enumValsAsVector.emplace_back(PyUnicode_AsUTF8(PySequence_GetItem(enumVals,i))); + } + propEnum->setEnums(enumValsAsVector); + } + } return Py::new_reference_to(this); }