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));
133 myWorkshop->processLaunchOperation(aFOperation);
136 //*****************************************************************************
137 //*****************************************************************************
138 //*****************************************************************************
139 ModuleBase_FilterItem::ModuleBase_FilterItem(
140 const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
141 : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
142 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
144 std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
145 if (aXmlString.length() == 0)
148 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
149 Config_ValidatorReader aValidatorReader(aXmlString, true);
150 aValidatorReader.setFeatureId(mySelection->getKind());
151 aValidatorReader.readAll();
153 QVBoxLayout* aLayout = new QVBoxLayout(this);
154 ModuleBase_Tools::zeroMargins(aLayout);
156 QWidget* aItemRow = new QWidget(this);
157 addItemRow(aItemRow);
158 aLayout->addWidget(aItemRow);
160 ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
161 aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
162 aFactory.createWidget(aParamsWgt);
163 ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
164 myWidgets = aFactory.getModelWidgets();
165 foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
166 aWidget->setFeature(theParent->feature());
167 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
168 theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
169 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
170 theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
172 aLayout->addWidget(aParamsWgt);
176 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
178 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
179 ModuleBase_Tools::zeroMargins(aLayout);
181 // Reverse filter button
182 myRevBtn = new QToolButton(theParent);
183 myRevBtn->setCheckable(true);
184 myRevBtn->setChecked(false);
185 myRevBtn->setAutoRaise(true);
186 myRevBtn->setIcon(QIcon(":pictures/add.png"));
187 myRevBtn->setToolTip(tr("Reverse the filter"));
188 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
189 aLayout->addWidget(myRevBtn);
191 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
192 aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
194 QToolButton* aDelBtn = new QToolButton(theParent);
195 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
196 aDelBtn->setAutoRaise(true);
197 aDelBtn->setToolTip(tr("Delete the filter"));
198 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
199 aLayout->addWidget(aDelBtn);
202 void ModuleBase_FilterItem::onReverse(bool theCheck)
204 mySelection->setReversed(myFilterID, theCheck);
206 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
208 myRevBtn->setIcon(QIcon(":pictures/add.png"));
209 emit reversedItem(this);
212 void ModuleBase_FilterItem::onDelete()
214 emit deleteItem(this);
217 QList<QWidget*> ModuleBase_FilterItem::getControls() const
219 QList<QWidget*> aWidgetsList;
220 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
221 QList<QWidget*> aSubList = aWgt->getControls();
222 foreach(QWidget* aSub, aSubList) {
223 aWidgetsList.append(aSub);
230 //*****************************************************************************
231 //*****************************************************************************
232 //*****************************************************************************
233 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
234 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
235 : ModuleBase_ModelWidget(theParent, theData),
236 myWorkshop(theWorkshop),
237 mySelectorFeature(SelectorFeature),
238 mySelectorAttribute(AttributeId)
240 // Clear Old selection
241 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
242 AttributeSelectionListPtr aSelListAttr =
243 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
244 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
245 aSelListAttr->clear();
248 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
249 ModuleBase_Tools::adjustMargins(aMainLayout);
251 QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
252 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
253 aGroupLayout->setContentsMargins(0, 0, 0, 0);
254 aGroupLayout->setSpacing(0);
256 myFiltersWgt = new QWidget();
257 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
258 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
259 aGroupLayout->addWidget(myFiltersWgt);
261 myFiltersCombo = new QComboBox(aFiltersGroup);
262 myFiltersCombo->addItem(tr("Add new filter..."));
263 SessionPtr aSession = ModelAPI_Session::get();
264 std::list<FilterPtr> allFilters =
265 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
267 std::list<FilterPtr>::const_iterator aIt;
268 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
269 aItems.push_back((*aIt)->name().c_str());
270 myFilters.push_back(aSession->filters()->id(*aIt));
272 myFiltersCombo->addItems(aItems);
273 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
275 aGroupLayout->addWidget(myFiltersCombo);
276 aMainLayout->addWidget(aFiltersGroup);
279 QWidget* aBtnWgt = new QWidget(this);
280 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
281 ModuleBase_Tools::adjustMargins(aBtnLayout);
283 aBtnLayout->addStretch(1);
285 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
286 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
287 aBtnLayout->addWidget(mySelectBtn);
289 aMainLayout->addWidget(aBtnWgt);
292 QWidget* aLblWgt = new QWidget(this);
293 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
294 ModuleBase_Tools::zeroMargins(aLblLayout);
296 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
298 myNbLbl = new QLabel("0", aLblWgt);
299 aLblLayout->addWidget(myNbLbl);
302 myShowBtn = new QCheckBox(tr("Show only"), this);
303 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
304 aLblLayout->addWidget(myShowBtn);
306 aMainLayout->addWidget(aLblWgt);
308 aMainLayout->addStretch(1);
313 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
316 if (!myPreview.IsNull()) {
317 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
318 aCtx->Remove(myPreview, false);
320 if (myShowBtn->isChecked()) {
321 AIS_ListOfInteractive::const_iterator aIt;
322 Handle(AIS_Shape) aShapeIO;
323 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
324 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
325 if (!aShapeIO.IsNull()) {
326 aCtx->Display(aShapeIO, false);
330 aCtx->UpdateCurrentViewer();
332 SelectorFeature = FeaturePtr();
336 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
341 std::list<std::string>::iterator aIt;
344 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
345 if (i == (theIndex - 1)) {
347 myFilters.erase(aIt);
351 if (!aFilter.empty()) {
352 myUseFilters.push_back(aFilter);
353 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(aFilter, this);
354 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
355 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
356 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
357 SLOT(onReverseItem(ModuleBase_FilterItem*)));
358 myFiltersLayout->addWidget(aItem);
360 FiltersFeaturePtr aFiltersFeature =
361 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
362 aFiltersFeature->addFilter(aFilter);
365 clearCurrentSelection(true);
366 updateNumberSelected();
367 myFiltersCombo->setCurrentIndex(0);
368 myFiltersCombo->removeItem(theIndex);
371 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
373 std::string aFilter = theItem->filter();
374 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
375 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
378 myFiltersLayout->removeWidget(theItem);
379 theItem->deleteLater();
381 myUseFilters.remove(aFilter);
382 myFilters.push_back(aFilter);
383 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
385 FiltersFeaturePtr aFiltersFeature =
386 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
387 aFiltersFeature->removeFilter(aFilter);
390 clearCurrentSelection(true);
391 updateNumberSelected();
393 myWorkshop->deactivateCurrentSelector();
394 myWorkshop->selectionActivate()->updateSelectionModes();
395 myWorkshop->selectionActivate()->updateSelectionFilters();
400 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
402 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
403 ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
404 Events_Loop::loop()->flush(aDispEvent);
407 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
410 clearCurrentSelection(true);
411 updateNumberSelected();
414 void ModuleBase_WidgetSelectionFilter::onSelect()
416 if (myUseFilters.size() == 0)
418 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
422 clearCurrentSelection();
424 BRep_Builder aBuilder;
425 TopoDS_Compound aComp;
426 aBuilder.MakeCompound(aComp);
428 DocumentPtr aDoc = myFeature->document();
429 int aNb = aDoc->size(ModelAPI_ResultBody::group());
433 for (int i = 0; i < aNb; i++) {
434 aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
435 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
436 GeomShapePtr aShape = aBody->shape();
437 std::list<GeomShapePtr> aSubShapes =
438 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
439 std::list<GeomShapePtr>::const_iterator aShapesIt;
440 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
441 GeomShapePtr aShape = (*aShapesIt);
442 SessionPtr aSession = ModelAPI_Session::get();
443 bool isValid = aSession->filters()->isValid(myFeature, aShape);
445 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
446 aBuilder.Add(aComp, aTShape);
447 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
448 myValues.append(aValue);
453 if (myValues.size() > 0)
454 updatePreview(aComp);
455 updateNumberSelected();
458 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
460 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
464 if (myPreview.IsNull()) {
465 myPreview = new AIS_Shape(theShape);
466 myPreview->SetDisplayMode(AIS_Shaded);
467 myPreview->SetColor(Quantity_NOC_BLUE1);
468 Handle(Prs3d_Drawer) aDrawer = myPreview->Attributes();
469 if (aDrawer->HasOwnPointAspect()) {
470 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_O_STAR);
471 aDrawer->PointAspect()->SetColor(Quantity_NOC_BLUE1);
472 aDrawer->PointAspect()->SetScale(2.);
475 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_O_STAR, Quantity_NOC_BLUE1, 2.));
476 myPreview->SetTransparency();
477 aCtx->Display(myPreview, true);
478 aCtx->Deactivate(myPreview);
481 myPreview->Set(theShape);
482 aCtx->Redisplay(myPreview, true);
487 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
489 if (myPreview.IsNull())
491 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
495 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
496 myListIO.Remove(myPreview);
498 AIS_ListOfInteractive::const_iterator aIt;
499 Handle(AIS_Shape) aShapeIO;
500 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
501 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
502 if (!aShapeIO.IsNull()) {
504 aCtx->Erase(aShapeIO, false);
506 aCtx->Display(aShapeIO, false);
509 aCtx->UpdateCurrentViewer();
512 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
514 mySelectBtn->setEnabled(myUseFilters.size() > 0);
517 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
519 myNbLbl->setText(QString::number(myValues.size()));
521 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
523 QList<QWidget*> aWidgets;
524 QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
525 foreach(ModuleBase_FilterItem* aItem, aItems) {
526 QList<QWidget*> aSubList = aItem->getControls();
527 foreach(QWidget* aWgt, aSubList) {
528 aWidgets.append(aWgt);
534 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
537 if (!myPreview.IsNull()) {
538 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
539 aCtx->Remove(myPreview, toUpdate);
544 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
546 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
547 AttributeSelectionListPtr aSelListAttr =
548 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
549 aSelListAttr->clear();
550 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
551 aSelListAttr->append(aPrs->object(), aPrs->shape());
555 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
560 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
562 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
564 return aActive->restoreValue();