Salome HOME
Create filter "Belongs To"
[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_ISelectionActivate.h"
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_IPropertyPanel.h"
27 #include "ModuleBase_PageWidget.h"
28 #include "ModuleBase_WidgetMultiSelector.h"
29 #include "ModuleBase_ResultPrs.h"
30 #include "ModuleBase_WidgetFactory.h"
31
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_Events.h>
35 #include <GeomAPI_ShapeExplorer.h>
36
37 #include <Events_Loop.h>
38
39 #include <AIS_InteractiveContext.hxx>
40 #include <StdSelect_BRepOwner.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <BRep_Builder.hxx>
43
44 #include <QLayout>
45 #include <QPushButton>
46 #include <QLabel>
47 #include <QComboBox>
48 #include <QGroupBox>
49 #include <QDialog>
50 #include <QToolButton>
51 #include <QCheckBox>
52
53 static FeaturePtr SelectorFeature;
54 static std::string AttributeId;
55
56
57 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
58 {
59   QString aType = theType.toUpper();
60   if ((aType == "VERTEX") || (aType == "VERTICES"))
61     return GeomAPI_Shape::VERTEX;
62   else if ((aType == "EDGE") || (aType == "EDGES"))
63     return GeomAPI_Shape::EDGE;
64   else if ((aType == "WIRE") || (aType == "WIRES"))
65     return GeomAPI_Shape::WIRE;
66   else if ((aType == "FACE") || (aType == "FACES"))
67     return GeomAPI_Shape::FACE;
68   else if ((aType == "SHELL") || (aType == "SHELLS"))
69     return GeomAPI_Shape::SHELL;
70   else if ((aType == "SOLID") || (aType == "SOLIDS"))
71     return GeomAPI_Shape::SOLID;
72   else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
73     return GeomAPI_Shape::COMPSOLID;
74   else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
75     return GeomAPI_Shape::COMPOUND;
76   else
77     return GeomAPI_Shape::SHAPE;
78 }
79
80
81 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
82   QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
83   : QWidget(theParent),
84   myFeatureName(theFeature),
85   myWorkshop(theWorkshop)
86 {
87   QHBoxLayout* aMainLayout = new QHBoxLayout(this);
88   ModuleBase_Tools::adjustMargins(aMainLayout);
89
90   aMainLayout->addStretch(1);
91   QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
92   connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
93   aMainLayout->addWidget(aLaunchBtn);
94
95   myFilterLbl = new QLabel(this);
96   myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
97   aMainLayout->addWidget(myFilterLbl);
98
99   myModifyLbl = new QLabel(this);
100   myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
101   aMainLayout->addWidget(myModifyLbl);
102   aMainLayout->addStretch(1);
103
104   myFilterLbl->hide();
105   myModifyLbl->hide();
106 }
107
108 void ModuleBase_FilterStarter::onFiltersLaunch()
109 {
110   QWidget* aParent = parentWidget();
111   ModuleBase_WidgetMultiSelector* aSelector =
112     dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
113   while (!aSelector) {
114     aParent = aParent->parentWidget();
115     aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
116   }
117   if (!aSelector)
118     return;
119   SelectorFeature = aSelector->feature();
120   AttributeId = aSelector->attributeID();
121
122   // Launch Filters operation
123   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124     (myWorkshop->module()->createOperation(myFeatureName));
125   myWorkshop->processLaunchOperation(aFOperation);
126 }
127
128 //*****************************************************************************
129 //*****************************************************************************
130 //*****************************************************************************
131 ModuleBase_FilterItem::ModuleBase_FilterItem(
132   const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
133   : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
134     mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
135 {
136   std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
137   if (aXmlString.length() == 0)
138     addItemRow(this);
139   else {
140     ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
141     QVBoxLayout* aLayout = new QVBoxLayout(this);
142     ModuleBase_Tools::zeroMargins(aLayout);
143
144     QWidget* aItemRow = new QWidget(this);
145     addItemRow(aItemRow);
146     aLayout->addWidget(aItemRow);
147
148     ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
149     aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
150     aFactory.createWidget(aParamsWgt);
151     ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
152     myWidgets = aFactory.getModelWidgets();
153     foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
154       aWidget->setFeature(theParent->feature());
155       connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
156         theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
157       connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
158         theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
159     }
160     aLayout->addWidget(aParamsWgt);
161   }
162 }
163
164 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
165 {
166   QHBoxLayout* aLayout = new QHBoxLayout(theParent);
167   ModuleBase_Tools::zeroMargins(aLayout);
168
169   // Reverse filter button
170   myRevBtn = new QToolButton(theParent);
171   myRevBtn->setCheckable(true);
172   myRevBtn->setChecked(false);
173   myRevBtn->setAutoRaise(true);
174   myRevBtn->setIcon(QIcon(":pictures/add.png"));
175   myRevBtn->setToolTip(tr("Reverse the filter"));
176   connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
177   aLayout->addWidget(myRevBtn);
178
179   const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
180   aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
181
182   QToolButton* aDelBtn = new QToolButton(theParent);
183   aDelBtn->setIcon(QIcon(":pictures/delete.png"));
184   aDelBtn->setAutoRaise(true);
185   aDelBtn->setToolTip(tr("Delete the filter"));
186   connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
187   aLayout->addWidget(aDelBtn);
188 }
189
190 void ModuleBase_FilterItem::onReverse(bool theCheck)
191 {
192   mySelection->setReversed(myFilterID, theCheck);
193   if (theCheck)
194     myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
195   else
196     myRevBtn->setIcon(QIcon(":pictures/add.png"));
197   emit reversedItem(this);
198 }
199
200 void ModuleBase_FilterItem::onDelete()
201 {
202   emit deleteItem(this);
203 }
204
205 QList<QWidget*>  ModuleBase_FilterItem::getControls() const
206 {
207   QList<QWidget*> aWidgetsList;
208   foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
209     QList<QWidget*> aSubList = aWgt->getControls();
210     foreach(QWidget* aSub, aSubList) {
211       aWidgetsList.append(aSub);
212     }
213   }
214   return aWidgetsList;
215 }
216
217
218
219 //*****************************************************************************
220 //*****************************************************************************
221 //*****************************************************************************
222 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
223   ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
224   : ModuleBase_ModelWidget(theParent, theData),
225   myWorkshop(theWorkshop),
226   mySelectorFeature(SelectorFeature),
227   mySelectorAttribute(AttributeId)
228 {
229   // Clear Old selection
230   AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
231   AttributeSelectionListPtr aSelListAttr =
232     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
233   mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
234   aSelListAttr->clear();
235
236   // Define widgets
237   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
238   ModuleBase_Tools::adjustMargins(aMainLayout);
239
240   QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
241   QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
242   aGroupLayout->setContentsMargins(0, 0, 0, 0);
243   aGroupLayout->setSpacing(0);
244
245   myFiltersWgt = new QWidget();
246   myFiltersLayout = new QVBoxLayout(myFiltersWgt);
247   myFiltersLayout->setContentsMargins(0, 0, 0, 0);
248   aGroupLayout->addWidget(myFiltersWgt);
249
250   myFiltersCombo = new QComboBox(aFiltersGroup);
251   myFiltersCombo->addItem(tr("Add new filter..."));
252   SessionPtr aSession = ModelAPI_Session::get();
253   std::list<FilterPtr> allFilters =
254     aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
255   QStringList aItems;
256   std::list<FilterPtr>::const_iterator aIt;
257   for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
258     aItems.push_back((*aIt)->name().c_str());
259     myFilters.push_back(aSession->filters()->id(*aIt));
260   }
261   myFiltersCombo->addItems(aItems);
262   connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
263
264   aGroupLayout->addWidget(myFiltersCombo);
265   aMainLayout->addWidget(aFiltersGroup);
266
267   // Select Button
268   QWidget* aBtnWgt = new QWidget(this);
269   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
270   ModuleBase_Tools::adjustMargins(aBtnLayout);
271
272   aBtnLayout->addStretch(1);
273
274   mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
275   connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
276   aBtnLayout->addWidget(mySelectBtn);
277
278   aMainLayout->addWidget(aBtnWgt);
279
280   // Label widgets
281   QWidget* aLblWgt = new QWidget(this);
282   QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
283   ModuleBase_Tools::zeroMargins(aLblLayout);
284
285   aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
286
287   myNbLbl = new QLabel("0", aLblWgt);
288   aLblLayout->addWidget(myNbLbl);
289
290   // Show only button
291   myShowBtn = new QCheckBox(tr("Show only"), this);
292   connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
293   aLblLayout->addWidget(myShowBtn);
294
295   aMainLayout->addWidget(aLblWgt);
296
297   aMainLayout->addStretch(1);
298
299   updateSelectBtn();
300 }
301
302 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
303 {
304   myValues.clear();
305   if (!myPreview.IsNull()) {
306     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
307     aCtx->Remove(myPreview, false);
308     myPreview.Nullify();
309     if (myShowBtn->isChecked()) {
310       AIS_ListOfInteractive::const_iterator aIt;
311       Handle(AIS_Shape) aShapeIO;
312       for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
313         aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
314         if (!aShapeIO.IsNull()) {
315           aCtx->Display(aShapeIO, false);
316         }
317       }
318     }
319     aCtx->UpdateCurrentViewer();
320   }
321   SelectorFeature = FeaturePtr();
322   AttributeId = "";
323 }
324
325 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
326 {
327   if (theIndex == 0)
328     return;
329
330   std::list<std::string>::iterator aIt;
331   int i;
332   std::string aFilter;
333   for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
334     if (i == (theIndex - 1)) {
335       aFilter = (*aIt);
336       myFilters.erase(aIt);
337       break;
338     }
339   }
340   if (!aFilter.empty()) {
341     myUseFilters.push_back(aFilter);
342     ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(aFilter, this);
343     connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
344       SLOT(onDeleteItem(ModuleBase_FilterItem*)));
345     connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
346       SLOT(onReverseItem(ModuleBase_FilterItem*)));
347     myFiltersLayout->addWidget(aItem);
348
349     FiltersFeaturePtr aFiltersFeature =
350       std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
351     aFiltersFeature->addFilter(aFilter);
352   }
353   updateSelectBtn();
354   clearCurrentSelection(true);
355   updateNumberSelected();
356   myFiltersCombo->setCurrentIndex(0);
357   myFiltersCombo->removeItem(theIndex);
358
359   enableFocusProcessing();
360 }
361
362 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
363 {
364   std::string aFilter = theItem->filter();
365   QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
366   foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
367     aWgt->deactivate();
368   }
369   myFiltersLayout->removeWidget(theItem);
370   theItem->deleteLater();
371
372   myUseFilters.remove(aFilter);
373   myFilters.push_back(aFilter);
374   myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
375
376   FiltersFeaturePtr aFiltersFeature =
377     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
378   aFiltersFeature->removeFilter(aFilter);
379
380   updateSelectBtn();
381   clearCurrentSelection(true);
382   updateNumberSelected();
383
384   enableFocusProcessing();
385   myWorkshop->deactivateCurrentSelector();
386   myWorkshop->selectionActivate()->updateSelectionModes();
387   myWorkshop->selectionActivate()->updateSelectionFilters();
388   redisplayFeature();
389 }
390
391
392 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
393 {
394   static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
395   ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
396   Events_Loop::loop()->flush(aDispEvent);
397 }
398
399 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
400 {
401   updateSelectBtn();
402   clearCurrentSelection(true);
403   updateNumberSelected();
404 }
405
406 void ModuleBase_WidgetSelectionFilter::onSelect()
407 {
408   if (myUseFilters.size() == 0)
409     return;
410   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
411   if (aCtx.IsNull())
412     return;
413
414   clearCurrentSelection();
415
416   BRep_Builder aBuilder;
417   TopoDS_Compound aComp;
418   aBuilder.MakeCompound(aComp);
419
420   if (!myShowBtn->isChecked()) {
421     myListIO.Clear();
422     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
423     if (!myPreview.IsNull())
424       myListIO.Remove(myPreview);
425   }
426   AIS_ListOfInteractive::const_iterator aIt;
427   Handle(ModuleBase_ResultPrs) aShapeIO;
428   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
429     aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
430     if (!aShapeIO.IsNull()) {
431       GeomShapePtr aShape(new GeomAPI_Shape);
432       aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
433       std::list<GeomShapePtr> aSubShapes =
434         aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
435       std::list<GeomShapePtr>::const_iterator aShapesIt;
436       for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
437         GeomShapePtr aShape = (*aShapesIt);
438         SessionPtr aSession = ModelAPI_Session::get();
439         bool isValid = aSession->filters()->isValid(myFeature, aShape);
440         if (isValid) {
441           TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
442           Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
443           aBuilder.Add(aComp, aTShape);
444
445           ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
446           myValues.append(aValue);
447         }
448       }
449     }
450   }
451   if (myValues.size() > 0)
452     updatePreview(aComp);
453   updateNumberSelected();
454 }
455
456 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
457 {
458   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
459   if (aCtx.IsNull())
460     return;
461
462   if (myPreview.IsNull()) {
463     myPreview = new AIS_Shape(theShape);
464     //myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
465     myPreview->SetDisplayMode(AIS_Shaded);
466     myPreview->SetColor(Quantity_NOC_BLUE1);
467     myPreview->SetTransparency();
468     aCtx->Display(myPreview, true);
469     aCtx->Deactivate(myPreview);
470   }
471   else {
472     myPreview->Set(theShape);
473     aCtx->Redisplay(myPreview, true);
474   }
475 }
476
477
478 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
479 {
480   if (myPreview.IsNull())
481     return;
482   Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
483
484   if (theShow) {
485     myListIO.Clear();
486     aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
487     myListIO.Remove(myPreview);
488   }
489   AIS_ListOfInteractive::const_iterator aIt;
490   Handle(AIS_Shape) aShapeIO;
491   for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
492     aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
493     if (!aShapeIO.IsNull()) {
494       if (theShow)
495         aCtx->Erase(aShapeIO, false);
496       else
497         aCtx->Display(aShapeIO, false);
498     }
499   }
500   aCtx->UpdateCurrentViewer();
501 }
502
503 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
504 {
505   mySelectBtn->setEnabled(myUseFilters.size() > 0);
506 }
507
508 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
509 {
510   myNbLbl->setText(QString::number(myValues.size()));
511 }
512 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
513 {
514   QList<QWidget*> aWidgets;
515   QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
516   foreach(ModuleBase_FilterItem* aItem, aItems) {
517     QList<QWidget*> aSubList = aItem->getControls();
518     foreach(QWidget* aWgt, aSubList) {
519       aWidgets.append(aWgt);
520     }
521   }
522   return aWidgets;
523 }
524
525 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
526 {
527   myValues.clear();
528   if (!myPreview.IsNull()) {
529     Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
530     aCtx->Remove(myPreview, toUpdate);
531     myPreview.Nullify();
532   }
533 }
534
535 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
536 {
537   AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
538   AttributeSelectionListPtr aSelListAttr =
539     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
540   aSelListAttr->clear();
541   foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
542     aSelListAttr->append(aPrs->object(), aPrs->shape());
543   }
544 }
545
546 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
547 {
548   return true;
549 }
550
551 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
552 {
553   ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
554   if (aActive)
555     return aActive->restoreValue();
556   return true;
557 }