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