Salome HOME
Issue #3073: Show filters for Group feature in information panel.
[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     myFilters.push_back(aSession->filters()->id(*aIt));
267   }
268   myFiltersCombo->addItems(aItems);
269   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
270
271   aGroupLayout->addWidget(myFiltersCombo);
272   aMainLayout->addWidget(aFiltersGroup);
273
274   // Select Button
275   QWidget* aBtnWgt = new QWidget(this);
276   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
277   ModuleBase_Tools::adjustMargins(aBtnLayout);
278
279   aBtnLayout->addStretch(1);
280
281   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
282   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
283   aBtnLayout->addWidget(mySelectBtn);
284
285   aMainLayout->addWidget(aBtnWgt);
286
287   // Label widgets
288   QWidget* aLblWgt = new QWidget(this);
289   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
290   ModuleBase_Tools::zeroMargins(aLblLayout);
291
292   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
293
294   myNbLbl = new QLabel("0", aLblWgt);
295   aLblLayout->addWidget(myNbLbl);
296
297   // Show only button
298   myShowBtn = new QCheckBox(tr("Show only"), this);
299   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
300   aLblLayout->addWidget(myShowBtn);
301
302   aMainLayout->addWidget(aLblWgt);
303
304   aMainLayout->addStretch(1);
305
306   updateSelectBtn();
307   if (theReadOnly) {
308     myFiltersCombo->hide();
309     mySelectBtn->hide();
310     aLblWgt->hide();
311     myShowBtn->hide();
312   }
313 }
314
315 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
316 {
317   myValues.clear();
318   if (!myPreview.IsNull()) {
319     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
320     aCtx->Remove(myPreview, false);
321     myPreview.Nullify();
322     if (myListIO.Size() > 0) {
323       Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
324       AIS_ListOfInteractive::const_iterator aIt;
325       Handle(AIS_Shape) aShapeIO;
326       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
327         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
328         if (!aShapeIO.IsNull()) {
329           aCtx->Display(aShapeIO, false);
330           std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
331           anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
332           myWorkshop->applyCurrentSelectionModes(anAISObj);
333         }
334       }
335       myListIO.Clear();
336       myShowBtn->setChecked(false);
337     }
338     myWorkshop->viewer()->update();
339   }
340   SelectorFeature = FeaturePtr();
341   AttributeId = "";
342 }
343
344 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
345 {
346   if (theIndex == 0)
347     return;
348
349   std::list<std::string>::iterator aIt;
350   int i;
351   std::string aFilter;
352   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
353     if (i == (theIndex - 1)) {
354       aFilter = (*aIt);
355       break;
356     }
357   }
358   ModuleBase_FilterItem* aItem = onAddFilter(aFilter);
359   FiltersFeaturePtr aFiltersFeature =
360     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
361   aFiltersFeature->addFilter(aFilter);
362
363   myFiltersCombo->setCurrentIndex(0);
364   myFiltersCombo->removeItem(theIndex);
365   updateObject(myFeature);
366
367   if (aItem && (aItem->widgets().size() > 0))
368     aItem->widgets().first()->emitFocusInWidget();
369   else
370     emitFocusInWidget();
371 }
372
373 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
374 {
375   if (theFilter.length() == 0)
376     return 0;
377   std::list<std::string>::const_iterator aIt;
378   for (aIt = myUseFilters.cbegin(); aIt != myUseFilters.cend(); aIt++) {
379     if (theFilter == (*aIt))
380       return 0;
381   }
382   myFilters.remove(theFilter);
383   myUseFilters.push_back(theFilter);
384   ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
385   connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
386     SLOT(onDeleteItem(ModuleBase_FilterItem*)));
387   connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
388     SLOT(onReverseItem(ModuleBase_FilterItem*)));
389   myFiltersLayout->addWidget(aItem);
390
391   updateSelectBtn();
392   clearCurrentSelection(true);
393   updateNumberSelected();
394   return aItem;
395 }
396
397 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
398 {
399   std::string aFilter = theItem->filter();
400   QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
401   foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
402     aWgt->deactivate();
403   }
404   myFiltersLayout->removeWidget(theItem);
405   theItem->deleteLater();
406
407   myUseFilters.remove(aFilter);
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   if (myUseFilters.size() == 0)
445     return;
446   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
447   if (aCtx.IsNull())
448     return;
449
450   clearCurrentSelection();
451
452   BRep_Builder aBuilder;
453   TopoDS_Compound aComp;
454   aBuilder.MakeCompound(aComp);
455
456   DocumentPtr aDoc = myFeature->document();
457   int aNb = aDoc->size(ModelAPI_ResultBody::group());
458   ObjectPtr aObj;
459   ResultBodyPtr aBody;
460   GeomShapePtr aShape;
461   for (int i = 0; i < aNb; i++) {
462     aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
463     aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
464     GeomShapePtr aShape = aBody->shape();
465     std::list<GeomShapePtr> aSubShapes =
466       aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
467     TopTools_MapOfShape alreadyThere;
468     std::list<GeomShapePtr>::const_iterator aShapesIt;
469     for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
470       GeomShapePtr aShape = (*aShapesIt);
471       TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
472       if (!alreadyThere.Add(aTShape))
473         continue;
474       static SessionPtr aSession = ModelAPI_Session::get();
475       bool isValid = aSession->filters()->isValid(myFeature, aBody, aShape);
476       if (isValid) {
477         aBuilder.Add(aComp, aTShape);
478         ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
479         myValues.append(aValue);
480       }
481     }
482   }
483
484   if (myValues.size() > 0)
485     updatePreview(aComp);
486   updateNumberSelected();
487   updateObject(myFeature);
488   onShowOnly(myShowBtn->isChecked());
489 }
490
491 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
492 {
493   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
494   if (aCtx.IsNull())
495     return;
496
497   if (myPreview.IsNull()) {
498     myPreview = new AIS_Shape(theShape);
499     myPreview->SetDisplayMode(AIS_Shaded);
500     myPreview->SetColor(Quantity_NOC_BLUE1);
501     Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
502     if (aDrawer->HasOwnPointAspect()) {
503       aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
504       aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
505       aDrawer->PointAspect()->SetScale(2.);
506     }
507     else
508       aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
509     myPreview->SetTransparency();
510     aCtx->Display(myPreview, true);
511     aCtx->Deactivate(myPreview);
512   }
513   else {
514     myPreview->Set(theShape);
515     aCtx->Redisplay(myPreview, true);
516   }
517 }
518
519
520 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
521 {
522   if (myPreview.IsNull())
523     return;
524   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
525
526   if (theShow) {
527     AIS_ListOfInteractive aList;
528     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
529     aList.Remove(myPreview);
530     if (aList.Size() > 0)
531       myListIO = aList;
532   }
533   AIS_ListOfInteractive::const_iterator aIt;
534   Handle(AIS_Shape) aShapeIO;
535   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
536     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
537     if (!aShapeIO.IsNull()) {
538       if (theShow)
539         aCtx->Erase(aShapeIO, false);
540       else {
541         aCtx->Display(aShapeIO, false);
542         std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
543         anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
544         myWorkshop->applyCurrentSelectionModes(anAISObj);
545       }
546     }
547   }
548   myWorkshop->viewer()->update();
549 }
550
551 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
552 {
553   mySelectBtn->setEnabled(myUseFilters.size() > 0);
554 }
555
556 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
557 {
558   int aNb = myValues.size();
559   myNbLbl->setText(QString::number(aNb));
560   //QString aErr = () ? tr("Selection is empty") : "";
561   if (aNb == 0)
562     myFeature->setError(tr("Selection is empty").toStdString(), false, false);
563   else {
564     myFeature->setError("", false, false);
565     myFeature->data()->execState(ModelAPI_StateDone);
566   }
567 }
568
569 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
570 {
571   QList<QWidget*> aWidgets;
572   aWidgets.append(myFiltersCombo);
573   return aWidgets;
574 }
575
576 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
577 {
578   myValues.clear();
579   if (!myPreview.IsNull()) {
580     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
581     aCtx->Remove(myPreview, toUpdate);
582     myPreview.Nullify();
583   }
584 }
585
586 void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
587 {
588   QMap<ObjectPtr, QList<GeomShapePtr>> myResShapes;
589   // Sort sub-shapes by result
590   foreach (ModuleBase_ViewerPrsPtr aPrs, theResults) {
591     if (myResShapes.contains(aPrs->object()))
592       myResShapes[aPrs->object()].append(aPrs->shape());
593     else {
594       QList<GeomShapePtr> aShapes;
595       aShapes << aPrs->shape();
596       myResShapes[aPrs->object()] = aShapes;
597     }
598   }
599   // Find Results to replace by whole result
600   QList<GeomShapePtr> aShapes;
601   QList<ObjectPtr> aToReplace;
602   std::list<GeomShapePtr> aSubShapes;
603   foreach(ObjectPtr aObj, myResShapes.keys()) {
604     aShapes = myResShapes[aObj];
605     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
606     TopTools_MapOfShape aShapesMap;
607     if (aRes.get()) {
608       GeomShapePtr aSubShape = aRes->shape();
609       const TopoDS_Shape& aShape = aSubShape->impl<TopoDS_Shape>();
610       for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theShapeType);
611         anExp.More(); anExp.Next()) {
612         aShapesMap.Add(anExp.Current());
613       }
614     }
615     if (aShapes.count() == aShapesMap.Size())
616       aToReplace.append(aObj);
617   }
618   // Replace the found results
619   QList<ModuleBase_ViewerPrsPtr>::iterator aIt;
620   foreach(ObjectPtr aObj, aToReplace) {
621     for (aIt = theResults.begin(); aIt != theResults.end(); aIt++) {
622       if ((*aIt)->object() == aObj) {
623         theResults.removeAll(*aIt);
624         aIt--;
625       }
626     }
627     ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
628     theResults.append(aValue);
629   }
630 }
631
632 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
633 {
634   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
635   AttributeSelectionListPtr aSelListAttr =
636     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
637   aSelListAttr->clear();
638   if (aSelListAttr->isWholeResultAllowed())
639     replaceSubShapesByResult(myValues, selectionType(aSelListAttr->selectionType().c_str()));
640   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
641     aSelListAttr->append(aPrs->object(), aPrs->shape());
642   }
643 }
644
645 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
646 {
647   ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
648   if (aActive)
649     return aActive->storeValue();
650   updateObject(myFeature);
651   return true;
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   std::list<std::string> aFilters = aFiltersFeature->filters();
666   std::list<std::string>::const_iterator aIt;
667   for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++) {
668     std::string aStr = (*aIt);
669     ModuleBase_FilterItem* aItem = onAddFilter(aStr);
670     FilterPtr aFilterObj = aFactory->filter(aStr);
671     int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
672     if (aId != -1)
673       myFiltersCombo->removeItem(aId);
674
675     if (aItem) {
676       QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
677       foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
678         aWgt->restoreValue();
679       }
680     }
681   }
682   return true;
683 }
684
685 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
686 {
687   QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
688   if (aErrorMsg.isEmpty()) {
689     if (myValues.size() == 0)
690       aErrorMsg = tr("Selection is empty");
691   }
692   return aErrorMsg;
693 }
694
695 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
696 {
697   myShowBtn->setChecked(false);
698   clearCurrentSelection(true);
699   updateNumberSelected();
700
701   QList<ModuleBase_FilterItem*> aItemsList = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
702   foreach(ModuleBase_FilterItem* aItem, aItemsList) {
703     QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
704     foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
705       if (!aWidget->feature().get())
706         aWidget->setFeature(myFeature);
707       aWidget->restoreValue();
708     }
709   }
710   updateObject(myFeature);
711
712   // Redisplay the feature on order to Customize presentations from filters with selectors
713   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
714   ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
715   Events_Loop::loop()->flush(EVENT_DISP);
716 }