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 std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
133 QHBoxLayout* aLayout = new QHBoxLayout(this);
134 ModuleBase_Tools::zeroMargins(aLayout);
136 // Reverse filter button
137 myRevBtn = new QToolButton(this);
138 myRevBtn->setCheckable(true);
139 myRevBtn->setChecked(false);
140 myRevBtn->setAutoRaise(true);
141 myRevBtn->setIcon(QIcon(":pictures/add.png"));
142 myRevBtn->setToolTip(tr("Reverse the filter"));
143 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
144 aLayout->addWidget(myRevBtn);
146 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
147 aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
149 QToolButton* aDelBtn = new QToolButton(this);
150 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
151 aDelBtn->setAutoRaise(true);
152 aDelBtn->setToolTip(tr("Delete the filter"));
153 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
154 aLayout->addWidget(aDelBtn);
158 void ModuleBase_FilterItem::onReverse(bool theCheck)
160 mySelection->setReversed(myFilterID, theCheck);
162 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
164 myRevBtn->setIcon(QIcon(":pictures/add.png"));
165 emit reversedItem(this);
168 void ModuleBase_FilterItem::onDelete()
170 emit deleteItem(this);
174 //*****************************************************************************
175 //*****************************************************************************
176 //*****************************************************************************
177 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
178 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
179 : ModuleBase_ModelWidget(theParent, theData),
180 myWorkshop(theWorkshop),
181 mySelectorFeature(SelectorFeature),
182 mySelectorAttribute(AttributeId)
184 // Clear Old selection
185 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
186 AttributeSelectionListPtr aSelListAttr =
187 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
188 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
189 aSelListAttr->clear();
192 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
193 ModuleBase_Tools::adjustMargins(aMainLayout);
195 QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
196 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
197 aGroupLayout->setContentsMargins(0, 0, 0, 0);
198 aGroupLayout->setSpacing(0);
200 myFiltersWgt = new QWidget();
201 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
202 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
203 aGroupLayout->addWidget(myFiltersWgt);
205 myFiltersCombo = new QComboBox(aFiltersGroup);
206 myFiltersCombo->addItem(tr("Add new filter..."));
207 SessionPtr aSession = ModelAPI_Session::get();
208 std::list<FilterPtr> allFilters =
209 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
211 std::list<FilterPtr>::const_iterator aIt;
212 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
213 aItems.push_back((*aIt)->name().c_str());
214 myFilters.push_back(aSession->filters()->id(*aIt));
216 myFiltersCombo->addItems(aItems);
217 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
219 aGroupLayout->addWidget(myFiltersCombo);
220 aMainLayout->addWidget(aFiltersGroup);
223 QWidget* aBtnWgt = new QWidget(this);
224 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
225 ModuleBase_Tools::adjustMargins(aBtnLayout);
227 aBtnLayout->addStretch(1);
229 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
230 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
231 aBtnLayout->addWidget(mySelectBtn);
233 aMainLayout->addWidget(aBtnWgt);
236 QWidget* aLblWgt = new QWidget(this);
237 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
238 ModuleBase_Tools::zeroMargins(aLblLayout);
240 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
242 myNbLbl = new QLabel("0", aLblWgt);
243 aLblLayout->addWidget(myNbLbl);
246 myShowBtn = new QCheckBox(tr("Show only"), this);
247 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
248 aLblLayout->addWidget(myShowBtn);
250 aMainLayout->addWidget(aLblWgt);
252 aMainLayout->addStretch(1);
257 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
260 if (!myPreview.IsNull()) {
261 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
262 aCtx->Remove(myPreview, false);
264 if (myShowBtn->isChecked()) {
265 AIS_ListOfInteractive::const_iterator aIt;
266 Handle(AIS_Shape) aShapeIO;
267 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
268 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
269 if (!aShapeIO.IsNull()) {
270 aCtx->Display(aShapeIO, false);
274 aCtx->UpdateCurrentViewer();
276 SelectorFeature = FeaturePtr();
280 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
285 std::list<std::string>::iterator aIt;
288 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
289 if (i == (theIndex - 1)) {
291 myFilters.erase(aIt);
295 if (!aFilter.empty()) {
296 myUseFilters.push_back(aFilter);
297 ModuleBase_FilterItem* aItem =
298 new ModuleBase_FilterItem(aFilter, myFeature, myFiltersWgt);
299 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
300 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
301 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
302 SLOT(onReverseItem(ModuleBase_FilterItem*)));
303 myFiltersLayout->addWidget(aItem);
305 FiltersFeaturePtr aFiltersFeature =
306 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
307 aFiltersFeature->addFilter(aFilter);
310 clearCurrentSelection(true);
311 updateNumberSelected();
312 myFiltersCombo->setCurrentIndex(0);
313 myFiltersCombo->removeItem(theIndex);
316 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
318 std::string aFilter = theItem->filter();
319 myFiltersLayout->removeWidget(theItem);
320 theItem->deleteLater();
322 myUseFilters.remove(aFilter);
323 myFilters.push_back(aFilter);
324 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
326 FiltersFeaturePtr aFiltersFeature =
327 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
328 aFiltersFeature->removeFilter(aFilter);
331 clearCurrentSelection(true);
332 updateNumberSelected();
335 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
338 clearCurrentSelection(true);
339 updateNumberSelected();
342 void ModuleBase_WidgetSelectionFilter::onSelect()
344 if (myUseFilters.size() == 0)
346 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
350 clearCurrentSelection();
352 BRep_Builder aBuilder;
353 TopoDS_Compound aComp;
354 aBuilder.MakeCompound(aComp);
356 if (!myShowBtn->isChecked()) {
358 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
359 if (!myPreview.IsNull())
360 myListIO.Remove(myPreview);
362 AIS_ListOfInteractive::const_iterator aIt;
363 Handle(ModuleBase_ResultPrs) aShapeIO;
364 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
365 aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
366 if (!aShapeIO.IsNull()) {
367 GeomShapePtr aShape(new GeomAPI_Shape);
368 aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
369 std::list<GeomShapePtr> aSubShapes =
370 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
371 std::list<GeomShapePtr>::const_iterator aShapesIt;
372 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
373 GeomShapePtr aShape = (*aShapesIt);
374 SessionPtr aSession = ModelAPI_Session::get();
375 bool isValid = aSession->filters()->isValid(myFeature, aShape);
377 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
378 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
379 aBuilder.Add(aComp, aTShape);
381 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
382 myValues.append(aValue);
387 if (myValues.size() > 0)
388 updatePreview(aComp);
389 updateNumberSelected();
392 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
394 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
398 if (myPreview.IsNull()) {
399 myPreview = new AIS_Shape(theShape);
400 myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
401 myPreview->SetColor(Quantity_NOC_YELLOW);
402 myPreview->SetTransparency();
403 aCtx->Display(myPreview, true);
404 aCtx->Deactivate(myPreview);
407 myPreview->Set(theShape);
408 aCtx->Redisplay(myPreview, true);
413 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
415 if (myPreview.IsNull())
417 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
420 aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
422 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
423 myListIO.Remove(myPreview);
426 aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
428 AIS_ListOfInteractive::const_iterator aIt;
429 Handle(AIS_Shape) aShapeIO;
430 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
431 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
432 if (!aShapeIO.IsNull()) {
434 aCtx->Erase(aShapeIO, false);
436 aCtx->Display(aShapeIO, false);
439 aCtx->UpdateCurrentViewer();
442 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
444 mySelectBtn->setEnabled(myUseFilters.size() > 0);
447 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
449 myNbLbl->setText(QString::number(myValues.size()));
451 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
453 return QList<QWidget*>();
456 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
459 if (!myPreview.IsNull()) {
460 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
461 aCtx->Remove(myPreview, toUpdate);
466 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
468 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
469 AttributeSelectionListPtr aSelListAttr =
470 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
471 aSelListAttr->clear();
472 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
473 aSelListAttr->append(aPrs->object(), aPrs->shape());