Debian Codegraph @

src/App/DocumentObserverPython.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2009 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 <functional>
24
25 #include <CXX/Objects.hxx>
26 #include "Application.h"
27 #include "Document.h"
28 #include "DocumentObject.h"
29 #include "DocumentObserverPython.h"
30 #include <Base/Interpreter.h>
31
32
33 using namespace App;
34 namespace sp = std::placeholders;
35
36 std::vector<DocumentObserverPython*> DocumentObserverPython::_instances;
37
38 void DocumentObserverPython::addObserver(const Py::Object& obj)
39 {
40 _instances.push_back(new DocumentObserverPython(obj));
41 }
42
43 void DocumentObserverPython::removeObserver(const Py::Object& obj)
44 {
45 DocumentObserverPython* obs = nullptr;
46 for (std::vector<DocumentObserverPython*>::iterator it = _instances.begin();
47 it != _instances.end();
48 ++it) {
49 if ((*it)->inst == obj) {
50 obs = *it;
51 _instances.erase(it);
52 break;
53 }
54 }
55
56 delete obs;
57 }
58
59 DocumentObserverPython::DocumentObserverPython(const Py::Object& obj)
60 : inst(obj)
61 {
62 #define FC_PY_ELEMENT_ARG0(_name1, _name2) \
63 do { \
64 FC_PY_GetCallable(obj.ptr(), "slot" #_name1, py##_name1.py); \
65 if (!py##_name1.py.isNone()) \
66 py##_name1.slot = App::GetApplication().signal##_name2.connect( \
67 std::bind(&DocumentObserverPython::slot##_name1, this)); \
68 } while (0);
69
70
71 #define FC_PY_ELEMENT_ARG1(_name1, _name2) \
72 do { \
73 FC_PY_GetCallable(obj.ptr(), "slot" #_name1, py##_name1.py); \
74 if (!py##_name1.py.isNone()) \
75 py##_name1.slot = App::GetApplication().signal##_name2.connect( \
76 std::bind(&DocumentObserverPython::slot##_name1, this, sp::_1)); \
77 } while (0);
78
79 // NOLINTBEGIN
80 #define FC_PY_ELEMENT_ARG2(_name1, _name2) \
81 do { \
82 FC_PY_GetCallable(obj.ptr(), "slot" #_name1, py##_name1.py); \
83 if (!py##_name1.py.isNone()) \
84 py##_name1.slot = App::GetApplication().signal##_name2.connect( \
85 std::bind(&DocumentObserverPython::slot##_name1, this, sp::_1, sp::_2)); \
86 } while (0);
87
88 FC_PY_ELEMENT_ARG1(CreatedDocument, NewDocument)
89 FC_PY_ELEMENT_ARG1(DeletedDocument, DeleteDocument)
90 FC_PY_ELEMENT_ARG1(RelabelDocument, RelabelDocument)
91 FC_PY_ELEMENT_ARG1(ActivateDocument, ActiveDocument)
92 FC_PY_ELEMENT_ARG1(UndoDocument, UndoDocument)
93 FC_PY_ELEMENT_ARG1(RedoDocument, RedoDocument)
94 FC_PY_ELEMENT_ARG2(BeforeChangeDocument, BeforeChangeDocument)
95 FC_PY_ELEMENT_ARG2(ChangedDocument, ChangedDocument)
96 FC_PY_ELEMENT_ARG1(CreatedObject, NewObject)
97 FC_PY_ELEMENT_ARG1(DeletedObject, DeletedObject)
98 FC_PY_ELEMENT_ARG2(BeforeChangeObject, BeforeChangeObject)
99 FC_PY_ELEMENT_ARG2(ChangedObject, ChangedObject)
100 FC_PY_ELEMENT_ARG1(RecomputedObject, ObjectRecomputed)
101 FC_PY_ELEMENT_ARG1(BeforeRecomputeDocument, BeforeRecomputeDocument)
102 FC_PY_ELEMENT_ARG1(RecomputedDocument, Recomputed)
103 FC_PY_ELEMENT_ARG2(OpenTransaction, OpenTransaction)
104 FC_PY_ELEMENT_ARG1(CommitTransaction, CommitTransaction)
105 FC_PY_ELEMENT_ARG1(AbortTransaction, AbortTransaction)
106 FC_PY_ELEMENT_ARG0(Undo, Undo)
107 FC_PY_ELEMENT_ARG0(Redo, Redo)
108 FC_PY_ELEMENT_ARG1(BeforeCloseTransaction, BeforeCloseTransaction)
109 FC_PY_ELEMENT_ARG1(CloseTransaction, CloseTransaction)
110 FC_PY_ELEMENT_ARG2(StartSaveDocument, StartSaveDocument)
111 FC_PY_ELEMENT_ARG2(FinishSaveDocument, FinishSaveDocument)
112 FC_PY_ELEMENT_ARG1(AppendDynamicProperty, AppendDynamicProperty)
113 FC_PY_ELEMENT_ARG1(RemoveDynamicProperty, RemoveDynamicProperty)
114 FC_PY_ELEMENT_ARG2(ChangePropertyEditor, ChangePropertyEditor)
115 FC_PY_ELEMENT_ARG2(BeforeAddingDynamicExtension, BeforeAddingDynamicExtension)
116 FC_PY_ELEMENT_ARG2(AddedDynamicExtension, AddedDynamicExtension)
117 // NOLINTEND
118 }
119
120 DocumentObserverPython::~DocumentObserverPython() = default;
121
122 void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc)
123 {
124 Base::PyGILStateLocker lock;
125 try {
126 Py::Tuple args(1);
127 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
128 Base::pyCall(pyCreatedDocument.ptr(), args.ptr());
129 }
130 catch (Py::Exception&) {
131 Base::PyException e; // extract the Python error text
132 e.reportException();
133 }
134 }
135
136 void DocumentObserverPython::slotDeletedDocument(const App::Document& Doc)
137 {
138 Base::PyGILStateLocker lock;
139 try {
140 Py::Tuple args(1);
141 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
142 Base::pyCall(pyDeletedDocument.ptr(), args.ptr());
143 }
144 catch (Py::Exception&) {
145 Base::PyException e; // extract the Python error text
146 e.reportException();
147 }
148 }
149
150 void DocumentObserverPython::slotRelabelDocument(const App::Document& Doc)
151 {
152 Base::PyGILStateLocker lock;
153 try {
154 Py::Tuple args(1);
155 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
156 Base::pyCall(pyRelabelDocument.ptr(), args.ptr());
157 }
158 catch (Py::Exception&) {
159 Base::PyException e; // extract the Python error text
160 e.reportException();
161 }
162 }
163
164 void DocumentObserverPython::slotActivateDocument(const App::Document& Doc)
165 {
166 Base::PyGILStateLocker lock;
167 try {
168 Py::Tuple args(1);
169 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
170 Base::pyCall(pyActivateDocument.ptr(), args.ptr());
171 }
172 catch (Py::Exception&) {
173 Base::PyException e; // extract the Python error text
174 e.reportException();
175 }
176 }
177
178 void DocumentObserverPython::slotUndoDocument(const App::Document& Doc)
179 {
180 Base::PyGILStateLocker lock;
181 try {
182 Py::Tuple args(1);
183 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
184 Base::pyCall(pyUndoDocument.ptr(), args.ptr());
185 }
186 catch (Py::Exception&) {
187 Base::PyException e; // extract the Python error text
188 e.reportException();
189 }
190 }
191
192
193 void DocumentObserverPython::slotRedoDocument(const App::Document& Doc)
194 {
195 Base::PyGILStateLocker lock;
196 try {
197 Py::Tuple args(1);
198 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
199 Base::pyCall(pyRedoDocument.ptr(), args.ptr());
200 }
201 catch (Py::Exception&) {
202 Base::PyException e; // extract the Python error text
203 e.reportException();
204 }
205 }
206
207 void DocumentObserverPython::slotUndo()
208 {
209 Base::PyGILStateLocker lock;
210 try {
211 Base::pyCall(pyUndo.ptr());
212 }
213 catch (Py::Exception&) {
214 Base::PyException e; // extract the Python error text
215 e.reportException();
216 }
217 }
218
219 void DocumentObserverPython::slotRedo()
220 {
221 Base::PyGILStateLocker lock;
222 try {
223 Base::pyCall(pyRedo.ptr());
224 }
225 catch (Py::Exception&) {
226 Base::PyException e; // extract the Python error text
227 e.reportException();
228 }
229 }
230
231 void DocumentObserverPython::slotBeforeCloseTransaction(bool abort)
232 {
233 Base::PyGILStateLocker lock;
234 try {
235 Py::Tuple args(1);
236 args.setItem(0, Py::Boolean(abort));
237 Base::pyCall(pyBeforeCloseTransaction.ptr(), args.ptr());
238 }
239 catch (Py::Exception&) {
240 Base::PyException e; // extract the Python error text
241 e.reportException();
242 }
243 }
244
245 void DocumentObserverPython::slotCloseTransaction(bool abort)
246 {
247 Base::PyGILStateLocker lock;
248 try {
249 Py::Tuple args(1);
250 args.setItem(0, Py::Boolean(abort));
251 Base::pyCall(pyCloseTransaction.ptr(), args.ptr());
252 }
253 catch (Py::Exception&) {
254 Base::PyException e; // extract the Python error text
255 e.reportException();
256 }
257 }
258
259 void DocumentObserverPython::slotBeforeChangeDocument(const App::Document& Doc,
260 const App::Property& Prop)
261 {
262 Base::PyGILStateLocker lock;
263 try {
264 Py::Tuple args(2);
265 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
266 // If a property is touched but not part of a document object then its name is null.
267 // In this case the slot function must not be called.
268 const char* prop_name = Doc.getPropertyName(&Prop);
269 if (prop_name) {
270 args.setItem(1, Py::String(prop_name));
271 Base::pyCall(pyBeforeChangeDocument.ptr(), args.ptr());
272 }
273 }
274 catch (Py::Exception&) {
275 Base::PyException e; // extract the Python error text
276 e.reportException();
277 }
278 }
279
280 void DocumentObserverPython::slotChangedDocument(const App::Document& Doc,
281 const App::Property& Prop)
282 {
283 Base::PyGILStateLocker lock;
284 try {
285 Py::Tuple args(2);
286 args.setItem(0, Py::asObject(const_cast<App::Document&>(Doc).getPyObject()));
287 // If a property is touched but not part of a document object then its name is null.
288 // In this case the slot function must not be called.
289 const char* prop_name = Doc.getPropertyName(&Prop);
290 if (prop_name) {
291 args.setItem(1, Py::String(prop_name));
292 Base::pyCall(pyChangedDocument.ptr(), args.ptr());
293 }
294 }
295 catch (Py::Exception&) {
296 Base::PyException e; // extract the Python error text
297 e.reportException();
298 }
299 }
300
301 void DocumentObserverPython::slotCreatedObject(const App::DocumentObject& Obj)
302 {
303 Base::PyGILStateLocker lock;
304 try {
305 Py::Tuple args(1);
306 args.setItem(0, Py::asObject(const_cast<App::DocumentObject&>(Obj).getPyObject()));
307 Base::pyCall(pyCreatedObject.ptr(), args.ptr());
308 }
309 catch (Py::Exception&) {
310 Base::PyException e; // extract the Python error text
311 e.reportException();
312 }
313 }
314
315 void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj)
316 {
317 Base::PyGILStateLocker lock;
318 try {
319 Py::Tuple args(1);
320 args.setItem(0, Py::asObject(const_cast<App::DocumentObject&>(Obj).getPyObject()));
321 Base::pyCall(pyDeletedObject.ptr(), args.ptr());
322 }
323 catch (Py::Exception&) {
324 Base::PyException e; // extract the Python error text
325 e.reportException();
326 }
327 }
328
329 void DocumentObserverPython::slotBeforeChangeObject(const App::DocumentObject& Obj,
330 const App::Property& Prop)
331 {
332 Base::PyGILStateLocker lock;
333 try {
334 Py::Tuple args(2);
335 args.setItem(0, Py::asObject(const_cast<App::DocumentObject&>(Obj).getPyObject()));
336 // If a property is touched but not part of a document object then its name is null.
337 // In this case the slot function must not be called.
338 const char* prop_name = Obj.getPropertyName(&Prop);
339 if (prop_name) {
340 args.setItem(1, Py::String(prop_name));
341 Base::pyCall(pyBeforeChangeObject.ptr(), args.ptr());
342 }
343 }
344 catch (Py::Exception&) {
345 Base::PyException e; // extract the Python error text
346 e.reportException();
347 }
348 }
349
350 void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj,
351 const App::Property& Prop)
352 {
353 Base::PyGILStateLocker lock;
354 try {
355 Py::Tuple args(2);
356 args.setItem(0, Py::asObject(const_cast<App::DocumentObject&>(Obj).getPyObject()));
357 // If a property is touched but not part of a document object then its name is null.
358 // In this case the slot function must not be called.
359 const char* prop_name = Obj.getPropertyName(&Prop);
360 if (prop_name) {
361 args.setItem(1, Py::String(prop_name));
362 Base::pyCall(pyChangedObject.ptr(), args.ptr());
363 }
364 }
365 catch (Py::Exception&) {
366 Base::PyException e; // extract the Python error text
367 e.reportException();
368 }
369 }
370
371 void DocumentObserverPython::slotRecomputedObject(const App::DocumentObject& Obj)
372 {
373 Base::PyGILStateLocker lock;
374 try {
375 Py::Tuple args(1);
376 args.setItem(0, Py::asObject(const_cast<App::DocumentObject&>(Obj).getPyObject()));
377 Base::pyCall(pyRecomputedObject.ptr(), args.ptr());
378 }
379 catch (Py::Exception&) {
380 Base::PyException e; // extract the Python error text
381 e.reportException();
382 }
383 }
384
385 void DocumentObserverPython::slotRecomputedDocument(const App::Document& doc)
386 {
387 Base::PyGILStateLocker lock;
388 try {
389 Py::Tuple args(1);
390 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
391 Base::pyCall(pyRecomputedDocument.ptr(), args.ptr());
392 }
393 catch (Py::Exception&) {
394 Base::PyException e; // extract the Python error text
395 e.reportException();
396 }
397 }
398
399 void DocumentObserverPython::slotBeforeRecomputeDocument(const App::Document& doc)
400 {
401 Base::PyGILStateLocker lock;
402 try {
403 Py::Tuple args(1);
404 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
405 Base::pyCall(pyBeforeRecomputeDocument.ptr(), args.ptr());
406 }
407 catch (Py::Exception&) {
408 Base::PyException e; // extract the Python error text
409 e.reportException();
410 }
411 }
412
413 void DocumentObserverPython::slotOpenTransaction(const App::Document& doc, std::string str)
414 {
415 Base::PyGILStateLocker lock;
416 try {
417 Py::Tuple args(2);
418 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
419 args.setItem(1, Py::String(str));
420 Base::pyCall(pyOpenTransaction.ptr(), args.ptr());
421 }
422 catch (Py::Exception&) {
423 Base::PyException e; // extract the Python error text
424 e.reportException();
425 }
426 }
427
428 void DocumentObserverPython::slotCommitTransaction(const App::Document& doc)
429 {
430 Base::PyGILStateLocker lock;
431 try {
432 Py::Tuple args(1);
433 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
434 Base::pyCall(pyCommitTransaction.ptr(), args.ptr());
435 }
436 catch (Py::Exception&) {
437 Base::PyException e; // extract the Python error text
438 e.reportException();
439 }
440 }
441
442 void DocumentObserverPython::slotAbortTransaction(const App::Document& doc)
443 {
444 Base::PyGILStateLocker lock;
445 try {
446 Py::Tuple args(1);
447 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
448 Base::pyCall(pyAbortTransaction.ptr(), args.ptr());
449 }
450 catch (Py::Exception&) {
451 Base::PyException e; // extract the Python error text
452 e.reportException();
453 }
454 }
455
456 void DocumentObserverPython::slotAppendDynamicProperty(const App::Property& Prop)
457 {
458 Base::PyGILStateLocker lock;
459 try {
460 auto container = Prop.getContainer();
461 Py::Tuple args(2);
462 args.setItem(0, Py::asObject(container->getPyObject()));
463 // If a property is touched but not part of a document object then its name is null.
464 // In this case the slot function must not be called.
465 const char* prop_name = container->getPropertyName(&Prop);
466 if (prop_name) {
467 args.setItem(1, Py::String(prop_name));
468 Base::pyCall(pyAppendDynamicProperty.ptr(), args.ptr());
469 }
470 }
471 catch (Py::Exception&) {
472 Base::PyException e; // extract the Python error text
473 e.reportException();
474 }
475 }
476
477 void DocumentObserverPython::slotRemoveDynamicProperty(const App::Property& Prop)
478 {
479 Base::PyGILStateLocker lock;
480 try {
481 auto container = Prop.getContainer();
482 Py::Tuple args(2);
483 args.setItem(0, Py::asObject(container->getPyObject()));
484 // If a property is touched but not part of a document object then its name is null.
485 // In this case the slot function must not be called.
486 const char* prop_name = container->getPropertyName(&Prop);
487 if (prop_name) {
488 args.setItem(1, Py::String(prop_name));
489 Base::pyCall(pyRemoveDynamicProperty.ptr(), args.ptr());
490 }
491 }
492 catch (Py::Exception&) {
493 Base::PyException e; // extract the Python error text
494 e.reportException();
495 }
496 }
497
498 void DocumentObserverPython::slotChangePropertyEditor(const App::Document&,
499 const App::Property& Prop)
500 {
501 Base::PyGILStateLocker lock;
502 try {
503 auto container = Prop.getContainer();
504 Py::Tuple args(2);
505 args.setItem(0, Py::asObject(container->getPyObject()));
506 // If a property is touched but not part of a document object then its name is null.
507 // In this case the slot function must not be called.
508 const char* prop_name = container->getPropertyName(&Prop);
509 if (prop_name) {
510 args.setItem(1, Py::String(prop_name));
511 Base::pyCall(pyChangePropertyEditor.ptr(), args.ptr());
512 }
513 }
514 catch (Py::Exception&) {
515 Base::PyException e; // extract the Python error text
516 e.reportException();
517 }
518 }
519
520 void DocumentObserverPython::slotStartSaveDocument(const App::Document& doc,
521 const std::string& file)
522 {
523 Base::PyGILStateLocker lock;
524 try {
525 Py::Tuple args(2);
526 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
527 args.setItem(1, Py::String(file));
528 Base::pyCall(pyStartSaveDocument.ptr(), args.ptr());
529 }
530 catch (Py::Exception&) {
531 Base::PyException e; // extract the Python error text
532 e.reportException();
533 }
534 }
535
536 void DocumentObserverPython::slotFinishSaveDocument(const App::Document& doc,
537 const std::string& file)
538 {
539 Base::PyGILStateLocker lock;
540 try {
541 Py::Tuple args(2);
542 args.setItem(0, Py::asObject(const_cast<App::Document&>(doc).getPyObject()));
543 args.setItem(1, Py::String(file));
544 Base::pyCall(pyFinishSaveDocument.ptr(), args.ptr());
545 }
546 catch (Py::Exception&) {
547 Base::PyException e; // extract the Python error text
548 e.reportException();
549 }
550 }
551
552 void DocumentObserverPython::slotBeforeAddingDynamicExtension(
553 const App::ExtensionContainer& extcont,
554 std::string extension)
555 {
556 Base::PyGILStateLocker lock;
557 try {
558 Py::Tuple args(2);
559 args.setItem(0, Py::asObject(const_cast<App::ExtensionContainer&>(extcont).getPyObject()));
560 args.setItem(1, Py::String(extension));
561 Base::pyCall(pyBeforeAddingDynamicExtension.ptr(), args.ptr());
562 }
563 catch (Py::Exception&) {
564 Base::PyException e; // extract the Python error text
565 e.reportException();
566 }
567 }
568
569 void DocumentObserverPython::slotAddedDynamicExtension(const App::ExtensionContainer& extcont,
570 std::string extension)
571 {
572 Base::PyGILStateLocker lock;
573 try {
574 Py::Tuple args(2);
575 args.setItem(0, Py::asObject(const_cast<App::ExtensionContainer&>(extcont).getPyObject()));
576 args.setItem(1, Py::String(extension));
577 Base::pyCall(pyAddedDynamicExtension.ptr(), args.ptr());
578 }
579 catch (Py::Exception&) {
580 Base::PyException e; // extract the Python error text
581 e.reportException();
582 }
583 }