1 // Copyright (C) 2014-2019 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_ResultBody.h>
36 #include <GeomAPI_ShapeExplorer.h>
38 #include <Events_Loop.h>
39 #include <Config_ValidatorReader.h>
41 #include <AIS_InteractiveContext.hxx>
42 #include <StdSelect_BRepOwner.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <BRep_Builder.hxx>
47 #include <QPushButton>
52 #include <QToolButton>
55 static FeaturePtr SelectorFeature;
56 static std::string AttributeId;
59 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
61 QString aType = theType.toUpper();
62 if ((aType == "VERTEX") || (aType == "VERTICES"))
63 return GeomAPI_Shape::VERTEX;
64 else if ((aType == "EDGE") || (aType == "EDGES"))
65 return GeomAPI_Shape::EDGE;
66 else if ((aType == "WIRE") || (aType == "WIRES"))
67 return GeomAPI_Shape::WIRE;
68 else if ((aType == "FACE") || (aType == "FACES"))
69 return GeomAPI_Shape::FACE;
70 else if ((aType == "SHELL") || (aType == "SHELLS"))
71 return GeomAPI_Shape::SHELL;
72 else if ((aType == "SOLID") || (aType == "SOLIDS"))
73 return GeomAPI_Shape::SOLID;
74 else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
75 return GeomAPI_Shape::COMPSOLID;
76 else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
77 return GeomAPI_Shape::COMPOUND;
79 return GeomAPI_Shape::SHAPE;
83 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
84 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
86 myFeatureName(theFeature),
87 myWorkshop(theWorkshop)
89 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
90 ModuleBase_Tools::adjustMargins(aMainLayout);
92 aMainLayout->addStretch(1);
93 QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
94 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
95 aMainLayout->addWidget(aLaunchBtn);
97 myFilterLbl = new QLabel(this);
98 myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
99 aMainLayout->addWidget(myFilterLbl);
101 myModifyLbl = new QLabel(this);
102 myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
103 aMainLayout->addWidget(myModifyLbl);
104 aMainLayout->addStretch(1);
110 void ModuleBase_FilterStarter::onFiltersLaunch()
112 ModuleBase_Operation* aParentOp = myWorkshop->currentOperation();
113 ModuleBase_OperationFeature* aFeatureOp = dynamic_cast<ModuleBase_OperationFeature*>(aParentOp);
115 // Open transaction on filters operation finish
116 aFeatureOp->openTransactionOnResume();
118 QWidget* aParent = parentWidget();
119 ModuleBase_WidgetMultiSelector* aSelector =
120 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
122 aParent = aParent->parentWidget();
123 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
127 SelectorFeature = aSelector->feature();
128 AttributeId = aSelector->attributeID();
130 // Launch Filters operation
131 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
132 (myWorkshop->module()->createOperation(myFeatureName));
134 AttributeSelectionListPtr aAttrList = SelectorFeature->selectionList(AttributeId);
135 FiltersFeaturePtr aFilters = aAttrList->filters();
137 aFOperation->setFeature(aFilters);
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 std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
150 if (aXmlString.length() == 0)
153 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
154 Config_ValidatorReader aValidatorReader(aXmlString, true);
155 aValidatorReader.setFeatureId(mySelection->getKind());
156 aValidatorReader.readAll();
158 QVBoxLayout* aLayout = new QVBoxLayout(this);
159 ModuleBase_Tools::zeroMargins(aLayout);
161 QWidget* aItemRow = new QWidget(this);
162 addItemRow(aItemRow);
163 aLayout->addWidget(aItemRow);
165 ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
166 aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
167 aFactory.createWidget(aParamsWgt);
168 ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
169 myWidgets = aFactory.getModelWidgets();
170 foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
171 aWidget->setFeature(theParent->feature());
172 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
173 theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
174 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
175 theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
177 aLayout->addWidget(aParamsWgt);
181 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
183 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
184 ModuleBase_Tools::zeroMargins(aLayout);
186 // Reverse filter button
187 myRevBtn = new QToolButton(theParent);
188 myRevBtn->setCheckable(true);
189 myRevBtn->setChecked(false);
190 myRevBtn->setAutoRaise(true);
191 myRevBtn->setIcon(QIcon(":pictures/add.png"));
192 myRevBtn->setToolTip(tr("Reverse the filter"));
193 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
194 aLayout->addWidget(myRevBtn);
196 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
197 aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
199 QToolButton* aDelBtn = new QToolButton(theParent);
200 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
201 aDelBtn->setAutoRaise(true);
202 aDelBtn->setToolTip(tr("Delete the filter"));
203 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
204 aLayout->addWidget(aDelBtn);
207 void ModuleBase_FilterItem::onReverse(bool theCheck)
209 mySelection->setReversed(myFilterID, theCheck);
211 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
213 myRevBtn->setIcon(QIcon(":pictures/add.png"));
214 emit reversedItem(this);
217 void ModuleBase_FilterItem::onDelete()
219 emit deleteItem(this);
222 QList<QWidget*> ModuleBase_FilterItem::getControls() const
224 QList<QWidget*> aWidgetsList;
225 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
226 QList<QWidget*> aSubList = aWgt->getControls();
227 foreach(QWidget* aSub, aSubList) {
228 aWidgetsList.append(aSub);
235 //*****************************************************************************
236 //*****************************************************************************
237 //*****************************************************************************
238 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
239 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
240 : ModuleBase_ModelWidget(theParent, theData),
241 myWorkshop(theWorkshop),
242 mySelectorFeature(SelectorFeature),
243 mySelectorAttribute(AttributeId)
245 // Clear Old selection
246 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
247 mySelectionType = selectionType(aAttrList->selectionType().c_str());
251 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
252 ModuleBase_Tools::adjustMargins(aMainLayout);
254 QGroupBox* aFiltersGroup = new QGroupBox(tr("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(tr("Add new filter..."));
266 SessionPtr aSession = ModelAPI_Session::get();
267 std::list<FilterPtr> allFilters =
268 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
270 std::list<FilterPtr>::const_iterator aIt;
271 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
272 aItems.push_back((*aIt)->name().c_str());
273 myFilters.push_back(aSession->filters()->id(*aIt));
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(tr("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(tr("Number of selected objects:"), aLblWgt));
301 myNbLbl = new QLabel("0", aLblWgt);
302 aLblLayout->addWidget(myNbLbl);
305 myShowBtn = new QCheckBox(tr("Show only"), this);
306 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
307 aLblLayout->addWidget(myShowBtn);
309 aMainLayout->addWidget(aLblWgt);
311 aMainLayout->addStretch(1);
316 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
319 if (!myPreview.IsNull()) {
320 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
321 aCtx->Remove(myPreview, false);
323 if (myShowBtn->isChecked()) {
324 AIS_ListOfInteractive::const_iterator aIt;
325 Handle(AIS_Shape) aShapeIO;
326 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
327 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
328 if (!aShapeIO.IsNull()) {
329 aCtx->Display(aShapeIO, false);
333 aCtx->UpdateCurrentViewer();
335 SelectorFeature = FeaturePtr();
339 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
344 std::list<std::string>::iterator aIt;
347 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
348 if (i == (theIndex - 1)) {
353 onAddFilter(aFilter);
354 FiltersFeaturePtr aFiltersFeature =
355 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
356 aFiltersFeature->addFilter(aFilter);
358 myFiltersCombo->setCurrentIndex(0);
359 myFiltersCombo->removeItem(theIndex);
362 void ModuleBase_WidgetSelectionFilter::onAddFilter(const std::string& theFilter)
364 if (theFilter.length() == 0)
366 std::list<std::string>::const_iterator aIt;
367 for (aIt = myUseFilters.cbegin(); aIt != myUseFilters.cend(); aIt++) {
368 if (theFilter == (*aIt))
371 myFilters.remove(theFilter);
372 myUseFilters.push_back(theFilter);
373 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
374 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
375 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
376 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
377 SLOT(onReverseItem(ModuleBase_FilterItem*)));
378 myFiltersLayout->addWidget(aItem);
381 clearCurrentSelection(true);
382 updateNumberSelected();
385 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
387 std::string aFilter = theItem->filter();
388 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
389 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
392 myFiltersLayout->removeWidget(theItem);
393 theItem->deleteLater();
395 myUseFilters.remove(aFilter);
396 myFilters.push_back(aFilter);
397 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
399 FiltersFeaturePtr aFiltersFeature =
400 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
401 aFiltersFeature->removeFilter(aFilter);
404 clearCurrentSelection(true);
405 updateNumberSelected();
407 myWorkshop->deactivateCurrentSelector();
408 myWorkshop->selectionActivate()->updateSelectionModes();
409 myWorkshop->selectionActivate()->updateSelectionFilters();
414 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
416 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
417 ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
418 Events_Loop::loop()->flush(aDispEvent);
421 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
424 clearCurrentSelection(true);
425 updateNumberSelected();
428 void ModuleBase_WidgetSelectionFilter::onSelect()
430 if (myUseFilters.size() == 0)
432 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
436 clearCurrentSelection();
438 BRep_Builder aBuilder;
439 TopoDS_Compound aComp;
440 aBuilder.MakeCompound(aComp);
442 DocumentPtr aDoc = myFeature->document();
443 int aNb = aDoc->size(ModelAPI_ResultBody::group());
447 for (int i = 0; i < aNb; i++) {
448 aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
449 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
450 GeomShapePtr aShape = aBody->shape();
451 std::list<GeomShapePtr> aSubShapes =
452 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
453 TopTools_MapOfShape alreadyThere;
454 std::list<GeomShapePtr>::const_iterator aShapesIt;
455 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
456 GeomShapePtr aShape = (*aShapesIt);
457 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
458 if (!alreadyThere.Add(aTShape))
460 static SessionPtr aSession = ModelAPI_Session::get();
461 bool isValid = aSession->filters()->isValid(myFeature, aShape);
463 aBuilder.Add(aComp, aTShape);
464 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
465 myValues.append(aValue);
470 if (myValues.size() > 0)
471 updatePreview(aComp);
472 updateNumberSelected();
475 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
477 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
481 if (myPreview.IsNull()) {
482 myPreview = new AIS_Shape(theShape);
483 myPreview->SetDisplayMode(AIS_Shaded);
484 myPreview->SetColor(Quantity_NOC_BLUE1);
485 Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
486 if (aDrawer->HasOwnPointAspect()) {
487 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
488 aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
489 aDrawer->PointAspect()->SetScale(2.);
492 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
493 myPreview->SetTransparency();
494 aCtx->Display(myPreview, true);
495 aCtx->Deactivate(myPreview);
498 myPreview->Set(theShape);
499 aCtx->Redisplay(myPreview, true);
504 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
506 if (myPreview.IsNull())
508 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
512 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
513 myListIO.Remove(myPreview);
515 AIS_ListOfInteractive::const_iterator aIt;
516 Handle(AIS_Shape) aShapeIO;
517 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
518 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
519 if (!aShapeIO.IsNull()) {
521 aCtx->Erase(aShapeIO, false);
523 aCtx->Display(aShapeIO, false);
526 aCtx->UpdateCurrentViewer();
529 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
531 mySelectBtn->setEnabled(myUseFilters.size() > 0);
534 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
536 myNbLbl->setText(QString::number(myValues.size()));
538 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
540 QList<QWidget*> aWidgets;
541 QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
542 foreach(ModuleBase_FilterItem* aItem, aItems) {
543 QList<QWidget*> aSubList = aItem->getControls();
544 foreach(QWidget* aWgt, aSubList) {
545 aWidgets.append(aWgt);
551 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
554 if (!myPreview.IsNull()) {
555 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
556 aCtx->Remove(myPreview, toUpdate);
561 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
563 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
564 AttributeSelectionListPtr aSelListAttr =
565 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
566 aSelListAttr->clear();
567 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
568 aSelListAttr->append(aPrs->object(), aPrs->shape());
572 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
574 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
576 return aActive->storeValue();
580 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
582 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
583 FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
584 std::list<std::string> aFilters = aFiltersFeature->filters();
585 std::list<std::string>::const_iterator aIt;
586 for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++) {
587 std::string aStr = (*aIt);
589 FilterPtr aFilterObj = aFactory->filter(aStr);
590 int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
592 myFiltersCombo->removeItem(aId);
594 // Init filters member of the parent attribute
595 AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
596 if (aAttrList->filters() != aFiltersFeature) {
597 aAttrList->setFilters(aFiltersFeature);
600 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
602 return aActive->restoreValue();
603 QList<QWidget*> aWidgets;
604 QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
605 foreach(ModuleBase_FilterItem* aItem, aItems) {
606 QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
607 foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
608 aWgt->restoreValue();