From d6b17b2dbb4e1a2834ac866bb97600d71499a077 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 15 Nov 2021 13:05:19 +0100 Subject: [PATCH] App: add PropertyLinkSubList::addValue() to extend the list --- src/App/PropertyLinks.cpp | 64 +++++++++++++++++++++++++++++++++++++++ src/App/PropertyLinks.h | 2 ++ 2 files changed, 66 insertions(+) diff --git a/src/App/PropertyLinks.cpp b/src/App/PropertyLinks.cpp index 6eb847c1b1..4d4e696d74 100644 --- a/src/App/PropertyLinks.cpp +++ b/src/App/PropertyLinks.cpp @@ -1801,6 +1801,70 @@ void PropertyLinkSubList::setValue(DocumentObject* lValue, const std::vector &subs, bool reset) +{ + auto parent = Base::freecad_dynamic_cast(getContainer()); + verifyObject(obj, parent); + +#ifndef USE_OLD_DAG + //maintain backlinks. + if (parent) { + // before accessing internals make sure the object is not about to be destroyed + // otherwise the backlink contains dangling pointers + if (!parent->testStatus(ObjectStatus::Destroy) && _pcScope != LinkScope::Hidden) { + //_lValueList can contain items multiple times, but we trust the document + //object to ensure that this works + if (reset) { + for(auto* value : _lValueList) { + if (value && value == obj) + value->_removeBackLink(parent); + } + } + + //maintain backlinks. lValue can contain items multiple times, but we trust the document + //object to ensure that the backlink is only added once + if (obj) + obj->_addBackLink(parent); + } + } +#endif + + std::vector valueList; + std::vector subList; + + if (reset) { + for (std::size_t i=0; i<_lValueList.size(); i++) { + if (_lValueList[i] != obj) { + valueList.push_back(_lValueList[i]); + subList.push_back(_lSubList[i]); + } + } + } + else { + valueList = _lValueList; + subList = _lSubList; + } + + std::size_t size = subs.size(); + if (size == 0) { + if (obj) { + valueList.push_back(obj); + subList.emplace_back(); + } + } + else if (obj) { + subList.insert(subList.end(), subs.begin(), subs.end()); + valueList.insert(valueList.end(), size, obj); + } + + aboutToSetValue(); + _lValueList = valueList; + _lSubList = subList; + updateElementReference(nullptr); + checkLabelReferences(_lSubList); + hasSetValue(); +} + const string PropertyLinkSubList::getPyReprString() const { assert(this->_lValueList.size() == this->_lSubList.size()); diff --git a/src/App/PropertyLinks.h b/src/App/PropertyLinks.h index b0d7dbcdd8..66c4d9bb80 100644 --- a/src/App/PropertyLinks.h +++ b/src/App/PropertyLinks.h @@ -935,6 +935,8 @@ public: */ void setValue(App::DocumentObject *lValue, const std::vector &SubList=std::vector()); + void addValue(App::DocumentObject *obj, const std::vector &SubList={}, bool reset = false); + const std::vector &getValues(void) const { return _lValueList; }