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