1
/***************************************************************************
2
* Copyright (c) 2009 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
***************************************************************************/
24
#include "PreCompiled.h"
29
#include "Application.h"
31
#include "DocumentObject.h"
32
#include "DocumentObserverPython.h"
33
#include <Base/Interpreter.h>
34
#include <Base/Console.h>
38
std::vector<DocumentObserverPython*> DocumentObserverPython::_instances;
40
void DocumentObserverPython::addObserver(const Py::Object& obj)
42
_instances.push_back(new DocumentObserverPython(obj));
45
void DocumentObserverPython::removeObserver(const Py::Object& obj)
47
DocumentObserverPython* obs=0;
48
for (std::vector<DocumentObserverPython*>::iterator it =
49
_instances.begin(); it != _instances.end(); ++it) {
50
if ((*it)->inst == obj) {
60
DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj)
62
this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
63
(&DocumentObserverPython::slotCreatedDocument, this, _1));
64
this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind
65
(&DocumentObserverPython::slotDeletedDocument, this, _1));
66
this->connectApplicationRelabelDocument = App::GetApplication().signalRelabelDocument.connect(boost::bind
67
(&DocumentObserverPython::slotRelabelDocument, this, _1));
68
this->connectApplicationActivateDocument = App::GetApplication().signalActiveDocument.connect(boost::bind
69
(&DocumentObserverPython::slotActivateDocument, this, _1));
71
this->connectDocumentCreatedObject = App::GetApplication().signalNewObject.connect(boost::bind
72
(&DocumentObserverPython::slotCreatedObject, this, _1));
73
this->connectDocumentDeletedObject = App::GetApplication().signalDeletedObject.connect(boost::bind
74
(&DocumentObserverPython::slotDeletedObject, this, _1));
75
this->connectDocumentChangedObject = App::GetApplication().signalChangedObject.connect(boost::bind
76
(&DocumentObserverPython::slotChangedObject, this, _1, _2));
79
DocumentObserverPython::~DocumentObserverPython()
81
this->connectApplicationCreatedDocument.disconnect();
82
this->connectApplicationDeletedDocument.disconnect();
83
this->connectApplicationRelabelDocument.disconnect();
84
this->connectApplicationActivateDocument.disconnect();
86
this->connectDocumentCreatedObject.disconnect();
87
this->connectDocumentDeletedObject.disconnect();
88
this->connectDocumentChangedObject.disconnect();
91
void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc)
93
Base::PyGILStateLocker lock;
95
if (this->inst.hasAttr(std::string("slotCreatedDocument"))) {
96
Py::Callable method(this->inst.getAttr(std::string("slotCreatedDocument")));
98
args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
102
catch (Py::Exception&) {
103
Base::PyException e; // extract the Python error text
104
Base::Console().Error("%s\n", e.what());
108
void DocumentObserverPython::slotDeletedDocument(const App::Document& Doc)
110
Base::PyGILStateLocker lock;
112
if (this->inst.hasAttr(std::string("slotDeletedDocument"))) {
113
Py::Callable method(this->inst.getAttr(std::string("slotDeletedDocument")));
115
args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
119
catch (Py::Exception&) {
120
Base::PyException e; // extract the Python error text
121
Base::Console().Error("%s\n", e.what());
125
void DocumentObserverPython::slotRelabelDocument(const App::Document& Doc)
127
Base::PyGILStateLocker lock;
129
if (this->inst.hasAttr(std::string("slotRelabelDocument"))) {
130
Py::Callable method(this->inst.getAttr(std::string("slotRelabelDocument")));
132
args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
136
catch (Py::Exception&) {
137
Base::PyException e; // extract the Python error text
138
Base::Console().Error("%s\n", e.what());
142
void DocumentObserverPython::slotActivateDocument(const App::Document& Doc)
144
Base::PyGILStateLocker lock;
146
if (this->inst.hasAttr(std::string("slotActivateDocument"))) {
147
Py::Callable method(this->inst.getAttr(std::string("slotActivateDocument")));
149
args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
153
catch (Py::Exception&) {
154
Base::PyException e; // extract the Python error text
155
Base::Console().Error("%s\n", e.what());
159
void DocumentObserverPython::slotCreatedObject(const App::DocumentObject& Obj)
161
Base::PyGILStateLocker lock;
163
if (this->inst.hasAttr(std::string("slotCreatedObject"))) {
164
Py::Callable method(this->inst.getAttr(std::string("slotCreatedObject")));
166
args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
170
catch (Py::Exception&) {
171
Base::PyException e; // extract the Python error text
172
Base::Console().Error("%s\n", e.what());
176
void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj)
178
Base::PyGILStateLocker lock;
180
if (this->inst.hasAttr(std::string("slotDeletedObject"))) {
181
Py::Callable method(this->inst.getAttr(std::string("slotDeletedObject")));
183
args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
187
catch (Py::Exception&) {
188
Base::PyException e; // extract the Python error text
189
Base::Console().Error("%s\n", e.what());
193
void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj,
194
const App::Property& Prop)
196
Base::PyGILStateLocker lock;
198
if (this->inst.hasAttr(std::string("slotChangedObject"))) {
199
Py::Callable method(this->inst.getAttr(std::string("slotChangedObject")));
201
args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
202
std::string prop_name = Obj.getName(&Prop);
203
args.setItem(1, Py::String(prop_name));
207
catch (Py::Exception&) {
208
Base::PyException e; // extract the Python error text
209
Base::Console().Error("%s\n", e.what());