Expose onBeforeChange to python document observer

This commit is contained in:
ickby
2017-08-15 06:26:26 +02:00
committed by wmayer
parent 0edf6892b3
commit 238c8a8567
6 changed files with 42 additions and 0 deletions

View File

@@ -418,6 +418,7 @@ Document* Application::newDocument(const char * Name, const char * UserName)
// connect the signals to the application for the new document
_pActiveDoc->signalNewObject.connect(boost::bind(&App::Application::slotNewObject, this, _1));
_pActiveDoc->signalDeletedObject.connect(boost::bind(&App::Application::slotDeletedObject, this, _1));
_pActiveDoc->signalBeforeChangeObject.connect(boost::bind(&App::Application::slotBeforeChangeObject, this, _1, _2));
_pActiveDoc->signalChangedObject.connect(boost::bind(&App::Application::slotChangedObject, this, _1, _2));
_pActiveDoc->signalRelabelObject.connect(boost::bind(&App::Application::slotRelabelObject, this, _1));
_pActiveDoc->signalActivatedObject.connect(boost::bind(&App::Application::slotActivatedObject, this, _1));
@@ -987,6 +988,11 @@ void Application::slotDeletedObject(const App::DocumentObject&O)
this->signalDeletedObject(O);
}
void Application::slotBeforeChangeObject(const DocumentObject& O, const Property& Prop)
{
this->signalBeforeChangeObject(O, Prop);
}
void Application::slotChangedObject(const App::DocumentObject&O, const App::Property& P)
{
this->signalChangedObject(O,P);

View File

@@ -134,6 +134,8 @@ public:
/// signal on deleted Object
boost::signal<void (const App::DocumentObject&)> signalDeletedObject;
/// signal on changed Object
boost::signal<void (const App::DocumentObject&, const App::Property&)> signalBeforeChangeObject;
/// signal on changed Object
boost::signal<void (const App::DocumentObject&, const App::Property&)> signalChangedObject;
/// signal on relabeled Object
boost::signal<void (const App::DocumentObject&)> signalRelabelObject;
@@ -276,6 +278,7 @@ protected:
//@{
void slotNewObject(const App::DocumentObject&);
void slotDeletedObject(const App::DocumentObject&);
void slotBeforeChangeObject(const App::DocumentObject&, const App::Property& Prop);
void slotChangedObject(const App::DocumentObject&, const App::Property& Prop);
void slotRelabelObject(const App::DocumentObject&);
void slotActivatedObject(const App::DocumentObject&);

View File

@@ -1140,6 +1140,9 @@ void Document::onChanged(const Property* prop)
void Document::onBeforeChangeProperty(const TransactionalObject *Who, const Property *What)
{
if(Who->isDerivedFrom(App::DocumentObject::getClassTypeId())
signalBeforeChangeObject(*static_cast<App::DocuemntObject*>(Who), *What);
if (d->activeUndoTransaction && !d->rollback)
d->activeUndoTransaction->addObjectChange(Who,What);
}

View File

@@ -117,6 +117,8 @@ public:
//boost::signal<void (const App::DocumentObject&)> m_sig;
/// signal on deleted Object
boost::signal<void (const App::DocumentObject&)> signalDeletedObject;
/// signal before changing an Object
boost::signal<void (const App::DocumentObject&, const App::Property&)> signalBeforeChangeObject;
/// signal on changed Object
boost::signal<void (const App::DocumentObject&, const App::Property&)> signalChangedObject;
/// signal on relabeled Object

View File

@@ -76,6 +76,8 @@ DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj
(&DocumentObserverPython::slotCreatedObject, this, _1));
this->connectDocumentDeletedObject = App::GetApplication().signalDeletedObject.connect(boost::bind
(&DocumentObserverPython::slotDeletedObject, this, _1));
this->connectDocumentChangedObject = App::GetApplication().signalBeforeChangeObject.connect(boost::bind
(&DocumentObserverPython::slotBeforeChangeObject, this, _1, _2));
this->connectDocumentChangedObject = App::GetApplication().signalChangedObject.connect(boost::bind
(&DocumentObserverPython::slotChangedObject, this, _1, _2));
@@ -247,6 +249,30 @@ void DocumentObserverPython::slotDeletedObject(const App::DocumentObject& Obj)
}
}
void DocumentObserverPython::slotBeforeChangeObject(const App::DocumentObject& Obj,
const App::Property& Prop)
{
Base::PyGILStateLocker lock;
try {
if (this->inst.hasAttr(std::string("slotBeforeChangeObject"))) {
Py::Callable method(this->inst.getAttr(std::string("slotBeforeChangeObject")));
Py::Tuple args(2);
args.setItem(0, Py::Object(const_cast<App::DocumentObject&>(Obj).getPyObject(), true));
// If a property is touched but not part of a document object then its name is null.
// In this case the slot function must not be called.
const char* prop_name = Obj.getPropertyName(&Prop);
if (prop_name) {
args.setItem(1, Py::String(prop_name));
method.apply(args);
}
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
void DocumentObserverPython::slotChangedObject(const App::DocumentObject& Obj,
const App::Property& Prop)
{

View File

@@ -64,6 +64,8 @@ private:
/** Checks if the given object is about to be removed. */
void slotDeletedObject(const App::DocumentObject& Obj);
/** The property of an observed object has changed */
void slotBeforeChangeObject(const App::DocumentObject& Obj, const App::Property& Prop);
/** The property of an observed object has changed */
void slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop);
/** Undoes the last transaction of the document */
void slotUndoDocument(const App::Document& Doc);