Gui: Change ExpressionBindingPy to new PyCXX extension type

This commit is contained in:
marioalexis
2023-08-16 23:01:01 -03:00
committed by wwmayer
parent f6b85d3721
commit 5af2a3c07a
2 changed files with 58 additions and 62 deletions

View File

@@ -32,23 +32,27 @@
using namespace Gui;
void ExpressionBindingPy::init_type()
ExpressionBindingPy::ExpressionBindingPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
: Py::PythonClass<ExpressionBindingPy>::PythonClass(self, args, kwds)
{
behaviors().name("ExpressionBinding");
behaviors().doc("Python interface class for ExpressionBinding");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
behaviors().set_tp_new(PyMake);
behaviors().readyType();
PyObject* pyObj;
if (!PyArg_ParseTuple(args.ptr(), "O", &pyObj)) {
throw Py::Exception();
}
add_varargs_method("bind", &ExpressionBindingPy::bind, "Bind with an expression");
add_varargs_method("isBound", &ExpressionBindingPy::isBound, "Check if already bound with an expression");
add_varargs_method("apply", &ExpressionBindingPy::apply, "apply");
add_varargs_method("hasExpression", &ExpressionBindingPy::hasExpression, "hasExpression");
add_varargs_method("autoApply", &ExpressionBindingPy::autoApply, "autoApply");
add_varargs_method("setAutoApply", &ExpressionBindingPy::setAutoApply, "setAutoApply");
PythonWrapper wrap;
wrap.loadWidgetsModule();
QWidget* obj = dynamic_cast<QWidget*>(wrap.toQObject(Py::Object(pyObj)));
expr = asBinding(obj);
if (!expr) {
throw Py::Exception(PyExc_TypeError, "Wrong type");
}
}
ExpressionBindingPy::~ExpressionBindingPy()
{
}
ExpressionBinding* ExpressionBindingPy::asBinding(QWidget* obj)
@@ -93,35 +97,6 @@ ExpressionBinding* ExpressionBindingPy::asBinding(QWidget* obj)
return expr;
}
PyObject *ExpressionBindingPy::PyMake(struct _typeobject *, PyObject * args, PyObject *)
{
PyObject* pyObj;
if (!PyArg_ParseTuple(args, "O", &pyObj))
return nullptr;
PythonWrapper wrap;
wrap.loadWidgetsModule();
QWidget* obj = dynamic_cast<QWidget*>(wrap.toQObject(Py::Object(pyObj)));
ExpressionBinding* expr = asBinding(obj);
if (!expr) {
PyErr_SetString(PyExc_TypeError, "Wrong type");
return nullptr;
}
return new ExpressionBindingPy(expr);
}
ExpressionBindingPy::ExpressionBindingPy(ExpressionBinding* expr)
: expr(expr)
{
}
ExpressionBindingPy::~ExpressionBindingPy()
{
}
Py::Object ExpressionBindingPy::repr()
{
std::stringstream s;
@@ -133,8 +108,9 @@ Py::Object ExpressionBindingPy::bind(const Py::Tuple& args)
{
PyObject* py;
const char* str;
if (!PyArg_ParseTuple(args.ptr(), "O!s", &App::DocumentObjectPy::Type, &py, &str))
if (!PyArg_ParseTuple(args.ptr(), "O!s", &App::DocumentObjectPy::Type, &py, &str)) {
throw Py::Exception();
}
try {
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(py)->getDocumentObjectPtr();
@@ -154,44 +130,65 @@ Py::Object ExpressionBindingPy::bind(const Py::Tuple& args)
throw Py::RuntimeError("Cannot bind to object");
}
}
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, bind)
Py::Object ExpressionBindingPy::isBound(const Py::Tuple& args)
Py::Object ExpressionBindingPy::isBound()
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
return Py::Boolean(expr->isBound());
}
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, isBound)
Py::Object ExpressionBindingPy::apply(const Py::Tuple& args)
{
const char* str;
if (!PyArg_ParseTuple(args.ptr(), "s", &str))
if (!PyArg_ParseTuple(args.ptr(), "s", &str)) {
throw Py::Exception();
}
return Py::Boolean(expr->apply(str));
}
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, apply)
Py::Object ExpressionBindingPy::hasExpression(const Py::Tuple& args)
Py::Object ExpressionBindingPy::hasExpression()
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
return Py::Boolean(expr->hasExpression());
}
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, hasExpression)
Py::Object ExpressionBindingPy::autoApply(const Py::Tuple& args)
Py::Object ExpressionBindingPy::autoApply()
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
return Py::Boolean(expr->autoApply());
}
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, autoApply)
Py::Object ExpressionBindingPy::setAutoApply(const Py::Tuple& args)
{
PyObject* b;
if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b))
if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b)) {
throw Py::Exception();
}
bool value = Base::asBoolean(b);
expr->setAutoApply(value);
return Py::None();
}
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, setAutoApply)
void ExpressionBindingPy::init_type()
{
behaviors().name("Gui.ExpressionBinding");
behaviors().doc("Python interface class for ExpressionBinding");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattro();
behaviors().supportSetattro();
PYCXX_ADD_VARARGS_METHOD(bind, bind, "Bind with an expression");
PYCXX_ADD_NOARGS_METHOD(isBound, isBound, "Check if already bound with an expression");
PYCXX_ADD_VARARGS_METHOD(apply, apply, "apply");
PYCXX_ADD_NOARGS_METHOD(hasExpression, hasExpression, "hasExpression");
PYCXX_ADD_NOARGS_METHOD(autoApply, autoApply, "autoApply");
PYCXX_ADD_VARARGS_METHOD(setAutoApply, setAutoApply, "setAutoApply");
behaviors().readyType();
}

View File

@@ -29,25 +29,24 @@ class QWidget;
namespace Gui {
class ExpressionBinding;
class ExpressionBindingPy : public Py::PythonExtension<ExpressionBindingPy>
class ExpressionBindingPy : public Py::PythonClass<ExpressionBindingPy>
{
public:
static void init_type(); // announce properties and methods
explicit ExpressionBindingPy(ExpressionBinding*);
ExpressionBindingPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds);
~ExpressionBindingPy() override;
Py::Object repr() override;
Py::Object bind(const Py::Tuple&);
Py::Object isBound(const Py::Tuple&);
Py::Object isBound();
Py::Object apply(const Py::Tuple&);
Py::Object hasExpression(const Py::Tuple&);
Py::Object autoApply(const Py::Tuple&);
Py::Object hasExpression();
Py::Object autoApply();
Py::Object setAutoApply(const Py::Tuple&);
private:
static PyObject *PyMake(struct _typeobject *, PyObject *, PyObject *);
static ExpressionBinding* asBinding(QWidget*);
private: