From ca3c8185e04edc55b1dd561dbf6080e82e2d8469 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 4 Mar 2022 18:54:42 +0100 Subject: [PATCH] App/Gui: move template classes (ViewProvider)ExtensionPythonT to their own header files --- src/App/Extension.cpp | 1 + src/App/Extension.h | 94 +------------- src/App/ExtensionPython.h | 118 ++++++++++++++++++ src/App/GroupExtension.h | 5 +- src/Gui/ViewProviderExtension.cpp | 1 + src/Gui/ViewProviderExtension.h | 25 +--- src/Gui/ViewProviderExtensionPython.h | 56 +++++++++ src/Gui/ViewProviderGroupExtension.h | 2 +- src/Mod/Part/App/AttachExtension.h | 1 + .../Part/Gui/ViewProviderAttachExtension.h | 2 +- src/Mod/Part/Gui/ViewProviderSpline.h | 2 +- .../PartDesign/Gui/TaskChamferParameters.cpp | 1 + .../PartDesign/Gui/TaskDraftParameters.cpp | 1 + .../PartDesign/Gui/TaskFilletParameters.cpp | 1 + .../Gui/TaskThicknessParameters.cpp | 1 + src/Mod/TechDraw/App/CosmeticExtension.h | 1 + .../Gui/ViewProviderCosmeticExtension.h | 2 +- 17 files changed, 195 insertions(+), 119 deletions(-) create mode 100644 src/App/ExtensionPython.h create mode 100644 src/Gui/ViewProviderExtensionPython.h diff --git a/src/App/Extension.cpp b/src/App/Extension.cpp index 3f5c623c99..ba30561c95 100644 --- a/src/App/Extension.cpp +++ b/src/App/Extension.cpp @@ -31,6 +31,7 @@ #include "Extension.h" #include "ExtensionContainer.h" +#include "ExtensionPython.h" #include diff --git a/src/App/Extension.h b/src/App/Extension.h index b4c54a8874..329b43d5ab 100644 --- a/src/App/Extension.h +++ b/src/App/Extension.h @@ -24,11 +24,8 @@ #ifndef APP_EXTENSION_H #define APP_EXTENSION_H -#include - #include "PropertyContainer.h" -#include "PropertyPythonObject.h" - +#include namespace App { @@ -244,7 +241,7 @@ public: bool isPythonExtension() {return m_isPythonExtension;} - virtual PyObject* getExtensionPyObject(void); + virtual PyObject* getExtensionPyObject(); /** @name Access properties */ @@ -283,7 +280,7 @@ public: bool extensionIsDerivedFrom(const Base::Type type) const {return getExtensionTypeId().isDerivedFrom(type);} protected: static void initExtensionSubclass(Base::Type &toInit,const char* ClassName, const char *ParentName, - Base::Type::instantiationMethod method=0); + Base::Type::instantiationMethod method=nullptr); //@} virtual void extensionOnChanged(const Property* p) {(void)(p);} @@ -293,7 +290,7 @@ protected: protected: void initExtensionType(Base::Type type); bool m_isPythonExtension = false; - Py::Object ExtensionPythonObject; + Py::SmartPtr ExtensionPythonObject; private: Base::Type m_extensionType; @@ -321,89 +318,6 @@ private: _EXTENSION_ADD_PROPERTY_TYPE(#_prop_, _prop_, _defaultval_, _group_,_type_,_Docu_) -/** - * Generic Python extension class which allows every extension derived - * class to behave as a Python extension -- simply by subclassing. - */ -template -class ExtensionPythonT : public ExtensionT -{ - EXTENSION_PROPERTY_HEADER(App::ExtensionPythonT); - -public: - typedef ExtensionT Inherited; - - ExtensionPythonT() { - ExtensionT::m_isPythonExtension = true; - ExtensionT::initExtensionType(ExtensionPythonT::getExtensionClassTypeId()); - } - virtual ~ExtensionPythonT() { - } -}; - -typedef ExtensionPythonT ExtensionPython; - -// Helper macros to define python extensions -#define EXTENSION_PROXY_FIRST(function) \ - Base::PyGILStateLocker lock;\ - Py::Object result;\ - try {\ - Property* proxy = this->getExtendedContainer()->getPropertyByName("Proxy");\ - if (proxy && proxy->getTypeId() == PropertyPythonObject::getClassTypeId()) {\ - Py::Object feature = static_cast(proxy)->getValue();\ - if (feature.hasAttr(std::string("function"))) {\ - if (feature.hasAttr("__object__")) {\ - Py::Callable method(feature.getAttr(std::string("function"))); - - - - -#define EXTENSION_PROXY_SECOND(function)\ - result = method.apply(args);\ - }\ - else {\ - Py::Callable method(feature.getAttr(std::string("function"))); - -#define EXTENSION_PROXY_THIRD()\ - result = method.apply(args);\ - }\ - }\ - }\ - }\ - catch (Py::Exception&) {\ - Base::PyException e;\ - e.ReportException();\ - } - -#define EXTENSION_PROXY_NOARG(function)\ - EXTENSION_PROXY_FIRST(function) \ - Py::Tuple args;\ - EXTENSION_PROXY_SECOND(function) \ - Py::Tuple args(1);\ - args.setItem(0, Py::Object(this->getExtensionPyObject(), true));\ - EXTENSION_PROXY_THIRD() - -#define EXTENSION_PROXY_ONEARG(function, arg)\ - EXTENSION_PROXY_FIRST(function) \ - Py::Tuple args;\ - args.setItem(0, arg); \ - EXTENSION_PROXY_SECOND(function) \ - Py::Tuple args(2);\ - args.setItem(0, Py::Object(this->getExtensionPyObject(), true));\ - args.setItem(1, arg); \ - EXTENSION_PROXY_THIRD() - -#define EXTENSION_PYTHON_OVERRIDE_VOID_NOARGS(function)\ - virtual void function() override {\ - EXTENSION_PROXY_NOARGS(function)\ - }; - -#define EXTENSION_PYTHON_OVERRIDE_OBJECT_NOARGS(function)\ - virtual PyObject* function() override {\ - EXTENSION_PROXY_NOARGS(function)\ - return res.ptr();\ - }; - } //App #endif // APP_EXTENSION_H diff --git a/src/App/ExtensionPython.h b/src/App/ExtensionPython.h new file mode 100644 index 0000000000..ac6920a885 --- /dev/null +++ b/src/App/ExtensionPython.h @@ -0,0 +1,118 @@ +/*************************************************************************** + * Copyright (c) 2016 Stefan Tröger * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef APP_EXTENSIONPYTHON_H +#define APP_EXTENSIONPYTHON_H + +#include +#include "Extension.h" +#include "PropertyPythonObject.h" + +namespace App { + +/** + * Generic Python extension class which allows every extension derived + * class to behave as a Python extension -- simply by subclassing. + */ +template +class ExtensionPythonT : public ExtensionT +{ + EXTENSION_PROPERTY_HEADER(App::ExtensionPythonT); + +public: + typedef ExtensionT Inherited; + + ExtensionPythonT() { + ExtensionT::m_isPythonExtension = true; + ExtensionT::initExtensionType(ExtensionPythonT::getExtensionClassTypeId()); + } + virtual ~ExtensionPythonT() { + } +}; + +typedef ExtensionPythonT ExtensionPython; + +// Helper macros to define python extensions +#define EXTENSION_PROXY_FIRST(function) \ + Base::PyGILStateLocker lock;\ + Py::Object result;\ + try {\ + Property* proxy = this->getExtendedContainer()->getPropertyByName("Proxy");\ + if (proxy && proxy->getTypeId() == PropertyPythonObject::getClassTypeId()) {\ + Py::Object feature = static_cast(proxy)->getValue();\ + if (feature.hasAttr(std::string("function"))) {\ + if (feature.hasAttr("__object__")) {\ + Py::Callable method(feature.getAttr(std::string("function"))); + + + + +#define EXTENSION_PROXY_SECOND(function)\ + result = method.apply(args);\ + }\ + else {\ + Py::Callable method(feature.getAttr(std::string("function"))); + +#define EXTENSION_PROXY_THIRD()\ + result = method.apply(args);\ + }\ + }\ + }\ + }\ + catch (Py::Exception&) {\ + Base::PyException e;\ + e.ReportException();\ + } + +#define EXTENSION_PROXY_NOARG(function)\ + EXTENSION_PROXY_FIRST(function) \ + Py::Tuple args;\ + EXTENSION_PROXY_SECOND(function) \ + Py::Tuple args(1);\ + args.setItem(0, Py::Object(this->getExtensionPyObject(), true));\ + EXTENSION_PROXY_THIRD() + +#define EXTENSION_PROXY_ONEARG(function, arg)\ + EXTENSION_PROXY_FIRST(function) \ + Py::Tuple args;\ + args.setItem(0, arg); \ + EXTENSION_PROXY_SECOND(function) \ + Py::Tuple args(2);\ + args.setItem(0, Py::Object(this->getExtensionPyObject(), true));\ + args.setItem(1, arg); \ + EXTENSION_PROXY_THIRD() + +#define EXTENSION_PYTHON_OVERRIDE_VOID_NOARGS(function)\ + virtual void function() override {\ + EXTENSION_PROXY_NOARGS(function)\ + }; + +#define EXTENSION_PYTHON_OVERRIDE_OBJECT_NOARGS(function)\ + virtual PyObject* function() override {\ + EXTENSION_PROXY_NOARGS(function)\ + return res.ptr();\ + }; + +} //App + +#endif // APP_EXTENSIONPYTHON_H diff --git a/src/App/GroupExtension.h b/src/App/GroupExtension.h index 38750b54fd..47a301cce9 100644 --- a/src/App/GroupExtension.h +++ b/src/App/GroupExtension.h @@ -24,8 +24,9 @@ #ifndef APP_GROUPEXTENSION_H #define APP_GROUPEXTENSION_H -#include "DocumentObject.h" -#include "DocumentObjectExtension.h" +#include +#include +#include #include diff --git a/src/Gui/ViewProviderExtension.cpp b/src/Gui/ViewProviderExtension.cpp index 047b3cedca..f7e9f5e2ab 100644 --- a/src/Gui/ViewProviderExtension.cpp +++ b/src/Gui/ViewProviderExtension.cpp @@ -30,6 +30,7 @@ #include "ViewProviderExtension.h" #include "ViewProviderExtensionPy.h" +#include "ViewProviderExtensionPython.h" using namespace Gui; diff --git a/src/Gui/ViewProviderExtension.h b/src/Gui/ViewProviderExtension.h index 27af83db83..3dd22a83ea 100644 --- a/src/Gui/ViewProviderExtension.h +++ b/src/Gui/ViewProviderExtension.h @@ -24,7 +24,8 @@ #ifndef GUI_VIEWPROVIDEREXTENSION_H #define GUI_VIEWPROVIDEREXTENSION_H -#include "App/Extension.h" +#include +#include #include "ViewProvider.h" #include "ViewProviderDocumentObject.h" @@ -115,28 +116,6 @@ private: //Gui::ViewProviderDocumentObject* m_viewBase = nullptr; }; -/** - * Generic Python extension class which allows to behave every extension - * derived class as Python extension -- simply by subclassing. - */ -template -class ViewProviderExtensionPythonT : public ExtensionT -{ - EXTENSION_PROPERTY_HEADER(Gui::ViewProviderExtensionPythonT); - -public: - typedef ExtensionT Inherited; - - ViewProviderExtensionPythonT() { - ExtensionT::m_isPythonExtension = true; - ExtensionT::initExtensionType(ViewProviderExtensionPythonT::getExtensionClassTypeId()); - } - virtual ~ViewProviderExtensionPythonT() { - } -}; - -typedef ViewProviderExtensionPythonT ViewProviderExtensionPython; - } //Gui #endif // GUI_VIEWPROVIDEREXTENSION_H diff --git a/src/Gui/ViewProviderExtensionPython.h b/src/Gui/ViewProviderExtensionPython.h new file mode 100644 index 0000000000..86f5b37902 --- /dev/null +++ b/src/Gui/ViewProviderExtensionPython.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (c) 2016 Stefan Tröger * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef GUI_VIEWPROVIDEREXTENSIONPYTHON_H +#define GUI_VIEWPROVIDEREXTENSIONPYTHON_H + +#include "ViewProviderExtension.h" +#include + +namespace Gui { + +/** + * Generic Python extension class which allows to behave every extension + * derived class as Python extension -- simply by subclassing. + */ +template +class ViewProviderExtensionPythonT : public ExtensionT +{ + EXTENSION_PROPERTY_HEADER(Gui::ViewProviderExtensionPythonT); + +public: + typedef ExtensionT Inherited; + + ViewProviderExtensionPythonT() { + ExtensionT::m_isPythonExtension = true; + ExtensionT::initExtensionType(ViewProviderExtensionPythonT::getExtensionClassTypeId()); + } + virtual ~ViewProviderExtensionPythonT() { + } +}; + +typedef ViewProviderExtensionPythonT ViewProviderExtensionPython; + +} //Gui + +#endif // GUI_VIEWPROVIDEREXTENSIONPYTHON_H diff --git a/src/Gui/ViewProviderGroupExtension.h b/src/Gui/ViewProviderGroupExtension.h index 7c0a026252..e2d1e385f6 100644 --- a/src/Gui/ViewProviderGroupExtension.h +++ b/src/Gui/ViewProviderGroupExtension.h @@ -25,7 +25,7 @@ #define GUI_VIEWPROVIDERGROUPEXTENSION_H #include -#include "ViewProviderExtension.h" +#include "ViewProviderExtensionPython.h" namespace Gui { diff --git a/src/Mod/Part/App/AttachExtension.h b/src/Mod/Part/App/AttachExtension.h index 6ab0ea6157..c0762160b1 100644 --- a/src/Mod/Part/App/AttachExtension.h +++ b/src/Mod/Part/App/AttachExtension.h @@ -27,6 +27,7 @@ #ifndef PARTATTACHABLEOBJECT_H #define PARTATTACHABLEOBJECT_H +#include #include #include #include diff --git a/src/Mod/Part/Gui/ViewProviderAttachExtension.h b/src/Mod/Part/Gui/ViewProviderAttachExtension.h index 281f9109db..fb19a7e5b6 100644 --- a/src/Mod/Part/Gui/ViewProviderAttachExtension.h +++ b/src/Mod/Part/Gui/ViewProviderAttachExtension.h @@ -25,7 +25,7 @@ #define GUI_VIEWPROVIDERATTACHEXTENSION_H #include -#include +#include namespace PartGui { diff --git a/src/Mod/Part/Gui/ViewProviderSpline.h b/src/Mod/Part/Gui/ViewProviderSpline.h index 2786e9fe01..1157782a70 100644 --- a/src/Mod/Part/Gui/ViewProviderSpline.h +++ b/src/Mod/Part/Gui/ViewProviderSpline.h @@ -24,7 +24,7 @@ #define PARTGUI_VIEWPROVIDERPARTSPLINE_H #include -#include +#include namespace PartGui { diff --git a/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp b/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp index 0fec7888ff..b9fac71562 100644 --- a/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp @@ -30,6 +30,7 @@ # include #endif +#include #include #include #include diff --git a/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp b/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp index 912019d261..aa635c7775 100644 --- a/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp @@ -31,6 +31,7 @@ # include #endif +#include #include #include #include diff --git a/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp b/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp index ad936d5637..1a7344dd7b 100644 --- a/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp @@ -29,6 +29,7 @@ # include #endif +#include #include #include #include diff --git a/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp b/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp index ea0bdec706..37b3ba2a74 100644 --- a/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp @@ -29,6 +29,7 @@ # include #endif +#include #include #include #include diff --git a/src/Mod/TechDraw/App/CosmeticExtension.h b/src/Mod/TechDraw/App/CosmeticExtension.h index e2e25f4247..d31535eae4 100644 --- a/src/Mod/TechDraw/App/CosmeticExtension.h +++ b/src/Mod/TechDraw/App/CosmeticExtension.h @@ -25,6 +25,7 @@ #include #include +#include #include #include diff --git a/src/Mod/TechDraw/Gui/ViewProviderCosmeticExtension.h b/src/Mod/TechDraw/Gui/ViewProviderCosmeticExtension.h index c1a1168e9e..46af972070 100644 --- a/src/Mod/TechDraw/Gui/ViewProviderCosmeticExtension.h +++ b/src/Mod/TechDraw/Gui/ViewProviderCosmeticExtension.h @@ -25,7 +25,7 @@ #define GUI_VIEWPROVIDERCOSMETICEXTENSION_H #include -#include +#include namespace TechDrawGui {