Salome HOME
Issue #2957: Set name of help page
[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
47 #include <QLayout>
48 #include <QPushButton>
49 #include <QLabel>
50 #include <QComboBox>
51 #include <QGroupBox>
52 #include <QDialog>
53 #include <QToolButton>
54 #include <QCheckBox>
55
56 static FeaturePtr SelectorFeature;
57 static std::string AttributeId;
58
59
60 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
61 {
62   QString aType = theType.toUpper();
63   if ((aType == "VERTEX") || (aType == "VERTICES"))
64     return GeomAPI_Shape::VERTEX;
65   else if ((aType == "EDGE") || (aType == "EDGES"))
66     return GeomAPI_Shape::EDGE;
67   else if ((aType == "WIRE") || (aType == "WIRES"))
68     return GeomAPI_Shape::WIRE;
69   else if ((aType == "FACE") || (aType == "FACES"))
70     return GeomAPI_Shape::FACE;
71   else if ((aType == "SHELL") || (aType == "SHELLS"))
72     return GeomAPI_Shape::SHELL;
73   else if ((aType == "SOLID") || (aType == "SOLIDS"))
74     return GeomAPI_Shape::SOLID;
75   else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
76     return GeomAPI_Shape::COMPSOLID;
77   else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
78     return GeomAPI_Shape::COMPOUND;
79   else
80     return GeomAPI_Shape::SHAPE;
81 }
82
83
84 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
85   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
86   : QWidget(theParent),
87   myFeatureName(theFeature),
88   myWorkshop(theWorkshop)
89 {
90   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
91   ModuleBase_Tools::adjustMargins(aMainLayout);
92
93   aMainLayout->addStretch(1);
94   QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
95   connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
96   aMainLayout->addWidget(aLaunchBtn);
97 }
98
99 void ModuleBase_FilterStarter::onFiltersLaunch()
100 {
101   ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
102   ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
103   if (aFeatureOp)
104     // Open transaction on filters operation finish
105     aFeatureOp->openTransactionOnResume();
106
107   QWidget* aParent = parentWidget();
108   ModuleBase_WidgetMultiSelector* aSelector =
109     dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
110   while (!aSelector) {
111     aParent = aParent->parentWidget();
112     aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
113   }
114   if (!aSelector)
115     return;
116   SelectorFeature = aSelector->feature();
117   AttributeId = aSelector->attributeID();
118
119   // Launch Filters operation
120   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
121     (myWorkshop->module()->createOperation(myFeatureName));
122
123   AttributeSelectionListPtr aAttrList = SelectorFeature->selectionList(AttributeId);
124   FiltersFeaturePtr aFilters = aAttrList->filters();
125   if (aFilters.get())
126     aFOperation->setFeature(aFilters);
127   aFOperation->setHelpFileName("FiltersPlugin.html");
128   myWorkshop->processLaunchOperation(aFOperation);
129 }
130
131 //*****************************************************************************
132 //*****************************************************************************
133 //*****************************************************************************
134 ModuleBase_FilterItem::ModuleBase_FilterItem(
135   const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
136   : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
137     mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
138 {
139   std::string aXmlString =
140       ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
141   if (aXmlString.length() == 0)
142     addItemRow(this);
143   else {
144     ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
145     Config_ValidatorReader aValidatorReader(aXmlString, true);
146     aValidatorReader.setFeatureId(mySelection->getKind());
147     aValidatorReader.readAll();
148
149     QVBoxLayout* aLayout = new QVBoxLayout(this);
150     ModuleBase_Tools::zeroMargins(aLayout);
151
152     QWidget* aItemRow = new QWidget(this);
153     addItemRow(aItemRow);
154     aLayout->addWidget(aItemRow);
155
156     ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
157     aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
158     aFactory.createWidget(aParamsWgt);
159     ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
160     myWidgets = aFactory.getModelWidgets();
161     foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
162       aWidget->setFeature(theParent->feature());
163       connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
164         theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
165       connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
166         theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
167       connect(aWidget, SIGNAL(objectUpdated()), theParent, SLOT(onObjectUpdated()));
168     }
169     aLayout->addWidget(aParamsWgt);
170   }
171 }
172
173 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
174 {
175   QHBoxLayout* aLayout = new QHBoxLayout(theParent);
176   ModuleBase_Tools::zeroMargins(aLayout);
177
178   // Reverse filter button
179   myRevBtn = new QToolButton(theParent);
180   myRevBtn->setCheckable(true);
181   bool isReversed = mySelection->isReversed(myFilterID);
182   myRevBtn->setChecked(isReversed);
183   myRevBtn->setAutoRaise(true);
184   if (isReversed)
185     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
186   else
187     myRevBtn->setIcon(QIcon(":pictures/add.png"));
188   myRevBtn->setToolTip(tr("Reverse the filter"));
189   connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
190   aLayout->addWidget(myRevBtn);
191
192   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
193   aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
194
195   QToolButton* aDelBtn = new QToolButton(theParent);
196   aDelBtn->setIcon(QIcon(":pictures/delete.png"));
197   aDelBtn->setAutoRaise(true);
198   aDelBtn->setToolTip(tr("Delete the filter"));
199   connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
200   aLayout->addWidget(aDelBtn);
201 }
202
203 void ModuleBase_FilterItem::onReverse(bool theCheck)
204 {
205   mySelection->setReversed(myFilterID, theCheck);
206   if (theCheck)
207     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
208   else
209     myRevBtn->setIcon(QIcon(":pictures/add.png"));
210   emit reversedItem(this);
211 }
212
213 void ModuleBase_FilterItem::onDelete()
214 {
215   emit deleteItem(this);
216 }
217
218
219 //*****************************************************************************
220 //*****************************************************************************
221 //*****************************************************************************
222 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
223   ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
224   : ModuleBase_ModelWidget(theParent, theData),
225   myWorkshop(theWorkshop),
226   mySelectorFeature(SelectorFeature),
227   mySelectorAttribute(AttributeId)
228 {
229   // Clear Old selection
230   AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
231   mySelectionType = selectionType(aAttrList->selectionType().c_str());
232   aAttrList->clear();
233
234   // Define widgets
235   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
236   ModuleBase_Tools::adjustMargins(aMainLayout);
237
238   QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
239   QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
240   aGroupLayout->setContentsMargins(0, 0, 0, 0);
241   aGroupLayout->setSpacing(0);
242
243   myFiltersWgt = new QWidget();
244   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
245   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
246   aGroupLayout->addWidget(myFiltersWgt);
247
248   myFiltersCombo = new QComboBox(aFiltersGroup);
249   myFiltersCombo->addItem(tr("Add new filter..."));
250   SessionPtr aSession = ModelAPI_Session::get();
251   std::list<FilterPtr> allFilters =
252     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
253   QStringList aItems;
254   std::list<FilterPtr>::const_iterator aIt;
255   for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
256     aItems.push_back((*aIt)->name().c_str());
257     myFilters.push_back(aSession->filters()->id(*aIt));
258   }
259   myFiltersCombo->addItems(aItems);
260   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
261
262   aGroupLayout->addWidget(myFiltersCombo);
263   aMainLayout->addWidget(aFiltersGroup);
264
265   // Select Button
266   QWidget* aBtnWgt = new QWidget(this);
267   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
268   ModuleBase_Tools::adjustMargins(aBtnLayout);
269
270   aBtnLayout->addStretch(1);
271
272   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
273   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
274   aBtnLayout->addWidget(mySelectBtn);
275
276   aMainLayout->addWidget(aBtnWgt);
277
278   // Label widgets
279   QWidget* aLblWgt = new QWidget(this);
280   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
281   ModuleBase_Tools::zeroMargins(aLblLayout);
282
283   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
284
285   myNbLbl = new QLabel("0", aLblWgt);
286   aLblLayout->addWidget(myNbLbl);
287
288   // Show only button
289   myShowBtn = new QCheckBox(tr("Show only"), this);
290   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
291   aLblLayout->addWidget(myShowBtn);
292
293   aMainLayout->addWidget(aLblWgt);
294
295   aMainLayout->addStretch(1);
296
297   updateSelectBtn();
298 }
299
300 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
301 {
302   myValues.clear();
303   if (!myPreview.IsNull()) {
304     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
305     aCtx->Remove(myPreview, false);
306     myPreview.Nullify();
307     if (myListIO.Size() > 0) {
308       Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
309       AIS_ListOfInteractive::const_iterator aIt;
310       Handle(AIS_Shape) aShapeIO;
311       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
312         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
313         if (!aShapeIO.IsNull()) {
314           aCtx->Display(aShapeIO, false);
315           std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
316           anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
317           myWorkshop->applyCurrentSelectionModes(anAISObj);
318         }
319       }
320       myListIO.Clear();
321       myShowBtn->setChecked(false);
322     }
323     aCtx->UpdateCurrentViewer();
324   }
325   SelectorFeature = FeaturePtr();
326   AttributeId = "";
327 }
328
329 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
330 {
331   if (theIndex == 0)
332     return;
333
334   std::list<std::string>::iterator aIt;
335   int i;
336   std::string aFilter;
337   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
338     if (i == (theIndex - 1)) {
339       aFilter = (*aIt);
340       break;
341     }
342   }
343   ModuleBase_FilterItem* aItem = onAddFilter(aFilter);
344   FiltersFeaturePtr aFiltersFeature =
345     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
346   aFiltersFeature->addFilter(aFilter);
347
348   myFiltersCombo->setCurrentIndex(0);
349   myFiltersCombo->removeItem(theIndex);
350   updateObject(myFeature);
351
352   if (aItem && (aItem->widgets().size() > 0))
353     aItem->widgets().first()->emitFocusInWidget();
354   else
355     emitFocusInWidget();
356 }
357
358 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
359 {
360   if (theFilter.length() == 0)
361     return 0;
362   std::list<std::string>::const_iterator aIt;
363   for (aIt = myUseFilters.cbegin(); aIt != myUseFilters.cend(); aIt++) {
364     if (theFilter == (*aIt))
365       return 0;
366   }
367   myFilters.remove(theFilter);
368   myUseFilters.push_back(theFilter);
369   ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
370   connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
371     SLOT(onDeleteItem(ModuleBase_FilterItem*)));
372   connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
373     SLOT(onReverseItem(ModuleBase_FilterItem*)));
374   myFiltersLayout->addWidget(aItem);
375
376   updateSelectBtn();
377   clearCurrentSelection(true);
378   updateNumberSelected();
379   return aItem;
380 }
381
382 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
383 {
384   std::string aFilter = theItem->filter();
385   QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
386   foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
387     aWgt->deactivate();
388   }
389   myFiltersLayout->removeWidget(theItem);
390   theItem->deleteLater();
391
392   myUseFilters.remove(aFilter);
393   myFilters.push_back(aFilter);
394   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
395
396   FiltersFeaturePtr aFiltersFeature =
397     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
398   aFiltersFeature->removeFilter(aFilter);
399
400   updateSelectBtn();
401   clearCurrentSelection(true);
402   updateNumberSelected();
403
404   myWorkshop->deactivateCurrentSelector();
405   myWorkshop->selectionActivate()->updateSelectionModes();
406   myWorkshop->selectionActivate()->updateSelectionFilters();
407   redisplayFeature();
408   emitFocusInWidget();
409   updateObject(myFeature);
410 }
411
412
413 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
414 {
415   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
416   ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
417   Events_Loop::loop()->flush(aDispEvent);
418 }
419
420 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
421 {
422   updateSelectBtn();
423   clearCurrentSelection(true);
424   updateNumberSelected();
425 }
426
427 void ModuleBase_WidgetSelectionFilter::onSelect()
428 {
429   if (myUseFilters.size() == 0)
430     return;
431   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
432   if (aCtx.IsNull())
433     return;
434
435   clearCurrentSelection();
436
437   BRep_Builder aBuilder;
438   TopoDS_Compound aComp;
439   aBuilder.MakeCompound(aComp);
440
441   DocumentPtr aDoc = myFeature->document();
442   int aNb = aDoc->size(ModelAPI_ResultBody::group());
443   ObjectPtr aObj;
444   ResultBodyPtr aBody;
445   GeomShapePtr aShape;
446   for (int i = 0; i < aNb; i++) {
447     aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
448     aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
449     GeomShapePtr aShape = aBody->shape();
450     std::list<GeomShapePtr> aSubShapes =
451       aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
452     TopTools_MapOfShape alreadyThere;
453     std::list<GeomShapePtr>::const_iterator aShapesIt;
454     for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
455       GeomShapePtr aShape = (*aShapesIt);
456       TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
457       if (!alreadyThere.Add(aTShape))
458         continue;
459       static SessionPtr aSession = ModelAPI_Session::get();
460       bool isValid = aSession->filters()->isValid(myFeature, aBody, aShape);
461       if (isValid) {
462         aBuilder.Add(aComp, aTShape);
463         ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
464         myValues.append(aValue);
465       }
466     }
467   }
468
469   if (myValues.size() > 0)
470     updatePreview(aComp);
471   updateNumberSelected();
472   updateObject(myFeature);
473   onShowOnly(myShowBtn->isChecked());
474 }
475
476 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
477 {
478   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
479   if (aCtx.IsNull())
480     return;
481
482   if (myPreview.IsNull()) {
483     myPreview = new AIS_Shape(theShape);
484     myPreview->SetDisplayMode(AIS_Shaded);
485     myPreview->SetColor(Quantity_NOC_BLUE1);
486     Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
487     if (aDrawer->HasOwnPointAspect()) {
488       aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
489       aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
490       aDrawer->PointAspect()->SetScale(2.);
491     }
492     else
493       aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
494     myPreview->SetTransparency();
495     aCtx->Display(myPreview, true);
496     aCtx->Deactivate(myPreview);
497   }
498   else {
499     myPreview->Set(theShape);
500     aCtx->Redisplay(myPreview, true);
501   }
502 }
503
504
505 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
506 {
507   if (myPreview.IsNull())
508     return;
509   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
510
511   if (theShow) {
512     AIS_ListOfInteractive aList;
513     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
514     aList.Remove(myPreview);
515     if (aList.Size() > 0)
516       myListIO = aList;
517   }
518   AIS_ListOfInteractive::const_iterator aIt;
519   Handle(AIS_Shape) aShapeIO;
520   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
521     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
522     if (!aShapeIO.IsNull()) {
523       if (theShow)
524         aCtx->Erase(aShapeIO, false);
525       else {
526         aCtx->Display(aShapeIO, false);
527         std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
528         anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
529         myWorkshop->applyCurrentSelectionModes(anAISObj);
530       }
531     }
532   }
533   aCtx->UpdateCurrentViewer();
534 }
535
536 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
537 {
538   mySelectBtn->setEnabled(myUseFilters.size() > 0);
539 }
540
541 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
542 {
543   int aNb = myValues.size();
544   myNbLbl->setText(QString::number(aNb));
545   //QString aErr = () ? tr("Selection is empty") : "";
546   if (aNb == 0)
547     myFeature->setError(tr("Selection is empty").toStdString(), false, false);
548   else {
549     myFeature->setError("", false, false);
550     myFeature->data()->execState(ModelAPI_StateDone);
551   }
552 }
553
554 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
555 {
556   QList<QWidget*> aWidgets;
557   aWidgets.append(myFiltersCombo);
558   return aWidgets;
559 }
560
561 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
562 {
563   myValues.clear();
564   if (!myPreview.IsNull()) {
565     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
566     aCtx->Remove(myPreview, toUpdate);
567     myPreview.Nullify();
568   }
569 }
570
571 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
572 {
573   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
574   AttributeSelectionListPtr aSelListAttr =
575     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
576   aSelListAttr->clear();
577   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
578     aSelListAttr->append(aPrs->object(), aPrs->shape());
579   }
580 }
581
582 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
583 {
584   ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
585   if (aActive)
586     return aActive->storeValue();
587   updateObject(myFeature);
588   return true;
589 }
590
591 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
592 {
593   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
594   FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
595   std::list<std::string> aFilters = aFiltersFeature->filters();
596   std::list<std::string>::const_iterator aIt;
597   for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++) {
598     std::string aStr = (*aIt);
599     onAddFilter(aStr);
600     FilterPtr aFilterObj = aFactory->filter(aStr);
601     int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
602     if (aId != -1)
603       myFiltersCombo->removeItem(aId);
604   }
605   // Init filters member of the parent attribute
606   AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
607   if (aAttrList->filters() != aFiltersFeature) {
608     aAttrList->setFilters(aFiltersFeature);
609   }
610
611   QList<QWidget*> aWidgets;
612   QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
613   foreach(ModuleBase_FilterItem* aItem, aItems) {
614     QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
615     foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
616       aWgt->restoreValue();
617     }
618   }
619   return true;
620 }
621
622 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
623 {
624   QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
625   if (aErrorMsg.isEmpty()) {
626     if (myValues.size() == 0)
627       aErrorMsg = tr("Selection is empty");
628   }
629   return aErrorMsg;
630 }
631
632 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
633 {
634   myShowBtn->setChecked(false);
635   clearCurrentSelection(true);
636   updateNumberSelected();
637
638   QList<ModuleBase_FilterItem*> aItemsList = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
639   foreach(ModuleBase_FilterItem* aItem, aItemsList) {
640     QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
641     foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
642       if (!aWidget->feature().get())
643         aWidget->setFeature(myFeature);
644       aWidget->restoreValue();
645     }
646   }
647   updateObject(myFeature);
648
649   // Redisplay the feature on order to Customize presentations from filters with selectors
650   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
651   ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
652   Events_Loop::loop()->flush(EVENT_DISP);
653 }