Salome HOME
Issue #412: Crash on delete sketch line with constraints
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index ec7defe2285bb1eff1724dc7da1695db04907e54..2c970d1740de74d2ffb4fbe1bb5f4813b1d5354c 100644 (file)
@@ -215,86 +215,12 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
   //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
   if (aSelected.size() > 0) {
-    if (setSelection(aSelected.first()))
+    Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
+    if (isValid(anOwner)) {
+      setSelection(anOwner);
       emit focusOutWidget(this);
-  }
-}
-
-//********************************************************************
-bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
-{
-  ObjectPtr aObject = theValue.object();
-  ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID()));
-  if ((!aCurrentObject) && (!aObject))
-    return false;
-
-  // Check that the selected object is result (others can not be accepted)
-  ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
-  if (!aRes)
-    return false;
-
-  if (myFeature) {
-    // We can not select a result of our feature
-    const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
-    std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
-    for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
-      if ((*aIt) == aRes)
-        return false;
-    }
-  }
-  // Check that object belongs to active document or PartSet
-  DocumentPtr aDoc = aRes->document();
-  SessionPtr aMgr = ModelAPI_Session::get();
-  if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
-    return false;
-
-  // Check that the result has a shape
-  GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
-  if (!aShape)
-    return false;
-
-  // Get sub-shapes from local selection
-  if (!theValue.shape().IsNull()) {
-    aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
-    aShape->setImpl(new TopoDS_Shape(theValue.shape()));
-  }
-
-  // Check that the selection corresponds to selection type
-  if (!acceptSubShape(aShape))
-    return false;
-//  if (!acceptObjectShape(aObject))
-//      return false;
-
-  // Check whether the value is valid for the viewer selection filters
-  Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
-  if (!anOwner.IsNull()) {
-    SelectMgr_ListOfFilter aFilters;
-    selectionFilters(myWorkshop, aFilters);
-    SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
-    for (; aIt.More(); aIt.Next()) {
-      const Handle(SelectMgr_Filter)& aFilter = aIt.Value();
-      if (!aFilter->IsOk(anOwner))
-        return false;
     }
   }
-  if (isValid(aObject, aShape)) {
-    setObject(aObject, aShape);
-    return true;
-  }
-  return false;
-}
-
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
-{
-  if (storeAttributeValues(theObj, theShape))
-    updateObject(myFeature);
-
-  if (theObj) {
-    raisePanel();
-  } 
-  updateSelectionName();
-  emit valuesChanged();
 }
 
 //********************************************************************
@@ -395,7 +321,17 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
       std::string aName = anObject->data()->name();
       myTextLine->setText(QString::fromStdString(aName));
     } else {
-      if (myIsActive) {
+      AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+      if (aRefAttr && aRefAttr->attr().get() != NULL) {
+        //myIsObject = aRefAttr->isObject();
+        AttributePtr anAttr = aRefAttr->attr();
+        if (anAttr.get() != NULL) {
+          std::stringstream aName;
+          aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
+          myTextLine->setText(QString::fromStdString(aName.str()));
+        }
+      }
+      else if (myIsActive) {
         myTextLine->setText("");
       }
     }
@@ -469,64 +405,65 @@ void ModuleBase_WidgetShapeSelector::backupAttributeValue(const bool isBackup)
     storeAttributeValues(myObject, myShape);
     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
     if (aRefAttr) {
-      if (myIsObject)
-        aRefAttr->setObject(myObject);
-      else
+      if (!myIsObject)
         aRefAttr->setAttr(myRefAttribute);
     }
   }
 }
 
 //********************************************************************
-void ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
+bool ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
 {
+  bool isDone = false;
 
-}
-
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::deactivate()
-{
-  activateSelection(false);
-  disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-}
+  ModuleBase_ViewerPrs aPrs;
+  myWorkshop->selection()->fillPresentation(aPrs, theOwner);
+  ObjectPtr aObject = aPrs.object();
+  ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID()));
+  if ((!aCurrentObject) && (!aObject))
+    return false;
 
-//********************************************************************
-bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
-{
-  bool isValid = ModuleBase_WidgetValidated::isValid(theObj, theShape);
-  if (!isValid)
+  // Check that the selected object is result (others can not be accepted)
+  ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+  if (!aRes)
     return false;
 
+  if (myFeature) {
+    // We can not select a result of our feature
+    const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
+    std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
+    for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
+      if ((*aIt) == aRes)
+        return false;
+    }
+  }
+  // Check that object belongs to active document or PartSet
+  DocumentPtr aDoc = aRes->document();
   SessionPtr aMgr = ModelAPI_Session::get();
-  ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-  std::list<ModelAPI_Validator*> aValidators;
-  std::list<std::list<std::string> > anArguments;
-  aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
+  if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
+    return false;
 
-  DataPtr aData = myFeature->data();
-  AttributePtr anAttribute = myFeature->attribute(attributeID());
+  // Check that the result has a shape
+  GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
+  if (!aShape)
+    return false;
 
-  // 1. make a backup of the previous attribute values
-  backupAttributeValue(true);
-  // 2. store the current values, disable the model's update
-  aData->blockSendAttributeUpdated(true);
-
-  // 3. check the acceptability of the current values
-  std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
-  std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
-  bool aValid = true;
-  for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
-    const ModelAPI_AttributeValidator* aAttrValidator =
-        dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
-    if (aAttrValidator) {
-      aValid = aAttrValidator->isValid(anAttribute, *aArgs);
-    }
+  // Get sub-shapes from local selection
+  if (!aPrs.shape().IsNull()) {
+    aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
+    aShape->setImpl(new TopoDS_Shape(aPrs.shape()));
   }
+  // Check that the selection corresponds to selection type
+  if (!acceptSubShape(aShape))
+    return false;
 
-  // 4. if the values are not valid, restore the previous values to the attribute
-  backupAttributeValue(false);
+  storeAttributeValues(aObject, aShape);
+  return true;
+}
 
-  // 5. enable the model's update
-  aData->blockSendAttributeUpdated(false);
-  return aValid;
+//********************************************************************
+void ModuleBase_WidgetShapeSelector::deactivate()
+{
+  activateSelection(false);
+  disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
 }