App/Gui: move template classes (ViewProvider)ExtensionPythonT to their own header files

This commit is contained in:
wmayer
2022-03-04 18:54:42 +01:00
parent 4ae1ca58d1
commit ca3c8185e0
17 changed files with 195 additions and 119 deletions

View File

@@ -31,6 +31,7 @@
#include "Extension.h"
#include "ExtensionContainer.h"
#include "ExtensionPython.h"
#include <ExtensionPy.h>

View File

@@ -24,11 +24,8 @@
#ifndef APP_EXTENSION_H
#define APP_EXTENSION_H
#include <Base/Interpreter.h>
#include "PropertyContainer.h"
#include "PropertyPythonObject.h"
#include <Base/SmartPtrPy.h>
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 ExtensionT>
class ExtensionPythonT : public ExtensionT
{
EXTENSION_PROPERTY_HEADER(App::ExtensionPythonT<ExtensionT>);
public:
typedef ExtensionT Inherited;
ExtensionPythonT() {
ExtensionT::m_isPythonExtension = true;
ExtensionT::initExtensionType(ExtensionPythonT::getExtensionClassTypeId());
}
virtual ~ExtensionPythonT() {
}
};
typedef ExtensionPythonT<App::Extension> 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<PropertyPythonObject*>(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

118
src/App/ExtensionPython.h Normal file
View File

@@ -0,0 +1,118 @@
/***************************************************************************
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.net> *
* *
* 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 <Base/Interpreter.h>
#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 ExtensionT>
class ExtensionPythonT : public ExtensionT
{
EXTENSION_PROPERTY_HEADER(App::ExtensionPythonT<ExtensionT>);
public:
typedef ExtensionT Inherited;
ExtensionPythonT() {
ExtensionT::m_isPythonExtension = true;
ExtensionT::initExtensionType(ExtensionPythonT::getExtensionClassTypeId());
}
virtual ~ExtensionPythonT() {
}
};
typedef ExtensionPythonT<App::Extension> 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<PropertyPythonObject*>(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

View File

@@ -24,8 +24,9 @@
#ifndef APP_GROUPEXTENSION_H
#define APP_GROUPEXTENSION_H
#include "DocumentObject.h"
#include "DocumentObjectExtension.h"
#include <App/DocumentObject.h>
#include <App/DocumentObjectExtension.h>
#include <App/ExtensionPython.h>
#include <vector>

View File

@@ -30,6 +30,7 @@
#include "ViewProviderExtension.h"
#include "ViewProviderExtensionPy.h"
#include "ViewProviderExtensionPython.h"
using namespace Gui;

View File

@@ -24,7 +24,8 @@
#ifndef GUI_VIEWPROVIDEREXTENSION_H
#define GUI_VIEWPROVIDEREXTENSION_H
#include "App/Extension.h"
#include <App/Extension.h>
#include <App/PropertyPythonObject.h>
#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 ExtensionT>
class ViewProviderExtensionPythonT : public ExtensionT
{
EXTENSION_PROPERTY_HEADER(Gui::ViewProviderExtensionPythonT<ExtensionT>);
public:
typedef ExtensionT Inherited;
ViewProviderExtensionPythonT() {
ExtensionT::m_isPythonExtension = true;
ExtensionT::initExtensionType(ViewProviderExtensionPythonT::getExtensionClassTypeId());
}
virtual ~ViewProviderExtensionPythonT() {
}
};
typedef ViewProviderExtensionPythonT<Gui::ViewProviderExtension> ViewProviderExtensionPython;
} //Gui
#endif // GUI_VIEWPROVIDEREXTENSION_H

View File

@@ -0,0 +1,56 @@
/***************************************************************************
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.net> *
* *
* 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 <App/PropertyPythonObject.h>
namespace Gui {
/**
* Generic Python extension class which allows to behave every extension
* derived class as Python extension -- simply by subclassing.
*/
template <class ExtensionT>
class ViewProviderExtensionPythonT : public ExtensionT
{
EXTENSION_PROPERTY_HEADER(Gui::ViewProviderExtensionPythonT<ExtensionT>);
public:
typedef ExtensionT Inherited;
ViewProviderExtensionPythonT() {
ExtensionT::m_isPythonExtension = true;
ExtensionT::initExtensionType(ViewProviderExtensionPythonT::getExtensionClassTypeId());
}
virtual ~ViewProviderExtensionPythonT() {
}
};
typedef ViewProviderExtensionPythonT<Gui::ViewProviderExtension> ViewProviderExtensionPython;
} //Gui
#endif // GUI_VIEWPROVIDEREXTENSIONPYTHON_H

View File

@@ -25,7 +25,7 @@
#define GUI_VIEWPROVIDERGROUPEXTENSION_H
#include <App/Extension.h>
#include "ViewProviderExtension.h"
#include "ViewProviderExtensionPython.h"
namespace Gui
{

View File

@@ -27,6 +27,7 @@
#ifndef PARTATTACHABLEOBJECT_H
#define PARTATTACHABLEOBJECT_H
#include <App/ExtensionPython.h>
#include <App/PropertyStandard.h>
#include <App/PropertyLinks.h>
#include <App/GeoFeature.h>

View File

@@ -25,7 +25,7 @@
#define GUI_VIEWPROVIDERATTACHEXTENSION_H
#include <App/Extension.h>
#include <Gui/ViewProviderExtension.h>
#include <Gui/ViewProviderExtensionPython.h>
namespace PartGui
{

View File

@@ -24,7 +24,7 @@
#define PARTGUI_VIEWPROVIDERPARTSPLINE_H
#include <Mod/Part/Gui/ViewProviderExt.h>
#include <Gui/ViewProviderExtension.h>
#include <Gui/ViewProviderExtensionPython.h>
namespace PartGui
{

View File

@@ -30,6 +30,7 @@
# include <QMessageBox>
#endif
#include <Base/Interpreter.h>
#include <App/DocumentObject.h>
#include <Gui/Selection.h>
#include <Gui/Tools.h>

View File

@@ -31,6 +31,7 @@
# include <QMessageBox>
#endif
#include <Base/Interpreter.h>
#include <App/DocumentObject.h>
#include <Gui/Command.h>
#include <Gui/Selection.h>

View File

@@ -29,6 +29,7 @@
# include <QMessageBox>
#endif
#include <Base/Interpreter.h>
#include <App/DocumentObject.h>
#include <Gui/Selection.h>
#include <Gui/ViewProvider.h>

View File

@@ -29,6 +29,7 @@
# include <QMessageBox>
#endif
#include <Base/Interpreter.h>
#include <App/DocumentObject.h>
#include <Gui/Selection.h>
#include <Gui/Command.h>

View File

@@ -25,6 +25,7 @@
#include <App/DocumentObject.h>
#include <App/DocumentObjectExtension.h>
#include <App/ExtensionPython.h>
#include <App/PropertyStandard.h>
#include <App/Material.h>

View File

@@ -25,7 +25,7 @@
#define GUI_VIEWPROVIDERCOSMETICEXTENSION_H
#include <App/Extension.h>
#include <Gui/ViewProviderExtension.h>
#include <Gui/ViewProviderExtensionPython.h>
namespace TechDrawGui
{