Salome HOME
Issue #1854 Recover feature control update
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index d4294aa2bc02cc58e80a2f4ad7d7c617b5ca14f8..8b07d585103bc4bfa081025d1275d10307b767f8 100644 (file)
@@ -1,22 +1,48 @@
-// File:        ModuleBase_WidgetShapeSelector.h
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+// File:        ModuleBase_WidgetShapeSelector.cpp
 // 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_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>
-#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 <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_AttributeValidator.h>
+
 #include <Config_WidgetAPI.h>
 
 #include <GeomAPI_Shape.h>
 
+#include <SelectMgr_ListIteratorOfListOfFilter.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopExp_Explorer.hxx>
 
 #include <QString>
 #include <QEvent>
 #include <QDockWidget>
+#include <QApplication>
+#include <QFormLayout>
 
-#include <stdexcept>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS_Shape.hxx>
 
-typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
-static ShapeTypes MyShapeTypes;
+#include <memory>
 
-TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
-{
-  if (MyShapeTypes.count() == 0) {
-    MyShapeTypes["face"] = TopAbs_FACE;
-    MyShapeTypes["vertex"] = TopAbs_VERTEX;
-    MyShapeTypes["wire"] = TopAbs_WIRE;
-    MyShapeTypes["edge"] = TopAbs_EDGE;
-    MyShapeTypes["shell"] = TopAbs_SHELL;
-    MyShapeTypes["solid"] = TopAbs_SOLID;
-  }
-  if (MyShapeTypes.contains(theType))
-    return MyShapeTypes[theType];
-  throw std::invalid_argument("Shape type defined in XML is not implemented!");
-}
+#include <list>
+#include <string>
 
 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
                                                      ModuleBase_IWorkshop* theWorkshop,
-                                                     const Config_WidgetAPI* theData,
-                                                     const std::string& theParentId)
-    : ModuleBase_ModelWidget(theParent, theData, theParentId),
-      myWorkshop(theWorkshop), myIsActive(false)
+                                                     const Config_WidgetAPI* theData)
+: ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
 {
-  myContainer = new QWidget(theParent);
-  QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
+  QFormLayout* aLayout = new QFormLayout(this);
+  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);
+  myLabel = new QLabel(aLabelText, this);
   if (!aLabelIcon.isEmpty())
-    myLabel->setPixmap(QPixmap(aLabelIcon));
+    myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
 
-  aLayout->addWidget(myLabel);
 
   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
-  myTextLine = new QLineEdit(myContainer);
+  myTextLine = new QLineEdit(this);
+  QString anObjName = QString::fromStdString(attributeID());
+  myTextLine->setObjectName(anObjName);
   myTextLine->setReadOnly(true);
   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);
+  aLayout->addRow(myLabel, myTextLine);
+  myLabel->setToolTip(aToolTip);
 
   std::string aTypes = theData->getProperty("shape_types");
-  myShapeTypes = QString(aTypes.c_str()).split(' ');
+  myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
 }
 
 //********************************************************************
@@ -91,35 +101,60 @@ ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::storeValue() const
+bool ModuleBase_WidgetShapeSelector::storeValueCustom()
 {
-  FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
-  if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
-    return false;
-
-  DataPtr aData = myFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
-      ModelAPI_AttributeReference>(aData->attribute(attributeID()));
+  // the value is stored on the selection changed signal processing
+  return true;
+}
 
-  ObjectPtr aObject = aRef->value();
-  if (!(aObject && aObject->isSame(mySelectedObject))) {
-    aRef->setValue(mySelectedObject);
-    updateObject(myFeature);
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
+                                                  const bool theToValidate)
+{
+  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;
   }
-  return true;
+  // 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;
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::restoreValue()
+QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
 {
-  DataPtr aData = myFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
+  QList<ModuleBase_ViewerPrsPtr> aSelected;
+  if(myFeature) {
+    DataPtr aData = myFeature->data();
+    AttributePtr anAttribute = myFeature->attribute(attributeID());
+
+    ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
+    std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
+    ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
+    aSelected.append(aPrs);
+  }
+  return aSelected;
+}
 
+//********************************************************************
+bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
+{
   bool isBlocked = this->blockSignals(true);
-  mySelectedObject = aRef->value();
   updateSelectionName();
-
   this->blockSignals(isBlocked);
+
   return true;
 }
 
@@ -127,138 +162,65 @@ bool ModuleBase_WidgetShapeSelector::restoreValue()
 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
 {
   QList<QWidget*> aControls;
-  aControls.append(myLabel);
   aControls.append(myTextLine);
   return aControls;
 }
 
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::onSelectionChanged()
+void ModuleBase_WidgetShapeSelector::updateFocus()
 {
-  QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
-  if (aObjects.size() > 0) {
-    ObjectPtr aObject = aObjects.first();
-    if ((!mySelectedObject) && (!aObject))
-      return;
-    if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
-      return;
-
-    // Check that the selection corresponds to selection type
-    if (!isAccepted(aObject))
-      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);
-    } else {
-      myTextLine->setText("");
-    }
-    activateSelection(false);
-    emit valuesChanged();
-    emit focusOutWidget(this);
-  }
+  emit focusOutWidget(this);
 }
 
 //********************************************************************
-bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
+QIntList ModuleBase_WidgetShapeSelector::shapeTypes() const
 {
-  ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
-  boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
-  if (!aShapePtr)
-    return false;
-  TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
-  if (aShape.IsNull())
-    return false;
-
-  TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
-  if (aShapeType == TopAbs_COMPOUND) {
-    foreach (QString aType, myShapeTypes) {
-      TopExp_Explorer aEx(aShape, shapeType(aType));
-      if (aEx.More())
-        return true;
-    }
-  } else {
-    foreach (QString aType, myShapeTypes) {
-      if (shapeType(aType) == aShapeType)
-        return true;
-    }
+  QIntList aShapeTypes;
+  foreach(QString aType, myShapeTypes) {
+    aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
   }
-  return false;
+  return aShapeTypes;
 }
 
 //********************************************************************
-void ModuleBase_WidgetShapeSelector::updateSelectionName()
+GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
 {
-  if (mySelectedObject) {
-    std::string aName = mySelectedObject->data()->name();
-    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"));
+  GeomShapePtr aShape;
+  DataPtr aData = myFeature->data();
+  if (aData->isValid()) {
+    AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
+    aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
   }
-}
-
-
-//********************************************************************
-void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
-{
-  myIsActive = toActivate;
-  if (myIsActive)
-    myTextLine->setPalette(myBasePalet);
-  else
-    myTextLine->setPalette(myInactivePalet);
-  updateSelectionName();
-
-  if (myIsActive)
-    connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-  else
-    disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+  return aShape;
 }
 
 //********************************************************************
-void ModuleBase_WidgetShapeSelector::raisePanel() const
+void ModuleBase_WidgetShapeSelector::updateSelectionName()
 {
-  QWidget* aParent = myContainer->parentWidget();
-  QWidget* aLastPanel = 0;
-  while (!aParent->inherits("QDockWidget")) {
-    aLastPanel = aParent;
-    aParent = aParent->parentWidget();
-    if (!aParent)
-      return;
+  DataPtr aData = myFeature->data();
+  if (!aData->isValid())
+    return;
+
+  bool isNameUpdated = false;
+  AttributeSelectionPtr aSelect = aData->selection(attributeID());
+  if (aSelect) {
+    myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
+    isNameUpdated = true;
   }
-  if (aParent->inherits("QDockWidget")) {
-    QDockWidget* aTabWgt = (QDockWidget*) aParent;
-    aTabWgt->raise();
+  if (!isNameUpdated) {
+    ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
+    if (anObject.get() != NULL) {
+      std::string aName = anObject->data()->name();
+      myTextLine->setText(QString::fromStdString(aName));
+    } else {
+      AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
+      if (aRefAttr && aRefAttr->attr().get() != NULL) {
+        //myIsObject = aRefAttr->isObject();
+        std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
+        myTextLine->setText(QString::fromStdString(anAttrName));
+      }
+      else {
+        myTextLine->setText(getDefaultValue().c_str());
+      }
+    }
   }
 }
-
-//********************************************************************
-bool ModuleBase_WidgetShapeSelector::focusTo()
-{
-  activateSelection(true);
-  return true;
-}
-
-//********************************************************************
-bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
-{
-  if (theObj == myTextLine) {
-    if (theEvent->type() == QEvent::FocusIn)
-      activateSelection(true);
-  }
-  return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
-}
\ No newline at end of file