Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectionFilter.cpp
index 94f2f54c46c49eaa78c1f4b1b19c51c60800d5a1..780d4cddd3f1da280c702508b3b0a688d4bfdda5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -34,7 +34,9 @@
 #include <ModelAPI_Events.h>
 #include <ModelAPI_FiltersFactory.h>
 #include <ModelAPI_ResultBody.h>
+
 #include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_Edge.h>
 
 #include <Events_Loop.h>
 #include <Config_ValidatorReader.h>
 #include <QCheckBox>
 #include <QDir>
 
+#ifdef WIN32
+#pragma warning(disable : 4456) // for nested foreach
+#endif
+
 FeaturePtr ModuleBase_WidgetSelectionFilter::SelectorFeature;
 std::string ModuleBase_WidgetSelectionFilter::AttributeId;
 
@@ -93,7 +99,8 @@ ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature
   ModuleBase_Tools::adjustMargins(aMainLayout);
 
   aMainLayout->addStretch(1);
-  QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
+  QPushButton* aLaunchBtn = new QPushButton(
+      ModuleBase_Tools::translate("FiltersSelection", "Selection by filters"), this);
   connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
   aMainLayout->addWidget(aLaunchBtn);
 }
@@ -177,6 +184,7 @@ ModuleBase_FilterItem::ModuleBase_FilterItem(
       connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
         theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
       connect(aWidget, SIGNAL(objectUpdated()), theParent, SLOT(onObjectUpdated()));
+      aWidget->enableFocusProcessing();
     }
     aLayout->addWidget(aParamsWgt);
   }
@@ -184,6 +192,7 @@ ModuleBase_FilterItem::ModuleBase_FilterItem(
 
 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
 {
+  std::string aContext = mySelection->getKind();
   QHBoxLayout* aLayout = new QHBoxLayout(theParent);
   ModuleBase_Tools::zeroMargins(aLayout);
 
@@ -197,17 +206,17 @@ void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
   else
     myRevBtn->setIcon(QIcon(":pictures/add.png"));
-  myRevBtn->setToolTip(tr("Reverse the filter"));
+  myRevBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Reverse the filter"));
   connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
   aLayout->addWidget(myRevBtn);
 
   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
-  aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
+  aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
 
   QToolButton* aDelBtn = new QToolButton(theParent);
   aDelBtn->setIcon(QIcon(":pictures/delete.png"));
   aDelBtn->setAutoRaise(true);
-  aDelBtn->setToolTip(tr("Delete the filter"));
+  aDelBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Delete the filter"));
   connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
   aLayout->addWidget(aDelBtn);
 }
@@ -249,7 +258,7 @@ ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theP
   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
   ModuleBase_Tools::adjustMargins(aMainLayout);
 
-  QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
+  QGroupBox* aFiltersGroup = new QGroupBox(translate("Filters"), this);
   QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
   aGroupLayout->setContentsMargins(0, 0, 0, 0);
   aGroupLayout->setSpacing(0);
@@ -260,14 +269,15 @@ ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theP
   aGroupLayout->addWidget(myFiltersWgt);
 
   myFiltersCombo = new QComboBox(aFiltersGroup);
-  myFiltersCombo->addItem(tr("Add new filter..."));
+  myFiltersCombo->addItem(translate("Add new filter..."));
   SessionPtr aSession = ModelAPI_Session::get();
   std::list<FilterPtr> allFilters =
     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
+  storeFilters(allFilters);
   QStringList aItems;
   std::list<FilterPtr>::const_iterator aIt;
   for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
-    aItems.push_back((*aIt)->name().c_str());
+    aItems.push_back(translate((*aIt)->name().c_str()));
   }
   myFiltersCombo->addItems(aItems);
   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
@@ -282,7 +292,7 @@ ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theP
 
   aBtnLayout->addStretch(1);
 
-  mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
+  mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
   aBtnLayout->addWidget(mySelectBtn);
 
@@ -293,13 +303,13 @@ ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theP
   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
   ModuleBase_Tools::zeroMargins(aLblLayout);
 
-  aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
+  aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
 
   myNbLbl = new QLabel("0", aLblWgt);
   aLblLayout->addWidget(myNbLbl);
 
   // Show only button
-  myShowBtn = new QCheckBox(tr("Show only"), this);
+  myShowBtn = new QCheckBox(translate("Show only"), this);
   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
   aLblLayout->addWidget(myShowBtn);
 
@@ -351,21 +361,22 @@ void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
     return;
 
   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
-  std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
   FiltersFeaturePtr aFiltersFeature =
     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
 
   std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
 
   std::list<FilterPtr>::iterator aIt;
-  int i;
   std::string aFilter;
-  for (aIt = aFilters.begin(), i = 0; aIt != aFilters.cend(); i++, aIt++) {
-    if (aText == (*aIt)->name()) {
-      aFilter = aFactory->id(*aIt);
-      break;
-    }
+  std::map<std::string, FilterPtr>::const_iterator aFound = myFilters.find(aText);
+  if (aFound == myFilters.end()) {
+    std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
+    storeFilters(aFilters);
+    aFound = myFilters.find(aText);
   }
+  if (aFound != myFilters.end())
+    aFilter = aFactory->id(aFound->second);
+
   aFiltersFeature->addFilter(aFilter);
   updateObject(myFeature);
 
@@ -447,35 +458,24 @@ void ModuleBase_WidgetSelectionFilter::onSelect()
 
   clearCurrentSelection();
 
+  FiltersFeaturePtr aFiltersFeature =
+    std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
+  static SessionPtr aSession = ModelAPI_Session::get();
+  std::list< std::pair<ResultPtr, GeomShapePtr> > aResList =
+    aSession->filters()->select(aFiltersFeature, (GeomAPI_Shape::ShapeType)mySelectionType);
+
   BRep_Builder aBuilder;
   TopoDS_Compound aComp;
   aBuilder.MakeCompound(aComp);
 
-  DocumentPtr aDoc = myFeature->document();
-  int aNb = aDoc->size(ModelAPI_ResultBody::group());
-  ObjectPtr aObj;
-  ResultBodyPtr aBody;
-  for (int i = 0; i < aNb; i++) {
-    aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
-    aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
-    GeomShapePtr aShape = aBody->shape();
-    std::list<GeomShapePtr> aSubShapes =
-      aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
-    TopTools_MapOfShape alreadyThere;
-    std::list<GeomShapePtr>::const_iterator aShapesIt;
-    for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
-      GeomShapePtr aSubShape = (*aShapesIt);
-      TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
-      if (!alreadyThere.Add(aTShape))
-        continue;
-      static SessionPtr aSession = ModelAPI_Session::get();
-      bool isValid = aSession->filters()->isValid(myFeature, aBody, aSubShape);
-      if (isValid) {
-        aBuilder.Add(aComp, aTShape);
-        ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aSubShape));
-        myValues.append(aValue);
-      }
-    }
+  std::list< std::pair<ResultPtr, GeomShapePtr> >::const_iterator itSelected = aResList.cbegin();
+  for (; itSelected != aResList.cend(); itSelected++) {
+    ResultPtr aCurRes = (*itSelected).first;
+    GeomShapePtr aSubShape = (*itSelected).second;
+    TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
+    aBuilder.Add(aComp, aTShape);
+    ModuleBase_ViewerPrsPtr aValue (new ModuleBase_ViewerPrs(aCurRes, aSubShape));
+    myValues.append(aValue);
   }
 
   if (myValues.size() > 0)
@@ -516,33 +516,40 @@ void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShap
 
 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
 {
-  if (myPreview.IsNull())
-    return;
   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
-
   if (theShow) {
     AIS_ListOfInteractive aList;
     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
-    aList.Remove(myPreview);
+    if (!myPreview.IsNull())
+      aList.Remove(myPreview);
     if (aList.Size() > 0)
       myListIO = aList;
   }
   AIS_ListOfInteractive::const_iterator aIt;
   Handle(AIS_Shape) aShapeIO;
+  bool isModified = false;
   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
     if (!aShapeIO.IsNull()) {
-      if (theShow)
-        aCtx->Erase(aShapeIO, false);
+      if (theShow) {
+        if (aCtx->IsDisplayed(aShapeIO)) {
+          aCtx->Erase(aShapeIO, false);
+          isModified = true;
+        }
+      }
       else {
-        aCtx->Display(aShapeIO, false);
-        std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
-        anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
-        myWorkshop->applyCurrentSelectionModes(anAISObj);
+        if (!aCtx->IsDisplayed(aShapeIO)) {
+          aCtx->Display(aShapeIO, false);
+          std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
+          anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
+          myWorkshop->applyCurrentSelectionModes(anAISObj);
+          isModified = true;
+        }
       }
     }
   }
-  myWorkshop->viewer()->update();
+  if (isModified)
+    myWorkshop->viewer()->update();
 }
 
 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
@@ -555,12 +562,16 @@ void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
 {
   int aNb = myValues.size();
   myNbLbl->setText(QString::number(aNb));
-  //QString aErr = () ? tr("Selection is empty") : "";
-  if (aNb == 0)
-    myFeature->setError(tr("Selection is empty").toStdString(), false, false);
+  if (aNb == 0) {
+    myFeature->setError(translate("Selection is empty").toStdString(), false, false);
+    myShowBtn->setChecked(false);
+    onShowOnly(false);
+    myShowBtn->setEnabled(false);
+  }
   else {
     myFeature->setError("", false, false);
     myFeature->data()->execState(ModelAPI_StateDone);
+    myShowBtn->setEnabled(true);
   }
 }
 
@@ -701,7 +712,7 @@ QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateCheck
   QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
   if (aErrorMsg.isEmpty()) {
     if (myValues.size() == 0)
-      aErrorMsg = tr("Selection is empty");
+      aErrorMsg = translate("Selection is empty");
   }
   return aErrorMsg;
 }
@@ -728,3 +739,17 @@ void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
   ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
   Events_Loop::loop()->flush(EVENT_DISP);
 }
+
+void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
+{
+  for (std::list<FilterPtr>::const_iterator anIt = theFilters.begin();
+       anIt != theFilters.end(); ++anIt) {
+    std::string aName = translate((*anIt)->name()).toStdString();
+    myFilters[aName] = *anIt;
+  }
+}
+
+QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
+{
+  return ModuleBase_Tools::translate(myFeatureId, theString);
+}