Salome HOME
Correction of Boolean crash
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index 47fc4b44c12474b84c49673fab4988121f6ac93b..013b89d3c9b74063bb472513cafe5fa04b9f0aa3 100644 (file)
@@ -12,6 +12,9 @@
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_FilterFactory.h>
 #include <ModuleBase_Filter.h>
+#include <ModuleBase_IModule.h>
+#include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_IconFactory.h>
 
 #include <Config_WidgetAPI.h>
 #include <Events_Loop.h>
@@ -65,9 +68,8 @@
 
 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
                                                      ModuleBase_IWorkshop* theWorkshop,
-                                                     const Config_WidgetAPI* theData,
-                                                     const std::string& theParentId)
- : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
+                                                     const Config_WidgetAPI* theData)
+: ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
 {
   QFormLayout* aLayout = new QFormLayout(this);
   ModuleBase_Tools::adjustMargins(aLayout);
@@ -76,11 +78,13 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen
   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
   myLabel = new QLabel(aLabelText, this);
   if (!aLabelIcon.isEmpty())
-    myLabel->setPixmap(QPixmap(aLabelIcon));
+    myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
 
 
   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);
@@ -98,67 +102,53 @@ ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
+bool ModuleBase_WidgetShapeSelector::storeValueCustom()
 {
   // the value is stored on the selection changed signal processing 
   return true;
 }
 
 //********************************************************************
-void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
-                                               GeomShapePtr theShape)
+bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
+                                                  const bool theToValidate)
 {
-  DataPtr aData = myFeature->data();
-  AttributeReferencePtr aRef = aData->reference(attributeID());
-  if (aRef) {
-    ObjectPtr aObject = aRef->value();
-    if (!(aObject && aObject->isSame(theSelectedObject))) {
-      aRef->setValue(theSelectedObject);
-    }
-  } else {
-    AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
-    if (aRefAttr) {
-      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);
-      }
-    }
+  if (theValues.empty()) {
+    // In order to make reselection possible, set empty object and shape should be done
+    setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
+                                                  ObjectPtr(), GeomShapePtr(), NULL)));
+    return false;
+  }
+  // it removes the processed value from the parameters list
+  ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
+  bool isDone = false;
+
+  if (!theToValidate || isValidInFilters(aValue)) {
+    isDone = setSelectionCustom(aValue);
+    // updateObject - to update/redisplay feature
+    // it is commented in order to perfom it outside the method
+    //updateObject(myFeature);
+    // to storeValue()
+    //emit valuesChanged();
   }
+  return isDone;
 }
 
 //********************************************************************
-QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
+QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
 {
-  QList<ModuleBase_ViewerPrs> aSelected;
+  QList<ModuleBase_ViewerPrsPtr> aSelected;
   if(myFeature) {
     DataPtr aData = myFeature->data();
     AttributePtr anAttribute = myFeature->attribute(attributeID());
 
     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
-    TopoDS_Shape aShape;
     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
-    if (aShapePtr.get()) {
-      aShape = aShapePtr->impl<TopoDS_Shape>();
-    }
-    ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
+    ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
     aSelected.append(aPrs);
   }
   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()
 {
@@ -197,13 +187,10 @@ GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
 {
   GeomShapePtr aShape;
   DataPtr aData = myFeature->data();
-  if (!aData->isValid())
-    return aShape;
-
-  AttributeSelectionPtr aSelect = aData->selection(attributeID());
-  if (aSelect)
-    aShape = aSelect->value();
-
+  if (aData->isValid()) {
+    AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
+    aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
+  }
   return aShape;
 }
 
@@ -229,12 +216,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());
@@ -242,38 +225,3 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName()
     }
   }
 }
-
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::storeAttributeValue()
-{
-  ModuleBase_WidgetValidated::storeAttributeValue();
-
-  DataPtr aData = myFeature->data();
-  AttributePtr anAttribute = myFeature->attribute(attributeID());
-
-  myObject = ModuleBase_Tools::getObject(anAttribute);
-  myShape = getShape();
-  myRefAttribute = AttributePtr();
-  myIsObject = false;
-  AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
-  if (aRefAttr) {
-    myIsObject = aRefAttr->isObject();
-    myRefAttribute = aRefAttr->attr();
-  }
-}
-
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
-{
-  ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
-
-  DataPtr aData = myFeature->data();
-  AttributePtr anAttribute = myFeature->attribute(attributeID());
-
-  setObject(myObject, myShape);
-  AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
-  if (aRefAttr) {
-    if (!myIsObject)
-      aRefAttr->setAttr(myRefAttribute);
-  }
-}