Gui: [skip ci] allow to unbind() an ExpressionBinding and automatically unbind it if the observed object has been deleted

For more details see: https://forum.freecadweb.org/viewtopic.php?f=8&t=56097
This commit is contained in:
wmayer
2021-02-28 12:37:50 +01:00
parent 648ae39f50
commit 88a2dc1c70
2 changed files with 27 additions and 5 deletions

View File

@@ -59,6 +59,13 @@ bool ExpressionBinding::isBound() const
return path.getDocumentObject() != 0;
}
void ExpressionBinding::unbind()
{
expressionchanged.disconnect();
objectdeleted.disconnect();
path = App::ObjectIdentifier();
}
void Gui::ExpressionBinding::setExpression(boost::shared_ptr<Expression> expr)
{
DocumentObject * docObj = path.getDocumentObject();
@@ -100,7 +107,11 @@ void ExpressionBinding::bind(const App::ObjectIdentifier &_path)
//connect to be informed about changes
DocumentObject * docObj = path.getDocumentObject();
connection = docObj->ExpressionEngine.expressionChanged.connect(boost::bind(&ExpressionBinding::expressionChange, this, bp::_1));
if (docObj) {
expressionchanged = docObj->ExpressionEngine.expressionChanged.connect(boost::bind(&ExpressionBinding::expressionChange, this, bp::_1));
App::Document* doc = docObj->getDocument();
objectdeleted = doc->signalDeletedObject.connect(boost::bind(&ExpressionBinding::objectDeleted, this, bp::_1));
}
}
void ExpressionBinding::bind(const Property &prop)
@@ -247,3 +258,11 @@ void ExpressionBinding::expressionChange(const ObjectIdentifier& id) {
if(id==path)
onChange();
}
void ExpressionBinding::objectDeleted(const App::DocumentObject& obj)
{
DocumentObject * docObj = path.getDocumentObject();
if (docObj == &obj) {
unbind();
}
}