Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index f07c339caa60e82bd6a68645d904559758b1d4d3..4a53eb0438d730b3671190346966dee17abb413d 100644 (file)
@@ -23,6 +23,7 @@
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_ResultBody.h>
 #include <Config_WidgetAPI.h>
 #include <Events_Error.h>
 
@@ -46,8 +47,7 @@
 #include <boost/smart_ptr/shared_ptr.hpp>
 
 #include <list>
-#include <stdexcept>
-#include <xstring>
+#include <string>
 
 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
 static ShapeTypes MyShapeTypes;
@@ -129,9 +129,12 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
 
-    ResultBodyPtr aBody = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(mySelectedObject);
-    if (aBody)
+    ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
+    if (aBody) {
       aSelect->setValue(aBody, myShape);
+      updateObject(myFeature);
+      return true;
+    }
   } else {
     boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
@@ -140,9 +143,10 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
     if (!(aObject && aObject->isSame(mySelectedObject))) {
       aRef->setValue(mySelectedObject);
       updateObject(myFeature);
+      return true;
     }
   }
-  return true;
+  return false;
 }
 
 //********************************************************************
@@ -183,14 +187,37 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
     ObjectPtr aObject = aObjects.first();
     if ((!mySelectedObject) && (!aObject))
       return;
-    if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
+
+    // Check that the selected object is result (others can not be accepted)
+    ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+    if (!aRes)
+      return;
+
+    if (myFeature) {
+      // We can not select a result of our feature
+      const std::list<boost::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
+      std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
+      for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
+        if ((*aIt) == aRes)
+          return;
+      }
+    }
+    // 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;
+
+    // Check that the result has a shape
+    GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
+    if (!aShape)
       return;
 
     // Get sub-shapes from local selection
-    boost::shared_ptr<GeomAPI_Shape> aShape;
     if (myUseSubShapes) {
       NCollection_List<TopoDS_Shape> aShapeList;
-      myWorkshop->selection()->selectedShapes(aShapeList);
+      std::list<ObjectPtr> aOwners;
+      myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
       if (aShapeList.Extent() > 0) {
         aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
@@ -206,6 +233,7 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
         return;
     }
     setObject(aObject, aShape);
+    //activateSelection(false);
     emit focusOutWidget(this);
   }
 }
@@ -213,8 +241,6 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
 {
-  if (mySelectedObject == theObj)
-    return;
   mySelectedObject = theObj;
   myShape = theShape;
   if (mySelectedObject) {
@@ -222,7 +248,6 @@ void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_p
     if (!myUseSubShapes) {
       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
-      Events_Loop::loop()->flush(anEvent);
     }
   } 
   updateSelectionName();
@@ -234,20 +259,6 @@ void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_p
 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
 {
   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
-  if (myFeature) {
-    // We can not select a result of our feature
-    const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
-    std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
-    for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
-      if ((*aIt) == aResult)
-        return false;
-    }
-  }
-  // Check that object belongs to active document or PartSet
-  DocumentPtr aDoc = aResult->document();
-  SessionPtr aMgr = ModelAPI_Session::get();
-  if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
-    return false;
 
   // Check that the shape of necessary type
   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);