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()));
184 aLayout->addWidget(aParamsWgt);
188 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
190 std::string aContext = mySelection->getKind();
191 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
192 ModuleBase_Tools::zeroMargins(aLayout);
194 // Reverse filter button
195 myRevBtn = new QToolButton(theParent);
196 myRevBtn->setCheckable(true);
197 bool isReversed = mySelection->isReversed(myFilterID);
198 myRevBtn->setChecked(isReversed);
199 myRevBtn->setAutoRaise(true);
201 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
203 myRevBtn->setIcon(QIcon(":pictures/add.png"));
204 myRevBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Reverse the filter"));
205 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
206 aLayout->addWidget(myRevBtn);
208 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
209 aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
211 QToolButton* aDelBtn = new QToolButton(theParent);
212 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
213 aDelBtn->setAutoRaise(true);
214 aDelBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Delete the filter"));
215 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
216 aLayout->addWidget(aDelBtn);
219 void ModuleBase_FilterItem::onReverse(bool theCheck)
221 mySelection->setReversed(myFilterID, theCheck);
223 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
225 myRevBtn->setIcon(QIcon(":pictures/add.png"));
226 emit reversedItem(this);
229 void ModuleBase_FilterItem::onDelete()
231 emit deleteItem(this);
235 //*****************************************************************************
236 //*****************************************************************************
237 //*****************************************************************************
238 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
239 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, bool theReadOnly)
240 : ModuleBase_ModelWidget(theParent, theData),
241 myWorkshop(theWorkshop),
242 mySelectorFeature(ModuleBase_WidgetSelectionFilter::SelectorFeature),
243 mySelectorAttribute(ModuleBase_WidgetSelectionFilter::AttributeId)
245 // Clear Old selection
246 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
247 mySelectionType = selectionType(aAttrList->selectionType().c_str());
253 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
254 ModuleBase_Tools::adjustMargins(aMainLayout);
256 QGroupBox* aFiltersGroup = new QGroupBox(translate("Filters"), this);
257 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
258 aGroupLayout->setContentsMargins(0, 0, 0, 0);
259 aGroupLayout->setSpacing(0);
261 myFiltersWgt = new QWidget();
262 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
263 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
264 aGroupLayout->addWidget(myFiltersWgt);
266 myFiltersCombo = new QComboBox(aFiltersGroup);
267 myFiltersCombo->addItem(translate("Add new filter..."));
268 SessionPtr aSession = ModelAPI_Session::get();
269 std::list<FilterPtr> allFilters =
270 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
271 storeFilters(allFilters);
273 std::list<FilterPtr>::const_iterator aIt;
274 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
275 aItems.push_back(translate((*aIt)->name().c_str()));
277 myFiltersCombo->addItems(aItems);
278 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
280 aGroupLayout->addWidget(myFiltersCombo);
281 aMainLayout->addWidget(aFiltersGroup);
284 QWidget* aBtnWgt = new QWidget(this);
285 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
286 ModuleBase_Tools::adjustMargins(aBtnLayout);
288 aBtnLayout->addStretch(1);
290 mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
291 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
292 aBtnLayout->addWidget(mySelectBtn);
294 aMainLayout->addWidget(aBtnWgt);
297 QWidget* aLblWgt = new QWidget(this);
298 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
299 ModuleBase_Tools::zeroMargins(aLblLayout);
301 aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
303 myNbLbl = new QLabel("0", aLblWgt);
304 aLblLayout->addWidget(myNbLbl);
307 myShowBtn = new QCheckBox(translate("Show only"), this);
308 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
309 aLblLayout->addWidget(myShowBtn);
311 aMainLayout->addWidget(aLblWgt);
313 aMainLayout->addStretch(1);
317 myFiltersCombo->hide();
324 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
327 if (!myPreview.IsNull()) {
328 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
329 aCtx->Remove(myPreview, false);
331 if (myListIO.Size() > 0) {
332 aCtx = myWorkshop->viewer()->AISContext();
333 AIS_ListOfInteractive::const_iterator aIt;
334 Handle(AIS_Shape) aShapeIO;
335 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
336 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
337 if (!aShapeIO.IsNull()) {
338 aCtx->Display(aShapeIO, false);
339 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
340 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
341 myWorkshop->applyCurrentSelectionModes(anAISObj);
345 myShowBtn->setChecked(false);
347 myWorkshop->viewer()->update();
349 SelectorFeature = FeaturePtr();
353 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
358 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
359 FiltersFeaturePtr aFiltersFeature =
360 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
362 std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
364 std::list<FilterPtr>::iterator aIt;
366 std::map<std::string, FilterPtr>::const_iterator aFound = myFilters.find(aText);
367 if (aFound == myFilters.end()) {
368 std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
369 storeFilters(aFilters);
370 aFound = myFilters.find(aText);
372 if (aFound != myFilters.end())
373 aFilter = aFactory->id(aFound->second);
375 aFiltersFeature->addFilter(aFilter);
376 updateObject(myFeature);
378 QList<ModuleBase_FilterItem*> aList = itemsList();
379 if (!aList.isEmpty() && (aList.last()->widgets().size() > 0))
380 aList.last()->widgets().first()->emitFocusInWidget();
385 ModuleBase_FilterItem* ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
387 if (theFilter.length() == 0)
389 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
390 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
391 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
392 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
393 SLOT(onReverseItem(ModuleBase_FilterItem*)));
394 myFiltersLayout->addWidget(aItem);
397 clearCurrentSelection(true);
398 updateNumberSelected();
402 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
404 std::string aFilter = theItem->filter();
405 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
406 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
409 myFiltersLayout->removeWidget(theItem);
410 theItem->deleteLater();
412 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
413 if (!aFactory->filter(aFilter)->isMultiple()) {
414 //myFilters.push_back(aFilter);
415 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
417 FiltersFeaturePtr aFiltersFeature =
418 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
419 aFiltersFeature->removeFilter(aFilter);
422 clearCurrentSelection(true);
423 updateNumberSelected();
425 myWorkshop->deactivateCurrentSelector();
426 myWorkshop->selectionActivate()->updateSelectionModes();
427 myWorkshop->selectionActivate()->updateSelectionFilters();
430 updateObject(myFeature);
434 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
436 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
437 ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
438 Events_Loop::loop()->flush(aDispEvent);
441 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
444 clearCurrentSelection(true);
445 updateNumberSelected();
448 void ModuleBase_WidgetSelectionFilter::onSelect()
450 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
454 clearCurrentSelection();
456 BRep_Builder aBuilder;
457 TopoDS_Compound aComp;
458 aBuilder.MakeCompound(aComp);
460 DocumentPtr aDoc = myFeature->document();
461 int aNb = aDoc->size(ModelAPI_ResultBody::group());
464 for (int i = 0; i < aNb; i++) {
465 aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
466 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
467 GeomShapePtr aShape = aBody->shape();
468 std::list<GeomShapePtr> aSubShapes =
469 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
470 TopTools_MapOfShape alreadyThere;
471 std::list<GeomShapePtr>::const_iterator aShapesIt;
472 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
473 GeomShapePtr aSubShape = (*aShapesIt);
474 TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
475 if (!alreadyThere.Add(aTShape))
478 // degenerated edge is not valid selection
479 if ((GeomAPI_Shape::ShapeType)mySelectionType == GeomAPI_Shape::EDGE)
480 if (aSubShape->edge()->isDegenerated())
483 static SessionPtr aSession = ModelAPI_Session::get();
484 bool isValid = aSession->filters()->isValid(myFeature, aBody, aSubShape);
487 aBuilder.Add(aComp, aTShape);
488 // bos #24043: Naming on a compsolid works wrong.
489 // Find a simple sub-result for the ViewerPrs context:
490 ResultBodyPtr aContext = aBody;
491 bool isComposite = aContext->numberOfSubs() > 0;
492 while (isComposite) {
494 int nbSubs = aContext->numberOfSubs();
495 for (int aSubIndex = 0; aSubIndex < nbSubs; aSubIndex++) {
496 ResultBodyPtr aSubResult = aContext->subResult(aSubIndex);
497 GeomShapePtr aSubResultShape = aSubResult->shape();
498 if (aSubResultShape->isSubShape(aSubShape)) {
499 aContext = aSubResult;
500 isComposite = aContext->numberOfSubs() > 0;
505 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aContext, aSubShape));
506 //ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aSubShape));
507 myValues.append(aValue);
512 if (myValues.size() > 0)
513 updatePreview(aComp);
514 updateNumberSelected();
515 updateObject(myFeature);
516 onShowOnly(myShowBtn->isChecked());
519 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
521 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
525 if (myPreview.IsNull()) {
526 myPreview = new AIS_Shape(theShape);
527 myPreview->SetDisplayMode(AIS_Shaded);
528 myPreview->SetColor(Quantity_NOC_BLUE1);
529 Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
530 if (aDrawer->HasOwnPointAspect()) {
531 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
532 aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
533 aDrawer->PointAspect()->SetScale(2.);
536 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
537 myPreview->SetTransparency();
538 aCtx->Display(myPreview, true);
539 aCtx->Deactivate(myPreview);
542 myPreview->Set(theShape);
543 aCtx->Redisplay(myPreview, true);
548 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
550 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
552 AIS_ListOfInteractive aList;
553 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
554 if (!myPreview.IsNull())
555 aList.Remove(myPreview);
556 if (aList.Size() > 0)
559 AIS_ListOfInteractive::const_iterator aIt;
560 Handle(AIS_Shape) aShapeIO;
561 bool isModified = false;
562 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
563 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
564 if (!aShapeIO.IsNull()) {
566 if (aCtx->IsDisplayed(aShapeIO)) {
567 aCtx->Erase(aShapeIO, false);
572 if (!aCtx->IsDisplayed(aShapeIO)) {
573 aCtx->Display(aShapeIO, false);
574 std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
575 anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
576 myWorkshop->applyCurrentSelectionModes(anAISObj);
583 myWorkshop->viewer()->update();
586 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
588 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
589 mySelectBtn->setEnabled(aFiltersFeature.get() && (aFiltersFeature->filters().size() > 0));
592 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
594 int aNb = myValues.size();
595 myNbLbl->setText(QString::number(aNb));
597 myFeature->setError(translate("Selection is empty").toStdString(), false, false);
598 myShowBtn->setChecked(false);
600 myShowBtn->setEnabled(false);
603 myFeature->setError("", false, false);
604 myFeature->data()->execState(ModelAPI_StateDone);
605 myShowBtn->setEnabled(true);
609 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
611 QList<QWidget*> aWidgets;
612 aWidgets.append(myFiltersCombo);
616 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
619 if (!myPreview.IsNull()) {
620 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
621 aCtx->Remove(myPreview, toUpdate);
626 void replaceSubShapesByResult(QList<ModuleBase_ViewerPrsPtr>& theResults, int theShapeType)
628 QMap<ObjectPtr, QList<GeomShapePtr>> myResShapes;
629 // Sort sub-shapes by result
630 foreach (ModuleBase_ViewerPrsPtr aPrs, theResults) {
631 if (myResShapes.contains(aPrs->object()))
632 myResShapes[aPrs->object()].append(aPrs->shape());
634 QList<GeomShapePtr> aShapes;
635 aShapes << aPrs->shape();
636 myResShapes[aPrs->object()] = aShapes;
639 // Find Results to replace by whole result
640 QList<GeomShapePtr> aShapes;
641 QList<ObjectPtr> aToReplace;
642 std::list<GeomShapePtr> aSubShapes;
643 foreach(ObjectPtr aObj, myResShapes.keys()) {
644 aShapes = myResShapes[aObj];
645 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
646 TopTools_MapOfShape aShapesMap;
648 GeomShapePtr aSubShape = aRes->shape();
649 const TopoDS_Shape& aShape = aSubShape->impl<TopoDS_Shape>();
650 for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theShapeType);
651 anExp.More(); anExp.Next()) {
652 aShapesMap.Add(anExp.Current());
655 if (aShapes.count() == aShapesMap.Size())
656 aToReplace.append(aObj);
658 // Replace the found results
659 QList<ModuleBase_ViewerPrsPtr>::iterator aIt;
660 foreach(ObjectPtr aObj, aToReplace) {
661 for (aIt = theResults.begin(); aIt != theResults.end(); aIt++) {
662 if ((*aIt)->object() == aObj) {
663 theResults.removeAll(*aIt);
667 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj));
668 theResults.append(aValue);
672 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
674 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
675 AttributeSelectionListPtr aSelListAttr =
676 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
677 aSelListAttr->clear();
678 if (aSelListAttr->isWholeResultAllowed())
679 replaceSubShapesByResult(myValues, selectionType(aSelListAttr->selectionType().c_str()));
680 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
681 aSelListAttr->append(aPrs->object(), aPrs->shape());
685 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
687 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
689 return aActive->storeValue();
690 updateObject(myFeature);
694 QList<ModuleBase_FilterItem*> ModuleBase_WidgetSelectionFilter::itemsList() const
696 return myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
700 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
702 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
703 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
705 // Init filters member of the parent attribute
706 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
707 if (aAttrList->filters() != aFiltersFeature) {
708 aAttrList->setFilters(aFiltersFeature);
711 QList<ModuleBase_FilterItem*> aItemsList = itemsList();
712 std::list<std::string> aFilters = aFiltersFeature->filters();
714 std::list<std::string>::const_iterator aIt;
716 int aNbItems = aItemsList.size();
717 ModuleBase_FilterItem* aItem = 0;
718 bool isBlocked = myFiltersCombo->blockSignals(true);
719 for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++, i++) {
720 std::string aStr = (*aIt);
723 aItem = onAddFilter(aStr);
724 FilterPtr aFilterObj = aFactory->filter(aStr);
725 int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
726 if ((aId != -1) && !aFilterObj->isMultiple())
727 myFiltersCombo->removeItem(aId);
729 QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
730 foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
731 aWgt->restoreValue();
736 myFiltersCombo->setCurrentIndex(0);
737 myFiltersCombo->blockSignals(isBlocked);
741 QString ModuleBase_WidgetSelectionFilter::getError(const bool theValueStateChecked) const
743 QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
744 if (aErrorMsg.isEmpty()) {
745 if (myValues.size() == 0)
746 aErrorMsg = translate("Selection is empty");
751 void ModuleBase_WidgetSelectionFilter::onObjectUpdated()
753 myShowBtn->setChecked(false);
754 clearCurrentSelection(true);
755 updateNumberSelected();
757 QList<ModuleBase_FilterItem*> aItemsList = itemsList();
758 foreach(ModuleBase_FilterItem* aItem, aItemsList) {
759 QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
760 foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
761 if (!aWidget->feature().get())
762 aWidget->setFeature(myFeature);
763 aWidget->restoreValue();
766 updateObject(myFeature);
768 // Redisplay the feature on order to Customize presentations from filters with selectors
769 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
770 ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
771 Events_Loop::loop()->flush(EVENT_DISP);
774 void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
776 for (std::list<FilterPtr>::const_iterator anIt = theFilters.begin();
777 anIt != theFilters.end(); ++anIt) {
778 std::string aName = translate((*anIt)->name()).toStdString();
779 myFilters[aName] = *anIt;
783 QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
785 return ModuleBase_Tools::translate(myFeatureId, theString);