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_IModule.h"
24 #include "ModuleBase_IViewer.h"
25 #include "ModuleBase_IPropertyPanel.h"
26 #include "ModuleBase_PageWidget.h"
27 #include "ModuleBase_WidgetMultiSelector.h"
28 #include "ModuleBase_ResultPrs.h"
29 #include "ModuleBase_WidgetFactory.h"
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <GeomAPI_ShapeExplorer.h>
35 #include <AIS_InteractiveContext.hxx>
36 #include <StdSelect_BRepOwner.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <BRep_Builder.hxx>
41 #include <QPushButton>
46 #include <QToolButton>
49 static FeaturePtr SelectorFeature;
50 static std::string AttributeId;
53 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
55 QString aType = theType.toUpper();
56 if ((aType == "VERTEX") || (aType == "VERTICES"))
57 return GeomAPI_Shape::VERTEX;
58 else if ((aType == "EDGE") || (aType == "EDGES"))
59 return GeomAPI_Shape::EDGE;
60 else if ((aType == "WIRE") || (aType == "WIRES"))
61 return GeomAPI_Shape::WIRE;
62 else if ((aType == "FACE") || (aType == "FACES"))
63 return GeomAPI_Shape::FACE;
64 else if ((aType == "SHELL") || (aType == "SHELLS"))
65 return GeomAPI_Shape::SHELL;
66 else if ((aType == "SOLID") || (aType == "SOLIDS"))
67 return GeomAPI_Shape::SOLID;
68 else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
69 return GeomAPI_Shape::COMPSOLID;
70 else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
71 return GeomAPI_Shape::COMPOUND;
73 return GeomAPI_Shape::SHAPE;
77 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
78 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
80 myFeatureName(theFeature),
81 myWorkshop(theWorkshop)
83 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
84 ModuleBase_Tools::adjustMargins(aMainLayout);
86 aMainLayout->addStretch(1);
87 QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
88 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
89 aMainLayout->addWidget(aLaunchBtn);
91 myFilterLbl = new QLabel(this);
92 myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
93 aMainLayout->addWidget(myFilterLbl);
95 myModifyLbl = new QLabel(this);
96 myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
97 aMainLayout->addWidget(myModifyLbl);
98 aMainLayout->addStretch(1);
104 void ModuleBase_FilterStarter::onFiltersLaunch()
106 QWidget* aParent = parentWidget();
107 ModuleBase_WidgetMultiSelector* aSelector =
108 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
110 aParent = aParent->parentWidget();
111 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
115 SelectorFeature = aSelector->feature();
116 AttributeId = aSelector->attributeID();
118 // Launch Filters operation
119 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
120 (myWorkshop->module()->createOperation(myFeatureName));
121 myWorkshop->processLaunchOperation(aFOperation);
124 //*****************************************************************************
125 //*****************************************************************************
126 //*****************************************************************************
127 ModuleBase_FilterItem::ModuleBase_FilterItem(
128 const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
129 : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
130 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
132 std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
133 if (aXmlString.length() == 0)
136 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
137 QVBoxLayout* aLayout = new QVBoxLayout(this);
138 ModuleBase_Tools::zeroMargins(aLayout);
140 QWidget* aItemRow = new QWidget(this);
141 addItemRow(aItemRow);
142 aLayout->addWidget(aItemRow);
144 ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
145 aFactory.createWidget(aParamsWgt);
146 ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
147 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
148 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
149 aWidget->setFeature(theParent->feature());
151 aLayout->addWidget(aParamsWgt);
155 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
157 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
158 ModuleBase_Tools::zeroMargins(aLayout);
160 // Reverse filter button
161 myRevBtn = new QToolButton(theParent);
162 myRevBtn->setCheckable(true);
163 myRevBtn->setChecked(false);
164 myRevBtn->setAutoRaise(true);
165 myRevBtn->setIcon(QIcon(":pictures/add.png"));
166 myRevBtn->setToolTip(tr("Reverse the filter"));
167 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
168 aLayout->addWidget(myRevBtn);
170 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
171 aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
173 QToolButton* aDelBtn = new QToolButton(theParent);
174 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
175 aDelBtn->setAutoRaise(true);
176 aDelBtn->setToolTip(tr("Delete the filter"));
177 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
178 aLayout->addWidget(aDelBtn);
181 void ModuleBase_FilterItem::onReverse(bool theCheck)
183 mySelection->setReversed(myFilterID, theCheck);
185 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
187 myRevBtn->setIcon(QIcon(":pictures/add.png"));
188 emit reversedItem(this);
191 void ModuleBase_FilterItem::onDelete()
193 emit deleteItem(this);
197 //*****************************************************************************
198 //*****************************************************************************
199 //*****************************************************************************
200 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
201 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
202 : ModuleBase_ModelWidget(theParent, theData),
203 myWorkshop(theWorkshop),
204 mySelectorFeature(SelectorFeature),
205 mySelectorAttribute(AttributeId)
207 // Clear Old selection
208 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
209 AttributeSelectionListPtr aSelListAttr =
210 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
211 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
212 aSelListAttr->clear();
215 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
216 ModuleBase_Tools::adjustMargins(aMainLayout);
218 QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
219 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
220 aGroupLayout->setContentsMargins(0, 0, 0, 0);
221 aGroupLayout->setSpacing(0);
223 myFiltersWgt = new QWidget();
224 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
225 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
226 aGroupLayout->addWidget(myFiltersWgt);
228 myFiltersCombo = new QComboBox(aFiltersGroup);
229 myFiltersCombo->addItem(tr("Add new filter..."));
230 SessionPtr aSession = ModelAPI_Session::get();
231 std::list<FilterPtr> allFilters =
232 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
234 std::list<FilterPtr>::const_iterator aIt;
235 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
236 aItems.push_back((*aIt)->name().c_str());
237 myFilters.push_back(aSession->filters()->id(*aIt));
239 myFiltersCombo->addItems(aItems);
240 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
242 aGroupLayout->addWidget(myFiltersCombo);
243 aMainLayout->addWidget(aFiltersGroup);
246 QWidget* aBtnWgt = new QWidget(this);
247 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
248 ModuleBase_Tools::adjustMargins(aBtnLayout);
250 aBtnLayout->addStretch(1);
252 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
253 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
254 aBtnLayout->addWidget(mySelectBtn);
256 aMainLayout->addWidget(aBtnWgt);
259 QWidget* aLblWgt = new QWidget(this);
260 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
261 ModuleBase_Tools::zeroMargins(aLblLayout);
263 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
265 myNbLbl = new QLabel("0", aLblWgt);
266 aLblLayout->addWidget(myNbLbl);
269 myShowBtn = new QCheckBox(tr("Show only"), this);
270 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
271 aLblLayout->addWidget(myShowBtn);
273 aMainLayout->addWidget(aLblWgt);
275 aMainLayout->addStretch(1);
280 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
283 if (!myPreview.IsNull()) {
284 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
285 aCtx->Remove(myPreview, false);
287 if (myShowBtn->isChecked()) {
288 AIS_ListOfInteractive::const_iterator aIt;
289 Handle(AIS_Shape) aShapeIO;
290 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
291 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
292 if (!aShapeIO.IsNull()) {
293 aCtx->Display(aShapeIO, false);
297 aCtx->UpdateCurrentViewer();
299 SelectorFeature = FeaturePtr();
303 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
308 std::list<std::string>::iterator aIt;
311 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
312 if (i == (theIndex - 1)) {
314 myFilters.erase(aIt);
318 if (!aFilter.empty()) {
319 myUseFilters.push_back(aFilter);
320 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(aFilter, this);
321 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
322 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
323 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
324 SLOT(onReverseItem(ModuleBase_FilterItem*)));
325 myFiltersLayout->addWidget(aItem);
327 FiltersFeaturePtr aFiltersFeature =
328 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
329 aFiltersFeature->addFilter(aFilter);
332 clearCurrentSelection(true);
333 updateNumberSelected();
334 myFiltersCombo->setCurrentIndex(0);
335 myFiltersCombo->removeItem(theIndex);
338 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
340 std::string aFilter = theItem->filter();
341 myFiltersLayout->removeWidget(theItem);
342 theItem->deleteLater();
344 myUseFilters.remove(aFilter);
345 myFilters.push_back(aFilter);
346 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
348 FiltersFeaturePtr aFiltersFeature =
349 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
350 aFiltersFeature->removeFilter(aFilter);
353 clearCurrentSelection(true);
354 updateNumberSelected();
357 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
360 clearCurrentSelection(true);
361 updateNumberSelected();
364 void ModuleBase_WidgetSelectionFilter::onSelect()
366 if (myUseFilters.size() == 0)
368 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
372 clearCurrentSelection();
374 BRep_Builder aBuilder;
375 TopoDS_Compound aComp;
376 aBuilder.MakeCompound(aComp);
378 if (!myShowBtn->isChecked()) {
380 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
381 if (!myPreview.IsNull())
382 myListIO.Remove(myPreview);
384 AIS_ListOfInteractive::const_iterator aIt;
385 Handle(ModuleBase_ResultPrs) aShapeIO;
386 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
387 aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
388 if (!aShapeIO.IsNull()) {
389 GeomShapePtr aShape(new GeomAPI_Shape);
390 aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
391 std::list<GeomShapePtr> aSubShapes =
392 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
393 std::list<GeomShapePtr>::const_iterator aShapesIt;
394 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
395 GeomShapePtr aShape = (*aShapesIt);
396 SessionPtr aSession = ModelAPI_Session::get();
397 bool isValid = aSession->filters()->isValid(myFeature, aShape);
399 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
400 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
401 aBuilder.Add(aComp, aTShape);
403 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
404 myValues.append(aValue);
409 if (myValues.size() > 0)
410 updatePreview(aComp);
411 updateNumberSelected();
414 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
416 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
420 if (myPreview.IsNull()) {
421 myPreview = new AIS_Shape(theShape);
422 myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
423 myPreview->SetColor(Quantity_NOC_YELLOW);
424 myPreview->SetTransparency();
425 aCtx->Display(myPreview, true);
426 aCtx->Deactivate(myPreview);
429 myPreview->Set(theShape);
430 aCtx->Redisplay(myPreview, true);
435 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
437 if (myPreview.IsNull())
439 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
442 aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
444 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
445 myListIO.Remove(myPreview);
448 aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
450 AIS_ListOfInteractive::const_iterator aIt;
451 Handle(AIS_Shape) aShapeIO;
452 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
453 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
454 if (!aShapeIO.IsNull()) {
456 aCtx->Erase(aShapeIO, false);
458 aCtx->Display(aShapeIO, false);
461 aCtx->UpdateCurrentViewer();
464 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
466 mySelectBtn->setEnabled(myUseFilters.size() > 0);
469 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
471 myNbLbl->setText(QString::number(myValues.size()));
473 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
475 return QList<QWidget*>();
478 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
481 if (!myPreview.IsNull()) {
482 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
483 aCtx->Remove(myPreview, toUpdate);
488 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
490 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
491 AttributeSelectionListPtr aSelListAttr =
492 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
493 aSelListAttr->clear();
494 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
495 aSelListAttr->append(aPrs->object(), aPrs->shape());