Salome HOME
Boost has been removed from code
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeature.cpp
index 5f625caeeafd045744a35b8504bd07e43e385445..3b28aeb6a00f6ab380c1e8af8cd9cce1611409f8 100644 (file)
@@ -6,17 +6,22 @@
 
 #include <ModuleBase_WidgetValueFeature.h>
 #include <ModuleBase_WidgetValue.h>
+#include <ModuleBase_Tools.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
 
 #include <Events_Loop.h>
-#include <Model_Events.h>
 
+#include <ModelAPI_Events.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_ResultValidator.h>
+#include <ModelAPI_RefAttrValidator.h>
+#include <ModelAPI_Session.h>
 
 #include <QWidget>
 #include <QLineEdit>
 #include <QLabel>
 
 ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
-                                                   const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData)
+                                                   const Config_WidgetAPI* theData,
+                                                   const std::string& theParentId)
+    : ModuleBase_ModelWidget(theParent, theData, theParentId)
 {
-  QString aKinds = QString::fromStdString(theData->getProperty(FEATURE_KEYSEQUENCE));
-  myFeatureKinds = aKinds.split(" ");
-
   myContainer = new QWidget(theParent);
   QHBoxLayout* aControlLay = new QHBoxLayout(myContainer);
-  aControlLay->setContentsMargins(0, 0, 0, 0);
+  ModuleBase_Tools::adjustMargins(aControlLay);
 
   QString aLabelText = QString::fromStdString(theData->widgetLabel());
   myLabel = new QLabel(aLabelText, myContainer);
@@ -46,7 +49,6 @@ ModuleBase_WidgetFeature::ModuleBase_WidgetFeature(QWidget* theParent,
 
   QString aTTip = QString::fromStdString(theData->widgetTooltip());
   myEditor->setToolTip(aTTip);
-
   aControlLay->addWidget(myEditor);
   aControlLay->setStretch(1, 1);
 }
@@ -60,53 +62,86 @@ bool ModuleBase_WidgetFeature::setValue(ModuleBase_WidgetValue* theValue)
   bool isDone = false;
 
   if (theValue) {
-    ModuleBase_WidgetValueFeature* aFeatureValue = 
-                         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
-    // TODO
-//    if (aFeatureValue)
-//      isDone = setFeature(aFeatureValue->feature());
+    ModuleBase_WidgetValueFeature* aFeatureValue =
+        dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
+    if (aFeatureValue)
+      isDone = setObject(aFeatureValue->object());
   }
   return isDone;
 }
 
-bool ModuleBase_WidgetFeature::setFeature(const FeaturePtr& theFeature)
+bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject, bool theSendEvent)
 {
-  if (!myFeatureKinds.contains(theFeature->getKind().c_str()))
+  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(theObject)) {
+        isValid = true;
+        break;
+      }
+    }
+  }
+  if (!isValid)
     return false;
 
-  myFeature = theFeature;
-  myEditor->setText(theFeature ? theFeature->data()->name().c_str() : "");
-  emit valuesChanged();
+  // 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, theObject)) {
+        return false;
+      }
+    }
+  }
+
+  myObject = theObject;
+  myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
+  if (theSendEvent)
+    emit valuesChanged();
   return true;
 }
 
-bool ModuleBase_WidgetFeature::storeValue(ObjectPtr theObject) const
+bool ModuleBase_WidgetFeature::storeValue() const
 {
-  FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
-  if (!aFeature)
+  //FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
+  if (!myObject)
     return false;
-  boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
 
-  ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
-  aRef->setObject(myFeature);
-  aFeature->execute();
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
+  std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
+      ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
 
+  ModuleBase_WidgetFeature* that = (ModuleBase_WidgetFeature*) this;
+  aRef->setObject(myObject);
+  myFeature->execute();
+  updateObject(myFeature);
   return true;
 }
 
-bool ModuleBase_WidgetFeature::restoreValue(ObjectPtr theObject)
+bool ModuleBase_WidgetFeature::restoreValue()
 {
-  boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
-
-  FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aRef->object());
-  if (aFeature) {
-    myFeature = aFeature;
-    myEditor->setText(myFeature ? myFeature->data()->name().c_str() : "");
+  std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
+  std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
+      ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
+
+  ObjectPtr anObjPtr = aRef->object();
+  if (anObjPtr) {
+    myObject = anObjPtr;
+    myEditor->setText(myObject ? myObject->data()->name().c_str() : "");
     return true;
   }
   return false;
@@ -120,7 +155,6 @@ QWidget* ModuleBase_WidgetFeature::getControl() const
 QList<QWidget*> ModuleBase_WidgetFeature::getControls() const
 {
   QList<QWidget*> aList;
-  aList.append(myLabel);
   aList.append(myEditor);
   return aList;
 }