Salome HOME
Example "PythonFeatures" plugin removed
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
index 227ec15863e74c2b089aca04513948391bc7e7c7..5c50ce38e318088dcc86d4182a45c35fae5901de 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * ModuleBase_WidgetMultiSelector.cpp
  *
@@ -25,7 +27,7 @@
 #include <QComboBox>
 #include <QEvent>
 
-#include <boost/smart_ptr/shared_ptr.hpp>
+#include <memory>
 #include <string>
 
 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
@@ -54,13 +56,15 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen
 
   myListControl = new QListWidget(myMainWidget);
   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
-  aMainLay->setColumnStretch(1, 1);
+  aMainLay->setRowStretch(2, 1);
+  aMainLay->addWidget(new QLabel(myMainWidget));
+  aMainLay->setRowMinimumHeight(3, 20);
   myMainWidget->setLayout(aMainLay);
-
   //TODO: Move into the base class
   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
   //TODO_END
   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
+
   activateSelection(true);
 }
 
@@ -77,9 +81,9 @@ bool ModuleBase_WidgetMultiSelector::storeValue() const
     return false;
   DataPtr aData = myFeature->data();
   AttributeSelectionListPtr aSelectionListAttr = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
 
-  if (aSelectionListAttr && (mySelection.size() > 0)) {
+  if (aSelectionListAttr) {
     aSelectionListAttr->clear();
     // Store shapes type
     TopAbs_ShapeEnum aCurrentType =
@@ -103,7 +107,7 @@ bool ModuleBase_WidgetMultiSelector::restoreValue()
     return false;
   DataPtr aData = myFeature->data();
   AttributeSelectionListPtr aSelectionListAttr = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
 
   if (aSelectionListAttr) {
     mySelection.clear();
@@ -139,13 +143,35 @@ QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
 //********************************************************************
 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
 {
-  if (theObj == myListControl) {
-    if (theEvent->type() == QEvent::FocusIn)
-      activateSelection(true);
-  }
+  //TODO: Remove maybe?
   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
 }
 
+//********************************************************************
+void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
+{
+  myIsActive = toActivate;
+  if (myIsActive) {
+    connect(myWorkshop, SIGNAL(selectionChanged()), 
+            this,       SLOT(onSelectionChanged()), 
+            Qt::UniqueConnection);
+    activateShapeSelection();
+  } else {
+    disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+    myWorkshop->deactivateSubShapesSelection();
+  }
+}
+
+//********************************************************************
+void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
+{
+  activateShapeSelection();
+  QObjectPtrList anEmptyList;
+  myWorkshop->setSelected(anEmptyList);
+  // Clear mySelection, myListControl and storeValue()
+  onSelectionChanged();
+}
+
 //********************************************************************
 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
 {
@@ -159,41 +185,29 @@ void ModuleBase_WidgetMultiSelector::onSelectionChanged()
   NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
   GeomShapePtr aShape;
   for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
-    ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
-    aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
+    if (myFeature) {
+      // We can not select a result of our feature
+      const std::list<ResultPtr>& aResList = myFeature->results();
+      std::list<ResultPtr>::const_iterator aIt;
+      bool isSkipSelf = false;
+      for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
+        if ((*aIt) == aResult) {
+          isSkipSelf = true;
+          break;
+        }
+      }
+      if(isSkipSelf)
+        continue;
+    }
+    aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
     aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
     mySelection.append(GeomSelection(aResult, aShape));
   }
   updateSelectionList();
-  storeValue();
   emit valuesChanged();
 }
 
-
-//********************************************************************
-void ModuleBase_WidgetMultiSelector::updateSelectionList()
-{
-  QString aType;
-  if (myTypeCombo->currentText().toLower() == "vertices")
-    aType = "vertex";
-  else if (myTypeCombo->currentText().toLower() == "edges")
-    aType = "edge";
-  else if (myTypeCombo->currentText().toLower() == "faces")
-    aType = "face";
-  else if (myTypeCombo->currentText().toLower() == "solids")
-    aType = "solid";
-  myListControl->clear();
-  int i = 1;
-  foreach (GeomSelection aSel, mySelection) {
-    QString aName(aSel.first->data()->name().c_str());
-    aName += ":" + aType + QString("_%1").arg(i);
-    myListControl->addItem(aName);
-    i++;
-  }
-}
-
-
 //********************************************************************
 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
                                                   NCollection_List<TopoDS_Shape>& theResult)
@@ -211,40 +225,53 @@ void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_
   }
 }
 
-
 //********************************************************************
 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
 {
   QString aShapeTypeName;
+  
   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
     aShapeTypeName = myTypeCombo->itemText(idx);
     TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
+      activateSelection(false);
+      bool isBlocked = myTypeCombo->blockSignals(true);
       myTypeCombo->setCurrentIndex(idx);
+      myTypeCombo->blockSignals(isBlocked);
+      activateSelection(true);
       break;
     }
   }
 }
 
-//********************************************************************
-void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
+void ModuleBase_WidgetMultiSelector::activateShapeSelection()
 {
-  myIsActive = toActivate;
-  if (myIsActive) {
-    connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-    onSelectionTypeChanged();
-  } else {
-    disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-    myWorkshop->deactivateSubShapesSelection();
-  }
+  QString aNewType = myTypeCombo->currentText();
+  QIntList aList;
+  aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
+  myWorkshop->activateSubShapesSelection(aList);
 }
 
 //********************************************************************
-void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
+void ModuleBase_WidgetMultiSelector::updateSelectionList()
 {
+  QString aType;
+  if (myTypeCombo->currentText().toLower() == "vertices")
+    aType = "vertex";
+  else if (myTypeCombo->currentText().toLower() == "edges")
+    aType = "edge";
+  else if (myTypeCombo->currentText().toLower() == "faces")
+    aType = "face";
+  else if (myTypeCombo->currentText().toLower() == "solids")
+    aType = "solid";
   myListControl->clear();
-  QString aNewType = myTypeCombo->currentText();
-  QIntList aList;
-  aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
-  myWorkshop->activateSubShapesSelection(aList);
+  int i = 1;
+  foreach (GeomSelection aSel, mySelection) {
+    QString aName(aSel.first->data()->name().c_str());
+    aName += ":" + aType + QString("_%1").arg(i);
+    myListControl->addItem(aName);
+    i++;
+  }
+  myListControl->repaint();
 }