From 249e806a316b6802ea0595ee852efd2c687fa563 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 5 Jul 2013 15:31:54 +0200 Subject: [PATCH] + playing in sandbox --- src/Mod/Sandbox/App/DocumentProtector.cpp | 24 ++++++++++++++++ src/Mod/Sandbox/App/DocumentProtector.h | 1 + src/Mod/Sandbox/App/DocumentProtectorPy.cpp | 31 +++++++++++++++++---- src/Mod/Sandbox/App/DocumentProtectorPy.h | 2 ++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/Mod/Sandbox/App/DocumentProtector.cpp b/src/Mod/Sandbox/App/DocumentProtector.cpp index a9da9bddc..36cf6042b 100644 --- a/src/Mod/Sandbox/App/DocumentProtector.cpp +++ b/src/Mod/Sandbox/App/DocumentProtector.cpp @@ -160,6 +160,25 @@ protected: const AbstractCallable& callable; }; +class CustomPurgeEvent : public AbstractCustomProtectorEvent +{ +public: + CustomPurgeEvent(App::DocumentObject* o) + : obj(o) + { + } + ~CustomPurgeEvent() + { + } + void execute() + { + obj->purgeTouched(); + } + +protected: + App::DocumentObject* obj; +}; + class DocumentReceiver : public QObject { public: @@ -324,3 +343,8 @@ void DocumentObjectProtector::execute(const AbstractCallable& call) DocumentReceiver::globalInstance()->postEventAndWait(new CustomCallableEvent(call)); } +void DocumentObjectProtector::purgeTouched() +{ + validate(); + DocumentReceiver::globalInstance()->postEventAndWait(new CustomPurgeEvent(obj)); +} diff --git a/src/Mod/Sandbox/App/DocumentProtector.h b/src/Mod/Sandbox/App/DocumentProtector.h index 6502c2642..c3ad49125 100644 --- a/src/Mod/Sandbox/App/DocumentProtector.h +++ b/src/Mod/Sandbox/App/DocumentProtector.h @@ -127,6 +127,7 @@ public: App::DocumentObject* getObject() const; bool setProperty(const std::string& name, const App::Property& p); void execute(const AbstractCallable&); + void purgeTouched(); private: void validate(); diff --git a/src/Mod/Sandbox/App/DocumentProtectorPy.cpp b/src/Mod/Sandbox/App/DocumentProtectorPy.cpp index 7110935fb..319849fb3 100644 --- a/src/Mod/Sandbox/App/DocumentProtectorPy.cpp +++ b/src/Mod/Sandbox/App/DocumentProtectorPy.cpp @@ -162,6 +162,8 @@ void DocumentObjectProtectorPy::init_type() behaviors().supportRepr(); behaviors().supportGetattr(); behaviors().supportSetattr(); + + add_varargs_method("purgeTouched",&DocumentObjectProtectorPy::purgeTouched,"purgeTouched()"); } DocumentObjectProtectorPy::DocumentObjectProtectorPy(App::DocumentObject *obj) @@ -179,6 +181,13 @@ DocumentObjectProtectorPy::~DocumentObjectProtectorPy() delete _dp; } +Py::Object DocumentObjectProtectorPy::getObject() const +{ + App::DocumentObject* obj = _dp->getObject(); + PyObject* py = obj->getPyObject(); + return Py::Object(py, true); +} + Py::Object DocumentObjectProtectorPy::repr() { std::string s; @@ -201,10 +210,11 @@ Py::Object DocumentObjectProtectorPy::getattr(const char * attr) App::DocumentObject* obj = _dp->getObject(); App::Property* prop = obj->getPropertyByName(attr); if (!prop) { - std::string s; - std::ostringstream s_out; - s_out << "No such attribute '" << attr << "'"; - throw Py::AttributeError(s_out.str()); + return Py::PythonExtension::getattr(attr); + //std::string s; + //std::ostringstream s_out; + //s_out << "No such attribute '" << attr << "'"; + //throw Py::AttributeError(s_out.str()); } return Py::asObject(prop->getPyObject()); @@ -231,7 +241,18 @@ int DocumentObjectProtectorPy::setattr(const char * attr, const Py::Object & val Base::PyGILStateRelease unlock; std::auto_ptr copy(static_cast (prop->getTypeId().createInstance())); - copy->setPyObject(value.ptr()); + if (PyObject_TypeCheck(value.ptr(), DocumentObjectProtectorPy::type_object())) { + copy->setPyObject(static_cast(value.ptr())->getObject().ptr()); + } + else { + copy->setPyObject(value.ptr()); + } return _dp->setProperty(attr, *copy) ? 0 : -1; } } + +Py::Object DocumentObjectProtectorPy::purgeTouched(const Py::Tuple&) +{ + _dp->purgeTouched(); + return Py::None(); +} diff --git a/src/Mod/Sandbox/App/DocumentProtectorPy.h b/src/Mod/Sandbox/App/DocumentProtectorPy.h index 318aac529..368057e53 100644 --- a/src/Mod/Sandbox/App/DocumentProtectorPy.h +++ b/src/Mod/Sandbox/App/DocumentProtectorPy.h @@ -72,7 +72,9 @@ public: Py::Object repr(); Py::Object getattr(const char *); + Py::Object getObject() const; int setattr(const char *, const Py::Object &); + Py::Object purgeTouched(const Py::Tuple&); private: DocumentObjectProtector* _dp;