1
/***************************************************************************
2
* Copyright (c) 2022 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 <unordered_map>
29
#include <Base/Interpreter.h>
30
#include <App/Document.h>
32
#include "MDIViewPyWrap.h"
33
#include "PythonWrapper.h"
37
namespace sp = std::placeholders;
44
MDIViewPyWrapImp(Py::Object pyobject)
47
Base::PyGILStateLocker lock;
48
std::vector<std::string> methods = {"widget", "onMsg", "onHasMsg", "canClose", "printDocument", "print", "printPdf", "printPreview", "redoActions", "undoActions"};
50
for (const auto& it : methods) {
51
if (pyobject.hasAttr(it)) {
52
func[it] = pyobject.getAttr(it);
59
Base::PyGILStateLocker lock;
60
pyobject = Py::None();
66
Base::PyGILStateLocker lock;
68
wrap.loadWidgetsModule();
69
if (func.count("widget") == 0) {
70
throw Py::AttributeError("Object has no attribute 'widget'");
72
Py::Callable target(func.at("widget"));
73
Py::Object pywidget(target.apply(Py::Tuple()));
74
return qobject_cast<QWidget*>(wrap.toQObject(pywidget));
77
bool onMsg(const char* pMsg)
79
Base::PyGILStateLocker lock;
80
Py::Callable target(func.at("onMsg"));
81
Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
82
return static_cast<bool>(result);
85
bool onHasMsg(const char* pMsg)
87
Base::PyGILStateLocker lock;
88
Py::Callable target(func.at("onHasMsg"));
89
Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
90
return static_cast<bool>(result);
95
Base::PyGILStateLocker lock;
96
Py::Callable target(func.at("canClose"));
97
Py::Boolean result(target.apply(Py::Tuple()));
98
return static_cast<bool>(result);
101
void printDocument(QPrinter* printer)
103
Base::PyGILStateLocker lock;
105
wrap.loadPrintSupportModule();
106
Py::Object pyprint = wrap.fromQPrinter(printer);
107
Py::Callable target(func.at("printDocument"));
108
target.apply(Py::TupleN(pyprint));
113
Base::PyGILStateLocker lock;
114
Py::Callable target(func.at("print"));
115
target.apply(Py::Tuple());
120
Base::PyGILStateLocker lock;
121
Py::Callable target(func.at("printPdf"));
122
target.apply(Py::Tuple());
127
Base::PyGILStateLocker lock;
128
Py::Callable target(func.at("printPreview"));
129
target.apply(Py::Tuple());
132
QStringList undoActions()
134
Base::PyGILStateLocker lock;
135
Py::Callable target(func.at("undoActions"));
136
Py::List list(target.apply(Py::Tuple()));
138
for (const auto& it : list) {
140
actions << QString::fromStdString(str);
145
QStringList redoActions()
147
Base::PyGILStateLocker lock;
148
Py::Callable target(func.at("redoActions"));
149
Py::List list(target.apply(Py::Tuple()));
151
for (const auto& it : list) {
153
actions << QString::fromStdString(str);
159
std::unordered_map<std::string, Py::Object> func;
166
TYPESYSTEM_SOURCE_ABSTRACT(Gui::MDIViewPyWrap,Gui::MDIView)
168
MDIViewPyWrap::MDIViewPyWrap(const Py::Object& py, Gui::Document* pcDocument,QWidget* parent, Qt::WindowFlags wflags)
169
: MDIView(pcDocument, parent, wflags)
170
, ptr(std::make_unique<MDIViewPyWrapImp>(py))
173
QWidget* widget = ptr->widget();
175
setCentralWidget(widget);
176
QString title = widget->windowTitle();
177
if (!title.isEmpty()) {
178
setWindowTitle(title);
182
catch (Py::Exception&) {
183
Base::PyGILStateLocker lock;
184
Base::PyException exc;
185
exc.ReportException();
189
MDIViewPyWrap::~MDIViewPyWrap()
194
PyObject* MDIViewPyWrap::getPyObject()
196
return MDIView::getPyObject();
199
bool MDIViewPyWrap::onMsg(const char* pMsg,const char** ppReturn)
202
if (ptr->onMsg(pMsg)) {
205
return MDIView::onMsg(pMsg, ppReturn);
207
catch (const std::exception&) {
208
return MDIView::onMsg(pMsg, ppReturn);
210
catch (Py::Exception&) {
211
Base::PyGILStateLocker lock;
212
Base::PyException exc;
213
exc.ReportException();
218
bool MDIViewPyWrap::onHasMsg(const char* pMsg) const
221
if (ptr->onHasMsg(pMsg)) {
224
return MDIView::onHasMsg(pMsg);
226
catch (const std::exception&) {
227
return MDIView::onHasMsg(pMsg);
229
catch (Py::Exception&) {
230
Base::PyGILStateLocker lock;
231
Base::PyException exc;
232
exc.ReportException();
237
bool MDIViewPyWrap::canClose()
240
return ptr->canClose();
242
catch (const std::exception&) {
243
return MDIView::canClose();
245
catch (Py::Exception&) {
246
Base::PyGILStateLocker lock;
247
Base::PyException exc;
248
exc.ReportException();
253
void MDIViewPyWrap::print(QPrinter* printer)
256
return ptr->printDocument(printer);
258
catch (const std::exception&) {
259
return MDIView::print(printer);
261
catch (Py::Exception&) {
262
Base::PyGILStateLocker lock;
263
Base::PyException exc;
264
exc.ReportException();
268
void MDIViewPyWrap::print()
273
catch (const std::exception&) {
274
return MDIView::print();
276
catch (Py::Exception&) {
277
Base::PyGILStateLocker lock;
278
Base::PyException exc;
279
exc.ReportException();
283
void MDIViewPyWrap::printPdf()
286
return ptr->printPdf();
288
catch (const std::exception&) {
289
return MDIView::printPdf();
291
catch (Py::Exception&) {
292
Base::PyGILStateLocker lock;
293
Base::PyException exc;
294
exc.ReportException();
298
void MDIViewPyWrap::printPreview()
301
return ptr->printPreview();
303
catch (const std::exception&) {
304
return MDIView::printPreview();
306
catch (Py::Exception&) {
307
Base::PyGILStateLocker lock;
308
Base::PyException exc;
309
exc.ReportException();
313
QStringList MDIViewPyWrap::undoActions() const
316
return ptr->undoActions();
318
catch (const std::exception&) {
319
return MDIView::undoActions();
321
catch (Py::Exception&) {
322
Base::PyGILStateLocker lock;
323
Base::PyException exc;
324
exc.ReportException();
325
return MDIView::undoActions();
329
QStringList MDIViewPyWrap::redoActions() const
332
return ptr->redoActions();
334
catch (const std::exception&) {
335
return MDIView::redoActions();
337
catch (Py::Exception&) {
338
Base::PyGILStateLocker lock;
339
Base::PyException exc;
340
exc.ReportException();
341
return MDIView::redoActions();
345
#include "moc_MDIViewPyWrap.cpp"