Salome HOME
Boost has been removed from code
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index e0c12b1973b8ba8d4534b33f5593dc83a72d619d..91cc255e8c7af1d3d5416549a88853f95d00a6ca 100644 (file)
@@ -2,18 +2,33 @@
 // Created:     2 June 2014
 // Author:      Vitaly Smetannikov
 
-#include "ModuleBase_WidgetShapeSelector.h"
-#include "ModuleBase_IWorkshop.h"
+#include <ModuleBase_WidgetShapeSelector.h>
+#include <ModuleBase_Definitions.h>
+#include <ModuleBase_ISelection.h>
+#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_IViewer.h>
+#include <ModuleBase_Tools.h>
+#include <ModuleBase_WidgetValueFeature.h>
 
+#include <Config_WidgetAPI.h>
 #include <Events_Loop.h>
-#include <ModelAPI_Events.h>
-#include <ModelAPI_Tools.h>
-
+#include <Events_Message.h>
+#include <GeomAPI_Interface.h>
+#include <GeomAPI_Shape.h>
+#include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Data.h>
-#include <ModelAPI_Object.h>
+#include <ModelAPI_Document.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
+#include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_AttributeSelection.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
+#include <ModelAPI_ResultBody.h>
 #include <Config_WidgetAPI.h>
+#include <Events_Error.h>
 
 #include <GeomAPI_Shape.h>
 
 #include <QEvent>
 #include <QDockWidget>
 
-#include <stdexcept>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS_Shape.hxx>
+
+#include <memory>
+
+#include <list>
+#include <string>
 
 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
 static ShapeTypes MyShapeTypes;
@@ -38,15 +59,21 @@ TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theTyp
 {
   if (MyShapeTypes.count() == 0) {
     MyShapeTypes["face"] = TopAbs_FACE;
+    MyShapeTypes["faces"] = TopAbs_FACE;
     MyShapeTypes["vertex"] = TopAbs_VERTEX;
+    MyShapeTypes["vertices"] = TopAbs_VERTEX;
     MyShapeTypes["wire"] = TopAbs_WIRE;
     MyShapeTypes["edge"] = TopAbs_EDGE;
+    MyShapeTypes["edges"] = TopAbs_EDGE;
     MyShapeTypes["shell"] = TopAbs_SHELL;
     MyShapeTypes["solid"] = TopAbs_SOLID;
+    MyShapeTypes["solids"] = TopAbs_SOLID;
   }
-  if (MyShapeTypes.contains(theType))
-    return MyShapeTypes[theType];
-  throw std::invalid_argument("Shape type defined in XML is not implemented!");
+  QString aType = theType.toLower();
+  if (MyShapeTypes.contains(aType))
+    return MyShapeTypes[aType];
+  Events_Error::send("Shape type defined in XML is not implemented!");
+  return TopAbs_SHAPE;
 }
 
 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
@@ -54,12 +81,12 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen
                                                      const Config_WidgetAPI* theData,
                                                      const std::string& theParentId)
     : ModuleBase_ModelWidget(theParent, theData, theParentId),
-      myWorkshop(theWorkshop), myIsActive(false)
+      myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
 {
   myContainer = new QWidget(theParent);
   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
+  ModuleBase_Tools::adjustMargins(aLayout);
 
-  aLayout->setContentsMargins(0, 0, 0, 0);
   QString aLabelText = QString::fromStdString(theData->widgetLabel());
   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
   myLabel = new QLabel(aLabelText, myContainer);
@@ -74,20 +101,21 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen
   myTextLine->setToolTip(aToolTip);
   myTextLine->installEventFilter(this);
 
-  myBasePalet = myTextLine->palette();
-  myInactivePalet = myBasePalet;
-  myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
-  myTextLine->setPalette(myInactivePalet);
-
   aLayout->addWidget(myTextLine, 1);
 
   std::string aTypes = theData->getProperty("shape_types");
-  myShapeTypes = QString(aTypes.c_str()).split(' ');
+  myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
+
+  std::string aObjTypes = theData->getProperty("object_types");
+  myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
+
+  myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
 }
 
 //********************************************************************
 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
 {
+  activateSelection(false);
 }
 
 //********************************************************************
@@ -98,25 +126,45 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
     return false;
 
   DataPtr aData = myFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
-      ModelAPI_AttributeReference>(aData->attribute(attributeID()));
-
-  ObjectPtr aObject = aRef->value();
-  if (!(aObject && aObject->isSame(mySelectedObject))) {
-    aRef->setValue(mySelectedObject);
-    updateObject(myFeature);
+  if (myUseSubShapes) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
+
+    ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
+    if (aBody) {
+      aSelect->setValue(aBody, myShape);
+      updateObject(myFeature);
+      return true;
+    }
+  } else {
+    std::shared_ptr<ModelAPI_AttributeReference> aRef = 
+      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
+
+    ObjectPtr aObject = aRef->value();
+    if (!(aObject && aObject->isSame(mySelectedObject))) {
+      aRef->setValue(mySelectedObject);
+      updateObject(myFeature);
+      return true;
+    }
   }
-  return true;
+  return false;
 }
 
 //********************************************************************
 bool ModuleBase_WidgetShapeSelector::restoreValue()
 {
   DataPtr aData = myFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
-
   bool isBlocked = this->blockSignals(true);
-  mySelectedObject = aRef->value();
+  if (myUseSubShapes) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
+    if (aSelect) {
+      mySelectedObject = aSelect->context();
+      myShape = aSelect->value();
+    }
+  } else {
+    std::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
+    mySelectedObject = aRef->value();
+  }
   updateSelectionName();
 
   this->blockSignals(isBlocked);
@@ -127,7 +175,6 @@ bool ModuleBase_WidgetShapeSelector::restoreValue()
 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 {
   QList<QWidget*> aControls;
-  aControls.append(myLabel);
   aControls.append(myTextLine);
   return aControls;
 }
@@ -135,48 +182,90 @@ QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
 {
-  QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
+  QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
   if (aObjects.size() > 0) {
     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 = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+    if (!aRes)
       return;
 
-    // Check that the selection corresponds to selection type
-    if (!isAccepted(aObject))
+    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;
+      }
+    }
+    // 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;
 
-    mySelectedObject = aObject;
-    if (mySelectedObject) {
-      updateSelectionName();
-      raisePanel();
-      static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
-      ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
-      Events_Loop::loop()->flush(anEvent);
+    // Check that the result has a shape
+    GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
+    if (!aShape)
+      return;
+
+    /// Check that object has acceptable type
+    if (!acceptObjectType(aObject)) 
+      return;
+
+    // Get sub-shapes from local selection
+    if (myUseSubShapes) {
+      NCollection_List<TopoDS_Shape> aShapeList;
+      std::list<ObjectPtr> aOwners;
+      myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
+      if (aShapeList.Extent() > 0) {
+        aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
+        aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
+      }
+    }
+
+    // Check that the selection corresponds to selection type
+    if (myUseSubShapes) {
+      if (!acceptSubShape(aShape))
+        return;
     } else {
-      myTextLine->setText("");
+      if (!acceptObjectShape(aObject))
+        return;
     }
-    activateSelection(false);
-    emit valuesChanged();
+    setObject(aObject, aShape);
+    //activateSelection(false);
     emit focusOutWidget(this);
   }
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
+void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
 {
-  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;
+  mySelectedObject = theObj;
+  myShape = theShape;
+  if (mySelectedObject) {
+    raisePanel();
+    if (!myUseSubShapes) {
+      static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
+      ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
     }
-  }
-  boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
+  } 
+  updateSelectionName();
+  activateSelection(false);
+  emit valuesChanged();
+}
+
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
+{
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
+
+  // Check that the shape of necessary type
+  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
   if (!aShapePtr)
     return false;
   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
@@ -199,6 +288,37 @@ bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
   return false;
 }
 
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
+{
+  TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
+  foreach (QString aType, myShapeTypes) {
+    if (aShape.ShapeType() == shapeType(aType))
+      return true;
+  }
+  return false;
+}
+
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
+{
+  // Definition of types is not obligatory. If types are not defined then
+  // it means that accepted any type
+  if (myObjectTypes.isEmpty())
+    return true;
+
+  foreach (QString aType, myObjectTypes) {
+    if (aType.toLower() == "construction") {
+      ResultConstructionPtr aConstr = 
+        std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
+      return (aConstr != NULL);
+    } // ToDo: Process other types of objects
+  }
+  // Object type is defined but not found
+  return false;
+}
+
+
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::updateSelectionName()
 {
@@ -207,17 +327,8 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
     myTextLine->setText(QString::fromStdString(aName));
   } else {
     if (myIsActive) {
-      QString aMsg = tr("Select a ");
-      int i = 0;
-      foreach (QString aType, myShapeTypes) {
-        if (i > 0)
-          aMsg += " or ";
-        aMsg += aType;
-        i++;
-      }
-      myTextLine->setText(aMsg);
-    } else
-      myTextLine->setText(tr("No object selected"));
+      myTextLine->setText("");
+    }
   }
 }
 
@@ -226,19 +337,32 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
 {
   myIsActive = toActivate;
-  if (myIsActive)
-    myTextLine->setPalette(myBasePalet);
-  else
-    myTextLine->setPalette(myInactivePalet);
   updateSelectionName();
 
-  if (myIsActive)
+  if (myIsActive) {
     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-  else
+    if (myUseSubShapes) {
+
+      QIntList aList;
+      foreach (QString aType, myShapeTypes)
+        aList.append(shapeType(aType));
+      myWorkshop->activateSubShapesSelection(aList);
+      if (!myObjectTypes.isEmpty()) {
+        myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
+        myWorkshop->viewer()->clearSelectionFilters();
+        myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
+      }
+    }
+  } else {
     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-
-//  if (myWorkshop->selectedObjects().size() > 0)
-//    onSelectionChanged();
+    if (myUseSubShapes) {
+      if (!myObjTypeFilter.IsNull()) {
+        myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
+        myObjTypeFilter.Nullify();
+      }
+      myWorkshop->deactivateSubShapesSelection();
+    }
+  }
 }
 
 //********************************************************************
@@ -262,7 +386,7 @@ void ModuleBase_WidgetShapeSelector::raisePanel() const
 bool ModuleBase_WidgetShapeSelector::focusTo()
 {
   activateSelection(true);
-  return true;
+  return ModuleBase_ModelWidget::focusTo();
 }
 
 //********************************************************************
@@ -273,4 +397,22 @@ bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEve
       activateSelection(true);
   }
   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
-}
\ No newline at end of file
+}
+
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
+{
+  if (theValue) {
+    ModuleBase_WidgetValueFeature* aFeatureValue =
+        dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
+    if (aFeatureValue && aFeatureValue->object()) {
+      ObjectPtr aObject = aFeatureValue->object();
+      if (acceptObjectShape(aObject)) {
+        setObject(aObject);
+        return true;
+      }
+    }
+  }
+  return false;
+}
+