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>
47 static FeaturePtr SelectorFeature;
48 static std::string AttributeId;
51 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
53 QString aType = theType.toUpper();
54 if ((aType == "VERTEX") || (aType == "VERTICES"))
55 return GeomAPI_Shape::VERTEX;
56 else if ((aType == "EDGE") || (aType == "EDGES"))
57 return GeomAPI_Shape::EDGE;
58 else if ((aType == "WIRE") || (aType == "WIRES"))
59 return GeomAPI_Shape::WIRE;
60 else if ((aType == "FACE") || (aType == "FACES"))
61 return GeomAPI_Shape::FACE;
62 else if ((aType == "SHELL") || (aType == "SHELLS"))
63 return GeomAPI_Shape::SHELL;
64 else if ((aType == "SOLID") || (aType == "SOLIDS"))
65 return GeomAPI_Shape::SOLID;
66 else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
67 return GeomAPI_Shape::COMPSOLID;
68 else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
69 return GeomAPI_Shape::COMPOUND;
71 return GeomAPI_Shape::SHAPE;
75 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
76 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
78 myFeatureName(theFeature),
79 myWorkshop(theWorkshop)
81 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
82 ModuleBase_Tools::adjustMargins(aMainLayout);
84 aMainLayout->addStretch(1);
85 QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
86 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
87 aMainLayout->addWidget(aLaunchBtn);
89 myFilterLbl = new QLabel(this);
90 myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
91 aMainLayout->addWidget(myFilterLbl);
93 myModifyLbl = new QLabel(this);
94 myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
95 aMainLayout->addWidget(myModifyLbl);
96 aMainLayout->addStretch(1);
102 void ModuleBase_FilterStarter::onFiltersLaunch()
104 ModuleBase_WidgetMultiSelector* aSelector =
105 dynamic_cast<ModuleBase_WidgetMultiSelector*>(parent());
106 SelectorFeature = aSelector->feature();
107 AttributeId = aSelector->attributeID();
109 // Launch Filters operation
110 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
111 (myWorkshop->module()->createOperation(myFeatureName));
112 myWorkshop->processLaunchOperation(aFOperation);
115 //*****************************************************************************
116 //*****************************************************************************
117 //*****************************************************************************
118 ModuleBase_FilterItem::ModuleBase_FilterItem(
119 const std::string& theFilter, FeaturePtr theSelection, QWidget* theParent)
120 : QWidget(theParent), myFilterID(theFilter),
121 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theSelection))
123 QHBoxLayout* aLayout = new QHBoxLayout(this);
124 aLayout->setContentsMargins(0, 0, 0, 0);
126 // Reverse filter button
127 myRevBtn = new QToolButton(this);
128 myRevBtn->setCheckable(true);
129 myRevBtn->setChecked(false);
130 myRevBtn->setAutoRaise(true);
131 myRevBtn->setIcon(QIcon(":pictures/accept.png"));
132 myRevBtn->setToolTip(tr("Reverse the filter"));
133 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
134 aLayout->addWidget(myRevBtn);
136 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
137 aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
139 QToolButton* aDelBtn = new QToolButton(this);
140 aDelBtn->setIcon(QIcon(":pictures/button_cancel.png"));
141 aDelBtn->setAutoRaise(true);
142 aDelBtn->setToolTip(tr("Delete the filter"));
143 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
144 aLayout->addWidget(aDelBtn);
146 myRevBtn->setChecked(mySelection->isReversed(myFilterID));
150 void ModuleBase_FilterItem::onReverse(bool theCheck)
152 mySelection->setReversed(myFilterID, theCheck);
154 myRevBtn->setIcon(QIcon(":pictures/stop.png"));
156 myRevBtn->setIcon(QIcon(":pictures/accept.png"));
159 void ModuleBase_FilterItem::onDelete()
161 emit deleteItem(this);
165 //*****************************************************************************
166 //*****************************************************************************
167 //*****************************************************************************
168 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
169 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
170 : ModuleBase_ModelWidget(theParent, theData),
171 myWorkshop(theWorkshop),
172 mySelectorFeature(SelectorFeature),
173 mySelectorAttribute(AttributeId)
175 // Clear Old selection
176 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
177 AttributeSelectionListPtr aSelListAttr =
178 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
179 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
180 aSelListAttr->clear();
183 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
184 ModuleBase_Tools::adjustMargins(aMainLayout);
186 myFiltersGroup = new QGroupBox(tr("Dynamic Filters"), this);
187 myGroupLayout = new QVBoxLayout(myFiltersGroup);
188 myGroupLayout->setContentsMargins(0, 0, 0, 0);
189 myGroupLayout->setSpacing(0);
191 QWidget* aFiltersWgt = new QWidget(myFiltersGroup);
192 QHBoxLayout* aFiltersLay = new QHBoxLayout(aFiltersWgt);
193 ModuleBase_Tools::adjustMargins(aFiltersLay);
195 QLabel* aFilterLbl = new QLabel(aFiltersWgt);
196 aFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
198 myFiltersCombo = new QComboBox(aFiltersWgt);
199 SessionPtr aSession = ModelAPI_Session::get();
200 std::list<FilterPtr> allFilters =
201 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
203 std::list<FilterPtr>::const_iterator aIt;
204 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
205 aItems.push_back((*aIt)->name().c_str());
206 myFilters.push_back(aSession->filters()->id(*aIt));
208 myFiltersCombo->addItems(aItems);
210 QToolButton* aAddBtn = new QToolButton(aFiltersWgt);
211 aAddBtn->setIcon(QIcon(":pictures/add.png"));
212 aAddBtn->setAutoRaise(true);
213 aAddBtn->setToolTip(tr("Add the current filter"));
214 connect(aAddBtn, SIGNAL(clicked()), SLOT(onAddItem()));
216 aFiltersLay->addWidget(aFilterLbl);
217 aFiltersLay->addWidget(myFiltersCombo, 1);
218 aFiltersLay->addWidget(aAddBtn);
220 myGroupLayout->addWidget(aFiltersWgt);
222 aMainLayout->addWidget(myFiltersGroup);
225 QWidget* aBtnWgt = new QWidget(this);
226 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
227 ModuleBase_Tools::adjustMargins(aBtnLayout);
229 aBtnLayout->addStretch(1);
231 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
232 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
233 aBtnLayout->addWidget(mySelectBtn);
235 aMainLayout->addWidget(aBtnWgt);
238 QWidget* aLblWgt = new QWidget(this);
239 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
240 ModuleBase_Tools::adjustMargins(aLblLayout);
242 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
244 myNbLbl = new QLabel("0", aLblWgt);
245 aLblLayout->addWidget(myNbLbl);
247 aMainLayout->addWidget(aLblWgt);
250 QWidget* aBtn2Wgt = new QWidget(this);
251 QHBoxLayout* aBtn2Layout = new QHBoxLayout(aBtn2Wgt);
252 ModuleBase_Tools::adjustMargins(aBtn2Layout);
254 aBtn2Layout->addStretch(1);
256 myShowBtn = new QPushButton(tr("Show only"), aBtn2Wgt);
257 myShowBtn->setCheckable(true);
258 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
259 aBtn2Layout->addWidget(myShowBtn);
261 aMainLayout->addWidget(aBtn2Wgt);
263 aMainLayout->addStretch(1);
268 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
271 if (!myPreview.IsNull()) {
272 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
273 aCtx->Remove(myPreview, false);
275 if (myShowBtn->isChecked()) {
276 AIS_ListOfInteractive::const_iterator aIt;
277 Handle(AIS_Shape) aShapeIO;
278 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
279 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
280 if (!aShapeIO.IsNull()) {
281 aCtx->Display(aShapeIO, false);
285 aCtx->UpdateCurrentViewer();
287 SelectorFeature = FeaturePtr();
291 void ModuleBase_WidgetSelectionFilter::onAddItem()
293 int aId = myFiltersCombo->currentIndex();
294 myFiltersCombo->removeItem(aId);
296 std::list<std::string>::iterator aIt;
299 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
302 myFilters.erase(aIt);
306 if (!aFilter.empty()) {
307 myUseFilters.push_back(aFilter);
308 ModuleBase_FilterItem* aItem =
309 new ModuleBase_FilterItem(aFilter, mySelectorFeature, myFiltersGroup);
310 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
311 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
312 myGroupLayout->addWidget(aItem);
315 clearCurrentSelection(true);
316 updateNumberSelected();
319 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
321 std::string aFilter = theItem->filter();
322 myGroupLayout->removeWidget(theItem);
323 theItem->deleteLater();
325 myUseFilters.remove(aFilter);
326 myFilters.push_back(aFilter);
327 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
330 clearCurrentSelection(true);
331 updateNumberSelected();
334 void ModuleBase_WidgetSelectionFilter::onSelect()
336 if (myUseFilters.size() == 0)
338 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
342 clearCurrentSelection();
344 BRep_Builder aBuilder;
345 TopoDS_Compound aComp;
346 aBuilder.MakeCompound(aComp);
348 if (!myShowBtn->isChecked()) {
350 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
351 if (!myPreview.IsNull())
352 myListIO.Remove(myPreview);
354 AIS_ListOfInteractive::const_iterator aIt;
355 Handle(ModuleBase_ResultPrs) aShapeIO;
356 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
357 aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
358 if (!aShapeIO.IsNull()) {
359 GeomShapePtr aShape(new GeomAPI_Shape);
360 aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
361 std::list<GeomShapePtr> aSubShapes =
362 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
363 std::list<GeomShapePtr>::const_iterator aShapesIt;
364 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
365 GeomShapePtr aShape = (*aShapesIt);
366 SessionPtr aSession = ModelAPI_Session::get();
367 bool isValid = aSession->filters()->isValid(myFeature, aShape);
369 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
370 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
371 aBuilder.Add(aComp, aTShape);
373 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
374 myValues.append(aValue);
379 if (myValues.size() > 0)
380 updatePreview(aComp);
381 updateNumberSelected();
384 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
386 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
390 if (myPreview.IsNull()) {
391 myPreview = new AIS_Shape(theShape);
392 myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
393 myPreview->SetColor(Quantity_NOC_YELLOW);
394 myPreview->SetTransparency();
395 aCtx->Display(myPreview, true);
396 aCtx->Deactivate(myPreview);
399 myPreview->Set(theShape);
400 aCtx->Redisplay(myPreview, true);
405 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theErase)
407 if (myPreview.IsNull())
409 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
412 aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
414 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
415 myListIO.Remove(myPreview);
418 aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
420 AIS_ListOfInteractive::const_iterator aIt;
421 Handle(AIS_Shape) aShapeIO;
422 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
423 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
424 if (!aShapeIO.IsNull()) {
426 aCtx->Erase(aShapeIO, false);
428 aCtx->Display(aShapeIO, false);
431 aCtx->UpdateCurrentViewer();
434 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
436 mySelectBtn->setEnabled(myUseFilters.size() > 0);
439 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
441 myNbLbl->setText(QString::number(myValues.size()));
443 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
445 return QList<QWidget*>();
448 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
451 if (!myPreview.IsNull()) {
452 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
453 aCtx->Remove(myPreview, toUpdate);
458 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
460 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
461 AttributeSelectionListPtr aSelListAttr =
462 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
463 aSelListAttr->clear();
464 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
465 aSelListAttr->append(aPrs->object(), aPrs->shape());