1
/***************************************************************************
2
* Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <App/DocumentObjectPy.h>
27
#include "ExpressionBindingPy.h"
28
#include "InputField.h"
29
#include "PythonWrapper.h"
30
#include "QuantitySpinBox.h"
35
ExpressionBindingPy::ExpressionBindingPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
36
: Py::PythonClass<ExpressionBindingPy>::PythonClass(self, args, kwds)
39
if (!PyArg_ParseTuple(args.ptr(), "O", &pyObj)) {
40
throw Py::Exception();
44
wrap.loadWidgetsModule();
46
QWidget* obj = dynamic_cast<QWidget*>(wrap.toQObject(Py::Object(pyObj)));
47
expr = asBinding(obj);
50
throw Py::Exception(PyExc_TypeError, "Wrong type");
54
ExpressionBindingPy::~ExpressionBindingPy() = default;
56
ExpressionBinding* ExpressionBindingPy::asBinding(QWidget* obj)
58
ExpressionBinding* expr = nullptr;
61
auto qsb = qobject_cast<QuantitySpinBox*>(obj);
66
auto usp = qobject_cast<UIntSpinBox*>(obj);
71
auto isp = qobject_cast<IntSpinBox*>(obj);
76
auto dsp = qobject_cast<DoubleSpinBox*>(obj);
81
auto exp = qobject_cast<ExpLineEdit*>(obj);
86
auto inp = qobject_cast<InputField*>(obj);
98
Py::Object ExpressionBindingPy::repr()
101
s << "<ExpressionBinding at " << this << ">";
102
return Py::String(s.str());
105
Py::Object ExpressionBindingPy::bind(const Py::Tuple& args)
109
if (!PyArg_ParseTuple(args.ptr(), "O!s", &App::DocumentObjectPy::Type, &py, &str)) {
110
throw Py::Exception();
114
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(py)->getDocumentObjectPtr();
115
App::ObjectIdentifier id(App::ObjectIdentifier::parse(obj, str));
116
if (!id.getProperty()) {
117
throw Base::AttributeError("Wrong property");
123
catch (const Base::Exception& e) {
125
throw Py::Exception();
128
throw Py::RuntimeError("Cannot bind to object");
131
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, bind)
133
Py::Object ExpressionBindingPy::isBound()
135
return Py::Boolean(expr->isBound());
137
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, isBound)
139
Py::Object ExpressionBindingPy::apply(const Py::Tuple& args)
142
if (!PyArg_ParseTuple(args.ptr(), "s", &str)) {
143
throw Py::Exception();
146
return Py::Boolean(expr->apply(str));
148
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, apply)
150
Py::Object ExpressionBindingPy::hasExpression()
152
return Py::Boolean(expr->hasExpression());
154
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, hasExpression)
156
Py::Object ExpressionBindingPy::autoApply()
158
return Py::Boolean(expr->autoApply());
160
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, autoApply)
162
Py::Object ExpressionBindingPy::setAutoApply(const Py::Tuple& args)
165
if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b)) {
166
throw Py::Exception();
169
bool value = Base::asBoolean(b);
170
expr->setAutoApply(value);
173
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, setAutoApply)
175
void ExpressionBindingPy::init_type()
177
behaviors().name("Gui.ExpressionBinding");
178
behaviors().doc("Python interface class for ExpressionBinding");
179
// you must have overwritten the virtual functions
180
behaviors().supportRepr();
181
behaviors().supportGetattro();
182
behaviors().supportSetattro();
184
PYCXX_ADD_VARARGS_METHOD(bind, bind, "Bind with an expression");
185
PYCXX_ADD_NOARGS_METHOD(isBound, isBound, "Check if already bound with an expression");
186
PYCXX_ADD_VARARGS_METHOD(apply, apply, "apply");
187
PYCXX_ADD_NOARGS_METHOD(hasExpression, hasExpression, "hasExpression");
188
PYCXX_ADD_NOARGS_METHOD(autoApply, autoApply, "autoApply");
189
PYCXX_ADD_VARARGS_METHOD(setAutoApply, setAutoApply, "setAutoApply");
191
behaviors().readyType();