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