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>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_Edge.h>
41 #include <Events_Loop.h>
42 #include <Config_ValidatorReader.h>
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>
51 #include <QPushButton>
56 #include <QToolButton>
60 FeaturePtr ModuleBase_WidgetSelectionFilter::SelectorFeature;
61 std::string ModuleBase_WidgetSelectionFilter::AttributeId;
64 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
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;
84 return GeomAPI_Shape::SHAPE;
88 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
89 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
91 myFeatureName(theFeature),
92 myWorkshop(theWorkshop)
94 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
95 ModuleBase_Tools::adjustMargins(aMainLayout);
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);
104 void ModuleBase_FilterStarter::onFiltersLaunch()
106 static QString aHelpFileName = QString("FiltersPlugin") + QDir::separator() +
107 QString("FiltersPlugin.html");
109 ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
110 ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
112 // Open transaction on filters operation finish
113 aFeatureOp->openTransactionOnResume();
115 QWidget* aParent = parentWidget();
116 ModuleBase_WidgetMultiSelector* aSelector =
117 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
119 aParent = aParent->parentWidget();
120 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
124 ModuleBase_WidgetSelectionFilter::SelectorFeature = aSelector->feature();
125 ModuleBase_WidgetSelectionFilter::AttributeId = aSelector->attributeID();
127 // Launch Filters operation
128 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
129 (myWorkshop->module()->createOperation(myFeatureName));
131 AttributeSelectionListPtr aAttrList =
132 ModuleBase_WidgetSelectionFilter::SelectorFeature->selectionList(
133 ModuleBase_WidgetSelectionFilter::AttributeId);
134 FiltersFeaturePtr aFilters = aAttrList->filters();
136 aFOperation->setFeature(aFilters);
137 aFOperation->setHelpFileName(aHelpFileName);
138 myWorkshop->processLaunchOperation(aFOperation);
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()))
149 FilterPtr aFilter = ModelAPI_Session::get()->filters()->filter(theFilter);
150 std::string aXmlString = aFilter->xmlRepresentation();
151 if (aXmlString.length() == 0)
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());
159 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop(), anAttrPrefix);
160 Config_ValidatorReader aValidatorReader(aXmlString, true);
161 aValidatorReader.setFeatureId(mySelection->getKind());
162 aValidatorReader.readAll();
164 QVBoxLayout* aLayout = new QVBoxLayout(this);
165 ModuleBase_Tools::zeroMargins(aLayout);
167 QWidget* aItemRow = new QWidget(this);
168 addItemRow(aItemRow);
169 aLayout->addWidget(aItemRow);
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();
185 aLayout->addWidget(aParamsWgt);
189 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
191 std::string aContext = mySelection->getKind();
192 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
193 ModuleBase_Tools::zeroMargins(aLayout);
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);
202 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
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);
209 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
210 aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
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);
220 void ModuleBase_FilterItem::onReverse(bool theCheck)
222 mySelection->setReversed(myFilterID, theCheck);
224 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
226 myRevBtn->setIcon(QIcon(":pictures/add.png"));
227 emit reversedItem(this);
230 void ModuleBase_FilterItem::onDelete()
232 emit deleteItem(this);
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)
246 // Clear Old selection
247 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
248 mySelectionType = selectionType(aAttrList->selectionType().c_str());
254 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
255 ModuleBase_Tools::adjustMargins(aMainLayout);
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);
262 myFiltersWgt = new QWidget();
263 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
264 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
265 aGroupLayout->addWidget(myFiltersWgt);
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);
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()));
278 myFiltersCombo->addItems(aItems);
279 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
281 aGroupLayout->addWidget(myFiltersCombo);
282 aMainLayout->addWidget(aFiltersGroup);
285 QWidget* aBtnWgt = new QWidget(this);
286 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
287 ModuleBase_Tools::adjustMargins(aBtnLayout);
289 aBtnLayout->addStretch(1);
291 mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
292 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
293 aBtnLayout->addWidget(mySelectBtn);
295 aMainLayout->addWidget(aBtnWgt);
298 QWidget* aLblWgt = new QWidget(this);
299 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
300 ModuleBase_Tools::zeroMargins(aLblLayout);
302 aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
304 myNbLbl = new QLabel("0", aLblWgt);
305 aLblLayout->addWidget(myNbLbl);
308 myShowBtn = new QCheckBox(translate("Show only"), this);
309 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
310 aLblLayout->addWidget(myShowBtn);
312 aMainLayout->addWidget(aLblWgt);
314 aMainLayout->addStretch(1);
318 myFiltersCombo->hide();
325 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
328 if (!myPreview.IsNull()) {
329 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
330 aCtx->Remove(myPreview, false);
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);
346 myShowBtn->setChecked(false);
348 myWorkshop->viewer()->update();
350 SelectorFeature = FeaturePtr();
354 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
359 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
360 FiltersFeaturePtr aFiltersFeature =
361 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
363 std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
365 std::list<FilterPtr>::iterator aIt;
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);
373 if (aFound != myFilters.end())
374 aFilter = aFactory->id(aFound->second);
376 aFiltersFeature->addFilter(aFilter);
377 updateObject(myFeature);
379 QList<ModuleBase_FilterItem*> aList = itemsList();
380 if (!aList.isEmpty() && (aList.last()->widgets().size() > 0))
381 aList.last()->widgets().first()->emitFocusInWidget();
386 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
388 if (theFilter.length() == 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);
398 clearCurrentSelection(true);
399 updateNumberSelected();
403 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
405 std::string aFilter = theItem->filter();
406 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
407 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
410 myFiltersLayout->removeWidget(theItem);
411 theItem->deleteLater();
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());
418 FiltersFeaturePtr aFiltersFeature =
419 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
420 aFiltersFeature->removeFilter(aFilter);
423 clearCurrentSelection(true);
424 updateNumberSelected();
426 myWorkshop->deactivateCurrentSelector();
427 myWorkshop->selectionActivate()->updateSelectionModes();
428 myWorkshop->selectionActivate()->updateSelectionFilters();
431 updateObject(myFeature);
435 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
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);
442 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
445 clearCurrentSelection(true);
446 updateNumberSelected();
449 void ModuleBase_WidgetSelectionFilter::onSelect()
451 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
455 clearCurrentSelection();
457 BRep_Builder aBuilder;
458 TopoDS_Compound aComp;
459 aBuilder.MakeCompound(aComp);
461 DocumentPtr aDoc = myFeature->document();
462 int aNb = aDoc->size(ModelAPI_ResultBody::group());
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))
479 // degenerated edge is not valid selection
480 if ((GeomAPI_Shape::ShapeType)mySelectionType == GeomAPI_Shape::EDGE)
481 if (aSubShape->edge()->isDegenerated())
484 static SessionPtr aSession = ModelAPI_Session::get();
485 bool isValid = aSession->filters()->isValid(myFeature, aBody, aSubShape);
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) {
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;
506 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aContext, aSubShape));
507 //ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aSubShape));
508 myValues.append(aValue);
513 if (myValues.size() > 0)
514 updatePreview(aComp);
515 updateNumberSelected();
516 updateObject(myFeature);
517 onShowOnly(myShowBtn->isChecked());
520 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
522 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
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.);
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);
543 myPreview->Set(theShape);
544 aCtx->Redisplay(myPreview, true);
549 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
551 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
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)
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()) {
567 if (aCtx->IsDisplayed(aShapeIO)) {
568 aCtx->Erase(aShapeIO, false);
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);
584 myWorkshop->viewer()->update();
587 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
589 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
590 mySelectBtn->setEnabled(aFiltersFeature.get() && (aFiltersFeature->filters().size() > 0));
593 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
595 int aNb = myValues.size();
596 myNbLbl->setText(QString::number(aNb));
598 myFeature->setError(translate("Selection is empty").toStdString(), false, false);
599 myShowBtn->setChecked(false);
601 myShowBtn->setEnabled(false);
604 myFeature->setError("", false, false);
605 myFeature->data()->execState(ModelAPI_StateDone);
606 myShowBtn->setEnabled(true);
610 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
612 QList<QWidget*> aWidgets;
613 aWidgets.append(myFiltersCombo);
617 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
620 if (!myPreview.IsNull()) {
621 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
622 aCtx->Remove(myPreview, toUpdate);
627 void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
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());
635 QList<GeomShapePtr> aShapes;
636 aShapes << aPrs->shape();
637 myResShapes[aPrs->object()] = aShapes;
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;
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());
656 if (aShapes.count() == aShapesMap.Size())
657 aToReplace.append(aObj);
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);
668 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
669 theResults.append(aValue);
673 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
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());
686 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
688 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
690 return aActive->storeValue();
691 updateObject(myFeature);
695 QList<ModuleBase_FilterItem*> ModuleBase_WidgetSelectionFilter::itemsList() const
697 return myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
701 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
703 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
704 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
706 // Init filters member of the parent attribute
707 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
708 if (aAttrList->filters() != aFiltersFeature) {
709 aAttrList->setFilters(aFiltersFeature);
712 QList<ModuleBase_FilterItem*> aItemsList = itemsList();
713 std::list<std::string> aFilters = aFiltersFeature->filters();
715 std::list<std::string>::const_iterator aIt;
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);
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);
730 QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
731 foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
732 aWgt->restoreValue();
737 myFiltersCombo->setCurrentIndex(0);
738 myFiltersCombo->blockSignals(isBlocked);
742 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
744 QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
745 if (aErrorMsg.isEmpty()) {
746 if (myValues.size() == 0)
747 aErrorMsg = translate("Selection is empty");
752 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
754 myShowBtn->setChecked(false);
755 clearCurrentSelection(true);
756 updateNumberSelected();
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();
767 updateObject(myFeature);
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);
775 void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
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;
784 QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
786 return ModuleBase_Tools::translate(myFeatureId, theString);