Gui: Allow to override onBeforeChange in view objects

This commit is contained in:
wmayer
2024-11-27 19:52:00 +01:00
committed by wwmayer
parent c07df6b392
commit a303eaf28e
2 changed files with 39 additions and 0 deletions

View File

@@ -638,6 +638,38 @@ void ViewProviderFeaturePythonImp::onChanged(const App::Property* prop)
}
}
void ViewProviderFeaturePythonImp::onBeforeChange(const App::Property* prop)
{
if(py_onBeforeChange.isNone())
return;
// Run the onChanged method of the proxy object.
Base::PyGILStateLocker lock;
try {
if (has__object__) {
Py::Tuple args(1);
const char* prop_name = object->getPropertyName(prop);
if (prop_name) {
args.setItem(0, Py::String(prop_name));
Base::pyCall(py_onBeforeChange.ptr(),args.ptr());
}
}
else {
Py::Tuple args(2);
args.setItem(0, Py::Object(object->getPyObject(), true));
const char* prop_name = object->getPropertyName(prop);
if (prop_name) {
args.setItem(1, Py::String(prop_name));
Base::pyCall(py_onBeforeChange.ptr(),args.ptr());
}
}
}
catch (Py::Exception&) {
Base::PyException e; // extract the Python error text
e.ReportException();
}
}
void ViewProviderFeaturePythonImp::startRestoring()
{
}

View File

@@ -72,6 +72,7 @@ public:
void attach(App::DocumentObject *pcObject);
void updateData(const App::Property*);
void onChanged(const App::Property* prop);
void onBeforeChange(const App::Property* prop);
void startRestoring();
void finishRestoring();
ValueT onDelete(const std::vector<std::string> & sub);
@@ -149,6 +150,7 @@ private:
FC_PY_ELEMENT(attach) \
FC_PY_ELEMENT(updateData) \
FC_PY_ELEMENT(onChanged) \
FC_PY_ELEMENT(onBeforeChange) \
FC_PY_ELEMENT(startRestoring) \
FC_PY_ELEMENT(finishRestoring) \
FC_PY_ELEMENT(onDelete) \
@@ -518,6 +520,11 @@ protected:
imp->onChanged(prop);
ViewProviderT::onChanged(prop);
}
void onBeforeChange(const App::Property* prop) override
{
imp->onBeforeChange(prop);
ViewProviderT::onBeforeChange(prop);
}
/// is called by the document when the provider goes in edit mode
bool setEdit(int ModNum) override
{