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