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 ---------------------------------------------------------

View File

@ -45,11 +45,11 @@ public:
/** @name Object handling */
//@{
/** Adds an object of \a sType with \a pObjectName to the document this group belongs to and
/** Adds an object of \a sType with \a pObjectName to the document this group belongs to and
* append it to this group as well.
*/
DocumentObject *addObject(const char* sType, const char* pObjectName);
/* Adds the object \a obj to this group.
/* Adds the object \a obj to this group.
*/
void addObject(DocumentObject* obj);
/** Removes an object from this group.

View File

@ -45,4 +45,4 @@
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>
</GenerateModel>

View File

@ -26,6 +26,7 @@
#ifndef _PreComp_
# include <QApplication>
# include <QPixmap>
# include <QMessageBox>
#endif
#include <App/DocumentObjectGroup.h>
@ -131,7 +132,10 @@ bool ViewProviderDocumentObjectGroup::canDragObjects() const
void ViewProviderDocumentObjectGroup::dragObject(App::DocumentObject* obj)
{
static_cast<App::DocumentObjectGroup*>(getObject())->removeObject(obj);
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument(\"%s\").getObject(\"%s\").removeObject("
"App.getDocument(\"%s\").getObject(\"%s\"))",
getObject()->getDocument()->getName(), getObject()->getNameInDocument(),
obj->getDocument()->getName(), obj->getNameInDocument() );
}
bool ViewProviderDocumentObjectGroup::canDropObjects() const
@ -141,7 +145,10 @@ bool ViewProviderDocumentObjectGroup::canDropObjects() const
void ViewProviderDocumentObjectGroup::dropObject(App::DocumentObject* obj)
{
static_cast<App::DocumentObjectGroup*>(getObject())->addObject(obj);
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument(\"%s\").getObject(\"%s\").addObject("
"App.getDocument(\"%s\").getObject(\"%s\"))",
getObject()->getDocument()->getName(), getObject()->getNameInDocument(),
obj->getDocument()->getName(), obj->getNameInDocument() );
}
std::vector<std::string> ViewProviderDocumentObjectGroup::getDisplayModes(void) const
@ -152,8 +159,22 @@ std::vector<std::string> ViewProviderDocumentObjectGroup::getDisplayModes(void)
bool ViewProviderDocumentObjectGroup::onDelete(const std::vector<std::string> &)
{
Gui::Command::doCommand(Gui::Command::Doc,"App.getDocument(\"%s\").getObject(\"%s\").removeObjectsFromDocument()"
,getObject()->getDocument()->getName(), getObject()->getNameInDocument());
assert ( getObject ()->isDerivedFrom ( App::DocumentObjectGroup::getClassTypeId () ) );
App::DocumentObjectGroup *group = static_cast<App::DocumentObjectGroup *> (getObject());
// If the group is nonempty ask the user if he wants to delete it's content
if ( group->Group.getSize () ) {
QMessageBox::StandardButton choice =
QMessageBox::question ( 0, QObject::tr ( "Delete group content?" ),
QObject::tr ( "The group %1 is not empty, delete it's content as well?")
.arg ( QString::fromUtf8 ( group->Label.getValue () ) ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes );
if ( choice == QMessageBox::Yes ) {
Gui::Command::doCommand(Gui::Command::Doc,
"App.getDocument(\"%s\").getObject(\"%s\").removeObjectsFromDocument()"
,getObject()->getDocument()->getName(), getObject()->getNameInDocument());
}
}
return true;
}