|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 | + * * |
| 4 | + * This file is part of the FreeCAD CAx development system. * |
| 5 | + * * |
| 6 | + * This library is free software; you can redistribute it and/or * |
| 7 | + * modify it under the terms of the GNU Library General Public * |
| 8 | + * License as published by the Free Software Foundation; either * |
| 9 | + * version 2 of the License, or (at your option) any later version. * |
| 10 | + * * |
| 11 | + * This library is distributed in the hope that it will be useful, * |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | + * GNU Library General Public License for more details. * |
| 15 | + * * |
| 16 | + * You should have received a copy of the GNU Library General Public * |
| 17 | + * License along with this library; see the file COPYING.LIB. If not, * |
| 18 | + * write to the Free Software Foundation, Inc., 51 Franklin Street, * |
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA * |
| 20 | + * * |
| 21 | + ***************************************************************************/ |
| 22 | + |
| 23 | +#include "PreCompiled.h" |
| 24 | +#ifndef _PreComp_ |
| 25 | +#endif |
| 26 | +#include "ExpressionBindingPy.h" |
| 27 | +#include "ExpressionBinding.h" |
| 28 | +#include "WidgetFactory.h" |
| 29 | +#include "QuantitySpinBox.h" |
| 30 | +#include "InputField.h" |
| 31 | +#include <App/DocumentObjectPy.h> |
| 32 | + |
| 33 | +using namespace Gui; |
| 34 | + |
| 35 | +void ExpressionBindingPy::init_type() |
| 36 | +{ |
| 37 | + behaviors().name("ExpressionBinding"); |
| 38 | + behaviors().doc("Python interface class for ExpressionBinding"); |
| 39 | + // you must have overwritten the virtual functions |
| 40 | + behaviors().supportRepr(); |
| 41 | + behaviors().supportGetattr(); |
| 42 | + behaviors().supportSetattr(); |
| 43 | + behaviors().set_tp_new(PyMake); |
| 44 | + behaviors().readyType(); |
| 45 | + |
| 46 | + add_varargs_method("bind",&ExpressionBindingPy::bind,"Bind with an expression"); |
| 47 | + add_varargs_method("isBound",&ExpressionBindingPy::isBound,"Check if already bound with an expression"); |
| 48 | + add_varargs_method("apply",&ExpressionBindingPy::apply,"apply"); |
| 49 | + add_varargs_method("hasExpression",&ExpressionBindingPy::hasExpression,"hasExpression"); |
| 50 | + add_varargs_method("autoApply",&ExpressionBindingPy::autoApply,"autoApply"); |
| 51 | + add_varargs_method("setAutoApply",&ExpressionBindingPy::setAutoApply,"setAutoApply"); |
| 52 | +} |
| 53 | + |
| 54 | +PyObject *ExpressionBindingPy::PyMake(struct _typeobject *, PyObject * args, PyObject *) |
| 55 | +{ |
| 56 | + Py::Tuple tuple(args); |
| 57 | + |
| 58 | + ExpressionBinding* expr = nullptr; |
| 59 | + PythonWrapper wrap; |
| 60 | + wrap.loadWidgetsModule(); |
| 61 | + |
| 62 | + QWidget* obj = dynamic_cast<QWidget*>(wrap.toQObject(tuple.getItem(0))); |
| 63 | + if (obj) { |
| 64 | + do { |
| 65 | + QuantitySpinBox* sb = qobject_cast<QuantitySpinBox*>(obj); |
| 66 | + if (sb) { |
| 67 | + expr = sb; |
| 68 | + break; |
| 69 | + } |
| 70 | + InputField* le = qobject_cast<InputField*>(obj); |
| 71 | + if (le) { |
| 72 | + expr = le; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + while(false); |
| 77 | + } |
| 78 | + |
| 79 | + if (!expr) { |
| 80 | + PyErr_SetString(PyExc_TypeError, "Wrong type"); |
| 81 | + return nullptr; |
| 82 | + } |
| 83 | + |
| 84 | + return new ExpressionBindingPy(expr); |
| 85 | +} |
| 86 | + |
| 87 | +ExpressionBindingPy::ExpressionBindingPy(ExpressionBinding* expr) |
| 88 | + : expr(expr) |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | +ExpressionBindingPy::~ExpressionBindingPy() |
| 93 | +{ |
| 94 | +} |
| 95 | + |
| 96 | +Py::Object ExpressionBindingPy::repr() |
| 97 | +{ |
| 98 | + std::stringstream s; |
| 99 | + s << "<ExpressionBinding at " << this << ">"; |
| 100 | + return Py::String(s.str()); |
| 101 | +} |
| 102 | + |
| 103 | +Py::Object ExpressionBindingPy::bind(const Py::Tuple& args) |
| 104 | +{ |
| 105 | + PyObject* py; |
| 106 | + const char* str; |
| 107 | + if (!PyArg_ParseTuple(args.ptr(), "O!s", &App::DocumentObjectPy::Type, &py, &str)) |
| 108 | + throw Py::Exception(); |
| 109 | + |
| 110 | + try { |
| 111 | + App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(py)->getDocumentObjectPtr(); |
| 112 | + App::ObjectIdentifier id(App::ObjectIdentifier::parse(obj, str)); |
| 113 | + if (!id.getProperty()) { |
| 114 | + throw Base::AttributeError("Wrong property"); |
| 115 | + } |
| 116 | + |
| 117 | + expr->bind(id); |
| 118 | + return Py::None(); |
| 119 | + } |
| 120 | + catch (const Base::Exception& e) { |
| 121 | + e.setPyException(); |
| 122 | + throw Py::Exception(); |
| 123 | + } |
| 124 | + catch (...) { |
| 125 | + throw Py::RuntimeError("Cannot bind to object"); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +Py::Object ExpressionBindingPy::isBound(const Py::Tuple& args) |
| 130 | +{ |
| 131 | + if (!PyArg_ParseTuple(args.ptr(), "")) |
| 132 | + throw Py::Exception(); |
| 133 | + return Py::Boolean(expr->isBound()); |
| 134 | +} |
| 135 | + |
| 136 | +Py::Object ExpressionBindingPy::apply(const Py::Tuple& args) |
| 137 | +{ |
| 138 | + const char* str; |
| 139 | + if (!PyArg_ParseTuple(args.ptr(), "s", &str)) |
| 140 | + throw Py::Exception(); |
| 141 | + |
| 142 | + return Py::Boolean(expr->apply(str)); |
| 143 | +} |
| 144 | + |
| 145 | +Py::Object ExpressionBindingPy::hasExpression(const Py::Tuple& args) |
| 146 | +{ |
| 147 | + if (!PyArg_ParseTuple(args.ptr(), "")) |
| 148 | + throw Py::Exception(); |
| 149 | + return Py::Boolean(expr->hasExpression()); |
| 150 | +} |
| 151 | + |
| 152 | +Py::Object ExpressionBindingPy::autoApply(const Py::Tuple& args) |
| 153 | +{ |
| 154 | + if (!PyArg_ParseTuple(args.ptr(), "")) |
| 155 | + throw Py::Exception(); |
| 156 | + return Py::Boolean(expr->autoApply()); |
| 157 | +} |
| 158 | + |
| 159 | +Py::Object ExpressionBindingPy::setAutoApply(const Py::Tuple& args) |
| 160 | +{ |
| 161 | + PyObject* b; |
| 162 | + if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b)) |
| 163 | + throw Py::Exception(); |
| 164 | + |
| 165 | + bool value = PyObject_IsTrue(b) ? true : false; |
| 166 | + expr->setAutoApply(value); |
| 167 | + return Py::None(); |
| 168 | +} |
0 commit comments