FreeCAD

Форк
0
/
ExpressionBindingPy.cpp 
192 строки · 6.1 Кб
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

25
#include <App/DocumentObjectPy.h>
26

27
#include "ExpressionBindingPy.h"
28
#include "InputField.h"
29
#include "PythonWrapper.h"
30
#include "QuantitySpinBox.h"
31

32

33
using namespace Gui;
34

35
ExpressionBindingPy::ExpressionBindingPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
36
  : Py::PythonClass<ExpressionBindingPy>::PythonClass(self, args, kwds)
37
{
38
    PyObject* pyObj;
39
    if (!PyArg_ParseTuple(args.ptr(), "O", &pyObj)) {
40
        throw Py::Exception();
41
    }
42

43
    PythonWrapper wrap;
44
    wrap.loadWidgetsModule();
45

46
    QWidget* obj = dynamic_cast<QWidget*>(wrap.toQObject(Py::Object(pyObj)));
47
    expr = asBinding(obj);
48

49
    if (!expr) {
50
        throw Py::Exception(PyExc_TypeError, "Wrong type");
51
    }
52
}
53

54
ExpressionBindingPy::~ExpressionBindingPy() = default;
55

56
ExpressionBinding* ExpressionBindingPy::asBinding(QWidget* obj)
57
{
58
    ExpressionBinding* expr = nullptr;
59
    if (obj) {
60
        do {
61
            auto qsb = qobject_cast<QuantitySpinBox*>(obj);
62
            if (qsb) {
63
                expr = qsb;
64
                break;
65
            }
66
            auto usp = qobject_cast<UIntSpinBox*>(obj);
67
            if (usp) {
68
                expr = usp;
69
                break;
70
            }
71
            auto isp = qobject_cast<IntSpinBox*>(obj);
72
            if (isp) {
73
                expr = isp;
74
                break;
75
            }
76
            auto dsp = qobject_cast<DoubleSpinBox*>(obj);
77
            if (dsp) {
78
                expr = dsp;
79
                break;
80
            }
81
            auto exp = qobject_cast<ExpLineEdit*>(obj);
82
            if (exp) {
83
                expr = exp;
84
                break;
85
            }
86
            auto inp = qobject_cast<InputField*>(obj);
87
            if (inp) {
88
                expr = inp;
89
                break;
90
            }
91
        }
92
        while (false);
93
    }
94

95
    return expr;
96
}
97

98
Py::Object ExpressionBindingPy::repr()
99
{
100
    std::stringstream s;
101
    s << "<ExpressionBinding at " << this << ">";
102
    return Py::String(s.str());
103
}
104

105
Py::Object ExpressionBindingPy::bind(const Py::Tuple& args)
106
{
107
    PyObject* py;
108
    const char* str;
109
    if (!PyArg_ParseTuple(args.ptr(), "O!s", &App::DocumentObjectPy::Type, &py, &str)) {
110
        throw Py::Exception();
111
    }
112

113
    try {
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");
118
        }
119

120
        expr->bind(id);
121
        return Py::None();
122
    }
123
    catch (const Base::Exception& e) {
124
        e.setPyException();
125
        throw Py::Exception();
126
    }
127
    catch (...) {
128
        throw Py::RuntimeError("Cannot bind to object");
129
    }
130
}
131
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, bind)
132

133
Py::Object ExpressionBindingPy::isBound()
134
{
135
    return Py::Boolean(expr->isBound());
136
}
137
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, isBound)
138

139
Py::Object ExpressionBindingPy::apply(const Py::Tuple& args)
140
{
141
    const char* str;
142
    if (!PyArg_ParseTuple(args.ptr(), "s", &str)) {
143
        throw Py::Exception();
144
    }
145

146
    return Py::Boolean(expr->apply(str));
147
}
148
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, apply)
149

150
Py::Object ExpressionBindingPy::hasExpression()
151
{
152
    return Py::Boolean(expr->hasExpression());
153
}
154
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, hasExpression)
155

156
Py::Object ExpressionBindingPy::autoApply()
157
{
158
    return Py::Boolean(expr->autoApply());
159
}
160
PYCXX_NOARGS_METHOD_DECL(ExpressionBindingPy, autoApply)
161

162
Py::Object ExpressionBindingPy::setAutoApply(const Py::Tuple& args)
163
{
164
    PyObject* b;
165
    if (!PyArg_ParseTuple(args.ptr(), "O!", &PyBool_Type, &b)) {
166
        throw Py::Exception();
167
    }
168

169
    bool value = Base::asBoolean(b);
170
    expr->setAutoApply(value);
171
    return Py::None();
172
}
173
PYCXX_VARARGS_METHOD_DECL(ExpressionBindingPy, setAutoApply)
174

175
void ExpressionBindingPy::init_type()
176
{
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();
183

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");
190

191
    behaviors().readyType();
192
}
193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.