]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelectionFilter.cpp
Salome HOME
Update of GUI
[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   QHBoxLayout* aLayout = new QHBoxLayout(this);
132   ModuleBase_Tools::zeroMargins(aLayout);
133
134   // Reverse filter button
135   myRevBtn = new QToolButton(this);
136   myRevBtn->setCheckable(true);
137   myRevBtn->setChecked(false);
138   myRevBtn->setAutoRaise(true);
139   myRevBtn->setIcon(QIcon(":pictures/add.png"));
140   myRevBtn->setToolTip(tr("Reverse the filter"));
141   connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
142   aLayout->addWidget(myRevBtn);
143
144   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
145   aLayout->addWidget(new QLabel(aFilterName.c_str(), this), 1);
146
147   QToolButton* aDelBtn = new QToolButton(this);
148   aDelBtn->setIcon(QIcon(":pictures/delete.png"));
149   aDelBtn->setAutoRaise(true);
150   aDelBtn->setToolTip(tr("Delete the filter"));
151   connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
152   aLayout->addWidget(aDelBtn);
153 }
154
155
156 void ModuleBase_FilterItem::onReverse(bool theCheck)
157 {
158   //mySelection->setReversed(myFilterID, theCheck);
159   if (theCheck)
160     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
161   else
162     myRevBtn->setIcon(QIcon(":pictures/add.png"));
163 }
164
165 void ModuleBase_FilterItem::onDelete()
166 {
167   emit deleteItem(this);
168 }
169
170
171 //*****************************************************************************
172 //*****************************************************************************
173 //*****************************************************************************
174 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
175   ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
176   : ModuleBase_ModelWidget(theParent, theData),
177   myWorkshop(theWorkshop),
178   mySelectorFeature(SelectorFeature),
179   mySelectorAttribute(AttributeId)
180 {
181   // Clear Old selection
182   AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
183   AttributeSelectionListPtr aSelListAttr =
184     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
185   mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
186   aSelListAttr->clear();
187
188   // Define widgets
189   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
190   ModuleBase_Tools::adjustMargins(aMainLayout);
191
192   QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
193   QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
194   aGroupLayout->setContentsMargins(0, 0, 0, 0);
195   aGroupLayout->setSpacing(0);
196
197   myFiltersWgt = new QWidget();
198   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
199   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
200   aGroupLayout->addWidget(myFiltersWgt);
201
202   myFiltersCombo = new QComboBox(aFiltersGroup);
203   myFiltersCombo->addItem(tr("Add new filter..."));
204   SessionPtr aSession = ModelAPI_Session::get();
205   std::list<FilterPtr> allFilters =
206     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
207   QStringList aItems;
208   std::list<FilterPtr>::const_iterator aIt;
209   for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
210     aItems.push_back((*aIt)->name().c_str());
211     myFilters.push_back(aSession->filters()->id(*aIt));
212   }
213   myFiltersCombo->addItems(aItems);
214   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
215
216   aGroupLayout->addWidget(myFiltersCombo);
217   aMainLayout->addWidget(aFiltersGroup);
218
219   // Select Button
220   QWidget* aBtnWgt = new QWidget(this);
221   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
222   ModuleBase_Tools::adjustMargins(aBtnLayout);
223
224   aBtnLayout->addStretch(1);
225
226   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
227   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
228   aBtnLayout->addWidget(mySelectBtn);
229
230   aMainLayout->addWidget(aBtnWgt);
231
232   // Label widgets
233   QWidget* aLblWgt = new QWidget(this);
234   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
235   ModuleBase_Tools::zeroMargins(aLblLayout);
236
237   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
238
239   myNbLbl = new QLabel("0", aLblWgt);
240   aLblLayout->addWidget(myNbLbl);
241
242   // Show only button
243   myShowBtn = new QCheckBox(tr("Show only"), this);
244   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
245   aLblLayout->addWidget(myShowBtn);
246
247   aMainLayout->addWidget(aLblWgt);
248
249   aMainLayout->addStretch(1);
250
251   updateSelectBtn();
252 }
253
254 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
255 {
256   myValues.clear();
257   if (!myPreview.IsNull()) {
258     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
259     aCtx->Remove(myPreview, false);
260     myPreview.Nullify();
261     if (myShowBtn->isChecked()) {
262       AIS_ListOfInteractive::const_iterator aIt;
263       Handle(AIS_Shape) aShapeIO;
264       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
265         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
266         if (!aShapeIO.IsNull()) {
267           aCtx->Display(aShapeIO, false);
268         }
269       }
270     }
271     aCtx->UpdateCurrentViewer();
272   }
273   SelectorFeature = FeaturePtr();
274   AttributeId = "";
275 }
276
277 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
278 {
279   if (theIndex == 0)
280     return;
281
282   std::list<std::string>::iterator aIt;
283   int i;
284   std::string aFilter;
285   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
286     if (i == (theIndex - 1)) {
287       aFilter = (*aIt);
288       myFilters.erase(aIt);
289       break;
290     }
291   }
292   if (!aFilter.empty()) {
293     myUseFilters.push_back(aFilter);
294     ModuleBase_FilterItem* aItem =
295       new ModuleBase_FilterItem(aFilter, mySelectorFeature, myFiltersWgt);
296     connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
297       SLOT(onDeleteItem(ModuleBase_FilterItem*)));
298     myFiltersLayout->addWidget(aItem);
299
300     FiltersFeaturePtr aFiltersFeature =
301       std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
302     aFiltersFeature->addFilter(aFilter);
303   }
304   updateSelectBtn();
305   clearCurrentSelection(true);
306   updateNumberSelected();
307   myFiltersCombo->setCurrentIndex(0);
308   myFiltersCombo->removeItem(theIndex);
309 }
310
311 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
312 {
313   std::string aFilter = theItem->filter();
314   myFiltersLayout->removeWidget(theItem);
315   theItem->deleteLater();
316
317   myUseFilters.remove(aFilter);
318   myFilters.push_back(aFilter);
319   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
320
321   FiltersFeaturePtr aFiltersFeature =
322     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
323   aFiltersFeature->removeFilter(aFilter);
324
325   updateSelectBtn();
326   clearCurrentSelection(true);
327   updateNumberSelected();
328 }
329
330 void ModuleBase_WidgetSelectionFilter::onSelect()
331 {
332   if (myUseFilters.size() == 0)
333     return;
334   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
335   if (aCtx.IsNull())
336     return;
337
338   clearCurrentSelection();
339
340   BRep_Builder aBuilder;
341   TopoDS_Compound aComp;
342   aBuilder.MakeCompound(aComp);
343
344   if (!myShowBtn->isChecked()) {
345     myListIO.Clear();
346     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
347     if (!myPreview.IsNull())
348       myListIO.Remove(myPreview);
349   }
350   AIS_ListOfInteractive::const_iterator aIt;
351   Handle(ModuleBase_ResultPrs) aShapeIO;
352   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
353     aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
354     if (!aShapeIO.IsNull()) {
355       GeomShapePtr aShape(new GeomAPI_Shape);
356       aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
357       std::list<GeomShapePtr> aSubShapes =
358         aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
359       std::list<GeomShapePtr>::const_iterator aShapesIt;
360       for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
361         GeomShapePtr aShape = (*aShapesIt);
362         SessionPtr aSession = ModelAPI_Session::get();
363         bool isValid = aSession->filters()->isValid(myFeature, aShape);
364         if (isValid) {
365           TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
366           Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
367           aBuilder.Add(aComp, aTShape);
368
369           ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
370           myValues.append(aValue);
371         }
372       }
373     }
374   }
375   if (myValues.size() > 0)
376     updatePreview(aComp);
377   updateNumberSelected();
378 }
379
380 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
381 {
382   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
383   if (aCtx.IsNull())
384     return;
385
386   if (myPreview.IsNull()) {
387     myPreview = new AIS_Shape(theShape);
388     myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
389     myPreview->SetColor(Quantity_NOC_YELLOW);
390     myPreview->SetTransparency();
391     aCtx->Display(myPreview, true);
392     aCtx->Deactivate(myPreview);
393   }
394   else {
395     myPreview->Set(theShape);
396     aCtx->Redisplay(myPreview, true);
397   }
398 }
399
400
401 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
402 {
403   if (myPreview.IsNull())
404     return;
405   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
406
407   if (theShow) {
408     aCtx->SetDisplayMode(myPreview, AIS_Shaded, false);
409     myListIO.Clear();
410     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
411     myListIO.Remove(myPreview);
412   }
413   else {
414     aCtx->SetDisplayMode(myPreview, AIS_WireFrame, false);
415   }
416   AIS_ListOfInteractive::const_iterator aIt;
417   Handle(AIS_Shape) aShapeIO;
418   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
419     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
420     if (!aShapeIO.IsNull()) {
421       if (theShow)
422         aCtx->Erase(aShapeIO, false);
423       else
424         aCtx->Display(aShapeIO, false);
425     }
426   }
427   aCtx->UpdateCurrentViewer();
428 }
429
430 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
431 {
432   mySelectBtn->setEnabled(myUseFilters.size() > 0);
433 }
434
435 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
436 {
437   myNbLbl->setText(QString::number(myValues.size()));
438 }
439 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
440 {
441   return QList<QWidget*>();
442 }
443
444 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
445 {
446   myValues.clear();
447   if (!myPreview.IsNull()) {
448     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
449     aCtx->Remove(myPreview, toUpdate);
450     myPreview.Nullify();
451   }
452 }
453
454 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
455 {
456   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
457   AttributeSelectionListPtr aSelListAttr =
458     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
459   aSelListAttr->clear();
460   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
461     aSelListAttr->append(aPrs->object(), aPrs->shape());
462   }
463 }