Salome HOME
Issue #1865: Create a field
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 88aa9c3..11811c4
@@ -1,96 +1,34 @@
-// File:        ModuleBase_WidgetSelector.h
-// Created:     2 June 2014
-// Author:      Vitaly Smetannikov
-
-
-#include "ModuleBase_WidgetSelector.h"
-#include "ModuleBase_IWorkshop.h"
-#include "ModuleBase_Tools.h"
-
-#include <Events_Loop.h>
-#include <Model_Events.h>
-
-#include <ModelAPI_Data.h>
-#include <ModelAPI_Object.h>
-#include <ModelAPI_Result.h>
-#include <ModelAPI_AttributeReference.h>
-#include <Config_WidgetAPI.h>
-
-#include <GeomAPI_Shape.h>
-
-#include <TopoDS_Shape.hxx>
-#include <TopExp_Explorer.hxx>
-
-#include <QWidget>
-#include <QLayout>
-#include <QLabel>
-#include <QLineEdit>
-#include <QToolButton>
-#include <QString>
-#include <QEvent>
-#include <QDockWidget>
-
-
-typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
-static ShapeTypes MyShapeTypes;
-
-TopAbs_ShapeEnum ModuleBase_WidgetSelector::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!");
-}
-
-
-
-
-ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent, 
-                                                     ModuleBase_IWorkshop* theWorkshop, 
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
+// File:        ModuleBase_WidgetSelector.cpp
+// Created:     19 June 2015
+// Author:      Natalia ERMOLAEVA
+
+#include <ModuleBase_WidgetSelector.h>
+
+#include <ModuleBase_ISelection.h>
+#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_Tools.h>
+#include <ModuleBase_Operation.h>
+#include <ModuleBase_OperationDescription.h>
+#include <ModuleBase_WidgetFactory.h>
+#include <ModuleBase_IModule.h>
+#include <ModuleBase_ResultPrs.h>
+#include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_Events.h>
+
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_AttributeSelection.h>
+#include <ModelAPI_AttributeSelectionList.h>
+
+#include <TopoDS_Iterator.hxx>
+
+ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
+                                                     ModuleBase_IWorkshop* theWorkshop,
                                                      const Config_WidgetAPI* theData)
-: ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop), myActivateOnStart(false)
+: ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
 {
-  myContainer = new QWidget(theParent);
-  QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
-
-  aLayout->setContentsMargins(0, 0, 0, 0);
-  QString aLabelText = QString::fromStdString(theData->widgetLabel());
-  QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
-  myLabel = new QLabel(aLabelText, myContainer);
-  myLabel->setPixmap(QPixmap(aLabelIcon));
-
-  aLayout->addWidget(myLabel);
-
-  QString aToolTip = QString::fromStdString(theData->widgetTooltip());
-  myTextLine = new QLineEdit(myContainer);
-  myTextLine->setReadOnly(true);
-  myTextLine->setToolTip(aToolTip);
-  myTextLine->installEventFilter(this);
-
-  aLayout->addWidget(myTextLine);
-
-  myActivateBtn = new QToolButton(myContainer);
-  myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
-  myActivateBtn->setCheckable(true);
-  myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
-  connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
-
-  aLayout->addWidget(myActivateBtn);
-
-  QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
-  if (!aActivateTxt.isNull()) {
-    myActivateOnStart = (aActivateTxt == "true");
-  }
-
-  std::string aTypes = theData->getProperty("shape_types");
-  myShapeTypes = QString(aTypes.c_str()).split(',');
 }
 
 //********************************************************************
@@ -99,155 +37,216 @@ ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
 }
 
 //********************************************************************
-bool ModuleBase_WidgetSelector::storeValue(ObjectPtr theObject) const
+void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
+                                                      ObjectPtr& theObject,
+                                                      GeomShapePtr& theShape)
 {
-  DataPtr aData = theObject->data();
-  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);
-    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
-  }
-  return true;
+  ModuleBase_ISelection* aSelection = myWorkshop->selection();
+  theObject = aSelection->getResult(thePrs);
+  theShape = aSelection->getShape(thePrs);
 }
 
 //********************************************************************
-bool ModuleBase_WidgetSelector::restoreValue(ObjectPtr theObject)
+void ModuleBase_WidgetSelector::onSelectionChanged()
 {
-  DataPtr aData = theObject->data();
-  boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
+  QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
+  // equal vertices should not be used here
+  ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
+
+  bool isDone = setSelection(aSelected, true/*false*/);
+  updateOnSelectionChanged(isDone);
+}
 
-  bool isBlocked = this->blockSignals(true);
-  mySelectedObject = aRef->value();
-  updateSelectionName(); 
+//********************************************************************
+void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
+{
+  // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
+  // the same place repeatedly without mouse moved. In the case validation by filters is not
+  // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
+  // the 3rd click in the same point allow using this point.
+  emit valuesChanged();
+  // the updateObject method should be called to flush the updated sigal. The workshop listens it,
+  // calls validators for the feature and, as a result, updates the Apply button state.
+  updateObject(myFeature);
+
+  // we need to forget about previous validation result as the current selection can influence on it
+  clearValidatedCash();
+
+  if (theDone)
+    updateFocus();
+}
 
-  this->blockSignals(isBlocked);
-  return true;
+//********************************************************************
+QIntList ModuleBase_WidgetSelector::getShapeTypes() const
+{
+  QIntList aShapeTypes = shapeTypes();
+  // this type should be mentioned in XML, poor selection otherwise
+  if (/*aShapeTypes.contains(TopAbs_SOLID) ||*/
+      aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result/*TopAbs_SHAPE*/)) {
+    // it should be selectable for both, "solids" and "objects" types
+    aShapeTypes.append(TopAbs_COMPSOLID);
+  }
+  return aShapeTypes;
 }
 
 //********************************************************************
-QList<QWidget*> ModuleBase_WidgetSelector::getControls() const
+QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
 {
-  QList<QWidget*> aControls;
-  aControls.append(myLabel);
-  aControls.append(myTextLine);
-  aControls.append(myActivateBtn);
-  return aControls;
+  return QList<ModuleBase_ViewerPrsPtr>();
 }
 
 //********************************************************************
-void ModuleBase_WidgetSelector::onSelectionChanged()
+bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
+                                               const ResultPtr& theResult) const
 {
-  QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
-  if (aObjects.size() > 0) {
-    ObjectPtr aObject = aObjects.first();
-    if ((!mySelectedObject) && (!aObject))
-      return;
-    if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
-      return;
+  bool aValid = false;
+
+  GeomShapePtr aShape = theShape;
 
+  QIntList aShapeTypes = getShapeTypes();
+  if (aShapeTypes.empty()) {
+    aValid = true;
+    return aValid;
+  }
+  // when the SHAPE type is provided by XML as Object, the whole result shape should be selected.
+  if (!aShape.get() && aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
+    aValid = true;
+    return aValid;
+  }
+
+  if (!aShape.get() && theResult.get()) {
+    if (theResult.get())
+      aShape = theResult->shape();
+  }
+  TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
+  if (aShape.get()) {
     // Check that the selection corresponds to selection type
-    if (!isAccepted(aObject)) return;
-
-    mySelectedObject = aObject;
-    if (mySelectedObject) {
-      updateSelectionName();
-      activateSelection(false);
-      raisePanel();
-    } else {
-      myTextLine->setText("");
+    TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
+    aShapeType = aTopoShape.ShapeType();
+    // for compounds check sub-shapes: it may be compound of needed type:
+    // Booleans may produce compounds of Solids
+    if (aShapeType == TopAbs_COMPOUND) {
+      aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
+    }
+  }
+
+  QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
+  for (; anIt != aLast && !aValid; anIt++) {
+    TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
+    if (aShapeType == aCurrentShapeType)
+      aValid = true;
+    else if (aCurrentShapeType == TopAbs_FACE) {
+      // try to process the construction shape only if there is no a selected shape in the viewer
+      if (!theShape.get() && theResult.get()) {
+        ResultConstructionPtr aCResult =
+                                std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
+        aValid = aCResult.get() && aCResult->facesNum() > 0;
+      }
     }
-    emit valuesChanged();
   }
+  return aValid;
 }
 
 //********************************************************************
-bool ModuleBase_WidgetSelector::isAccepted(const ObjectPtr theResult) const
+bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
 {
-  ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
-  boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_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;
-    }
+  updateSelectionName();
+
+  if (toActivate) {
+    myWorkshop->activateSubShapesSelection(getShapeTypes());
   } else {
-    foreach (QString aType, myShapeTypes) {
-      if (shapeType(aType) == aShapeType)
-        return true;
-    }
+    myWorkshop->deactivateSubShapesSelection();
   }
-  return false;
+  return activateFilters(toActivate);
 }
 
 //********************************************************************
-void ModuleBase_WidgetSelector::updateSelectionName()
+void ModuleBase_WidgetSelector::activateCustom()
 {
-  if (mySelectedObject) {
-    std::string aName = mySelectedObject->data()->name();
-    myTextLine->setText(QString::fromStdString(aName));
-  } else 
-    myTextLine->setText("");
+  connect(myWorkshop, SIGNAL(selectionChanged()), this,
+          SLOT(onSelectionChanged()), Qt::UniqueConnection);
+
+  activateSelectionAndFilters(true);
+
+  // Restore selection in the viewer by the attribute selection list
+  // it should be postponed to have current widget as active to validate restored selection
+  static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
+  ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
 }
 
 //********************************************************************
-bool ModuleBase_WidgetSelector::eventFilter(QObject* theObj, QEvent* theEvent)
+bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
 {
-  if (theObj == myTextLine) {
-    if (theEvent->type() == QEvent::Polish) {
-      activateSelection(myActivateOnStart);
-      onSelectionChanged();
-    }
+  GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
+  ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
+  bool aValid = acceptSubShape(aShape, aResult);
+
+  if (aValid) {
+    // In order to avoid selection of the same object
+    ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
+    FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
+    aValid = aSelectedFeature != myFeature;
   }
-  return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
+  return aValid;
 }
 
 //********************************************************************
-void ModuleBase_WidgetSelector::enableOthersControls(bool toEnable) const
+bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
 {
-  QWidget* aParent = myContainer->parentWidget();
-  QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
-  foreach(QWidget* aWgt, aChldList) {
-    if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
-      aWgt->setEnabled(toEnable);
-  }
+  ObjectPtr anObject;
+  GeomShapePtr aShape;
+  getGeomSelection(thePrs, anObject, aShape);
+
+  // the last flag is to be depending on hasObject is called before. To be corrected later
+  return ModuleBase_Tools::setObject(attribute(), anObject, aShape,
+                                     myWorkshop, myIsInValidate, true);
 }
 
 //********************************************************************
-void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
+void ModuleBase_WidgetSelector::deactivate()
 {
-  enableOthersControls(!toActivate);
-  myTextLine->setEnabled(toActivate);
-
-  if (toActivate)
-    connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-  else
-    disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-
-  myActivateBtn->setDown(toActivate);
+  ModuleBase_ModelWidget::deactivate();
+  disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+  activateSelectionAndFilters(false);
+
+  /// clear temporary cash
+  AttributePtr anAttribute = attribute();
+  if (!anAttribute.get())
+    return;
+  std::string aType = anAttribute->attributeType();
+  if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) {
+    AttributeSelectionPtr aSelectAttr =
+                             std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
+    aSelectAttr->removeTemporaryValues();
+  }
+  else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
+    AttributeSelectionListPtr aSelectAttr =
+                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
+    aSelectAttr->removeTemporaryValues();
+  }
 }
 
 //********************************************************************
-void ModuleBase_WidgetSelector::raisePanel() const
+std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
+                                                    ModuleBase_IWorkshop* theWorkshop)
 {
-  QWidget* aParent = myContainer->parentWidget();
-  QWidget* aLastPanel = 0;
-  while (!aParent->inherits("QDockWidget")) {
-    aLastPanel = aParent;
-    aParent = aParent->parentWidget();
-    if (!aParent) return;
-  }
-  if (aParent->inherits("QDockWidget")) {
-    QDockWidget* aTabWgt = (QDockWidget*) aParent;
-    aTabWgt->raise();
+  std::string aName;
+  if (theAttribute.get() != NULL) {
+    ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
+
+    FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
+    if (aFeature.get()) {
+      std::string aXmlCfg, aDescription;
+      theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
+
+      ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
+      std::string anAttributeTitle;
+      aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
+
+      std::stringstream aStreamName;
+      aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
+      aName = aStreamName.str();
+    }
   }
-}
\ No newline at end of file
+  return aName;
+}