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