Salome HOME
5112ba68bcaed95001dbb34d3385a96acc3236ba
[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
47 static FeaturePtr SelectorFeature;
48 static std::string AttributeId;
49
50
51 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
52 {
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;
70   else
71     return GeomAPI_Shape::SHAPE;
72 }
73
74
75 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
76   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
77   : QWidget(theParent),
78   myFeatureName(theFeature),
79   myWorkshop(theWorkshop)
80 {
81   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
82   ModuleBase_Tools::adjustMargins(aMainLayout);
83
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);
88
89   myFilterLbl = new QLabel(this);
90   myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
91   aMainLayout->addWidget(myFilterLbl);
92
93   myModifyLbl = new QLabel(this);
94   myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
95   aMainLayout->addWidget(myModifyLbl);
96   aMainLayout->addStretch(1);
97
98   myFilterLbl->hide();
99   myModifyLbl->hide();
100 }
101
102 void ModuleBase_FilterStarter::onFiltersLaunch()
103 {
104   ModuleBase_WidgetMultiSelector* aSelector =
105     dynamic_cast<ModuleBase_WidgetMultiSelector*>(parent());
106   SelectorFeature = aSelector->feature();
107   AttributeId = aSelector->attributeID();
108
109   // Launch Filters operation
110   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
111     (myWorkshop->module()->createOperation(myFeatureName));
112   myWorkshop->processLaunchOperation(aFOperation);
113 }
114
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))
122 {
123   QHBoxLayout* aLayout = new QHBoxLayout(this);
124   aLayout->setContentsMargins(0, 0, 0, 0);
125
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);
135
136   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
137   aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
138
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);
145
146   myRevBtn->setChecked(mySelection->isReversed(myFilterID));
147 }
148
149
150 void ModuleBase_FilterItem::onReverse(bool theCheck)
151 {
152   mySelection->setReversed(myFilterID, theCheck);
153   if (theCheck)
154     myRevBtn->setIcon(QIcon(":pictures/stop.png"));
155   else
156     myRevBtn->setIcon(QIcon(":pictures/accept.png"));
157 }
158
159 void ModuleBase_FilterItem::onDelete()
160 {
161   emit deleteItem(this);
162 }
163
164
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)
174 {
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();
181
182   // Define widgets
183   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
184   ModuleBase_Tools::adjustMargins(aMainLayout);
185
186   myFiltersGroup = new QGroupBox(tr("Dynamic Filters"), this);
187   myGroupLayout = new QVBoxLayout(myFiltersGroup);
188   myGroupLayout->setContentsMargins(0, 0, 0, 0);
189   myGroupLayout->setSpacing(0);
190
191   QWidget* aFiltersWgt = new QWidget(myFiltersGroup);
192   QHBoxLayout* aFiltersLay = new QHBoxLayout(aFiltersWgt);
193   ModuleBase_Tools::adjustMargins(aFiltersLay);
194
195   QLabel* aFilterLbl = new QLabel(aFiltersWgt);
196   aFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
197
198   myFiltersCombo = new QComboBox(aFiltersWgt);
199   SessionPtr aSession = ModelAPI_Session::get();
200   std::list<FilterPtr> allFilters =
201     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
202   QStringList aItems;
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));
207   }
208   myFiltersCombo->addItems(aItems);
209
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()));
215
216   aFiltersLay->addWidget(aFilterLbl);
217   aFiltersLay->addWidget(myFiltersCombo, 1);
218   aFiltersLay->addWidget(aAddBtn);
219
220   myGroupLayout->addWidget(aFiltersWgt);
221
222   aMainLayout->addWidget(myFiltersGroup);
223
224   // Select Button
225   QWidget* aBtnWgt = new QWidget(this);
226   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
227   ModuleBase_Tools::adjustMargins(aBtnLayout);
228
229   aBtnLayout->addStretch(1);
230
231   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
232   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
233   aBtnLayout->addWidget(mySelectBtn);
234
235   aMainLayout->addWidget(aBtnWgt);
236
237   // Label widgets
238   QWidget* aLblWgt = new QWidget(this);
239   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
240   ModuleBase_Tools::adjustMargins(aLblLayout);
241
242   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
243
244   myNbLbl = new QLabel("0", aLblWgt);
245   aLblLayout->addWidget(myNbLbl);
246
247   aMainLayout->addWidget(aLblWgt);
248
249   // Show only button
250   QWidget* aBtn2Wgt = new QWidget(this);
251   QHBoxLayout* aBtn2Layout = new QHBoxLayout(aBtn2Wgt);
252   ModuleBase_Tools::adjustMargins(aBtn2Layout);
253
254   aBtn2Layout->addStretch(1);
255
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);
260
261   aMainLayout->addWidget(aBtn2Wgt);
262
263   aMainLayout->addStretch(1);
264
265   updateSelectBtn();
266 }
267
268 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
269 {
270   myValues.clear();
271   if (!myPreview.IsNull()) {
272     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
273     aCtx->Remove(myPreview, false);
274     myPreview.Nullify();
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);
282         }
283       }
284     }
285     aCtx->UpdateCurrentViewer();
286   }
287   SelectorFeature = FeaturePtr();
288   AttributeId = "";
289 }
290
291 void ModuleBase_WidgetSelectionFilter::onAddItem()
292 {
293   int aId = myFiltersCombo->currentIndex();
294   myFiltersCombo->removeItem(aId);
295
296   std::list<std::string>::iterator aIt;
297   int i;
298   std::string aFilter;
299   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
300     if (i == aId) {
301       aFilter = (*aIt);
302       myFilters.erase(aIt);
303       break;
304     }
305   }
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);
313   }
314   updateSelectBtn();
315   clearCurrentSelection(true);
316   updateNumberSelected();
317 }
318
319 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
320 {
321   std::string aFilter = theItem->filter();
322   myGroupLayout->removeWidget(theItem);
323   theItem->deleteLater();
324
325   myUseFilters.remove(aFilter);
326   myFilters.push_back(aFilter);
327   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
328
329   updateSelectBtn();
330   clearCurrentSelection(true);
331   updateNumberSelected();
332 }
333
334 void ModuleBase_WidgetSelectionFilter::onSelect()
335 {
336   if (myUseFilters.size() == 0)
337     return;
338   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
339   if (aCtx.IsNull())
340     return;
341
342   clearCurrentSelection();
343
344   BRep_Builder aBuilder;
345   TopoDS_Compound aComp;
346   aBuilder.MakeCompound(aComp);
347
348   if (!myShowBtn->isChecked()) {
349     myListIO.Clear();
350     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
351     if (!myPreview.IsNull())
352       myListIO.Remove(myPreview);
353   }
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);
368         if (isValid) {
369           TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
370           Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
371           aBuilder.Add(aComp, aTShape);
372
373           ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
374           myValues.append(aValue);
375         }
376       }
377     }
378   }
379   if (myValues.size() > 0)
380     updatePreview(aComp);
381   updateNumberSelected();
382 }
383
384 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
385 {
386   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
387   if (aCtx.IsNull())
388     return;
389
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);
397   }
398   else {
399     myPreview->Set(theShape);
400     aCtx->Redisplay(myPreview, true);
401   }
402 }
403
404
405 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theErase)
406 {
407   if (myPreview.IsNull())
408     return;
409   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
410
411   if (theErase) {
412     aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
413     myListIO.Clear();
414     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
415     myListIO.Remove(myPreview);
416   }
417   else {
418     aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
419   }
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()) {
425       if (theErase)
426         aCtx->Erase(aShapeIO, false);
427       else
428         aCtx->Display(aShapeIO, false);
429     }
430   }
431   aCtx->UpdateCurrentViewer();
432 }
433
434 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
435 {
436   mySelectBtn->setEnabled(myUseFilters.size() > 0);
437 }
438
439 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
440 {
441   myNbLbl->setText(QString::number(myValues.size()));
442 }
443 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
444 {
445   return QList<QWidget*>();
446 }
447
448 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
449 {
450   myValues.clear();
451   if (!myPreview.IsNull()) {
452     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
453     aCtx->Remove(myPreview, toUpdate);
454     myPreview.Nullify();
455   }
456 }
457
458 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
459 {
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());
466   }
467 }