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