DocumentObserverPython.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025
00026 #ifndef _PreComp_
00027 #endif
00028
00029 #include "Application.h"
00030 #include "Document.h"
00031 #include "DocumentObject.h"
00032 #include "DocumentObserverPython.h"
00033 #include <Base/Interpreter.h>
00034 #include <Base/Console.h>
00035
00036 using namespace App;
00037
00038 std::vector<DocumentObserverPython*> DocumentObserverPython::_instances;
00039
00040 void DocumentObserverPython::addObserver(const Py::Object& obj)
00041 {
00042 _instances.push_back(new DocumentObserverPython(obj));
00043 }
00044
00045 void DocumentObserverPython::removeObserver(const Py::Object& obj)
00046 {
00047 DocumentObserverPython* obs=0;
00048 for (std::vector<DocumentObserverPython*>::iterator it =
00049 _instances.begin(); it != _instances.end(); ++it) {
00050 if ((*it)->inst == obj) {
00051 obs = *it;
00052 _instances.erase(it);
00053 break;
00054 }
00055 }
00056
00057 delete obs;
00058 }
00059
00060 DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj)
00061 {
00062 this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
00063 (&DocumentObserverPython::slotCreatedDocument, this, _1));
00064 this->connectApplicationDeletedDocument = App::GetApplication().signalDeleteDocument.connect(boost::bind
00065 (&DocumentObserverPython::slotDeletedDocument, this, _1));
00066 this->connectApplicationRelabelDocument = App::GetApplication().signalRelabelDocument.connect(boost::bind
00067 (&DocumentObserverPython::slotRelabelDocument, this, _1));
00068 this->connectApplicationActivateDocument = App::GetApplication().signalActiveDocument.connect(boost::bind
00069 (&DocumentObserverPython::slotActivateDocument, this, _1));
00070
00071 this->connectDocumentCreatedObject = App::GetApplication().signalNewObject.connect(boost::bind
00072 (&DocumentObserverPython::slotCreatedObject, this, _1));
00073 this->connectDocumentDeletedObject = App::GetApplication().signalDeletedObject.connect(boost::bind
00074 (&DocumentObserverPython::slotDeletedObject, this, _1));
00075 this->connectDocumentChangedObject = App::GetApplication().signalChangedObject.connect(boost::bind
00076 (&DocumentObserverPython::slotChangedObject, this, _1, _2));
00077 }
00078
00079 DocumentObserverPython::~DocumentObserverPython()
00080 {
00081 this->connectApplicationCreatedDocument.disconnect();
00082 this->connectApplicationDeletedDocument.disconnect();
00083 this->connectApplicationRelabelDocument.disconnect();
00084 this->connectApplicationActivateDocument.disconnect();
00085
00086 this->connectDocumentCreatedObject.disconnect();
00087 this->connectDocumentDeletedObject.disconnect();
00088 this->connectDocumentChangedObject.disconnect();
00089 }
00090
00091 void DocumentObserverPython::slotCreatedDocument(const App::Document& Doc)
00092 {
00093 Base::PyGILStateLocker lock;
00094 try {
00095 if (this->inst.hasAttr(std::string("slotCreatedDocument"))) {
00096 Py::Callable method(this->inst.getAttr(std::string("slotCreatedDocument")));
00097 Py::Tuple args(1);
00098 args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
00099 method.apply(args);
00100 }
00101 }
00102 catch (Py::Exception&) {
00103 Base::PyException e;
00104 Base::Console().Error("%s\n", e.what());
00105 }
00106 }
00107
00108 void DocumentObserverPython::slotDeletedDocument(const App::Document& Doc)
00109 {
00110 Base::PyGILStateLocker lock;
00111 try {
00112 if (this->inst.hasAttr(std::string("slotDeletedDocument"))) {
00113 Py::Callable method(this->inst.getAttr(std::string("slotDeletedDocument")));
00114 Py::Tuple args(1);
00115 args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
00116 method.apply(args);
00117 }
00118 }
00119 catch (Py::Exception&) {
00120 Base::PyException e;
00121 Base::Console().Error("%s\n", e.what());
00122 }
00123 }
00124
00125 void DocumentObserverPython::slotRelabelDocument(const App::Document& Doc)
00126 {
00127 Base::PyGILStateLocker lock;
00128 try {
00129 if (this->inst.hasAttr(std::string("slotRelabelDocument"))) {
00130 Py::Callable method(this->inst.getAttr(std::string("slotRelabelDocument")));
00131 Py::Tuple args(1);
00132 args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
00133 method.apply(args);
00134 }
00135 }
00136 catch (Py::Exception&) {
00137 Base::PyException e;
00138 Base::Console().Error("%s\n", e.what());
00139 }
00140 }
00141
00142 void DocumentObserverPython::slotActivateDocument(const App::Document& Doc)
00143 {
00144 Base::PyGILStateLocker lock;
00145 try {
00146 if (this->inst.hasAttr(std::string("slotActivateDocument"))) {
00147 Py::Callable method(this->inst.getAttr(std::string("slotActivateDocument")));
00148 Py::Tuple args(1);
00149 args.setItem(0, Py::Object(const_cast<App::Document&>(Doc).getPyObject(), true));
00150 method.apply(args);
00151 }
00152 }
00153 catch (Py::Exception&) {
00154 Base::PyException e;
00155 Base::Console().Error("%s\n", e.what());
00156 }
00157 }
00158
00159 void DocumentObserverPython::slotCreatedObject(const App::DocumentObject& Obj)
00160 {
00161 Base::PyGILStateLocker lock;
00162 try {
00163 if (this->inst.hasAttr(std::string("slotCreatedObject"))) {
00164 Py::Callable method(this->inst.getAttr(std::string("slotCreatedObject")));
00165 Py::Tuple args(1);
00166 args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
00167 method.apply(args);
00168 }
00169 }
00170 catch (Py::Exception&) {
00171 Base::PyException e;
00172 Base::Console().Error("%s\n", e.what());
00173 }
00174 }
00175
00176 void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj)
00177 {
00178 Base::PyGILStateLocker lock;
00179 try {
00180 if (this->inst.hasAttr(std::string("slotDeletedObject"))) {
00181 Py::Callable method(this->inst.getAttr(std::string("slotDeletedObject")));
00182 Py::Tuple args(1);
00183 args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
00184 method.apply(args);
00185 }
00186 }
00187 catch (Py::Exception&) {
00188 Base::PyException e;
00189 Base::Console().Error("%s\n", e.what());
00190 }
00191 }
00192
00193 void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj,
00194 const App::Property& Prop)
00195 {
00196 Base::PyGILStateLocker lock;
00197 try {
00198 if (this->inst.hasAttr(std::string("slotChangedObject"))) {
00199 Py::Callable method(this->inst.getAttr(std::string("slotChangedObject")));
00200 Py::Tuple args(2);
00201 args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
00202 std::string prop_name = Obj.getName(&Prop);
00203 args.setItem(1, Py::String(prop_name));
00204 method.apply(args);
00205 }
00206 }
00207 catch (Py::Exception&) {
00208 Base::PyException e;
00209 Base::Console().Error("%s\n", e.what());
00210 }
00211 }