1
/***************************************************************************
2
* Copyright (c) 2019 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., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <App/DocumentObject.h>
26
#include <App/DocumentObjectPy.h>
27
#include <Base/Exception.h>
36
void MDIViewPy::init_type()
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);
46
add_varargs_method("printView",&MDIViewPy::printView,"printView()");
47
add_varargs_method("printPdf",&MDIViewPy::printPdf,"printPdf()");
48
add_varargs_method("printPreview",&MDIViewPy::printPreview,"printPreview()");
50
add_varargs_method("undoActions",&MDIViewPy::undoActions,"undoActions()");
51
add_varargs_method("redoActions",&MDIViewPy::redoActions,"redoActions()");
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");
62
PyObject *MDIViewPy::extension_object_new(struct _typeobject * /*type*/, PyObject * /*args*/, PyObject * /*kwds*/)
64
return new MDIViewPy(nullptr);
67
Py::Object MDIViewPy::type()
69
return Py::Object( reinterpret_cast<PyObject *>( behaviors().type_object() ) );
72
Py::ExtensionObject<MDIViewPy> MDIViewPy::create(MDIView *mdi)
74
Py::Callable class_type(type());
76
auto inst = Py::ExtensionObject<MDIViewPy>(class_type.apply(arg, Py::Dict()));
77
inst.extensionObject()->_view = mdi;
81
MDIViewPy::MDIViewPy(MDIView *mdi)
86
MDIViewPy::~MDIViewPy()
88
// in case the class is instantiated on the stack
92
Py::Object MDIViewPy::repr()
95
std::ostringstream s_out;
97
throw Py::RuntimeError("Cannot print representation of deleted object");
98
s_out << _view->getTypeId().getName();
99
return Py::String(s_out.str());
102
Py::Object MDIViewPy::printView(const Py::Tuple& args)
104
if (!PyArg_ParseTuple(args.ptr(), ""))
105
throw Py::Exception();
113
Py::Object MDIViewPy::printPdf(const Py::Tuple& args)
115
if (!PyArg_ParseTuple(args.ptr(), ""))
116
throw Py::Exception();
124
Py::Object MDIViewPy::printPreview(const Py::Tuple& args)
126
if (!PyArg_ParseTuple(args.ptr(), ""))
127
throw Py::Exception();
130
_view->printPreview();
135
Py::Object MDIViewPy::undoActions(const Py::Tuple& args)
137
if (!PyArg_ParseTuple(args.ptr(), ""))
138
throw Py::Exception();
142
QStringList undo = _view->undoActions();
143
for (const auto& it : undo)
144
list.append(Py::String(it.toStdString()));
150
Py::Object MDIViewPy::redoActions(const Py::Tuple& args)
152
if (!PyArg_ParseTuple(args.ptr(), ""))
153
throw Py::Exception();
157
QStringList redo = _view->redoActions();
158
for (const auto& it : redo)
159
list.append(Py::String(it.toStdString()));
165
Py::Object MDIViewPy::sendMessage(const Py::Tuple& args)
167
const char **ppReturn = nullptr;
169
if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)",&psMsgStr))
170
throw Py::Exception();
175
ok = _view->onMsg(psMsgStr,ppReturn);
176
return Py::Boolean(ok);
178
catch (const Base::Exception& e) {
179
throw Py::RuntimeError(e.what());
181
catch (const std::exception& e) {
182
throw Py::RuntimeError(e.what());
185
throw Py::RuntimeError("Unknown C++ exception");
189
Py::Object MDIViewPy::supportMessage(const Py::Tuple& args)
192
if (!PyArg_ParseTuple(args.ptr(), "s;Message string needed (string)",&psMsgStr))
193
throw Py::Exception();
198
_view->onHasMsg(psMsgStr);
199
return Py::Boolean(ok);
201
catch (const Base::Exception& e) {
202
throw Py::RuntimeError(e.what());
204
catch (const std::exception& e) {
205
throw Py::RuntimeError(e.what());
208
throw Py::RuntimeError("Unknown C++ exception");
212
Py::Object MDIViewPy::fitAll(const Py::Tuple& args)
214
if (!PyArg_ParseTuple(args.ptr(), ""))
215
throw Py::Exception();
221
catch (const Base::Exception& e) {
222
throw Py::RuntimeError(e.what());
224
catch (const std::exception& e) {
225
throw Py::RuntimeError(e.what());
228
throw Py::RuntimeError("Unknown C++ exception");
233
Py::Object MDIViewPy::setActiveObject(const Py::Tuple& args)
235
PyObject* docObject = Py_None;
237
char *subname = nullptr;
238
if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname))
239
throw Py::Exception();
242
Base::PyTypeCheck(&docObject, &App::DocumentObjectPy::Type,
243
"Expect the second argument to be a document object or None");
245
App::DocumentObject* obj = docObject ?
246
static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr() : nullptr;
247
_view->setActiveObject(obj, name, subname);
250
catch (const Base::Exception& e) {
251
throw Py::Exception(e.getPyExceptionType(), e.what());
257
Py::Object MDIViewPy::getActiveObject(const Py::Tuple& args)
260
PyObject *resolve = Py_True; // NOLINT
261
if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) {
262
throw Py::Exception();
265
App::DocumentObject *parent = nullptr;
267
App::DocumentObject* obj = nullptr;
269
obj = _view->getActiveObject<App::DocumentObject*>(name,&parent,&subname);
272
if (Base::asBoolean(resolve)) {
274
return Py::asObject(obj->getPyObject());
280
// NOLINTBEGIN(cppcoreguidelines-slicing)
283
Py::asObject(obj->getPyObject()),
284
Py::asObject(parent->getPyObject()),
285
Py::String(subname.c_str()));
288
return Py::TupleN(Py::None(), Py::None(), Py::String());
289
// NOLINTEND(cppcoreguidelines-slicing)
292
Py::Object MDIViewPy::cast_to_base(const Py::Tuple&)
294
return Py::Object(this);