From 2a8fe51038cbc18a179c79fcb796fef21afa5e7a Mon Sep 17 00:00:00 2001 From: donovaly Date: Sun, 26 Jul 2020 17:07:41 +0200 Subject: [PATCH] [App/Gui] improve coding style inspired by the changes made with https://github.com/FreeCAD/FreeCAD/commit/dc65b055e514 --- src/App/DocumentPyImp.cpp | 71 +++++++++++++++++++++------------------ src/Gui/DocumentPyImp.cpp | 23 +++++++------ 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index ab13703045..6e991a8304 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -200,11 +200,12 @@ PyObject* DocumentPy::addObject(PyObject *args, PyObject *kwd) DocumentObject *pcFtr = 0; - if(!obj || !PyObject_IsTrue(attach)) { + if (!obj || !PyObject_IsTrue(attach)) { pcFtr = getDocumentPtr()->addObject(sType,sName,true,sViewType); - }else{ + } + else { Base::BaseClass* base = static_cast(Base::Type::createInstanceByName(sType,true)); - if(base) { + if (base) { if (!base->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) { delete base; std::stringstream str; @@ -227,16 +228,17 @@ PyObject* DocumentPy::addObject(PyObject *args, PyObject *kwd) } pyftr.setAttr("Proxy", pyobj); - if(PyObject_IsTrue(attach)) { + if (PyObject_IsTrue(attach)) { getDocumentPtr()->addObject(pcFtr,sName); try { Py::Callable method(pyobj.getAttr("attach")); - if(!method.isNone()) { + if (!method.isNone()) { Py::TupleN arg(pyftr); method.apply(arg); } - }catch (Py::Exception&) { + } + catch (Py::Exception&) { Base::PyException e; e.ReportException(); } @@ -277,10 +279,11 @@ PyObject* DocumentPy::removeObject(PyObject *args) DocumentObject *pcFtr = getDocumentPtr()->getObject(sName); - if(pcFtr) { + if (pcFtr) { getDocumentPtr()->removeObject( sName ); Py_Return; - } else { + } + else { std::stringstream str; str << "No document object found with name '" << sName << "'" << std::ends; throw Py::Exception(Base::BaseExceptionFreeCADError,str.str()); @@ -295,20 +298,22 @@ PyObject* DocumentPy::copyObject(PyObject *args) std::vector objs; bool single = false; - if(PySequence_Check(obj)) { + if (PySequence_Check(obj)) { Py::Sequence seq(obj); - for(size_t i=0;i(seq[i].ptr())->getDocumentObjectPtr()); } - }else if(!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) { + } + else if (!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) { PyErr_SetString(PyExc_TypeError, "Expect first argument to be either a document object or sequence of document objects"); return 0; - }else { + } + else { objs.push_back(static_cast(obj)->getDocumentObjectPtr()); single = true; } @@ -319,10 +324,10 @@ PyObject* DocumentPy::copyObject(PyObject *args) return ret[0]->getPyObject(); Py::Tuple tuple(ret.size()); - for(size_t i=0;igetPyObject(),true)); return Py::new_reference_to(tuple); - }PY_CATCH + } PY_CATCH } PyObject* DocumentPy::importLinks(PyObject *args) @@ -334,18 +339,18 @@ PyObject* DocumentPy::importLinks(PyObject *args) std::vector objs; if (PySequence_Check(obj)) { Py::Sequence seq(obj); - for(size_t i=0;i(seq[i].ptr())->getDocumentObjectPtr()); } } - else if(obj == Py_None) { + else if (obj == Py_None) { // do nothing here } - else if(!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) { + else if (!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) { PyErr_SetString(PyExc_TypeError, "Expect first argument to be either a document object or sequence of document objects"); return 0; @@ -354,17 +359,17 @@ PyObject* DocumentPy::importLinks(PyObject *args) objs.push_back(static_cast(obj)->getDocumentObjectPtr()); } - if(objs.empty()) + if (objs.empty()) objs = getDocumentPtr()->getObjects(); PY_TRY { auto ret = getDocumentPtr()->importLinks(objs); Py::Tuple tuple(ret.size()); - for(size_t i=0;igetPyObject(),true)); return Py::new_reference_to(tuple); - }PY_CATCH + } PY_CATCH } PyObject* DocumentPy::moveObject(PyObject *args) @@ -512,7 +517,7 @@ PyObject* DocumentPy::getObject(PyObject *args) long id = -1; char *sName = 0; if (!PyArg_ParseTuple(args, "s",&sName)) { // convert args: Python->C - if(!PyArg_ParseTuple(args, "l", &id)) + if (!PyArg_ParseTuple(args, "l", &id)) return NULL; // NULL triggers exception } @@ -579,7 +584,7 @@ PyObject* DocumentPy::findObjects(PyObject *args, PyObject *kwds) Py::Object DocumentPy::getActiveObject(void) const { DocumentObject *pcFtr = getDocumentPtr()->getActiveObject(); - if(pcFtr) + if (pcFtr) return Py::Object(pcFtr->getPyObject(), true); return Py::None(); } @@ -799,8 +804,8 @@ PyObject* DocumentPy::getLinksTo(PyObject *args) PY_TRY { DocumentObject *obj = 0; - if(pyobj!=Py_None) { - if(!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) { + if (pyobj!=Py_None) { + if (!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) { PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object"); return 0; } @@ -810,18 +815,18 @@ PyObject* DocumentPy::getLinksTo(PyObject *args) getDocumentPtr()->getLinksTo(links,obj,options,count); Py::Tuple ret(links.size()); int i=0; - for(auto o : links) + for (auto o : links) ret.setItem(i++,Py::Object(o->getPyObject(),true)); return Py::new_reference_to(ret); - }PY_CATCH + } PY_CATCH } Py::List DocumentPy::getInList(void) const { Py::List ret; auto lists = PropertyXLink::getDocumentInList(getDocumentPtr()); - if(lists.size()==1) { - for(auto doc : lists.begin()->second) + if (lists.size()==1) { + for (auto doc : lists.begin()->second) ret.append(Py::Object(doc->getPyObject(), true)); } return ret; @@ -831,8 +836,8 @@ Py::List DocumentPy::getOutList(void) const { Py::List ret; auto lists = PropertyXLink::getDocumentOutList(getDocumentPtr()); - if(lists.size()==1) { - for(auto doc : lists.begin()->second) + if (lists.size()==1) { + for (auto doc : lists.begin()->second) ret.append(Py::Object(doc->getPyObject(), true)); } return ret; @@ -845,7 +850,7 @@ PyObject *DocumentPy::getDependentDocuments(PyObject *args) { PY_TRY { auto docs = getDocumentPtr()->getDependentDocuments(PyObject_IsTrue(sort)); Py::List ret; - for(auto doc : docs) + for (auto doc : docs) ret.append(Py::Object(doc->getPyObject(), true)); return Py::new_reference_to(ret); } PY_CATCH; diff --git a/src/Gui/DocumentPyImp.cpp b/src/Gui/DocumentPyImp.cpp index b6dd1cff23..4d638a2df5 100644 --- a/src/Gui/DocumentPyImp.cpp +++ b/src/Gui/DocumentPyImp.cpp @@ -116,15 +116,16 @@ PyObject* DocumentPy::setEdit(PyObject *args) PyErr_Format(Base::BaseExceptionFreeCADError, "No such object found in document: '%s'", psFeatStr); return 0; } - }else{ + } + else { PyErr_Clear(); PyObject *pyObj; - if(!PyArg_ParseTuple(args, "O|is", &pyObj,&mod,&subname)) + if (!PyArg_ParseTuple(args, "O|is", &pyObj,&mod,&subname)) return 0; - if(PyObject_TypeCheck(pyObj,&App::DocumentObjectPy::Type)) + if (PyObject_TypeCheck(pyObj,&App::DocumentObjectPy::Type)) obj = static_cast(pyObj)->getDocumentObjectPtr(); - else if(PyObject_TypeCheck(pyObj,&ViewProviderPy::Type)) + else if (PyObject_TypeCheck(pyObj,&ViewProviderPy::Type)) vp = static_cast(pyObj)->getViewProviderPtr(); else { PyErr_SetString(PyExc_TypeError,"Expect the first argument to be string|DocObject|ViewObject"); @@ -132,8 +133,8 @@ PyObject* DocumentPy::setEdit(PyObject *args) } } - if(!vp) { - if(!obj || !obj->getNameInDocument() || !(vp=Application::Instance->getViewProvider(obj))) { + if (!vp) { + if (!obj || !obj->getNameInDocument() || !(vp=Application::Instance->getViewProvider(obj))) { PyErr_SetString(PyExc_ValueError,"Invalid document object"); return 0; } @@ -303,9 +304,9 @@ PyObject* DocumentPy::toggleTreeItem(PyObject *args) assert(Object); App::DocumentObject *parent = 0; - if(subname) { + if (subname) { auto sobj = Object->getSubObject(subname); - if(!sobj) + if (!sobj) throw Py::RuntimeError("Sub-object not found"); parent = Object; Object = sobj; @@ -316,7 +317,7 @@ PyObject* DocumentPy::toggleTreeItem(PyObject *args) //ActiveGuiDoc = Gui::Application::Instance->getDocument(getDocumentPtr()); Gui::ViewProviderDocumentObject* ActiveVp = dynamic_cast (getDocumentPtr()->getViewProvider(Object)); assert(ActiveVp); - switch(mod) { + switch (mod) { case 0: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ToggleItem, parent, subname); break; case 1: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::CollapseItem, parent, subname); break; case 2: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandItem, parent, subname); break; @@ -399,7 +400,7 @@ Py::Object DocumentPy::getEditingTransform(void) const { } void DocumentPy::setEditingTransform(Py::Object arg) { - if(!PyObject_TypeCheck(arg.ptr(),&Base::MatrixPy::Type)) + if (!PyObject_TypeCheck(arg.ptr(),&Base::MatrixPy::Type)) throw Py::TypeError("Expecting type of matrix"); getDocumentPtr()->setEditingTransform( *static_cast(arg.ptr())->getMatrixPtr()); @@ -410,7 +411,7 @@ Py::Object DocumentPy::getInEditInfo(void) const { std::string subname,subelement; int mode = 0; getDocumentPtr()->getInEdit(&vp,&subname,&mode,&subelement); - if(!vp || !vp->getObject() || !vp->getObject()->getNameInDocument()) + if (!vp || !vp->getObject() || !vp->getObject()->getNameInDocument()) return Py::None(); return Py::TupleN(Py::Object(vp->getObject()->getPyObject(),true), Py::String(subname),Py::String(subelement),Py::Int(mode));