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