Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / PartSet / PartSet_WidgetShapeSelector.cpp
index 98c25d47b1dc4eda49d7e3e2af954673bc2ad324..82c0479bd67ced814630e957d64583b71d6212e5 100644 (file)
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 
+#include <ModuleBase_Definitions.h>
+
 #include <PartSet_Tools.h>
 #include <SketchPlugin_Feature.h>
 
-#include <ModuleBase_IWorkshop.h>
-#include <XGUI_ModuleConnector.h>
+#include <SketchPlugin_ConstraintRigid.h>
+
 #include <XGUI_Workshop.h>
-#include <XGUI_Displayer.h>
+
+PartSet_WidgetShapeSelector::PartSet_WidgetShapeSelector(QWidget* theParent,
+                                                         ModuleBase_IWorkshop* theWorkshop,
+                                                         const Config_WidgetAPI* theData,
+                                                         const std::string& theParentId)
+: ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId)
+{
+}
 
 bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomShapePtr theShape)
 {
@@ -76,30 +85,11 @@ bool PartSet_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject, GeomSha
   return ModuleBase_WidgetShapeSelector::setObject(aSelectedObject, aShape);
 }
 
-//********************************************************************
-void PartSet_WidgetShapeSelector::storeAttributeValue()
-{
-  /// this is a temporary code, will be removed when master is merged to this branch
-  /*XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
-  aWorkshop->displayer()->enableUpdateViewer(false);
-  */
-  ModuleBase_WidgetShapeSelector::storeAttributeValue();
-}
-
 //********************************************************************
 void PartSet_WidgetShapeSelector::restoreAttributeValue(const bool theValid)
 {
   ModuleBase_WidgetShapeSelector::restoreAttributeValue(theValid);
-  /// this is a temporary code, will be removed when master is merged to this branch
-  /// after merge, the external edge should be removed always, without flag checking
-  if (!theValid)
-    removeExternal();
-  /*
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
-  aWorkshop->displayer()->enableUpdateViewer(false);//->erase(myExternalObject);
-  aWorkshop->displayer()->enableUpdateViewer(true);*/
+  removeExternal();
 }
 
 //********************************************************************
@@ -111,40 +101,6 @@ void PartSet_WidgetShapeSelector::createExternal(ObjectPtr theSelectedObject,
   if (aObj != myExternalObject) {
     removeExternal();
     myExternalObject = aObj;
-// TODO(nds) v1.0.2 master
-//  // Check the acceptability of the object and shape as validator attribute
-//  AttributePtr aPntAttr;
-//  DataPtr aData = myFeature->data();
-//  if (theShape.get() != NULL) {
-//    AttributePtr aAttr = aData->attribute(attributeID());
-//    AttributeRefAttrPtr aRefAttr =
-//      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aAttr);
-//    if (aRefAttr) {
-//      TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
-//      aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theObj, aShape, mySketch);
-//    }
-//  }
-//  // Check the acceptability of the object as attribute
-//  aValidator = aValidators.begin();
-//  std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
-//  for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
-//    const ModelAPI_RefAttrValidator* aAttrValidator =
-//        dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
-//    if (aAttrValidator) {
-//      if (aPntAttr.get() != NULL)
-//      {
-//        if (!aAttrValidator->isValid(myFeature, *aArgs, aPntAttr)) {
-//          return false;
-//        }
-//      }
-//      else
-//      {
-//        if (!aAttrValidator->isValid(myFeature, *aArgs, theObj, theShape)) {
-//          return false;
-//        }
-//      }
-//    }
-// ======= end of todo
   }
 }
 
@@ -155,8 +111,31 @@ void PartSet_WidgetShapeSelector::removeExternal()
     DocumentPtr aDoc = myExternalObject->document();
     FeaturePtr aFeature = ModelAPI_Feature::feature(myExternalObject);
     if (aFeature.get() != NULL) {
-      aDoc->removeFeature(aFeature);
+      // 1. check whether the external object can be deleted
+      // It should not be deleted if there are references to the object from other features,
+      // which are not the sketch or a rigid constraints.
+      std::set<FeaturePtr> aRefFeatures;
+      aFeature->document()->refsToFeature(aFeature, aRefFeatures, false);
+      std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
+                                       aLast = aRefFeatures.end();
+      bool aReferenceExist = false;
+      CompositeFeaturePtr aSketch = sketch();
+      for (; anIt != aLast && !aReferenceExist; anIt++) {
+        FeaturePtr aFeature = (*anIt);
+        aReferenceExist = aFeature != aSketch &&
+                          aFeature->getKind() != SketchPlugin_ConstraintRigid::ID();
+      }
+      if (aReferenceExist)
+        return;
+
+      // 2. delete external object
+      QObjectPtrList anObjects;
+      anObjects.append(aFeature);
+      // the external feature should be removed with all references, sketch feature should be ignored
+      std::set<FeaturePtr> anIgnoredFeatures;
+      anIgnoredFeatures.insert(sketch());
+      XGUI_Workshop::deleteFeatures(anObjects, anIgnoredFeatures);
     }
-    myExternalObject = NULL;
+    myExternalObject = ObjectPtr();
   }
 }