Salome HOME
e027461f6034178b999cd2c36bfdc2631665a820
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectionFilter.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_WidgetSelectionFilter.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IWorkshop.h"
23 #include "ModuleBase_ISelectionActivate.h"
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_IPropertyPanel.h"
27 #include "ModuleBase_PageWidget.h"
28 #include "ModuleBase_WidgetMultiSelector.h"
29 #include "ModuleBase_ResultPrs.h"
30 #include "ModuleBase_WidgetFactory.h"
31
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_FiltersFactory.h>
36 #include <ModelAPI_ResultBody.h>
37
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_Edge.h>
40
41 #include <Events_Loop.h>
42 #include <Config_ValidatorReader.h>
43
44 #include <AIS_InteractiveContext.hxx>
45 #include <StdSelect_BRepOwner.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <BRep_Builder.hxx>
48 #include <TopExp_Explorer.hxx>
49
50 #include <QLayout>
51 #include <QPushButton>
52 #include <QLabel>
53 #include <QComboBox>
54 #include <QGroupBox>
55 #include <QDialog>
56 #include <QToolButton>
57 #include <QCheckBox>
58 #include <QDir>
59
60 FeaturePtr ModuleBase_WidgetSelectionFilter::SelectorFeature;
61 std::string ModuleBase_WidgetSelectionFilter::AttributeId;
62
63
64 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
65 {
66   QString aType = theType.toUpper();
67   if ((aType == "VERTEX") || (aType == "VERTICES"))
68     return GeomAPI_Shape::VERTEX;
69   else if ((aType == "EDGE") || (aType == "EDGES"))
70     return GeomAPI_Shape::EDGE;
71   else if ((aType == "WIRE") || (aType == "WIRES"))
72     return GeomAPI_Shape::WIRE;
73   else if ((aType == "FACE") || (aType == "FACES"))
74     return GeomAPI_Shape::FACE;
75   else if ((aType == "SHELL") || (aType == "SHELLS"))
76     return GeomAPI_Shape::SHELL;
77   else if ((aType == "SOLID") || (aType == "SOLIDS"))
78     return GeomAPI_Shape::SOLID;
79   else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
80     return GeomAPI_Shape::COMPSOLID;
81   else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
82     return GeomAPI_Shape::COMPOUND;
83   else
84     return GeomAPI_Shape::SHAPE;
85 }
86
87
88 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
89   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
90   : QWidget(theParent),
91   myFeatureName(theFeature),
92   myWorkshop(theWorkshop)
93 {
94   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
95   ModuleBase_Tools::adjustMargins(aMainLayout);
96
97   aMainLayout->addStretch(1);
98   QPushButton* aLaunchBtn = new QPushButton(
99       ModuleBase_Tools::translate("FiltersSelection", "Selection by filters"), this);
100   connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
101   aMainLayout->addWidget(aLaunchBtn);
102 }
103
104 void ModuleBase_FilterStarter::onFiltersLaunch()
105 {
106   static QString aHelpFileName = QString("FiltersPlugin") + QDir::separator() +
107     QString("FiltersPlugin.html");
108
109   ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
110   ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
111   if (aFeatureOp)
112     // Open transaction on filters operation finish
113     aFeatureOp->openTransactionOnResume();
114
115   QWidget* aParent = parentWidget();
116   ModuleBase_WidgetMultiSelector* aSelector =
117     dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
118   while (!aSelector) {
119     aParent = aParent->parentWidget();
120     aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
121   }
122   if (!aSelector)
123     return;
124   ModuleBase_WidgetSelectionFilter::SelectorFeature = aSelector->feature();
125   ModuleBase_WidgetSelectionFilter::AttributeId = aSelector->attributeID();
126
127   // Launch Filters operation
128   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
129     (myWorkshop->module()->createOperation(myFeatureName));
130
131   AttributeSelectionListPtr aAttrList =
132     ModuleBase_WidgetSelectionFilter::SelectorFeature->selectionList(
133       ModuleBase_WidgetSelectionFilter::AttributeId);
134   FiltersFeaturePtr aFilters = aAttrList->filters();
135   if (aFilters.get())
136     aFOperation->setFeature(aFilters);
137   aFOperation->setHelpFileName(aHelpFileName);
138   myWorkshop->processLaunchOperation(aFOperation);
139 }
140
141 //*****************************************************************************
142 //*****************************************************************************
143 //*****************************************************************************
144 ModuleBase_FilterItem::ModuleBase_FilterItem(
145   const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
146   : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
147     mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
148 {
149   FilterPtr aFilter = ModelAPI_Session::get()->filters()->filter(theFilter);
150   std::string aXmlString = aFilter->xmlRepresentation();
151   if (aXmlString.length() == 0)
152     addItemRow(this);
153   else {
154     std::string anAttrPrefix; // this must be added to the attributes names for multiple filters
155     std::string aFilterKind = ModelAPI_Session::get()->filters()->id(aFilter);
156     if (theFilter != aFilterKind) {
157       anAttrPrefix = theFilter.substr(0, theFilter.size() - aFilterKind.size());
158     }
159     ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop(), anAttrPrefix);
160     Config_ValidatorReader aValidatorReader(aXmlString, true);
161     aValidatorReader.setFeatureId(mySelection->getKind());
162     aValidatorReader.readAll();
163
164     QVBoxLayout* aLayout = new QVBoxLayout(this);
165     ModuleBase_Tools::zeroMargins(aLayout);
166
167     QWidget* aItemRow = new QWidget(this);
168     addItemRow(aItemRow);
169     aLayout->addWidget(aItemRow);
170
171     ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
172     aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
173     aFactory.createWidget(aParamsWgt);
174     ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
175     myWidgets = aFactory.getModelWidgets();
176     foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
177       aWidget->setFeature(theParent->feature());
178       connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
179         theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
180       connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
181         theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
182       connect(aWidget, SIGNAL(objectUpdated()), theParent, SLOT(onObjectUpdated()));
183       aWidget->enableFocusProcessing();
184     }
185     aLayout->addWidget(aParamsWgt);
186   }
187 }
188
189 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
190 {
191   std::string aContext = mySelection->getKind();
192   QHBoxLayout* aLayout = new QHBoxLayout(theParent);
193   ModuleBase_Tools::zeroMargins(aLayout);
194
195   // Reverse filter button
196   myRevBtn = new QToolButton(theParent);
197   myRevBtn->setCheckable(true);
198   bool isReversed = mySelection->isReversed(myFilterID);
199   myRevBtn->setChecked(isReversed);
200   myRevBtn->setAutoRaise(true);
201   if (isReversed)
202     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
203   else
204     myRevBtn->setIcon(QIcon(":pictures/add.png"));
205   myRevBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Reverse the filter"));
206   connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
207   aLayout->addWidget(myRevBtn);
208
209   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
210   aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
211
212   QToolButton* aDelBtn = new QToolButton(theParent);
213   aDelBtn->setIcon(QIcon(":pictures/delete.png"));
214   aDelBtn->setAutoRaise(true);
215   aDelBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Delete the filter"));
216   connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
217   aLayout->addWidget(aDelBtn);
218 }
219
220 void ModuleBase_FilterItem::onReverse(bool theCheck)
221 {
222   mySelection->setReversed(myFilterID, theCheck);
223   if (theCheck)
224     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
225   else
226     myRevBtn->setIcon(QIcon(":pictures/add.png"));
227   emit reversedItem(this);
228 }
229
230 void ModuleBase_FilterItem::onDelete()
231 {
232   emit deleteItem(this);
233 }
234
235
236 //*****************************************************************************
237 //*****************************************************************************
238 //*****************************************************************************
239 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
240   ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, bool theReadOnly)
241   : ModuleBase_ModelWidget(theParent, theData),
242   myWorkshop(theWorkshop),
243   mySelectorFeature(ModuleBase_WidgetSelectionFilter::SelectorFeature),
244   mySelectorAttribute(ModuleBase_WidgetSelectionFilter::AttributeId)
245 {
246   // Clear Old selection
247     AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
248     mySelectionType = selectionType(aAttrList->selectionType().c_str());
249   if (!theReadOnly) {
250     aAttrList->clear();
251   }
252
253   // Define widgets
254   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
255   ModuleBase_Tools::adjustMargins(aMainLayout);
256
257   QGroupBox* aFiltersGroup = new QGroupBox(translate("Filters"), this);
258   QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
259   aGroupLayout->setContentsMargins(0, 0, 0, 0);
260   aGroupLayout->setSpacing(0);
261
262   myFiltersWgt = new QWidget();
263   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
264   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
265   aGroupLayout->addWidget(myFiltersWgt);
266
267   myFiltersCombo = new QComboBox(aFiltersGroup);
268   myFiltersCombo->addItem(translate("Add new filter..."));
269   SessionPtr aSession = ModelAPI_Session::get();
270   std::list<FilterPtr> allFilters =
271     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
272   storeFilters(allFilters);
273   QStringList aItems;
274   std::list<FilterPtr>::const_iterator aIt;
275   for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
276     aItems.push_back(translate((*aIt)->name().c_str()));
277   }
278   myFiltersCombo->addItems(aItems);
279   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
280
281   aGroupLayout->addWidget(myFiltersCombo);
282   aMainLayout->addWidget(aFiltersGroup);
283
284   // Select Button
285   QWidget* aBtnWgt = new QWidget(this);
286   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
287   ModuleBase_Tools::adjustMargins(aBtnLayout);
288
289   aBtnLayout->addStretch(1);
290
291   mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
292   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
293   aBtnLayout->addWidget(mySelectBtn);
294
295   aMainLayout->addWidget(aBtnWgt);
296
297   // Label widgets
298   QWidget* aLblWgt = new QWidget(this);
299   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
300   ModuleBase_Tools::zeroMargins(aLblLayout);
301
302   aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
303
304   myNbLbl = new QLabel("0", aLblWgt);
305   aLblLayout->addWidget(myNbLbl);
306
307   // Show only button
308   myShowBtn = new QCheckBox(translate("Show only"), this);
309   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
310   aLblLayout->addWidget(myShowBtn);
311
312   aMainLayout->addWidget(aLblWgt);
313
314   aMainLayout->addStretch(1);
315
316   updateSelectBtn();
317   if (theReadOnly) {
318     myFiltersCombo->hide();
319     mySelectBtn->hide();
320     aLblWgt->hide();
321     myShowBtn->hide();
322   }
323 }
324
325 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
326 {
327   myValues.clear();
328   if (!myPreview.IsNull()) {
329     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
330     aCtx->Remove(myPreview, false);
331     myPreview.Nullify();
332     if (myListIO.Size() > 0) {
333       aCtx = myWorkshop->viewer()->AISContext();
334       AIS_ListOfInteractive::const_iterator aIt;
335       Handle(AIS_Shape) aShapeIO;
336       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
337         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
338         if (!aShapeIO.IsNull()) {
339           aCtx->Display(aShapeIO, false);
340           std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
341           anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
342           myWorkshop->applyCurrentSelectionModes(anAISObj);
343         }
344       }
345       myListIO.Clear();
346       myShowBtn->setChecked(false);
347     }
348     myWorkshop->viewer()->update();
349   }
350   SelectorFeature = FeaturePtr();
351   AttributeId = "";
352 }
353
354 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
355 {
356   if (theIndex == 0)
357     return;
358
359   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
360   FiltersFeaturePtr aFiltersFeature =
361     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
362
363   std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
364
365   std::list<FilterPtr>::iterator aIt;
366   std::string aFilter;
367   std::map<std::string, FilterPtr>::const_iterator aFound = myFilters.find(aText);
368   if (aFound == myFilters.end()) {
369     std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
370     storeFilters(aFilters);
371     aFound = myFilters.find(aText);
372   }
373   if (aFound != myFilters.end())
374     aFilter = aFactory->id(aFound->second);
375
376   aFiltersFeature->addFilter(aFilter);
377   updateObject(myFeature);
378
379   QList<ModuleBase_FilterItem*> aList = itemsList();
380   if (!aList.isEmpty() && (aList.last()->widgets().size() > 0))
381     aList.last()->widgets().first()->emitFocusInWidget();
382   else
383     emitFocusInWidget();
384 }
385
386 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
387 {
388   if (theFilter.length() == 0)
389     return 0;
390   ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
391   connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
392     SLOT(onDeleteItem(ModuleBase_FilterItem*)));
393   connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
394     SLOT(onReverseItem(ModuleBase_FilterItem*)));
395   myFiltersLayout->addWidget(aItem);
396
397   updateSelectBtn();
398   clearCurrentSelection(true);
399   updateNumberSelected();
400   return aItem;
401 }
402
403 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
404 {
405   std::string aFilter = theItem->filter();
406   QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
407   foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
408     aWgt->deactivate();
409   }
410   myFiltersLayout->removeWidget(theItem);
411   theItem->deleteLater();
412
413   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
414   if (!aFactory->filter(aFilter)->isMultiple()) {
415     //myFilters.push_back(aFilter);
416     myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
417   }
418   FiltersFeaturePtr aFiltersFeature =
419     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
420   aFiltersFeature->removeFilter(aFilter);
421
422   updateSelectBtn();
423   clearCurrentSelection(true);
424   updateNumberSelected();
425
426   myWorkshop->deactivateCurrentSelector();
427   myWorkshop->selectionActivate()->updateSelectionModes();
428   myWorkshop->selectionActivate()->updateSelectionFilters();
429   redisplayFeature();
430   emitFocusInWidget();
431   updateObject(myFeature);
432 }
433
434
435 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
436 {
437   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
438   ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
439   Events_Loop::loop()->flush(aDispEvent);
440 }
441
442 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
443 {
444   updateSelectBtn();
445   clearCurrentSelection(true);
446   updateNumberSelected();
447 }
448
449 void ModuleBase_WidgetSelectionFilter::onSelect()
450 {
451   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
452   if (aCtx.IsNull())
453     return;
454
455   clearCurrentSelection();
456
457   FiltersFeaturePtr aFiltersFeature =
458     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
459   static SessionPtr aSession = ModelAPI_Session::get();
460   std::list< std::pair<ResultPtr, GeomShapePtr> > aResList =
461     aSession->filters()->select(aFiltersFeature, (GeomAPI_Shape::ShapeType)mySelectionType);
462
463   BRep_Builder aBuilder;
464   TopoDS_Compound aComp;
465   aBuilder.MakeCompound(aComp);
466
467   std::list< std::pair<ResultPtr, GeomShapePtr> >::const_iterator itSelected = aResList.cbegin();
468   for (; itSelected != aResList.cend(); itSelected++) {
469     ResultPtr aCurRes = (*itSelected).first;
470     GeomShapePtr aSubShape = (*itSelected).second;
471     TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
472     aBuilder.Add(aComp, aTShape);
473     ModuleBase_ViewerPrsPtr aValue (new ModuleBase_ViewerPrs(aCurRes, aSubShape));
474     myValues.append(aValue);
475   }
476
477   if (myValues.size() > 0)
478     updatePreview(aComp);
479   updateNumberSelected();
480   updateObject(myFeature);
481   onShowOnly(myShowBtn->isChecked());
482 }
483
484 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
485 {
486   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
487   if (aCtx.IsNull())
488     return;
489
490   if (myPreview.IsNull()) {
491     myPreview = new AIS_Shape(theShape);
492     myPreview->SetDisplayMode(AIS_Shaded);
493     myPreview->SetColor(Quantity_NOC_BLUE1);
494     Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
495     if (aDrawer->HasOwnPointAspect()) {
496       aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
497       aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
498       aDrawer->PointAspect()->SetScale(2.);
499     }
500     else
501       aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
502     myPreview->SetTransparency();
503     aCtx->Display(myPreview, true);
504     aCtx->Deactivate(myPreview);
505   }
506   else {
507     myPreview->Set(theShape);
508     aCtx->Redisplay(myPreview, true);
509   }
510 }
511
512
513 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
514 {
515   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
516   if (theShow) {
517     AIS_ListOfInteractive aList;
518     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
519     if (!myPreview.IsNull())
520       aList.Remove(myPreview);
521     if (aList.Size() > 0)
522       myListIO = aList;
523   }
524   AIS_ListOfInteractive::const_iterator aIt;
525   Handle(AIS_Shape) aShapeIO;
526   bool isModified = false;
527   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
528     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
529     if (!aShapeIO.IsNull()) {
530       if (theShow) {
531         if (aCtx->IsDisplayed(aShapeIO)) {
532           aCtx->Erase(aShapeIO, false);
533           isModified = true;
534         }
535       }
536       else {
537         if (!aCtx->IsDisplayed(aShapeIO)) {
538           aCtx->Display(aShapeIO, false);
539           std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
540           anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
541           myWorkshop->applyCurrentSelectionModes(anAISObj);
542           isModified = true;
543         }
544       }
545     }
546   }
547   if (isModified)
548     myWorkshop->viewer()->update();
549 }
550
551 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
552 {
553   FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
554   mySelectBtn->setEnabled(aFiltersFeature.get() && (aFiltersFeature->filters().size() > 0));
555 }
556
557 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
558 {
559   int aNb = myValues.size();
560   myNbLbl->setText(QString::number(aNb));
561   if (aNb == 0) {
562     myFeature->setError(translate("Selection is empty").toStdString(), false, false);
563     myShowBtn->setChecked(false);
564     onShowOnly(false);
565     myShowBtn->setEnabled(false);
566   }
567   else {
568     myFeature->setError("", false, false);
569     myFeature->data()->execState(ModelAPI_StateDone);
570     myShowBtn->setEnabled(true);
571   }
572 }
573
574 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
575 {
576   QList<QWidget*> aWidgets;
577   aWidgets.append(myFiltersCombo);
578   return aWidgets;
579 }
580
581 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
582 {
583   myValues.clear();
584   if (!myPreview.IsNull()) {
585     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
586     aCtx->Remove(myPreview, toUpdate);
587     myPreview.Nullify();
588   }
589 }
590
591 void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
592 {
593   QMap<ObjectPtr, QList<GeomShapePtr>> myResShapes;
594   // Sort sub-shapes by result
595   foreach (ModuleBase_ViewerPrsPtr aPrs, theResults) {
596     if (myResShapes.contains(aPrs->object()))
597       myResShapes[aPrs->object()].append(aPrs->shape());
598     else {
599       QList<GeomShapePtr> aShapes;
600       aShapes << aPrs->shape();
601       myResShapes[aPrs->object()] = aShapes;
602     }
603   }
604   // Find Results to replace by whole result
605   QList<GeomShapePtr> aShapes;
606   QList<ObjectPtr> aToReplace;
607   std::list<GeomShapePtr> aSubShapes;
608   foreach(ObjectPtr aObj, myResShapes.keys()) {
609     aShapes = myResShapes[aObj];
610     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
611     TopTools_MapOfShape aShapesMap;
612     if (aRes.get()) {
613       GeomShapePtr aSubShape = aRes->shape();
614       const TopoDS_Shape& aShape = aSubShape->impl<TopoDS_Shape>();
615       for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theShapeType);
616         anExp.More(); anExp.Next()) {
617         aShapesMap.Add(anExp.Current());
618       }
619     }
620     if (aShapes.count() == aShapesMap.Size())
621       aToReplace.append(aObj);
622   }
623   // Replace the found results
624   QList<ModuleBase_ViewerPrsPtr>::iterator aIt;
625   foreach(ObjectPtr aObj, aToReplace) {
626     for (aIt = theResults.begin(); aIt != theResults.end(); aIt++) {
627       if ((*aIt)->object() == aObj) {
628         theResults.removeAll(*aIt);
629         aIt--;
630       }
631     }
632     ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
633     theResults.append(aValue);
634   }
635 }
636
637 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
638 {
639   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
640   AttributeSelectionListPtr aSelListAttr =
641     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
642   aSelListAttr->clear();
643   if (aSelListAttr->isWholeResultAllowed())
644     replaceSubShapesByResult(myValues, selectionType(aSelListAttr->selectionType().c_str()));
645   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
646     aSelListAttr->append(aPrs->object(), aPrs->shape());
647   }
648 }
649
650 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
651 {
652   ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
653   if (aActive)
654     return aActive->storeValue();
655   updateObject(myFeature);
656   return true;
657 }
658
659 QList<ModuleBase_FilterItem*> ModuleBase_WidgetSelectionFilter::itemsList() const
660 {
661   return  myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
662 }
663
664
665 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
666 {
667   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
668   FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
669
670   // Init filters member of the parent attribute
671   AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
672   if (aAttrList->filters() != aFiltersFeature) {
673     aAttrList->setFilters(aFiltersFeature);
674   }
675
676   QList<ModuleBase_FilterItem*> aItemsList = itemsList();
677   std::list<std::string> aFilters = aFiltersFeature->filters();
678
679   std::list<std::string>::const_iterator aIt;
680   int i = 0;
681   int aNbItems = aItemsList.size();
682   ModuleBase_FilterItem* aItem = 0;
683   bool isBlocked = myFiltersCombo->blockSignals(true);
684   for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++, i++) {
685     std::string aStr = (*aIt);
686     aItem = 0;
687     if (i >= aNbItems) {
688       aItem = onAddFilter(aStr);
689       FilterPtr aFilterObj = aFactory->filter(aStr);
690       int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
691       if ((aId != -1) && !aFilterObj->isMultiple())
692         myFiltersCombo->removeItem(aId);
693       if (aItem) {
694         QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
695         foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
696           aWgt->restoreValue();
697         }
698       }
699     }
700   }
701   myFiltersCombo->setCurrentIndex(0);
702   myFiltersCombo->blockSignals(isBlocked);
703   return true;
704 }
705
706 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
707 {
708   QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
709   if (aErrorMsg.isEmpty()) {
710     if (myValues.size() == 0)
711       aErrorMsg = translate("Selection is empty");
712   }
713   return aErrorMsg;
714 }
715
716 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
717 {
718   myShowBtn->setChecked(false);
719   clearCurrentSelection(true);
720   updateNumberSelected();
721
722   QList<ModuleBase_FilterItem*> aItemsList = itemsList();
723   foreach(ModuleBase_FilterItem* aItem, aItemsList) {
724     QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
725     foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
726       if (!aWidget->feature().get())
727         aWidget->setFeature(myFeature);
728       aWidget->restoreValue();
729     }
730   }
731   updateObject(myFeature);
732
733   // Redisplay the feature on order to Customize presentations from filters with selectors
734   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
735   ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
736   Events_Loop::loop()->flush(EVENT_DISP);
737 }
738
739 void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
740 {
741   for (std::list<FilterPtr>::const_iterator anIt = theFilters.begin();
742        anIt != theFilters.end(); ++anIt) {
743     std::string aName = translate((*anIt)->name()).toStdString();
744     myFilters[aName] = *anIt;
745   }
746 }
747
748 QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
749 {
750   return ModuleBase_Tools::translate(myFeatureId, theString);
751 }