DocumentObjectGroup: fix deleting of groups in case of malformed links

This one prevents thurther object structure damage if
DocumentObjectGroup::Group connains multiple links to the same feature.
Also minorr changes:
 - Fix some spacing
 - Ask the user if he wants to delete group content
This commit is contained in:
Alexander Golubev
2015-08-27 12:34:29 +03:00
committed by Stefan Tröger
parent abc925b62e
commit aa32522115
4 changed files with 45 additions and 24 deletions

View File

@@ -29,14 +29,14 @@
#include "DocumentObjectGroup.h"
#include "DocumentObjectGroupPy.h"
#include "Document.h"
#include "FeaturePythonPyImp.h"
#include "FeaturePythonPyImp.h"
using namespace App;
PROPERTY_SOURCE(App::DocumentObjectGroup, App::DocumentObject)
DocumentObjectGroup::DocumentObjectGroup()
DocumentObjectGroup::DocumentObjectGroup()
{
ADD_PROPERTY_TYPE(Group,(0),"Base",(App::PropertyType)(Prop_Output),"List of referenced objects");
}
@@ -63,20 +63,22 @@ void DocumentObjectGroup::addObject(DocumentObject* obj)
void DocumentObjectGroup::removeObject(DocumentObject* obj)
{
std::vector<DocumentObject*> grp = Group.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
if (*it == obj) {
grp.erase(it);
Group.setValues(grp);
break;
}
const std::vector<DocumentObject*> & grp = Group.getValues();
std::vector<DocumentObject*> newGrp;
std::remove_copy (grp.begin(), grp.end(), std::back_inserter (newGrp), obj);
if (grp.size() != newGrp.size()) {
Group.setValues (newGrp);
}
}
void DocumentObjectGroup::removeObjectsFromDocument()
{
std::vector<DocumentObject*> grp = Group.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
const std::vector<DocumentObject*> & grp = Group.getValues();
// Use set so iterate on each linked object exactly one time (in case of multiple links to the same document)
std::set<DocumentObject*> grpSet (grp.begin(), grp.end());
for (std::set<DocumentObject*>::iterator it = grpSet.begin(); it != grpSet.end(); ++it) {
removeObjectFromDocument(*it);
}
}
@@ -85,11 +87,9 @@ void DocumentObjectGroup::removeObjectFromDocument(DocumentObject* obj)
{
// remove all children
if (obj->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) {
std::vector<DocumentObject*> grp = static_cast<DocumentObjectGroup*>(obj)->Group.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
// recursive call to remove all subgroups
removeObjectFromDocument(*it);
}
DocumentObjectGroup *grp = static_cast<DocumentObjectGroup*>(obj);
// recursive call to remove all subgroups
grp->removeObjectsFromDocument();
}
this->getDocument()->remObject(obj->getNameInDocument());
@@ -177,7 +177,7 @@ PyObject *DocumentObjectGroup::getPyObject()
// ref counter is set to 1
PythonObject = Py::Object(new DocumentObjectGroupPy(this),true);
}
return Py::new_reference_to(PythonObject);
return Py::new_reference_to(PythonObject);
}
// Python feature ---------------------------------------------------------