Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index 785d6b6f26320df71932d9699d12333997286c4b..0a15d04b6e674b3fc1f8c181d610d4f069ed83fe 100644 (file)
@@ -6,23 +6,32 @@
 #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 <Events_Message.h>
 #include <GeomAPI_Interface.h>
 #include <GeomAPI_Shape.h>
+
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Data.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 <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_ResultValidator.h>
+#include <ModelAPI_RefAttrValidator.h>
+
 #include <Config_WidgetAPI.h>
 #include <Events_Error.h>
 
 #include <QString>
 #include <QEvent>
 #include <QDockWidget>
+#include <QApplication>
 
 #include <TopExp_Explorer.hxx>
 #include <TopoDS_Shape.hxx>
 
-#include <boost/smart_ptr/shared_ptr.hpp>
+#include <memory>
 
 #include <list>
-#include <stdexcept>
-#include <xstring>
+#include <string>
 
 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
 static ShapeTypes MyShapeTypes;
@@ -98,15 +107,13 @@ 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); 
 }
@@ -126,25 +133,68 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
 
   DataPtr aData = myFeature->data();
   if (myUseSubShapes) {
-    boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
-      boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
-
-    ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
+    ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
     if (aBody) {
-      aSelect->setValue(aBody, myShape);
-      updateObject(myFeature);
+      AttributePtr aAttr = aData->attribute(attributeID());
+
+      // We have to check several attributes types
+      AttributeSelectionPtr aSelectAttr = 
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
+      if (aSelectAttr) {
+        aSelectAttr->setValue(aBody, myShape);
+        updateObject(myFeature);
+        return true;
+      } else {
+        AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+        if (aRefAttr) {
+          aRefAttr->setObject(mySelectedObject);
+          updateObject(myFeature);
+          return true;
+        }
+      }
     }
   } else {
-    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);
+    AttributeReferencePtr aRef = aData->reference(attributeID());
+    if (aRef) {
+      ObjectPtr aObject = aRef->value();
+      if (!(aObject && aObject->isSame(mySelectedObject))) {
+        aRef->setValue(mySelectedObject);
+        updateObject(myFeature);
+        return true;
+      }
+    } else {
+      AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+      if (aRefAttr) {
+        ObjectPtr aObject = aRefAttr->object();
+        if (!(aObject && aObject->isSame(mySelectedObject))) {
+          aRefAttr->setObject(mySelectedObject);
+          updateObject(myFeature);
+          return true;
+        }
+      }
     }
   }
-  return true;
+  return false;
+}
+
+//********************************************************************
+void ModuleBase_WidgetShapeSelector::clearAttribute()
+{
+  DataPtr aData = myFeature->data();
+  AttributeSelectionPtr aSelect = aData->selection(attributeID());
+  if (aSelect) {
+    aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
+    return;
+  }
+  AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+  if (aRefAttr) {
+    aRefAttr->setObject(ObjectPtr());
+    return;
+  }
+  AttributeReferencePtr aRef = aData->reference(attributeID());
+  if (aRef) {
+    aRef->setObject(ObjectPtr());
+  }
 }
 
 //********************************************************************
@@ -153,14 +203,25 @@ bool ModuleBase_WidgetShapeSelector::restoreValue()
   DataPtr aData = myFeature->data();
   bool isBlocked = this->blockSignals(true);
   if (myUseSubShapes) {
-    boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
+    AttributeSelectionPtr aSelect = aData->selection(attributeID());
     if (aSelect) {
       mySelectedObject = aSelect->context();
       myShape = aSelect->value();
+    } else {
+      AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+      if (aRefAttr) {
+        mySelectedObject = aRefAttr->object();
+      }
     }
   } else {
-    boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
-    mySelectedObject = aRef->value();
+    AttributeReferencePtr aRef = aData->reference(attributeID());
+    if (aRef)
+      mySelectedObject = aRef->value();
+    else {
+      AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+      if (aRefAttr)
+        mySelectedObject = aRefAttr->object();
+    }
   }
   updateSelectionName();
 
@@ -172,7 +233,6 @@ bool ModuleBase_WidgetShapeSelector::restoreValue()
 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 {
   QList<QWidget*> aControls;
-  aControls.append(myLabel);
   aControls.append(myTextLine);
   return aControls;
 }
@@ -180,79 +240,93 @@ QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
 {
-  QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
-  if (aObjects.size() > 0) {
-    ObjectPtr aObject = aObjects.first();
-    if ((!mySelectedObject) && (!aObject))
-      return;
-    if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
-      return;
+  // In order to make reselection possible
+  // TODO: check with MPV clearAttribute();
 
-    // Get sub-shapes from local selection
-    boost::shared_ptr<GeomAPI_Shape> aShape;
-    if (myUseSubShapes) {
-      NCollection_List<TopoDS_Shape> aShapeList;
-      myWorkshop->selection()->selectedShapes(aShapeList);
-      if (aShapeList.Extent() > 0) {
-        aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
-        aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
-      }
+  //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
+  QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
+  if (aSelected.size() > 0)
+    setSelection(aSelected.first());
+}
+
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
+{
+  ObjectPtr aObject = theValue.object();
+  if ((!mySelectedObject) && (!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 selection corresponds to selection type
-    if (myUseSubShapes) {
-      if (!isAccepted(aShape))
-        return;
-    } else {
-      if (!isAccepted(aObject))
-        return;
+  // Check that the result has a shape
+  GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
+  if (!aShape)
+    return false;
+
+  /// Check that object has acceptable type
+  if (!acceptObjectType(aObject)) 
+    return false;
+
+  // Get sub-shapes from local selection
+  if (myUseSubShapes) {
+    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 (myUseSubShapes) {
+    if (!acceptSubShape(aShape))
+      return false;
+  } else {
+    if (!acceptObjectShape(aObject))
+      return false;
+  }
+  if (isValid(aObject, aShape)) {
     setObject(aObject, aShape);
     emit focusOutWidget(this);
   }
+  return true;
 }
 
 //********************************************************************
-void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
+void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
 {
-  if (mySelectedObject == theObj)
-    return;
   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);
-      Events_Loop::loop()->flush(anEvent);
-    }
   } 
   updateSelectionName();
-  activateSelection(false);
   emit valuesChanged();
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
+bool ModuleBase_WidgetShapeSelector::acceptObjectShape(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;
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
 
   // Check that the shape of necessary type
-  boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
+  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
   if (!aShapePtr)
     return false;
   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
@@ -276,7 +350,7 @@ bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape> theShape) const
+bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
 {
   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
   foreach (QString aType, myShapeTypes) {
@@ -286,6 +360,26 @@ bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape>
   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()
 {
@@ -294,17 +388,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("");
+    }
   }
 }
 
@@ -312,25 +397,34 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
 //********************************************************************
 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
 {
+  if (myIsActive == toActivate)
+    return;
   myIsActive = toActivate;
-  if (myIsActive)
-    myTextLine->setPalette(myBasePalet);
-  else
-    myTextLine->setPalette(myInactivePalet);
   updateSelectionName();
 
   if (myIsActive) {
     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
     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 (myUseSubShapes) 
+    if (myUseSubShapes) {
+      if (!myObjTypeFilter.IsNull()) {
+        myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
+        myObjTypeFilter.Nullify();
+      }
       myWorkshop->deactivateSubShapesSelection();
+    }
   }
 }
 
@@ -352,36 +446,67 @@ void ModuleBase_WidgetShapeSelector::raisePanel() const
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::focusTo()
+//bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
+//{
+//  if (theValue.object()) {
+//    ObjectPtr aObject = theValue.object();
+//    if (acceptObjectShape(aObject)) {
+//      setObject(aObject);
+//      return true;
+//    }
+//  }
+//  return false;
+//}
+
+//********************************************************************
+void ModuleBase_WidgetShapeSelector::activate()
 {
   activateSelection(true);
-  return true;
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
+void ModuleBase_WidgetShapeSelector::deactivate()
 {
-  if (theObj == myTextLine) {
-    if (theEvent->type() == QEvent::FocusIn)
-      activateSelection(true);
-  }
-  return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
+  activateSelection(false);
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
+bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
 {
-  if (theValue) {
-    ModuleBase_WidgetValueFeature* aFeatureValue =
-        dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
-    if (aFeatureValue && aFeatureValue->object()) {
-      ObjectPtr aObject = aFeatureValue->object();
-      if (isAccepted(aObject)) {
-        setObject(aObject);
-        return true;
+  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);
+
+  // Check the type of selected object
+  std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
+  bool isValid = true;
+  for (; aValidator != aValidators.end(); aValidator++) {
+    const ModelAPI_ResultValidator* aResValidator =
+        dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
+    if (aResValidator) {
+      isValid = false;
+      if (aResValidator->isValid(theObj)) {
+        isValid = true;
+        break;
       }
     }
   }
-  return false;
-}
+  if (!isValid)
+    return false;
 
+  // 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 (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
\ No newline at end of file