]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelectionFilter.cpp
Salome HOME
Add BelongsTo filter prototipe
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelectionFilter.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
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
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <AIS_InteractiveContext.hxx>
35 #include <StdSelect_BRepOwner.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <BRep_Builder.hxx>
38
39 #include <QLayout>
40 #include <QPushButton>
41 #include <QLabel>
42 #include <QComboBox>
43 #include <QGroupBox>
44 #include <QDialog>
45 #include <QToolButton>
46 #include <QCheckBox>
47
48 static FeaturePtr SelectorFeature;
49 static std::string AttributeId;
50
51
52 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
53 {
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;
71   else
72     return GeomAPI_Shape::SHAPE;
73 }
74
75
76 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
77   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
78   : QWidget(theParent),
79   myFeatureName(theFeature),
80   myWorkshop(theWorkshop)
81 {
82   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
83   ModuleBase_Tools::adjustMargins(aMainLayout);
84
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);
89
90   myFilterLbl = new QLabel(this);
91   myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
92   aMainLayout->addWidget(myFilterLbl);
93
94   myModifyLbl = new QLabel(this);
95   myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
96   aMainLayout->addWidget(myModifyLbl);
97   aMainLayout->addStretch(1);
98
99   myFilterLbl->hide();
100   myModifyLbl->hide();
101 }
102
103 void ModuleBase_FilterStarter::onFiltersLaunch()
104 {
105   QWidget* aParent = parentWidget();
106   ModuleBase_WidgetMultiSelector* aSelector =
107     dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
108   while (!aSelector) {
109     aParent = aParent->parentWidget();
110     aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
111   }
112   if (!aSelector)
113     return;
114   SelectorFeature = aSelector->feature();
115   AttributeId = aSelector->attributeID();
116
117   // Launch Filters operation
118   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
119     (myWorkshop->module()->createOperation(myFeatureName));
120   myWorkshop->processLaunchOperation(aFOperation);
121 }
122
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))
130 {
131   std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
132
133   QHBoxLayout* aLayout = new QHBoxLayout(this);
134   ModuleBase_Tools::zeroMargins(aLayout);
135
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);
145
146   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
147   aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
148
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);
155 }
156
157
158 void ModuleBase_FilterItem::onReverse(bool theCheck)
159 {
160   mySelection->setReversed(myFilterID, theCheck);
161   if (theCheck)
162     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
163   else
164     myRevBtn->setIcon(QIcon(":pictures/add.png"));
165   emit reversedItem(this);
166 }
167
168 void ModuleBase_FilterItem::onDelete()
169 {
170   emit deleteItem(this);
171 }
172
173
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)
183 {
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();
190
191   // Define widgets
192   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
193   ModuleBase_Tools::adjustMargins(aMainLayout);
194
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);
199
200   myFiltersWgt = new QWidget();
201   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
202   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
203   aGroupLayout->addWidget(myFiltersWgt);
204
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);
210   QStringList aItems;
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));
215   }
216   myFiltersCombo->addItems(aItems);
217   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
218
219   aGroupLayout->addWidget(myFiltersCombo);
220   aMainLayout->addWidget(aFiltersGroup);
221
222   // Select Button
223   QWidget* aBtnWgt = new QWidget(this);
224   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
225   ModuleBase_Tools::adjustMargins(aBtnLayout);
226
227   aBtnLayout->addStretch(1);
228
229   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
230   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
231   aBtnLayout->addWidget(mySelectBtn);
232
233   aMainLayout->addWidget(aBtnWgt);
234
235   // Label widgets
236   QWidget* aLblWgt = new QWidget(this);
237   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
238   ModuleBase_Tools::zeroMargins(aLblLayout);
239
240   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
241
242   myNbLbl = new QLabel("0", aLblWgt);
243   aLblLayout->addWidget(myNbLbl);
244
245   // Show only button
246   myShowBtn = new QCheckBox(tr("Show only"), this);
247   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
248   aLblLayout->addWidget(myShowBtn);
249
250   aMainLayout->addWidget(aLblWgt);
251
252   aMainLayout->addStretch(1);
253
254   updateSelectBtn();
255 }
256
257 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
258 {
259   myValues.clear();
260   if (!myPreview.IsNull()) {
261     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
262     aCtx->Remove(myPreview, false);
263     myPreview.Nullify();
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);
271         }
272       }
273     }
274     aCtx->UpdateCurrentViewer();
275   }
276   SelectorFeature = FeaturePtr();
277   AttributeId = "";
278 }
279
280 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
281 {
282   if (theIndex == 0)
283     return;
284
285   std::list<std::string>::iterator aIt;
286   int i;
287   std::string aFilter;
288   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
289     if (i == (theIndex - 1)) {
290       aFilter = (*aIt);
291       myFilters.erase(aIt);
292       break;
293     }
294   }
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);
304
305     FiltersFeaturePtr aFiltersFeature =
306       std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
307     aFiltersFeature->addFilter(aFilter);
308   }
309   updateSelectBtn();
310   clearCurrentSelection(true);
311   updateNumberSelected();
312   myFiltersCombo->setCurrentIndex(0);
313   myFiltersCombo->removeItem(theIndex);
314 }
315
316 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
317 {
318   std::string aFilter = theItem->filter();
319   myFiltersLayout->removeWidget(theItem);
320   theItem->deleteLater();
321
322   myUseFilters.remove(aFilter);
323   myFilters.push_back(aFilter);
324   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
325
326   FiltersFeaturePtr aFiltersFeature =
327     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
328   aFiltersFeature->removeFilter(aFilter);
329
330   updateSelectBtn();
331   clearCurrentSelection(true);
332   updateNumberSelected();
333 }
334
335 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
336 {
337   updateSelectBtn();
338   clearCurrentSelection(true);
339   updateNumberSelected();
340 }
341
342 void ModuleBase_WidgetSelectionFilter::onSelect()
343 {
344   if (myUseFilters.size() == 0)
345     return;
346   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
347   if (aCtx.IsNull())
348     return;
349
350   clearCurrentSelection();
351
352   BRep_Builder aBuilder;
353   TopoDS_Compound aComp;
354   aBuilder.MakeCompound(aComp);
355
356   if (!myShowBtn->isChecked()) {
357     myListIO.Clear();
358     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
359     if (!myPreview.IsNull())
360       myListIO.Remove(myPreview);
361   }
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);
376         if (isValid) {
377           TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
378           Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
379           aBuilder.Add(aComp, aTShape);
380
381           ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
382           myValues.append(aValue);
383         }
384       }
385     }
386   }
387   if (myValues.size() > 0)
388     updatePreview(aComp);
389   updateNumberSelected();
390 }
391
392 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
393 {
394   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
395   if (aCtx.IsNull())
396     return;
397
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);
405   }
406   else {
407     myPreview->Set(theShape);
408     aCtx->Redisplay(myPreview, true);
409   }
410 }
411
412
413 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
414 {
415   if (myPreview.IsNull())
416     return;
417   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
418
419   if (theShow) {
420     aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
421     myListIO.Clear();
422     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
423     myListIO.Remove(myPreview);
424   }
425   else {
426     aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
427   }
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()) {
433       if (theShow)
434         aCtx->Erase(aShapeIO, false);
435       else
436         aCtx->Display(aShapeIO, false);
437     }
438   }
439   aCtx->UpdateCurrentViewer();
440 }
441
442 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
443 {
444   mySelectBtn->setEnabled(myUseFilters.size() > 0);
445 }
446
447 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
448 {
449   myNbLbl->setText(QString::number(myValues.size()));
450 }
451 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
452 {
453   return QList<QWidget*>();
454 }
455
456 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
457 {
458   myValues.clear();
459   if (!myPreview.IsNull()) {
460     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
461     aCtx->Remove(myPreview, toUpdate);
462     myPreview.Nullify();
463   }
464 }
465
466 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
467 {
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());
474   }
475 }