Debian Codegraph @

src/Gui/MDIViewPyWrap.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2022 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
24 #include <unordered_map>
25 #include <stdexcept>
26
27
28 #include <Base/Interpreter.h>
29 #include <App/Document.h>
30
31 #include "MDIViewPyWrap.h"
32 #include "PythonWrapper.h"
33
34
35 using namespace Gui;
36 namespace sp = std::placeholders;
37
38 namespace Gui
39 {
40
41 class MDIViewPyWrapImp
42 {
43 public:
44 MDIViewPyWrapImp(Py::Object pyobject)
45 : pyobject {pyobject}
46 {
47 Base::PyGILStateLocker lock;
48 std::vector<std::string> methods = {
49 "widget",
50 "onMsg",
51 "onHasMsg",
52 "canClose",
53 "printDocument",
54 "print",
55 "printPdf",
56 "printPreview",
57 "redoActions",
58 "undoActions"
59 };
60
61 for (const auto& it : methods) {
62 if (pyobject.hasAttr(it)) {
63 func[it] = pyobject.getAttr(it);
64 }
65 }
66 }
67
68 ~MDIViewPyWrapImp()
69 {
70 Base::PyGILStateLocker lock;
71 pyobject = Py::None();
72 func.clear();
73 }
74
75 QWidget* widget()
76 {
77 Base::PyGILStateLocker lock;
78 PythonWrapper wrap;
79 wrap.loadWidgetsModule();
80 if (!func.contains("widget")) {
81 throw Py::AttributeError("Object has no attribute 'widget'");
82 }
83 Py::Callable target(func.at("widget"));
84 Py::Object pywidget(target.apply(Py::Tuple()));
85 return qobject_cast<QWidget*>(wrap.toQObject(pywidget));
86 }
87
88 bool onMsg(const char* pMsg)
89 {
90 Base::PyGILStateLocker lock;
91 Py::Callable target(func.at("onMsg"));
92 Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
93 return static_cast<bool>(result);
94 }
95
96 bool onHasMsg(const char* pMsg)
97 {
98 Base::PyGILStateLocker lock;
99 Py::Callable target(func.at("onHasMsg"));
100 Py::Boolean result(target.apply(Py::TupleN(Py::String(pMsg))));
101 return static_cast<bool>(result);
102 }
103
104 bool canClose()
105 {
106 Base::PyGILStateLocker lock;
107 Py::Callable target(func.at("canClose"));
108 Py::Boolean result(target.apply(Py::Tuple()));
109 return static_cast<bool>(result);
110 }
111
112 void printDocument(QPrinter* printer)
113 {
114 Base::PyGILStateLocker lock;
115 PythonWrapper wrap;
116 wrap.loadPrintSupportModule();
117 Py::Object pyprint = wrap.fromQPrinter(printer);
118 Py::Callable target(func.at("printDocument"));
119 target.apply(Py::TupleN(pyprint));
120 }
121
122 void print()
123 {
124 Base::PyGILStateLocker lock;
125 Py::Callable target(func.at("print"));
126 target.apply(Py::Tuple());
127 }
128
129 void printPdf()
130 {
131 Base::PyGILStateLocker lock;
132 Py::Callable target(func.at("printPdf"));
133 target.apply(Py::Tuple());
134 }
135
136 void printPreview()
137 {
138 Base::PyGILStateLocker lock;
139 Py::Callable target(func.at("printPreview"));
140 target.apply(Py::Tuple());
141 }
142
143 QStringList undoActions()
144 {
145 Base::PyGILStateLocker lock;
146 Py::Callable target(func.at("undoActions"));
147 Py::List list(target.apply(Py::Tuple()));
148 QStringList actions;
149 for (const auto& it : list) {
150 Py::String str(it);
151 actions << QString::fromStdString(str);
152 }
153 return actions;
154 }
155
156 QStringList redoActions()
157 {
158 Base::PyGILStateLocker lock;
159 Py::Callable target(func.at("redoActions"));
160 Py::List list(target.apply(Py::Tuple()));
161 QStringList actions;
162 for (const auto& it : list) {
163 Py::String str(it);
164 actions << QString::fromStdString(str);
165 }
166 return actions;
167 }
168
169 private:
170 std::unordered_map<std::string, Py::Object> func;
171 Py::Object pyobject;
172 };
173
174 } // namespace Gui
175
176
177 TYPESYSTEM_SOURCE_ABSTRACT(Gui::MDIViewPyWrap, Gui::MDIView)
178
179 MDIViewPyWrap::MDIViewPyWrap(
180 const Py::Object& py,
181 Gui::Document* pcDocument,
182 QWidget* parent,
183 Qt::WindowFlags wflags
184 )
185 : MDIView(pcDocument, parent, wflags)
186 , ptr(std::make_unique<MDIViewPyWrapImp>(py))
187 {
188 try {
189 QWidget* widget = ptr->widget();
190 if (widget) {
191 setCentralWidget(widget);
192 QString title = widget->windowTitle();
193 if (!title.isEmpty()) {
194 setWindowTitle(title);
195 }
196 }
197 }
198 catch (Py::Exception&) {
199 Base::PyGILStateLocker lock;
200 Base::PyException exc;
201 exc.reportException();
202 }
203 }
204
205 MDIViewPyWrap::~MDIViewPyWrap()
206 {
207 ptr.reset(nullptr);
208 }
209
210 PyObject* MDIViewPyWrap::getPyObject()
211 {
212 return MDIView::getPyObject();
213 }
214
215 bool MDIViewPyWrap::onMsg(const char* pMsg, const char** ppReturn)
216 {
217 try {
218 if (ptr->onMsg(pMsg)) {
219 return true;
220 }
221 return MDIView::onMsg(pMsg, ppReturn);
222 }
223 catch (const std::exception&) {
224 return MDIView::onMsg(pMsg, ppReturn);
225 }
226 catch (Py::Exception&) {
227 Base::PyGILStateLocker lock;
228 Base::PyException exc;
229 exc.reportException();
230 return false;
231 }
232 }
233
234 bool MDIViewPyWrap::onHasMsg(const char* pMsg) const
235 {
236 try {
237 if (ptr->onHasMsg(pMsg)) {
238 return true;
239 }
240 return MDIView::onHasMsg(pMsg);
241 }
242 catch (const std::exception&) {
243 return MDIView::onHasMsg(pMsg);
244 }
245 catch (Py::Exception&) {
246 Base::PyGILStateLocker lock;
247 Base::PyException exc;
248 exc.reportException();
249 return false;
250 }
251 }
252
253 bool MDIViewPyWrap::canClose()
254 {
255 try {
256 return ptr->canClose();
257 }
258 catch (const std::exception&) {
259 return MDIView::canClose();
260 }
261 catch (Py::Exception&) {
262 Base::PyGILStateLocker lock;
263 Base::PyException exc;
264 exc.reportException();
265 return false;
266 }
267 }
268
269 void MDIViewPyWrap::print(QPrinter* printer)
270 {
271 try {
272 return ptr->printDocument(printer);
273 }
274 catch (const std::exception&) {
275 return MDIView::print(printer);
276 }
277 catch (Py::Exception&) {
278 Base::PyGILStateLocker lock;
279 Base::PyException exc;
280 exc.reportException();
281 }
282 }
283
284 void MDIViewPyWrap::print()
285 {
286 try {
287 return ptr->print();
288 }
289 catch (const std::exception&) {
290 return MDIView::print();
291 }
292 catch (Py::Exception&) {
293 Base::PyGILStateLocker lock;
294 Base::PyException exc;
295 exc.reportException();
296 }
297 }
298
299 void MDIViewPyWrap::printPdf()
300 {
301 try {
302 return ptr->printPdf();
303 }
304 catch (const std::exception&) {
305 return MDIView::printPdf();
306 }
307 catch (Py::Exception&) {
308 Base::PyGILStateLocker lock;
309 Base::PyException exc;
310 exc.reportException();
311 }
312 }
313
314 void MDIViewPyWrap::printPreview()
315 {
316 try {
317 return ptr->printPreview();
318 }
319 catch (const std::exception&) {
320 return MDIView::printPreview();
321 }
322 catch (Py::Exception&) {
323 Base::PyGILStateLocker lock;
324 Base::PyException exc;
325 exc.reportException();
326 }
327 }
328
329 QStringList MDIViewPyWrap::undoActions() const
330 {
331 try {
332 return ptr->undoActions();
333 }
334 catch (const std::exception&) {
335 return MDIView::undoActions();
336 }
337 catch (Py::Exception&) {
338 Base::PyGILStateLocker lock;
339 Base::PyException exc;
340 exc.reportException();
341 return MDIView::undoActions();
342 }
343 }
344
345 QStringList MDIViewPyWrap::redoActions() const
346 {
347 try {
348 return ptr->redoActions();
349 }
350 catch (const std::exception&) {
351 return MDIView::redoActions();
352 }
353 catch (Py::Exception&) {
354 Base::PyGILStateLocker lock;
355 Base::PyException exc;
356 exc.reportException();
357 return MDIView::redoActions();
358 }
359 }
360
361 #include "moc_MDIViewPyWrap.cpp"