FreeCAD

Форк
0
/
MDIViewPy.cpp 
295 строк · 8.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2019 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., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#include <App/DocumentObject.h>
26
#include <App/DocumentObjectPy.h>
27
#include <Base/Exception.h>
28

29
#include "MDIViewPy.h"
30
#include "MDIView.h"
31

32

33
using namespace Gui;
34

35

36
void MDIViewPy::init_type()
37
{
38
    behaviors().name("MDIViewPy");
39
    behaviors().doc("Python binding class for the MDI view class");
40
    // you must have overwritten the virtual functions
41
    behaviors().supportRepr();
42
    behaviors().supportGetattr();
43
    behaviors().supportSetattr();
44
    behaviors().set_tp_new(extension_object_new);
45

46
    add_varargs_method("printView",&MDIViewPy::printView,"printView()");
47
    add_varargs_method("printPdf",&MDIViewPy::printPdf,"printPdf()");
48
    add_varargs_method("printPreview",&MDIViewPy::printPreview,"printPreview()");
49

50
    add_varargs_method("undoActions",&MDIViewPy::undoActions,"undoActions()");
51
    add_varargs_method("redoActions",&MDIViewPy::redoActions,"redoActions()");
52

53
    add_varargs_method("message",&MDIViewPy::sendMessage,"deprecated: use sendMessage");
54
    add_varargs_method("sendMessage",&MDIViewPy::sendMessage,"sendMessage(str)");
55
    add_varargs_method("supportMessage",&MDIViewPy::supportMessage,"supportMessage(str)");
56
    add_varargs_method("fitAll",&MDIViewPy::fitAll,"fitAll()");
57
    add_varargs_method("setActiveObject", &MDIViewPy::setActiveObject, "setActiveObject(name,object,subname=None)\nadd or set a new active object");
58
    add_varargs_method("getActiveObject", &MDIViewPy::getActiveObject, "getActiveObject(name,resolve=True)\nreturns the active object for the given type");
59
    add_varargs_method("cast_to_base", &MDIViewPy::cast_to_base, "cast_to_base() cast to MDIView class");
60
}
61

62
PyObject *MDIViewPy::extension_object_new(struct _typeobject * /*type*/, PyObject * /*args*/, PyObject * /*kwds*/)
63
{
64
    return new MDIViewPy(nullptr);
65
}
66

67
Py::Object MDIViewPy::type()
68
{
69
    return Py::Object( reinterpret_cast<PyObject *>( behaviors().type_object() ) );
70
}
71

72
Py::ExtensionObject<MDIViewPy> MDIViewPy::create(MDIView *mdi)
73
{
74
    Py::Callable class_type(type());
75
    Py::Tuple arg;
76
    auto inst = Py::ExtensionObject<MDIViewPy>(class_type.apply(arg, Py::Dict()));
77
    inst.extensionObject()->_view = mdi;
78
    return inst;
79
}
80

81
MDIViewPy::MDIViewPy(MDIView *mdi)
82
  : _view(mdi)
83
{
84
}
85

86
MDIViewPy::~MDIViewPy()
87
{
88
    // in case the class is instantiated on the stack
89
    ob_refcnt = 0;
90
}
91

92
Py::Object MDIViewPy::repr()
93
{
94
    std::string s;
95
    std::ostringstream s_out;
96
    if (!_view)
97
        throw Py::RuntimeError("Cannot print representation of deleted object");
98
    s_out << _view->getTypeId().getName();
99
    return Py::String(s_out.str());
100
}
101

102
Py::Object MDIViewPy::printView(const Py::Tuple& args)
103
{
104
    if (!PyArg_ParseTuple(args.ptr(), ""))
105
        throw Py::Exception();
106

107
    if (_view)
108
        _view->print();
109

110
    return Py::None();
111
}
112

113
Py::Object MDIViewPy::printPdf(const Py::Tuple& args)
114
{
115
    if (!PyArg_ParseTuple(args.ptr(), ""))
116
        throw Py::Exception();
117

118
    if (_view)
119
        _view->printPdf();
120

121
    return Py::None();
122
}
123

124
Py::Object MDIViewPy::printPreview(const Py::Tuple& args)
125
{
126
    if (!PyArg_ParseTuple(args.ptr(), ""))
127
        throw Py::Exception();
128

129
    if (_view)
130
        _view->printPreview();
131

132
    return Py::None();
133
}
134

135
Py::Object MDIViewPy::undoActions(const Py::Tuple& args)
136
{
137
    if (!PyArg_ParseTuple(args.ptr(), ""))
138
        throw Py::Exception();
139

140
    Py::List list;
141
    if (_view) {
142
        QStringList undo = _view->undoActions();
143
        for (const auto& it : undo)
144
            list.append(Py::String(it.toStdString()));
145
    }
146

147
    return list;
148
}
149

150
Py::Object MDIViewPy::redoActions(const Py::Tuple& args)
151
{
152
    if (!PyArg_ParseTuple(args.ptr(), ""))
153
        throw Py::Exception();
154

155
    Py::List list;
156
    if (_view) {
157
        QStringList redo = _view->redoActions();
158
        for (const auto& it : redo)
159
            list.append(Py::String(it.toStdString()));
160
    }
161

162
    return list;
163
}
164

165
Py::Object MDIViewPy::sendMessage(const Py::Tuple& args)
166
{
167
    const char **ppReturn = nullptr;
168
    char *psMsgStr;
169
    if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)",&psMsgStr))
170
        throw Py::Exception();
171

172
    try {
173
        bool ok = false;
174
        if (_view)
175
            ok = _view->onMsg(psMsgStr,ppReturn);
176
        return Py::Boolean(ok);
177
    }
178
    catch (const Base::Exception& e) {
179
        throw Py::RuntimeError(e.what());
180
    }
181
    catch (const std::exception& e) {
182
        throw Py::RuntimeError(e.what());
183
    }
184
    catch (...) {
185
        throw Py::RuntimeError("Unknown C++ exception");
186
    }
187
}
188

189
Py::Object MDIViewPy::supportMessage(const Py::Tuple& args)
190
{
191
    char *psMsgStr;
192
    if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)",&psMsgStr))
193
        throw Py::Exception();
194

195
    try {
196
        bool ok = false;
197
        if (_view)
198
            _view->onHasMsg(psMsgStr);
199
        return Py::Boolean(ok);
200
    }
201
    catch (const Base::Exception& e) {
202
        throw Py::RuntimeError(e.what());
203
    }
204
    catch (const std::exception& e) {
205
        throw Py::RuntimeError(e.what());
206
    }
207
    catch (...) {
208
        throw Py::RuntimeError("Unknown C++ exception");
209
    }
210
}
211

212
Py::Object MDIViewPy::fitAll(const Py::Tuple& args)
213
{
214
    if (!PyArg_ParseTuple(args.ptr(), ""))
215
        throw Py::Exception();
216

217
    try {
218
        if (_view)
219
            _view->viewAll();
220
    }
221
    catch (const Base::Exception& e) {
222
        throw Py::RuntimeError(e.what());
223
    }
224
    catch (const std::exception& e) {
225
        throw Py::RuntimeError(e.what());
226
    }
227
    catch (...) {
228
        throw Py::RuntimeError("Unknown C++ exception");
229
    }
230
    return Py::None();
231
}
232

233
Py::Object MDIViewPy::setActiveObject(const Py::Tuple& args)
234
{
235
    PyObject* docObject = Py_None;
236
    char* name;
237
    char *subname = nullptr;
238
    if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname))
239
        throw Py::Exception();
240

241
    try {
242
        Base::PyTypeCheck(&docObject, &App::DocumentObjectPy::Type,
243
            "Expect the second argument to be a document object or None");
244
        if (_view) {
245
            App::DocumentObject* obj = docObject ?
246
                static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr() : nullptr;
247
            _view->setActiveObject(obj, name, subname);
248
        }
249
    }
250
    catch (const Base::Exception& e) {
251
        throw Py::Exception(e.getPyExceptionType(), e.what());
252
    }
253

254
    return Py::None();
255
}
256

257
Py::Object MDIViewPy::getActiveObject(const Py::Tuple& args)
258
{
259
    const char* name{};
260
    PyObject *resolve = Py_True; // NOLINT
261
    if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) {
262
        throw Py::Exception();
263
    }
264

265
    App::DocumentObject *parent = nullptr;
266
    std::string subname;
267
    App::DocumentObject* obj = nullptr;
268
    if (_view) {
269
        obj = _view->getActiveObject<App::DocumentObject*>(name,&parent,&subname);
270
    }
271

272
    if (Base::asBoolean(resolve)) {
273
        if (obj) {
274
            return Py::asObject(obj->getPyObject());
275
        }
276

277
        return Py::None();
278
    }
279

280
    // NOLINTBEGIN(cppcoreguidelines-slicing)
281
    if (obj) {
282
        return Py::TupleN(
283
                Py::asObject(obj->getPyObject()),
284
                Py::asObject(parent->getPyObject()),
285
                Py::String(subname.c_str()));
286
    }
287

288
    return Py::TupleN(Py::None(), Py::None(), Py::String());
289
    // NOLINTEND(cppcoreguidelines-slicing)
290
}
291

292
Py::Object MDIViewPy::cast_to_base(const Py::Tuple&)
293
{
294
    return Py::Object(this);
295
}
296

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

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

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

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