Salome HOME
c3bf95327950ba72585b4ba20e8238517d12e462
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetMultiSelector.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetMultiSelector.h>
11 #include <ModuleBase_WidgetShapeSelector.h>
12 #include <ModuleBase_FilterNoDegeneratedEdge.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IWorkshop.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_Tools.h>
17
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Object.h>
20
21 #include <Config_WidgetAPI.h>
22
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QListWidget>
26 #include <QObject>
27 #include <QString>
28 #include <QComboBox>
29 #include <QEvent>
30 #include <QAction>
31 #include <QApplication>
32 #include <QClipboard>
33
34 #include <memory>
35 #include <string>
36
37 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
38                                                                ModuleBase_IWorkshop* theWorkshop,
39                                                                const Config_WidgetAPI* theData,
40                                                                const std::string& theParentId)
41     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
42       myWorkshop(theWorkshop), myIsActive(false)
43 {
44   QGridLayout* aMainLay = new QGridLayout(this);
45   ModuleBase_Tools::adjustMargins(aMainLay);
46
47   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
48   aMainLay->addWidget(aTypeLabel, 0, 0);
49
50   myTypeCombo = new QComboBox(this);
51   // There is no sence to paramerize list of types while we can not parametrize selection mode
52
53   std::string aPropertyTypes = theData->getProperty("type_choice");
54   QString aTypesStr = aPropertyTypes.c_str();
55   QStringList aShapeTypes = aTypesStr.split(' ');
56
57   myTypeCombo->addItems(aShapeTypes);
58   aMainLay->addWidget(myTypeCombo, 0, 1);
59   // if the xml definition contains one type, the controls to select a type should not be shown
60   if (aShapeTypes.size() == 1) {
61     aTypeLabel->setVisible(false);
62     myTypeCombo->setVisible(false);
63   }
64
65   QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
66   aMainLay->addWidget(aListLabel, 1, 0);
67   // if the xml definition contains one type, an information label should be shown near to the latest
68   if (aShapeTypes.size() == 1) {
69     QString aLabelText = QString::fromStdString(theData->widgetLabel());
70     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
71     QLabel* aSelectedLabel = new QLabel(aLabelText, this);
72     if (!aLabelIcon.isEmpty())
73       aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
74     aMainLay->addWidget(aSelectedLabel, 1, 1);
75     aMainLay->setColumnStretch(2, 1);
76   }
77
78   myListControl = new QListWidget(this);
79   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
80   aMainLay->setRowStretch(2, 1);
81   aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
82   aMainLay->setRowMinimumHeight(3, 20);
83   this->setLayout(aMainLay);
84   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
85
86   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
87   myCopyAction->setShortcut(QKeySequence::Copy);
88   myCopyAction->setEnabled(false);
89   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
90   myListControl->addAction(myCopyAction);
91   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
92   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
93 }
94
95 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
96 {
97   myIsActive = false;
98   activateShapeSelection();
99 }
100
101 //********************************************************************
102 void ModuleBase_WidgetMultiSelector::activateCustom()
103 {
104   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
105   connect(myWorkshop, SIGNAL(selectionChanged()), 
106           this,       SLOT(onSelectionChanged()), 
107           Qt::UniqueConnection);
108
109   myIsActive = true;
110   activateShapeSelection();
111 }
112
113 //********************************************************************
114 void ModuleBase_WidgetMultiSelector::deactivate()
115 {
116   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
117   myIsActive = false;
118   activateShapeSelection();
119 }
120
121 //********************************************************************
122 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
123 {
124   // A rare case when plugin was not loaded. 
125   if(!myFeature)
126     return false;
127   DataPtr aData = myFeature->data();
128   AttributeSelectionListPtr aSelectionListAttr = 
129     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
130
131   if (aSelectionListAttr) {
132     aSelectionListAttr->clear();
133     // Store shapes type
134     TopAbs_ShapeEnum aCurrentType =
135           ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
136     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
137     // Store selection in the attribute
138     foreach (GeomSelection aSelec, mySelection) {
139       aSelectionListAttr->append(aSelec.first, aSelec.second);
140     }
141     //updateSelectionList(aSelectionListAttr);
142     updateObject(myFeature);
143     return true;
144   }
145   return false;
146 }
147
148 //********************************************************************
149 bool ModuleBase_WidgetMultiSelector::restoreValue()
150 {
151   // A rare case when plugin was not loaded. 
152   if(!myFeature)
153     return false;
154   DataPtr aData = myFeature->data();
155   AttributeSelectionListPtr aSelectionListAttr = 
156     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
157
158   if (aSelectionListAttr) {
159     mySelection.clear();
160     // Restore shape type
161     setCurrentShapeType(
162       ModuleBase_WidgetShapeSelector::shapeType(aSelectionListAttr->selectionType().c_str()));
163     // Restore selection in the list
164     for (int i = 0; i < aSelectionListAttr->size(); i++) {
165       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
166       mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
167     }
168     updateSelectionList(aSelectionListAttr);
169     return true;
170   }
171   return false;
172 }
173
174 //********************************************************************
175 void ModuleBase_WidgetMultiSelector::backupAttributeValue(const bool isBackup)
176 {
177
178 }
179
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
182 {
183
184 }
185
186 //********************************************************************
187 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
188 {
189   QList<QWidget*> result;
190   //result << myTypeCombo;
191   result << myListControl;
192   return result;
193 }
194
195 //********************************************************************
196 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
197 {
198   //TODO: Remove maybe?
199   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
200 }
201
202 //********************************************************************
203 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
204 {
205   activateShapeSelection();
206   QObjectPtrList anEmptyList;
207   myWorkshop->setSelected(anEmptyList);
208   // Clear mySelection, myListControl and storeValue()
209   onSelectionChanged();
210 }
211
212 //********************************************************************
213 #include <AIS_ListIteratorOfListOfInteractive.hxx>
214 #include <ModuleBase_ResultPrs.h>
215 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
216 {
217   ModuleBase_ISelection* aSelection = myWorkshop->selection();
218   NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
219   std::list<ObjectPtr> aOwnersList;
220   aSelection->selectedShapes(aSelectedShapes, aOwnersList);
221
222   mySelection.clear();
223   std::list<ObjectPtr>::const_iterator aIt;
224   NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
225   GeomShapePtr aShape;
226   for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
227     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
228     // this case should be moved to PartSet module after redesign this class
229     /*if (aShpIt.Value().ShapeType() == TopAbs_COMPOUND) {
230       int aValue = 0;
231       AIS_ListOfInteractive aList;
232       aSelection->selectedAISObjects(aList);
233       AIS_ListIteratorOfListOfInteractive aLIt(aList);
234       Handle(AIS_InteractiveObject) anAISIO;
235       for(; aLIt.More(); aLIt.Next()){
236         anAISIO = aLIt.Value();
237         Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(anAISIO);
238         if (!aResultPrs.IsNull()) {
239           const std::list<std::shared_ptr<GeomAPI_Shape> >& aResultFaces = aResultPrs->facesList();
240           std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
241           for (aIt = aResultFaces.cbegin(); aIt != aResultFaces.cend(); ++aIt) {
242             TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
243             
244             aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
245             aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
246
247             mySelection.append(GeomSelection(aResult, aShape));
248           }
249         }
250       }
251     }
252     else*/
253     {
254       if (myFeature) {
255         // We can not select a result of our feature
256         const std::list<ResultPtr>& aResList = myFeature->results();
257         std::list<ResultPtr>::const_iterator aIt;
258         bool isSkipSelf = false;
259         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
260           if ((*aIt) == aResult) {
261             isSkipSelf = true;
262             break;
263           }
264         }
265         if(isSkipSelf)
266           continue;
267       }
268       aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
269       aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
270
271       if (aShape->isEqual(aResult->shape())) {
272         //aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
273         mySelection.append(GeomSelection(aResult, NULL));//aShape));
274       }
275       else
276         mySelection.append(GeomSelection(aResult, aShape));
277     }
278   }
279   //updateSelectionList();
280   emit valuesChanged();
281 }
282
283 //********************************************************************
284 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
285 {
286   QString aShapeTypeName;
287   
288   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
289     aShapeTypeName = myTypeCombo->itemText(idx);
290     TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
291     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
292       myIsActive = false;
293       activateShapeSelection();
294       bool isBlocked = myTypeCombo->blockSignals(true);
295       myTypeCombo->setCurrentIndex(idx);
296       myIsActive = true;
297       myTypeCombo->blockSignals(isBlocked);
298       activateShapeSelection();
299       break;
300     }
301   }
302 }
303
304 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
305 {
306   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
307
308   if (myIsActive) {
309     QString aNewType = myTypeCombo->currentText();
310     QIntList aList;
311     aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
312     myWorkshop->activateSubShapesSelection(aList);
313
314     // it is necessary to filter the selected edges to be non-degenerated
315     // it is not possible to build naming name for such edges
316     if (aNewType == "Edges") {
317       myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge();
318       aViewer->addSelectionFilter(myEdgesTypeFilter);
319     }
320     else {
321       aViewer->removeSelectionFilter(myEdgesTypeFilter);
322     }
323
324   } else {
325     myWorkshop->deactivateSubShapesSelection();
326     aViewer->removeSelectionFilter(myEdgesTypeFilter);
327   }
328
329   activateFilters(myWorkshop, myIsActive);
330 }
331
332 //********************************************************************
333 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
334 {
335   myListControl->clear();
336   for (int i = 0; i < theList->size(); i++) {
337     AttributeSelectionPtr aAttr = theList->value(i);
338     myListControl->addItem(aAttr->namingName().c_str());
339   }
340 }
341
342 //********************************************************************
343 void ModuleBase_WidgetMultiSelector::onCopyItem()
344 {
345   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
346   QString aRes;
347   foreach(QListWidgetItem* aItem, aItems) {
348     if (!aRes.isEmpty())
349       aRes += "\n";
350     aRes += aItem->text();
351   }
352   if (!aRes.isEmpty()) {
353     QClipboard *clipboard = QApplication::clipboard();
354     clipboard->setText(aRes);
355   }
356 }
357
358 //********************************************************************
359 void ModuleBase_WidgetMultiSelector::onListSelection()
360 {
361   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
362   myCopyAction->setEnabled(!aItems.isEmpty());
363 }
364