Gui: add function to observe before changing a property

This commit is contained in:
wmayer
2020-01-13 02:17:39 +01:00
parent 127ae92e61
commit e5d23469f7
6 changed files with 38 additions and 0 deletions

View File

@@ -109,6 +109,8 @@ public:
boost::signals2::signal<void (const Gui::ViewProvider&)> signalNewObject;
/// signal on deleted Object
boost::signals2::signal<void (const Gui::ViewProvider&)> signalDeletedObject;
/// signal on changed Object
boost::signals2::signal<void (const Gui::ViewProvider&, const App::Property&)> signalBeforeChangeObject;
/// signal on changed object property
boost::signals2::signal<void (const Gui::ViewProvider&, const App::Property&)> signalChangedObject;
/// signal on renamed Object

View File

@@ -83,6 +83,7 @@ DocumentObserverPython::DocumentObserverPython(const Py::Object& obj) : inst(obj
FC_PY_ELEMENT_ARG1(ActivateDocument, ActiveDocument)
FC_PY_ELEMENT_ARG1(CreatedObject, NewObject)
FC_PY_ELEMENT_ARG1(DeletedObject, DeletedObject)
FC_PY_ELEMENT_ARG2(BeforeChangeObject, BeforeChangeObject)
FC_PY_ELEMENT_ARG2(ChangedObject, ChangedObject)
FC_PY_ELEMENT_ARG1(InEdit, InEdit)
FC_PY_ELEMENT_ARG1(ResetEdit, ResetEdit)
@@ -190,6 +191,27 @@ void DocumentObserverPython::slotDeletedObject(const Gui::ViewProvider& Obj)
}
}
void DocumentObserverPython::slotBeforeChangeObject(const Gui::ViewProvider& Obj,
const App::Property& Prop)
{
Base::PyGILStateLocker lock;
try {
Py::Tuple args(2);
args.setItem(0, Py::Object(const_cast<Gui::ViewProvider&>(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));
Base::pyCall(pyBeforeChangeObject.ptr(),args.ptr());
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
void DocumentObserverPython::slotChangedObject(const Gui::ViewProvider& Obj,
const App::Property& Prop)
{

View File

@@ -65,6 +65,8 @@ private:
/** Checks if the given object is about to be removed. */
void slotDeletedObject(const Gui::ViewProvider& Obj);
/** The property of an observed object has changed */
void slotBeforeChangeObject(const Gui::ViewProvider& Obj, const App::Property& Prop);
/** The property of an observed object has changed */
void slotChangedObject(const Gui::ViewProvider& Obj, const App::Property& Prop);
/** The object was set into edit mode */
void slotInEdit(const Gui::ViewProviderDocumentObject& Obj);
@@ -90,6 +92,7 @@ private:
Connection pyActivateDocument;
Connection pyCreatedObject;
Connection pyDeletedObject;
Connection pyBeforeChangeObject;
Connection pyChangedObject;
Connection pyInEdit;
Connection pyResetEdit;

View File

@@ -514,6 +514,13 @@ int ViewProvider::getDefaultMode() const {
return viewOverrideMode>=0?viewOverrideMode:_iActualMode;
}
void ViewProvider::onBeforeChange(const App::Property* prop)
{
Application::Instance->signalBeforeChangeObject(*this, *prop);
App::TransactionalObject::onBeforeChange(prop);
}
void ViewProvider::onChanged(const App::Property* prop)
{
Application::Instance->signalChangedObject(*this, *prop);

View File

@@ -522,6 +522,8 @@ protected:
SoPickedPoint* getPointOnRay(const SbVec3f& pos, const SbVec3f& dir,
const View3DInventorViewer* viewer) const;
/// Reimplemented from subclass
void onBeforeChange(const App::Property* prop);
/// Reimplemented from subclass
void onChanged(const App::Property* prop);

View File

@@ -169,6 +169,8 @@ void ViewProviderDocumentObject::onBeforeChange(const App::Property* prop)
onBeforeChangeProperty(doc, prop);
}
}
ViewProvider::onBeforeChange(prop);
}
void ViewProviderDocumentObject::onChanged(const App::Property* prop)