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