Salome HOME
Issue #1330 correction for attribute ref attr list. Crash of rectangle creation
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index 84c409d5008b2b65b0a8dffd11260f9fd95bce68..b68e282b69630d35b31d42bb8790a3f5064adf2e 100644 (file)
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_FilterFactory.h>
 #include <ModuleBase_Filter.h>
+#include <ModuleBase_IModule.h>
 
 #include <Config_WidgetAPI.h>
 #include <Events_Loop.h>
 #include <Events_Message.h>
 #include <GeomAPI_Interface.h>
 #include <GeomAPI_Shape.h>
-#include <GeomValidators_Tools.h>
 
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Data.h>
@@ -35,7 +35,6 @@
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_AttributeValidator.h>
-#include <ModelAPI_ShapeValidator.h>
 
 #include <Config_WidgetAPI.h>
 #include <Events_Error.h>
@@ -83,11 +82,14 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen
 
   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
   myTextLine = new QLineEdit(this);
+  QString anObjName = QString::fromStdString(attributeID());
+  myTextLine->setObjectName(anObjName);
   myTextLine->setReadOnly(true);
   myTextLine->setToolTip(aToolTip);
   myTextLine->installEventFilter(this);
 
   aLayout->addRow(myLabel, myTextLine);
+  myLabel->setToolTip(aToolTip);
 
   std::string aTypes = theData->getProperty("shape_types");
   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
@@ -110,25 +112,30 @@ void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
                                                GeomShapePtr theShape)
 {
   DataPtr aData = myFeature->data();
-  AttributeReferencePtr aRef = aData->reference(attributeID());
-  if (aRef) {
+  std::string aType = aData->attribute(attributeID())->attributeType();
+  if (aType == ModelAPI_AttributeReference::typeId()) {
+    AttributeReferencePtr aRef = aData->reference(attributeID());
     ObjectPtr aObject = aRef->value();
     if (!(aObject && aObject->isSame(theSelectedObject))) {
       aRef->setValue(theSelectedObject);
     }
-  } else {
+  } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
-    if (aRefAttr) {
+
+    AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
+    if (anAttribute.get())
+      aRefAttr->setAttr(anAttribute);
+    else {
       ObjectPtr aObject = aRefAttr->object();
       if (!(aObject && aObject->isSame(theSelectedObject))) {
         aRefAttr->setObject(theSelectedObject);
       }
-    } else {
-      AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
-      ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
-      if (aSelectAttr.get() != NULL) {
-        aSelectAttr->setValue(aResult, theShape);
-      }
+    }
+  } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
+    if (aSelectAttr.get() != NULL) {
+      aSelectAttr->setValue(aResult, theShape);
     }
   }
 }
@@ -141,7 +148,7 @@ QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelectio
     DataPtr aData = myFeature->data();
     AttributePtr anAttribute = myFeature->attribute(attributeID());
 
-    ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
+    ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
     TopoDS_Shape aShape;
     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
     if (aShapePtr.get()) {
@@ -153,13 +160,6 @@ QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelectio
   return aSelected;
 }
 
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::clearAttribute()
-{
-  // In order to make reselection possible, set empty object and shape should be done
-  setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
-}
-
 //********************************************************************
 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
 {
@@ -201,9 +201,18 @@ GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
   if (!aData->isValid())
     return aShape;
 
-  AttributeSelectionPtr aSelect = aData->selection(attributeID());
-  if (aSelect)
-    aShape = aSelect->value();
+  std::string aType = aData->attribute(attributeID())->attributeType();
+  if (aType == ModelAPI_AttributeReference::typeId()) {
+  } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
+    AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+    if (aRefAttr.get() && !aRefAttr->isObject()) {
+      AttributePtr anAttribute = aRefAttr->attr();
+      aShape = myWorkshop->module()->findShape(anAttribute);
+    }
+  } else if (aType == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
+    aShape = aSelectAttr->value();
+  }
 
   return aShape;
 }
@@ -222,7 +231,7 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
     isNameUpdated = true;
   }
   if (!isNameUpdated) {
-    ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
+    ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
     if (anObject.get() != NULL) {
       std::string aName = anObject->data()->name();
       myTextLine->setText(QString::fromStdString(aName));
@@ -230,12 +239,8 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
       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()));
-        }
+        std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
+        myTextLine->setText(QString::fromStdString(anAttrName));
       }
       else {
         myTextLine->setText(getDefaultValue().c_str());
@@ -252,7 +257,7 @@ void ModuleBase_WidgetShapeSelector::storeAttributeValue()
   DataPtr aData = myFeature->data();
   AttributePtr anAttribute = myFeature->attribute(attributeID());
 
-  myObject = GeomValidators_Tools::getObject(anAttribute);
+  myObject = ModuleBase_Tools::getObject(anAttribute);
   myShape = getShape();
   myRefAttribute = AttributePtr();
   myIsObject = false;