Salome HOME
bccb9032b8a1f90bda986ca375bd0356625f64c5
[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 #include "ModuleBase_WidgetFactory.h"
30
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <GeomAPI_ShapeExplorer.h>
34
35 #include <AIS_InteractiveContext.hxx>
36 #include <StdSelect_BRepOwner.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <BRep_Builder.hxx>
39
40 #include <QLayout>
41 #include <QPushButton>
42 #include <QLabel>
43 #include <QComboBox>
44 #include <QGroupBox>
45 #include <QDialog>
46 #include <QToolButton>
47 #include <QCheckBox>
48
49 static FeaturePtr SelectorFeature;
50 static std::string AttributeId;
51
52
53 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
54 {
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;
72   else
73     return GeomAPI_Shape::SHAPE;
74 }
75
76
77 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
78   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
79   : QWidget(theParent),
80   myFeatureName(theFeature),
81   myWorkshop(theWorkshop)
82 {
83   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
84   ModuleBase_Tools::adjustMargins(aMainLayout);
85
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);
90
91   myFilterLbl = new QLabel(this);
92   myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
93   aMainLayout->addWidget(myFilterLbl);
94
95   myModifyLbl = new QLabel(this);
96   myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
97   aMainLayout->addWidget(myModifyLbl);
98   aMainLayout->addStretch(1);
99
100   myFilterLbl->hide();
101   myModifyLbl->hide();
102 }
103
104 void ModuleBase_FilterStarter::onFiltersLaunch()
105 {
106   QWidget* aParent = parentWidget();
107   ModuleBase_WidgetMultiSelector* aSelector =
108     dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
109   while (!aSelector) {
110     aParent = aParent->parentWidget();
111     aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
112   }
113   if (!aSelector)
114     return;
115   SelectorFeature = aSelector->feature();
116   AttributeId = aSelector->attributeID();
117
118   // Launch Filters operation
119   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
120     (myWorkshop->module()->createOperation(myFeatureName));
121   myWorkshop->processLaunchOperation(aFOperation);
122 }
123
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()))
131 {
132   std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
133   if (aXmlString.length() == 0)
134     addItemRow(this);
135   else {
136     ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
137     QVBoxLayout* aLayout = new QVBoxLayout(this);
138     ModuleBase_Tools::zeroMargins(aLayout);
139
140     QWidget* aItemRow = new QWidget(this);
141     addItemRow(aItemRow);
142     aLayout->addWidget(aItemRow);
143
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());
150     }
151     aLayout->addWidget(aParamsWgt);
152   }
153 }
154
155 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
156 {
157   QHBoxLayout* aLayout = new QHBoxLayout(theParent);
158   ModuleBase_Tools::zeroMargins(aLayout);
159
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);
169
170   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
171   aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
172
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);
179 }
180
181 void ModuleBase_FilterItem::onReverse(bool theCheck)
182 {
183   mySelection->setReversed(myFilterID, theCheck);
184   if (theCheck)
185     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
186   else
187     myRevBtn->setIcon(QIcon(":pictures/add.png"));
188   emit reversedItem(this);
189 }
190
191 void ModuleBase_FilterItem::onDelete()
192 {
193   emit deleteItem(this);
194 }
195
196
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)
206 {
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();
213
214   // Define widgets
215   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
216   ModuleBase_Tools::adjustMargins(aMainLayout);
217
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);
222
223   myFiltersWgt = new QWidget();
224   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
225   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
226   aGroupLayout->addWidget(myFiltersWgt);
227
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);
233   QStringList aItems;
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));
238   }
239   myFiltersCombo->addItems(aItems);
240   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
241
242   aGroupLayout->addWidget(myFiltersCombo);
243   aMainLayout->addWidget(aFiltersGroup);
244
245   // Select Button
246   QWidget* aBtnWgt = new QWidget(this);
247   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
248   ModuleBase_Tools::adjustMargins(aBtnLayout);
249
250   aBtnLayout->addStretch(1);
251
252   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
253   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
254   aBtnLayout->addWidget(mySelectBtn);
255
256   aMainLayout->addWidget(aBtnWgt);
257
258   // Label widgets
259   QWidget* aLblWgt = new QWidget(this);
260   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
261   ModuleBase_Tools::zeroMargins(aLblLayout);
262
263   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
264
265   myNbLbl = new QLabel("0", aLblWgt);
266   aLblLayout->addWidget(myNbLbl);
267
268   // Show only button
269   myShowBtn = new QCheckBox(tr("Show only"), this);
270   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
271   aLblLayout->addWidget(myShowBtn);
272
273   aMainLayout->addWidget(aLblWgt);
274
275   aMainLayout->addStretch(1);
276
277   updateSelectBtn();
278 }
279
280 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
281 {
282   myValues.clear();
283   if (!myPreview.IsNull()) {
284     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
285     aCtx->Remove(myPreview, false);
286     myPreview.Nullify();
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);
294         }
295       }
296     }
297     aCtx->UpdateCurrentViewer();
298   }
299   SelectorFeature = FeaturePtr();
300   AttributeId = "";
301 }
302
303 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
304 {
305   if (theIndex == 0)
306     return;
307
308   std::list<std::string>::iterator aIt;
309   int i;
310   std::string aFilter;
311   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
312     if (i == (theIndex - 1)) {
313       aFilter = (*aIt);
314       myFilters.erase(aIt);
315       break;
316     }
317   }
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);
326
327     FiltersFeaturePtr aFiltersFeature =
328       std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
329     aFiltersFeature->addFilter(aFilter);
330   }
331   updateSelectBtn();
332   clearCurrentSelection(true);
333   updateNumberSelected();
334   myFiltersCombo->setCurrentIndex(0);
335   myFiltersCombo->removeItem(theIndex);
336 }
337
338 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
339 {
340   std::string aFilter = theItem->filter();
341   myFiltersLayout->removeWidget(theItem);
342   theItem->deleteLater();
343
344   myUseFilters.remove(aFilter);
345   myFilters.push_back(aFilter);
346   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
347
348   FiltersFeaturePtr aFiltersFeature =
349     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
350   aFiltersFeature->removeFilter(aFilter);
351
352   updateSelectBtn();
353   clearCurrentSelection(true);
354   updateNumberSelected();
355 }
356
357 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
358 {
359   updateSelectBtn();
360   clearCurrentSelection(true);
361   updateNumberSelected();
362 }
363
364 void ModuleBase_WidgetSelectionFilter::onSelect()
365 {
366   if (myUseFilters.size() == 0)
367     return;
368   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
369   if (aCtx.IsNull())
370     return;
371
372   clearCurrentSelection();
373
374   BRep_Builder aBuilder;
375   TopoDS_Compound aComp;
376   aBuilder.MakeCompound(aComp);
377
378   if (!myShowBtn->isChecked()) {
379     myListIO.Clear();
380     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
381     if (!myPreview.IsNull())
382       myListIO.Remove(myPreview);
383   }
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);
398         if (isValid) {
399           TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
400           Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
401           aBuilder.Add(aComp, aTShape);
402
403           ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
404           myValues.append(aValue);
405         }
406       }
407     }
408   }
409   if (myValues.size() > 0)
410     updatePreview(aComp);
411   updateNumberSelected();
412 }
413
414 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
415 {
416   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
417   if (aCtx.IsNull())
418     return;
419
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);
427   }
428   else {
429     myPreview->Set(theShape);
430     aCtx->Redisplay(myPreview, true);
431   }
432 }
433
434
435 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
436 {
437   if (myPreview.IsNull())
438     return;
439   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
440
441   if (theShow) {
442     aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
443     myListIO.Clear();
444     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
445     myListIO.Remove(myPreview);
446   }
447   else {
448     aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
449   }
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()) {
455       if (theShow)
456         aCtx->Erase(aShapeIO, false);
457       else
458         aCtx->Display(aShapeIO, false);
459     }
460   }
461   aCtx->UpdateCurrentViewer();
462 }
463
464 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
465 {
466   mySelectBtn->setEnabled(myUseFilters.size() > 0);
467 }
468
469 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
470 {
471   myNbLbl->setText(QString::number(myValues.size()));
472 }
473 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
474 {
475   return QList<QWidget*>();
476 }
477
478 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
479 {
480   myValues.clear();
481   if (!myPreview.IsNull()) {
482     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
483     aCtx->Remove(myPreview, toUpdate);
484     myPreview.Nullify();
485   }
486 }
487
488 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
489 {
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());
496   }
497 }