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"
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <GeomAPI_ShapeExplorer.h>
34 #include <AIS_InteractiveContext.hxx>
35 #include <StdSelect_BRepOwner.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <BRep_Builder.hxx>
40 #include <QPushButton>
45 #include <QToolButton>
48 static FeaturePtr SelectorFeature;
49 static std::string AttributeId;
52 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
54 QString aType = theType.toUpper();
55 if ((aType == "VERTEX") || (aType == "VERTICES"))
56 return GeomAPI_Shape::VERTEX;
57 else if ((aType == "EDGE") || (aType == "EDGES"))
58 return GeomAPI_Shape::EDGE;
59 else if ((aType == "WIRE") || (aType == "WIRES"))
60 return GeomAPI_Shape::WIRE;
61 else if ((aType == "FACE") || (aType == "FACES"))
62 return GeomAPI_Shape::FACE;
63 else if ((aType == "SHELL") || (aType == "SHELLS"))
64 return GeomAPI_Shape::SHELL;
65 else if ((aType == "SOLID") || (aType == "SOLIDS"))
66 return GeomAPI_Shape::SOLID;
67 else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
68 return GeomAPI_Shape::COMPSOLID;
69 else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
70 return GeomAPI_Shape::COMPOUND;
72 return GeomAPI_Shape::SHAPE;
76 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
77 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
79 myFeatureName(theFeature),
80 myWorkshop(theWorkshop)
82 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
83 ModuleBase_Tools::adjustMargins(aMainLayout);
85 aMainLayout->addStretch(1);
86 QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
87 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
88 aMainLayout->addWidget(aLaunchBtn);
90 myFilterLbl = new QLabel(this);
91 myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
92 aMainLayout->addWidget(myFilterLbl);
94 myModifyLbl = new QLabel(this);
95 myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
96 aMainLayout->addWidget(myModifyLbl);
97 aMainLayout->addStretch(1);
103 void ModuleBase_FilterStarter::onFiltersLaunch()
105 QWidget* aParent = parentWidget();
106 ModuleBase_WidgetMultiSelector* aSelector =
107 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
109 aParent = aParent->parentWidget();
110 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
114 SelectorFeature = aSelector->feature();
115 AttributeId = aSelector->attributeID();
117 // Launch Filters operation
118 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
119 (myWorkshop->module()->createOperation(myFeatureName));
120 myWorkshop->processLaunchOperation(aFOperation);
123 //*****************************************************************************
124 //*****************************************************************************
125 //*****************************************************************************
126 ModuleBase_FilterItem::ModuleBase_FilterItem(
127 const std::string& theFilter, FeaturePtr theSelection, QWidget* theParent)
128 : QWidget(theParent), myFilterID(theFilter),
129 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theSelection))
131 QHBoxLayout* aLayout = new QHBoxLayout(this);
132 ModuleBase_Tools::zeroMargins(aLayout);
134 // Reverse filter button
135 myRevBtn = new QToolButton(this);
136 myRevBtn->setCheckable(true);
137 myRevBtn->setChecked(false);
138 myRevBtn->setAutoRaise(true);
139 myRevBtn->setIcon(QIcon(":pictures/add.png"));
140 myRevBtn->setToolTip(tr("Reverse the filter"));
141 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
142 aLayout->addWidget(myRevBtn);
144 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
145 aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
147 QToolButton* aDelBtn = new QToolButton(this);
148 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
149 aDelBtn->setAutoRaise(true);
150 aDelBtn->setToolTip(tr("Delete the filter"));
151 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
152 aLayout->addWidget(aDelBtn);
156 void ModuleBase_FilterItem::onReverse(bool theCheck)
158 //mySelection->setReversed(myFilterID, theCheck);
160 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
162 myRevBtn->setIcon(QIcon(":pictures/add.png"));
165 void ModuleBase_FilterItem::onDelete()
167 emit deleteItem(this);
171 //*****************************************************************************
172 //*****************************************************************************
173 //*****************************************************************************
174 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
175 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
176 : ModuleBase_ModelWidget(theParent, theData),
177 myWorkshop(theWorkshop),
178 mySelectorFeature(SelectorFeature),
179 mySelectorAttribute(AttributeId)
181 // Clear Old selection
182 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
183 AttributeSelectionListPtr aSelListAttr =
184 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
185 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
186 aSelListAttr->clear();
189 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
190 ModuleBase_Tools::adjustMargins(aMainLayout);
192 QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
193 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
194 aGroupLayout->setContentsMargins(0, 0, 0, 0);
195 aGroupLayout->setSpacing(0);
197 myFiltersWgt = new QWidget();
198 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
199 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
200 aGroupLayout->addWidget(myFiltersWgt);
202 myFiltersCombo = new QComboBox(aFiltersGroup);
203 myFiltersCombo->addItem(tr("Add new filter..."));
204 SessionPtr aSession = ModelAPI_Session::get();
205 std::list<FilterPtr> allFilters =
206 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
208 std::list<FilterPtr>::const_iterator aIt;
209 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
210 aItems.push_back((*aIt)->name().c_str());
211 myFilters.push_back(aSession->filters()->id(*aIt));
213 myFiltersCombo->addItems(aItems);
214 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
216 aGroupLayout->addWidget(myFiltersCombo);
217 aMainLayout->addWidget(aFiltersGroup);
220 QWidget* aBtnWgt = new QWidget(this);
221 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
222 ModuleBase_Tools::adjustMargins(aBtnLayout);
224 aBtnLayout->addStretch(1);
226 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
227 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
228 aBtnLayout->addWidget(mySelectBtn);
230 aMainLayout->addWidget(aBtnWgt);
233 QWidget* aLblWgt = new QWidget(this);
234 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
235 ModuleBase_Tools::zeroMargins(aLblLayout);
237 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
239 myNbLbl = new QLabel("0", aLblWgt);
240 aLblLayout->addWidget(myNbLbl);
243 myShowBtn = new QCheckBox(tr("Show only"), this);
244 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
245 aLblLayout->addWidget(myShowBtn);
247 aMainLayout->addWidget(aLblWgt);
249 aMainLayout->addStretch(1);
254 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
257 if (!myPreview.IsNull()) {
258 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
259 aCtx->Remove(myPreview, false);
261 if (myShowBtn->isChecked()) {
262 AIS_ListOfInteractive::const_iterator aIt;
263 Handle(AIS_Shape) aShapeIO;
264 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
265 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
266 if (!aShapeIO.IsNull()) {
267 aCtx->Display(aShapeIO, false);
271 aCtx->UpdateCurrentViewer();
273 SelectorFeature = FeaturePtr();
277 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
282 std::list<std::string>::iterator aIt;
285 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
286 if (i == (theIndex - 1)) {
288 myFilters.erase(aIt);
292 if (!aFilter.empty()) {
293 myUseFilters.push_back(aFilter);
294 ModuleBase_FilterItem* aItem =
295 new ModuleBase_FilterItem(aFilter, mySelectorFeature, myFiltersWgt);
296 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
297 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
298 myFiltersLayout->addWidget(aItem);
300 FiltersFeaturePtr aFiltersFeature =
301 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
302 aFiltersFeature->addFilter(aFilter);
305 clearCurrentSelection(true);
306 updateNumberSelected();
307 myFiltersCombo->setCurrentIndex(0);
308 myFiltersCombo->removeItem(theIndex);
311 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
313 std::string aFilter = theItem->filter();
314 myFiltersLayout->removeWidget(theItem);
315 theItem->deleteLater();
317 myUseFilters.remove(aFilter);
318 myFilters.push_back(aFilter);
319 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
321 FiltersFeaturePtr aFiltersFeature =
322 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
323 aFiltersFeature->removeFilter(aFilter);
326 clearCurrentSelection(true);
327 updateNumberSelected();
330 void ModuleBase_WidgetSelectionFilter::onSelect()
332 if (myUseFilters.size() == 0)
334 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
338 clearCurrentSelection();
340 BRep_Builder aBuilder;
341 TopoDS_Compound aComp;
342 aBuilder.MakeCompound(aComp);
344 if (!myShowBtn->isChecked()) {
346 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
347 if (!myPreview.IsNull())
348 myListIO.Remove(myPreview);
350 AIS_ListOfInteractive::const_iterator aIt;
351 Handle(ModuleBase_ResultPrs) aShapeIO;
352 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
353 aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
354 if (!aShapeIO.IsNull()) {
355 GeomShapePtr aShape(new GeomAPI_Shape);
356 aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
357 std::list<GeomShapePtr> aSubShapes =
358 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
359 std::list<GeomShapePtr>::const_iterator aShapesIt;
360 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
361 GeomShapePtr aShape = (*aShapesIt);
362 SessionPtr aSession = ModelAPI_Session::get();
363 bool isValid = aSession->filters()->isValid(myFeature, aShape);
365 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
366 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
367 aBuilder.Add(aComp, aTShape);
369 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
370 myValues.append(aValue);
375 if (myValues.size() > 0)
376 updatePreview(aComp);
377 updateNumberSelected();
380 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
382 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
386 if (myPreview.IsNull()) {
387 myPreview = new AIS_Shape(theShape);
388 myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
389 myPreview->SetColor(Quantity_NOC_YELLOW);
390 myPreview->SetTransparency();
391 aCtx->Display(myPreview, true);
392 aCtx->Deactivate(myPreview);
395 myPreview->Set(theShape);
396 aCtx->Redisplay(myPreview, true);
401 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
403 if (myPreview.IsNull())
405 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
408 aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
410 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
411 myListIO.Remove(myPreview);
414 aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
416 AIS_ListOfInteractive::const_iterator aIt;
417 Handle(AIS_Shape) aShapeIO;
418 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
419 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
420 if (!aShapeIO.IsNull()) {
422 aCtx->Erase(aShapeIO, false);
424 aCtx->Display(aShapeIO, false);
427 aCtx->UpdateCurrentViewer();
430 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
432 mySelectBtn->setEnabled(myUseFilters.size() > 0);
435 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
437 myNbLbl->setText(QString::number(myValues.size()));
439 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
441 return QList<QWidget*>();
444 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
447 if (!myPreview.IsNull()) {
448 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
449 aCtx->Remove(myPreview, toUpdate);
454 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
456 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
457 AttributeSelectionListPtr aSelListAttr =
458 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
459 aSelListAttr->clear();
460 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
461 aSelListAttr->append(aPrs->object(), aPrs->shape());