+bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
+ (QList<ModuleBase_ViewerPrsPtr>& theValues)
+{
+ bool isDone = false;
+
+ std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
+ DataPtr aData = myFeature->data();
+ AttributePtr anAttribute = aData->attribute(attributeID());
+ std::string aType = anAttribute->attributeType();
+ std::set<GeomShapePtr> aShapes;
+ std::set<int> anIndicesToBeRemoved;
+ if (aType == ModelAPI_AttributeSelectionList::typeId()) {
+ // iteration through data model to find not selected elements to remove them
+ AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
+ for (int i = 0; i < aSelectionListAttr->size(); i++) {
+ AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
+ bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
+ if (!aFound)
+ anIndicesToBeRemoved.insert(i);
+ }
+ isDone = anIndicesToBeRemoved.size() > 0;
+ aSelectionListAttr->remove(anIndicesToBeRemoved);
+ }
+ else if (aType == ModelAPI_AttributeRefList::typeId()) {
+ AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
+ for (int i = 0; i < aRefListAttr->size(); i++) {
+ ObjectPtr anObject = aRefListAttr->object(i);
+ if (anObject.get()) {
+ bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
+ if (!aFound)
+ anIndicesToBeRemoved.insert(i);
+ }
+ }
+ isDone = anIndicesToBeRemoved.size() > 0;
+ aRefListAttr->remove(anIndicesToBeRemoved);
+ }
+ else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
+ std::set<AttributePtr> anAttributes;
+ QList<ModuleBase_ViewerPrsPtr>::const_iterator
+ anIt = theValues.begin(), aLast = theValues.end();
+ ObjectPtr anObject;
+ GeomShapePtr aShape;
+ for (; anIt != aLast; anIt++) {
+ ModuleBase_ViewerPrsPtr aPrs = *anIt;
+ getGeomSelection(aPrs, anObject, aShape);
+ AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
+ if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
+ anAttributes.insert(anAttr);
+ }
+
+ AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
+ for (int i = 0; i < aRefAttrListAttr->size(); i++) {
+ bool aFound = false;
+ if (aRefAttrListAttr->isAttribute(i)) {
+ AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
+ aFound = anAttributes.find(anAttribute) != anAttributes.end();
+ }
+ else {
+ aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
+ }
+ if (!aFound)
+ anIndicesToBeRemoved.insert(i);
+ }
+ isDone = anIndicesToBeRemoved.size() > 0;
+ aRefAttrListAttr->remove(anIndicesToBeRemoved);
+ }
+
+ return isDone;
+}
+
+std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
+ (QList<ModuleBase_ViewerPrsPtr>& theValues)
+{
+ // convert prs list to objects map
+ std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
+ std::set<GeomShapePtr> aShapes;
+ QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
+ ObjectPtr anObject;
+ GeomShapePtr aShape;
+ GeomShapePtr anEmptyShape(new GeomAPI_Shape());
+ for (; anIt != aLast; anIt++) {
+ ModuleBase_ViewerPrsPtr aPrs = *anIt;
+ getGeomSelection(aPrs, anObject, aShape);
+ aShapes.clear();
+ if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
+ aShapes = aGeomSelection[anObject];
+ // we need to know if there was an empty shape in selection for the object
+ if (!aShape.get())
+ aShape = anEmptyShape;
+ if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
+ aShapes.insert(aShape);
+ aGeomSelection[anObject] = aShapes;
+ }
+ return aGeomSelection;
+}
+
+bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
+ const GeomShapePtr& theShape,
+ const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
+{
+ bool aFound = false;
+ GeomShapePtr anEmptyShape(new GeomAPI_Shape());
+ GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
+ if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
+ const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
+ std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
+ for (; anIt != aLast && !aFound; anIt++) {
+ GeomShapePtr aCShape = *anIt;
+ if (aCShape.get())
+ aFound = aCShape->isEqual(aShape);
+ }
+ }
+ return aFound;
+}