[Core] Use keyword arguments in DocumentObjectPy addProperty

This commit is contained in:
Ajinkya Dahale
2023-08-21 22:45:45 +05:30
committed by Yorik van Havre
parent c95a99cd6f
commit 49dfaaefa0
2 changed files with 23 additions and 9 deletions

View File

@@ -13,12 +13,10 @@
<Author Licence="LGPL" Name="Juergen Riegel" EMail="FreeCAD@juergen-riegel.net" />
<UserDocu>This is the father of all classes handled by the document</UserDocu>
</Documentation>
<Methode Name="addProperty">
<Methode Name="addProperty" Keyword="true">
<Documentation>
<UserDocu>
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, enum_vals=[]) -- Add a generic property.
</UserDocu>
</Documentation>
</Methode>

View File

@@ -76,14 +76,17 @@ Py::Object DocumentObjectPy::getDocument() const
}
}
PyObject* DocumentObjectPy::addProperty(PyObject *args)
PyObject* DocumentObjectPy::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;
const std::array<const char *, 9> kwlist {"type","name","group","doc","attr","read_only","hidden","enum_vals",nullptr};
if (!Base::Wrapped_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) {
@@ -91,8 +94,21 @@ PyObject* DocumentObjectPy::addProperty(PyObject *args)
PyMem_Free(sDoc);
}
getDocumentObjectPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr,
Base::asBoolean(ro), Base::asBoolean(hd));
Property *prop = getDocumentObjectPtr()->
addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr,
Base::asBoolean(ro), Base::asBoolean(hd));
// enum support
auto* propEnum = dynamic_cast<App::PropertyEnumeration*>(prop);
if (propEnum) {
if (enumVals && PySequence_Check(enumVals)) {
std::vector<std::string> 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);
}