1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
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>
39 #include <Events_Loop.h>
40 #include <Config_ValidatorReader.h>
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>
49 #include <QPushButton>
54 #include <QToolButton>
58 FeaturePtr ModuleBase_WidgetSelectionFilter::SelectorFeature;
59 std::string ModuleBase_WidgetSelectionFilter::AttributeId;
62 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
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;
82 return GeomAPI_Shape::SHAPE;
86 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
87 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
89 myFeatureName(theFeature),
90 myWorkshop(theWorkshop)
92 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
93 ModuleBase_Tools::adjustMargins(aMainLayout);
95 aMainLayout->addStretch(1);
96 QPushButton* aLaunchBtn = new QPushButton(
97 ModuleBase_Tools::translate("FiltersSelection", "Selection by filters"), this);
98 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
99 aMainLayout->addWidget(aLaunchBtn);
102 void ModuleBase_FilterStarter::onFiltersLaunch()
104 static QString aHelpFileName = QString("FiltersPlugin") + QDir::separator() +
105 QString("FiltersPlugin.html");
107 ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
108 ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
110 // Open transaction on filters operation finish
111 aFeatureOp->openTransactionOnResume();
113 QWidget* aParent = parentWidget();
114 ModuleBase_WidgetMultiSelector* aSelector =
115 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
117 aParent = aParent->parentWidget();
118 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
122 ModuleBase_WidgetSelectionFilter::SelectorFeature = aSelector->feature();
123 ModuleBase_WidgetSelectionFilter::AttributeId = aSelector->attributeID();
125 // Launch Filters operation
126 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
127 (myWorkshop->module()->createOperation(myFeatureName));
129 AttributeSelectionListPtr aAttrList =
130 ModuleBase_WidgetSelectionFilter::SelectorFeature->selectionList(
131 ModuleBase_WidgetSelectionFilter::AttributeId);
132 FiltersFeaturePtr aFilters = aAttrList->filters();
134 aFOperation->setFeature(aFilters);
135 aFOperation->setHelpFileName(aHelpFileName);
136 myWorkshop->processLaunchOperation(aFOperation);
139 //*****************************************************************************
140 //*****************************************************************************
141 //*****************************************************************************
142 ModuleBase_FilterItem::ModuleBase_FilterItem(
143 const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
144 : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
145 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
147 FilterPtr aFilter = ModelAPI_Session::get()->filters()->filter(theFilter);
148 std::string aXmlString = aFilter->xmlRepresentation();
149 if (aXmlString.length() == 0)
152 std::string anAttrPrefix; // this must be added to the attributes names for multiple filters
153 std::string aFilterKind = ModelAPI_Session::get()->filters()->id(aFilter);
154 if (theFilter != aFilterKind) {
155 anAttrPrefix = theFilter.substr(0, theFilter.size() - aFilterKind.size());
157 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop(), anAttrPrefix);
158 Config_ValidatorReader aValidatorReader(aXmlString, true);
159 aValidatorReader.setFeatureId(mySelection->getKind());
160 aValidatorReader.readAll();
162 QVBoxLayout* aLayout = new QVBoxLayout(this);
163 ModuleBase_Tools::zeroMargins(aLayout);
165 QWidget* aItemRow = new QWidget(this);
166 addItemRow(aItemRow);
167 aLayout->addWidget(aItemRow);
169 ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
170 aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
171 aFactory.createWidget(aParamsWgt);
172 ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
173 myWidgets = aFactory.getModelWidgets();
174 foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
175 aWidget->setFeature(theParent->feature());
176 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
177 theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
178 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
179 theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
180 connect(aWidget, SIGNAL(objectUpdated()), theParent, SLOT(onObjectUpdated()));
182 aLayout->addWidget(aParamsWgt);
186 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
188 std::string aContext = mySelection->getKind();
189 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
190 ModuleBase_Tools::zeroMargins(aLayout);
192 // Reverse filter button
193 myRevBtn = new QToolButton(theParent);
194 myRevBtn->setCheckable(true);
195 bool isReversed = mySelection->isReversed(myFilterID);
196 myRevBtn->setChecked(isReversed);
197 myRevBtn->setAutoRaise(true);
199 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
201 myRevBtn->setIcon(QIcon(":pictures/add.png"));
202 myRevBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Reverse the filter"));
203 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
204 aLayout->addWidget(myRevBtn);
206 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
207 aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
209 QToolButton* aDelBtn = new QToolButton(theParent);
210 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
211 aDelBtn->setAutoRaise(true);
212 aDelBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Delete the filter"));
213 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
214 aLayout->addWidget(aDelBtn);
217 void ModuleBase_FilterItem::onReverse(bool theCheck)
219 mySelection->setReversed(myFilterID, theCheck);
221 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
223 myRevBtn->setIcon(QIcon(":pictures/add.png"));
224 emit reversedItem(this);
227 void ModuleBase_FilterItem::onDelete()
229 emit deleteItem(this);
233 //*****************************************************************************
234 //*****************************************************************************
235 //*****************************************************************************
236 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
237 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, bool theReadOnly)
238 : ModuleBase_ModelWidget(theParent, theData),
239 myWorkshop(theWorkshop),
240 mySelectorFeature(ModuleBase_WidgetSelectionFilter::SelectorFeature),
241 mySelectorAttribute(ModuleBase_WidgetSelectionFilter::AttributeId)
243 // Clear Old selection
244 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
245 mySelectionType = selectionType(aAttrList->selectionType().c_str());
251 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
252 ModuleBase_Tools::adjustMargins(aMainLayout);
254 QGroupBox* aFiltersGroup = new QGroupBox(translate("Filters"), this);
255 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
256 aGroupLayout->setContentsMargins(0, 0, 0, 0);
257 aGroupLayout->setSpacing(0);
259 myFiltersWgt = new QWidget();
260 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
261 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
262 aGroupLayout->addWidget(myFiltersWgt);
264 myFiltersCombo = new QComboBox(aFiltersGroup);
265 myFiltersCombo->addItem(translate("Add new filter..."));
266 SessionPtr aSession = ModelAPI_Session::get();
267 std::list<FilterPtr> allFilters =
268 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
269 storeFilters(allFilters);
271 std::list<FilterPtr>::const_iterator aIt;
272 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
273 aItems.push_back(translate((*aIt)->name().c_str()));
275 myFiltersCombo->addItems(aItems);
276 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
278 aGroupLayout->addWidget(myFiltersCombo);
279 aMainLayout->addWidget(aFiltersGroup);
282 QWidget* aBtnWgt = new QWidget(this);
283 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
284 ModuleBase_Tools::adjustMargins(aBtnLayout);
286 aBtnLayout->addStretch(1);
288 mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
289 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
290 aBtnLayout->addWidget(mySelectBtn);
292 aMainLayout->addWidget(aBtnWgt);
295 QWidget* aLblWgt = new QWidget(this);
296 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
297 ModuleBase_Tools::zeroMargins(aLblLayout);
299 aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
301 myNbLbl = new QLabel("0", aLblWgt);
302 aLblLayout->addWidget(myNbLbl);
305 myShowBtn = new QCheckBox(translate("Show only"), this);
306 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
307 aLblLayout->addWidget(myShowBtn);
309 aMainLayout->addWidget(aLblWgt);
311 aMainLayout->addStretch(1);
315 myFiltersCombo->hide();
322 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
325 if (!myPreview.IsNull()) {
326 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
327 aCtx->Remove(myPreview, false);
329 if (myListIO.Size() > 0) {
330 aCtx = myWorkshop->viewer()->AISContext();
331 AIS_ListOfInteractive::const_iterator aIt;
332 Handle(AIS_Shape) aShapeIO;
333 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
334 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
335 if (!aShapeIO.IsNull()) {
336 aCtx->Display(aShapeIO, false);
337 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
338 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
339 myWorkshop->applyCurrentSelectionModes(anAISObj);
343 myShowBtn->setChecked(false);
345 myWorkshop->viewer()->update();
347 SelectorFeature = FeaturePtr();
351 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
356 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
357 FiltersFeaturePtr aFiltersFeature =
358 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
360 std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
362 std::list<FilterPtr>::iterator aIt;
364 std::map<std::string, FilterPtr>::const_iterator aFound = myFilters.find(aText);
365 if (aFound == myFilters.end()) {
366 std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
367 storeFilters(aFilters);
368 aFound = myFilters.find(aText);
370 if (aFound != myFilters.end())
371 aFilter = aFactory->id(aFound->second);
373 aFiltersFeature->addFilter(aFilter);
374 updateObject(myFeature);
376 QList<ModuleBase_FilterItem*> aList = itemsList();
377 if (!aList.isEmpty() && (aList.last()->widgets().size() > 0))
378 aList.last()->widgets().first()->emitFocusInWidget();
383 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
385 if (theFilter.length() == 0)
387 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
388 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
389 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
390 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
391 SLOT(onReverseItem(ModuleBase_FilterItem*)));
392 myFiltersLayout->addWidget(aItem);
395 clearCurrentSelection(true);
396 updateNumberSelected();
400 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
402 std::string aFilter = theItem->filter();
403 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
404 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
407 myFiltersLayout->removeWidget(theItem);
408 theItem->deleteLater();
410 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
411 if (!aFactory->filter(aFilter)->isMultiple()) {
412 //myFilters.push_back(aFilter);
413 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
415 FiltersFeaturePtr aFiltersFeature =
416 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
417 aFiltersFeature->removeFilter(aFilter);
420 clearCurrentSelection(true);
421 updateNumberSelected();
423 myWorkshop->deactivateCurrentSelector();
424 myWorkshop->selectionActivate()->updateSelectionModes();
425 myWorkshop->selectionActivate()->updateSelectionFilters();
428 updateObject(myFeature);
432 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
434 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
435 ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
436 Events_Loop::loop()->flush(aDispEvent);
439 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
442 clearCurrentSelection(true);
443 updateNumberSelected();
446 void ModuleBase_WidgetSelectionFilter::onSelect()
448 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
452 clearCurrentSelection();
454 BRep_Builder aBuilder;
455 TopoDS_Compound aComp;
456 aBuilder.MakeCompound(aComp);
458 DocumentPtr aDoc = myFeature->document();
459 int aNb = aDoc->size(ModelAPI_ResultBody::group());
462 for (int i = 0; i < aNb; i++) {
463 aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
464 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
465 GeomShapePtr aShape = aBody->shape();
466 std::list<GeomShapePtr> aSubShapes =
467 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
468 TopTools_MapOfShape alreadyThere;
469 std::list<GeomShapePtr>::const_iterator aShapesIt;
470 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
471 GeomShapePtr aSubShape = (*aShapesIt);
472 TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
473 if (!alreadyThere.Add(aTShape))
475 static SessionPtr aSession = ModelAPI_Session::get();
476 bool isValid = aSession->filters()->isValid(myFeature, aBody, aSubShape);
478 aBuilder.Add(aComp, aTShape);
479 // bos #24043: Naming on a compsolid works wrong.
480 // Find a simple sub-result for the ViewerPrs context:
481 ObjectPtr aSubResultObj = aObj;
482 for (int aSubIndex = 0; aSubIndex < aBody->numberOfSubs(); aSubIndex++) {
483 ResultBodyPtr aSubResult = aBody->subResult(aSubIndex);
484 GeomShapePtr aSubResultShape = aSubResult->shape();
485 if (aSubResultShape->isSubShape(aSubShape)) {
486 aSubResultObj = aSubResult;
490 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aSubResultObj, aSubShape));
491 //ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aSubShape));
492 myValues.append(aValue);
497 if (myValues.size() > 0)
498 updatePreview(aComp);
499 updateNumberSelected();
500 updateObject(myFeature);
501 onShowOnly(myShowBtn->isChecked());
504 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
506 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
510 if (myPreview.IsNull()) {
511 myPreview = new AIS_Shape(theShape);
512 myPreview->SetDisplayMode(AIS_Shaded);
513 myPreview->SetColor(Quantity_NOC_BLUE1);
514 Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
515 if (aDrawer->HasOwnPointAspect()) {
516 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
517 aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
518 aDrawer->PointAspect()->SetScale(2.);
521 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
522 myPreview->SetTransparency();
523 aCtx->Display(myPreview, true);
524 aCtx->Deactivate(myPreview);
527 myPreview->Set(theShape);
528 aCtx->Redisplay(myPreview, true);
533 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
535 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
537 AIS_ListOfInteractive aList;
538 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
539 if (!myPreview.IsNull())
540 aList.Remove(myPreview);
541 if (aList.Size() > 0)
544 AIS_ListOfInteractive::const_iterator aIt;
545 Handle(AIS_Shape) aShapeIO;
546 bool isModified = false;
547 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
548 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
549 if (!aShapeIO.IsNull()) {
551 if (aCtx->IsDisplayed(aShapeIO)) {
552 aCtx->Erase(aShapeIO, false);
557 if (!aCtx->IsDisplayed(aShapeIO)) {
558 aCtx->Display(aShapeIO, false);
559 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
560 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
561 myWorkshop->applyCurrentSelectionModes(anAISObj);
568 myWorkshop->viewer()->update();
571 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
573 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
574 mySelectBtn->setEnabled(aFiltersFeature.get() && (aFiltersFeature->filters().size() > 0));
577 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
579 int aNb = myValues.size();
580 myNbLbl->setText(QString::number(aNb));
582 myFeature->setError(translate("Selection is empty").toStdString(), false, false);
583 myShowBtn->setChecked(false);
585 myShowBtn->setEnabled(false);
588 myFeature->setError("", false, false);
589 myFeature->data()->execState(ModelAPI_StateDone);
590 myShowBtn->setEnabled(true);
594 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
596 QList<QWidget*> aWidgets;
597 aWidgets.append(myFiltersCombo);
601 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
604 if (!myPreview.IsNull()) {
605 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
606 aCtx->Remove(myPreview, toUpdate);
611 void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
613 QMap<ObjectPtr, QList<GeomShapePtr>> myResShapes;
614 // Sort sub-shapes by result
615 foreach (ModuleBase_ViewerPrsPtr aPrs, theResults) {
616 if (myResShapes.contains(aPrs->object()))
617 myResShapes[aPrs->object()].append(aPrs->shape());
619 QList<GeomShapePtr> aShapes;
620 aShapes << aPrs->shape();
621 myResShapes[aPrs->object()] = aShapes;
624 // Find Results to replace by whole result
625 QList<GeomShapePtr> aShapes;
626 QList<ObjectPtr> aToReplace;
627 std::list<GeomShapePtr> aSubShapes;
628 foreach(ObjectPtr aObj, myResShapes.keys()) {
629 aShapes = myResShapes[aObj];
630 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
631 TopTools_MapOfShape aShapesMap;
633 GeomShapePtr aSubShape = aRes->shape();
634 const TopoDS_Shape& aShape = aSubShape->impl<TopoDS_Shape>();
635 for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theShapeType);
636 anExp.More(); anExp.Next()) {
637 aShapesMap.Add(anExp.Current());
640 if (aShapes.count() == aShapesMap.Size())
641 aToReplace.append(aObj);
643 // Replace the found results
644 QList<ModuleBase_ViewerPrsPtr>::iterator aIt;
645 foreach(ObjectPtr aObj, aToReplace) {
646 for (aIt = theResults.begin(); aIt != theResults.end(); aIt++) {
647 if ((*aIt)->object() == aObj) {
648 theResults.removeAll(*aIt);
652 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
653 theResults.append(aValue);
657 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
659 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
660 AttributeSelectionListPtr aSelListAttr =
661 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
662 aSelListAttr->clear();
663 if (aSelListAttr->isWholeResultAllowed())
664 replaceSubShapesByResult(myValues, selectionType(aSelListAttr->selectionType().c_str()));
665 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
666 aSelListAttr->append(aPrs->object(), aPrs->shape());
670 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
672 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
674 return aActive->storeValue();
675 updateObject(myFeature);
679 QList<ModuleBase_FilterItem*> ModuleBase_WidgetSelectionFilter::itemsList() const
681 return myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
685 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
687 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
688 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
690 // Init filters member of the parent attribute
691 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
692 if (aAttrList->filters() != aFiltersFeature) {
693 aAttrList->setFilters(aFiltersFeature);
696 QList<ModuleBase_FilterItem*> aItemsList = itemsList();
697 std::list<std::string> aFilters = aFiltersFeature->filters();
699 std::list<std::string>::const_iterator aIt;
701 int aNbItems = aItemsList.size();
702 ModuleBase_FilterItem* aItem = 0;
703 bool isBlocked = myFiltersCombo->blockSignals(true);
704 for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++, i++) {
705 std::string aStr = (*aIt);
708 aItem = onAddFilter(aStr);
709 FilterPtr aFilterObj = aFactory->filter(aStr);
710 int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
711 if ((aId != -1) && !aFilterObj->isMultiple())
712 myFiltersCombo->removeItem(aId);
714 QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
715 foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
716 aWgt->restoreValue();
721 myFiltersCombo->setCurrentIndex(0);
722 myFiltersCombo->blockSignals(isBlocked);
726 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
728 QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
729 if (aErrorMsg.isEmpty()) {
730 if (myValues.size() == 0)
731 aErrorMsg = translate("Selection is empty");
736 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
738 myShowBtn->setChecked(false);
739 clearCurrentSelection(true);
740 updateNumberSelected();
742 QList<ModuleBase_FilterItem*> aItemsList = itemsList();
743 foreach(ModuleBase_FilterItem* aItem, aItemsList) {
744 QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
745 foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
746 if (!aWidget->feature().get())
747 aWidget->setFeature(myFeature);
748 aWidget->restoreValue();
751 updateObject(myFeature);
753 // Redisplay the feature on order to Customize presentations from filters with selectors
754 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
755 ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
756 Events_Loop::loop()->flush(EVENT_DISP);
759 void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
761 for (std::list<FilterPtr>::const_iterator anIt = theFilters.begin();
762 anIt != theFilters.end(); ++anIt) {
763 std::string aName = translate((*anIt)->name()).toStdString();
764 myFilters[aName] = *anIt;
768 QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
770 return ModuleBase_Tools::translate(myFeatureId, theString);