Debian Codegraph @

src/Gui/ExpressionBindingPy.cpp

raw
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
24 #include <App/DocumentObjectPy.h>
25
26 #include "ExpressionBindingPy.h"
27 #include "InputField.h"
28 #include "PythonWrapper.h"
29 #include "QuantitySpinBox.h"
30
31
32 using namespace Gui;
33
34 ExpressionBindingPy::ExpressionBindingPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
35 : Py::PythonClass<ExpressionBindingPy>::PythonClass(self, args, kwds)
36 {
37 PyObject* pyObj;
38 if (!PyArg_ParseTuple(args.ptr(), "O", &pyObj)) {
39 throw Py::Exception();
40 }
41
42 PythonWrapper wrap;
43 wrap.loadWidgetsModule();
44
45 QWidget* obj = qobject_cast<QWidget*>(wrap.toQObject(Py::Object(pyObj)));
46 expr = asBinding(obj);
47
48 if (!expr) {
49 throw Py::Exception(PyExc_TypeError, "Wrong type");
50 }
51 }
52
53 ExpressionBindingPy::~ExpressionBindingPy() = default;
54
55 ExpressionBinding* ExpressionBindingPy::asBinding(QWidget* obj)
56 {
57 ExpressionBinding* expr = nullptr;
58 if (obj) {
59 do {
60 auto qsb = qobject_cast<QuantitySpinBox*>(obj);
61 if (qsb) {
62 expr = qsb;
63 break;
64 }
65 auto usp = qobject_cast<UIntSpinBox*>(obj);
66 if (usp) {
67 expr = usp;
68 break;
69 }
70 auto isp = qobject_cast<IntSpinBox*>(obj);
71 if (isp) {
72 expr = isp;
73 break;
74 }
75 auto dsp = qobject_cast<DoubleSpinBox*>(obj);
76 if (dsp) {
77 expr = dsp;
78 break;
79 }
80 auto exp = qobject_cast<ExpLineEdit*>(obj);
81 if (exp) {
82 expr = exp;
83 break;
84 }
85 auto inp = qobject_cast<InputField*>(obj);
86 if (inp) {
87 expr = inp;
88 break;
89 }
90 } while (false);
91 }
92
93 return expr;
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
111 try {
112 App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(py)->getDocumentObjectPtr();
113 App::ObjectIdentifier id(App::ObjectIdentifier::parse(obj, str));
114 if (!id.getProperty()) {
115 throw Base::AttributeError("Wrong property");
116 }
117
118 expr->bind(id);
119 return Py::None();
120 }
121 catch (const Base::Exception& e) {
122 e.setPyException();
123 throw Py::Exception();
124 }
125 catch (...) {
126 throw Py::RuntimeError("Cannot bind to object");
127 }
128 }
129 PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, bind)
130
131 Py::Object ExpressionBindingPy::isBound()
132 {
133 return Py::Boolean(expr->isBound());
134 }
135 PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, isBound)
136
137 Py::Object ExpressionBindingPy::apply(const Py::Tuple& args)
138 {
139 const char* str;
140 if (!PyArg_ParseTuple(args.ptr(), "s", &str)) {
141 throw Py::Exception();
142 }
143
144 return Py::Boolean(expr->apply(str));
145 }
146 PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, apply)
147
148 Py::Object ExpressionBindingPy::hasExpression()
149 {
150 return Py::Boolean(expr->hasExpression());
151 }
152 PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, hasExpression)
153
154 Py::Object ExpressionBindingPy::autoApply()
155 {
156 return Py::Boolean(expr->autoApply());
157 }
158 PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, autoApply)
159
160 Py::Object ExpressionBindingPy::setAutoApply(const Py::Tuple& args)
161 {
162 PyObject* b;
163 if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b)) {
164 throw Py::Exception();
165 }
166
167 bool value = Base::asBoolean(b);
168 expr->setAutoApply(value);
169 return Py::None();
170 }
171 PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, setAutoApply)
172
173 void ExpressionBindingPy::init_type()
174 {
175 behaviors().name("Gui.ExpressionBinding");
176 behaviors().doc("Python interface class for ExpressionBinding");
177 // you must have overwritten the virtual functions
178 behaviors().supportRepr();
179 behaviors().supportGetattro();
180 behaviors().supportSetattro();
181
182 PYCXX_ADD_VARARGS_METHOD(bind, bind, "Bind with an expression");
183 PYCXX_ADD_NOARGS_METHOD(isBound, isBound, "Check if already bound with an expression");
184 PYCXX_ADD_VARARGS_METHOD(apply, apply, "apply");
185 PYCXX_ADD_NOARGS_METHOD(hasExpression, hasExpression, "hasExpression");
186 PYCXX_ADD_NOARGS_METHOD(autoApply, autoApply, "autoApply");
187 PYCXX_ADD_VARARGS_METHOD(setAutoApply, setAutoApply, "setAutoApply");
188
189 behaviors().readyType();
190 }