]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
[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_ISelection.h>
13 #include <ModuleBase_IWorkshop.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_Definitions.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)
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   activateShapeSelection(false);
98   activateFilters(myWorkshop, false);
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   activateShapeSelection(true);
110
111   // Restore selection in the viewer by the attribute selection list
112   myWorkshop->setSelected(getCurrentSelection());
113
114   activateFilters(myWorkshop, true);
115 }
116
117 //********************************************************************
118 void ModuleBase_WidgetMultiSelector::deactivate()
119 {
120   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
121   activateShapeSelection(false);
122   activateFilters(myWorkshop, false);
123 }
124
125 //********************************************************************
126 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
127 {
128   // the value is stored on the selection changed signal processing 
129   // A rare case when plugin was not loaded. 
130   if(!myFeature)
131     return false;
132   DataPtr aData = myFeature->data();
133   AttributeSelectionListPtr aSelectionListAttr = 
134     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
135
136   if (aSelectionListAttr) {
137     // Store shapes type
138      TopAbs_ShapeEnum aCurrentType =
139            ModuleBase_Tools::shapeType(myTypeCombo->currentText());
140      aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
141   }   
142    return true;
143 }
144
145 //********************************************************************
146 bool ModuleBase_WidgetMultiSelector::restoreValue()
147 {
148   // A rare case when plugin was not loaded. 
149   if(!myFeature)
150     return false;
151   DataPtr aData = myFeature->data();
152   AttributeSelectionListPtr aSelectionListAttr = 
153     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
154
155   if (aSelectionListAttr) {
156     // Restore shape type
157     if (!aSelectionListAttr->selectionType().empty())
158       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
159     updateSelectionList(aSelectionListAttr);
160     return true;
161   }
162   return false;
163 }
164
165 //********************************************************************
166 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
167 {
168   DataPtr aData = myFeature->data();
169   AttributeSelectionListPtr aSelectionListAttr = 
170     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
171   if (aSelectionListAttr.get() == NULL)
172     return;
173
174   mySelectionType = aSelectionListAttr->selectionType();
175   mySelection.clear();
176   int aSize = aSelectionListAttr->size();
177   for (int i = 0; i < aSelectionListAttr->size(); i++) {
178     AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
179     mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
180   }
181 }
182
183 //********************************************************************
184 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
185 {
186   DataPtr aData = myFeature->data();
187   AttributeSelectionListPtr aSelectionListAttr = 
188     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
189   if (aSelectionListAttr.get() == NULL)
190     return;
191   aSelectionListAttr->clear();
192
193   // Store shapes type
194   aSelectionListAttr->setSelectionType(mySelectionType);
195
196   // Store selection in the attribute
197   int aSize = mySelection.size();
198   foreach (GeomSelection aSelec, mySelection) {
199     aSelectionListAttr->append(aSelec.first, aSelec.second);
200   }
201 }
202
203 //********************************************************************
204 bool ModuleBase_WidgetMultiSelector::setSelection(const QList<ModuleBase_ViewerPrs>& theValues,
205                                                   int& thePosition)
206 {
207   if (thePosition < 0)
208     return false;
209
210   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
211   bool isDone = false;
212   for (int i = thePosition; i < theValues.size(); i++) {
213     ModuleBase_ViewerPrs aValue = theValues[i];
214     thePosition++;
215     bool aProcessed = false;
216     if (isValidSelection(aValue)) {
217       aProcessed = setSelectionCustom(aValue);
218     }
219     // if there is at least one set, the result is true
220     isDone = isDone || aProcessed;
221     // when an object, which do not satisfy the validating process, stop set selection
222     if (!aProcessed)
223       break;
224   }
225   if (isDone) {
226     updateObject(myFeature);
227     emit valuesChanged();
228   }
229   return isDone;
230 }
231
232 //********************************************************************
233 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
234 {
235   TopoDS_Shape aShape = thePrs.shape();
236   if ((myTypeCombo->count() > 1) && (!aShape.IsNull())) {
237     TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
238     if (aShape.ShapeType() != aType)
239       return false;
240   }
241   ResultPtr aResult;
242   if (!thePrs.owner().IsNull()) {
243     ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
244     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
245   }
246   else {
247     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
248   }
249
250
251   if (myFeature) {
252     // We can not select a result of our feature
253     const std::list<ResultPtr>& aResList = myFeature->results();
254     std::list<ResultPtr>::const_iterator aIt;
255     bool isSkipSelf = false;
256     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
257       if ((*aIt) == aResult) {
258         isSkipSelf = true;
259         break;
260       }
261     }
262     if(isSkipSelf)
263       return false;
264   }
265
266   // if the result has the similar shap as the parameter shape, just the context is set to the
267   // selection list attribute.
268   DataPtr aData = myFeature->data();
269   AttributeSelectionListPtr aSelectionListAttr = 
270     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
271
272   const TopoDS_Shape& aTDSShape = thePrs.shape();
273   // if only result is selected, an empty shape is set to the model
274   if (aTDSShape.IsNull()) {
275     aSelectionListAttr->append(aResult, GeomShapePtr());
276   }
277   else {
278     GeomShapePtr aShape(new GeomAPI_Shape());
279     aShape->setImpl(new TopoDS_Shape(aTDSShape));
280     // We can not select a result of our feature
281     if (aShape->isEqual(aResult->shape()))
282       aSelectionListAttr->append(aResult, GeomShapePtr());
283     else
284       aSelectionListAttr->append(aResult, aShape);
285   }
286   return true;
287 }
288
289 //********************************************************************
290 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
291 {
292   QList<QWidget*> result;
293   //result << myTypeCombo;
294   result << myListControl;
295   return result;
296 }
297
298 //********************************************************************
299 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
300 {
301   //TODO: Remove maybe?
302   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
303 }
304
305 //********************************************************************
306 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
307 {
308   activateShapeSelection(true);
309   activateFilters(myWorkshop, true);
310   QList<ModuleBase_ViewerPrs> anEmptyList;
311   // This method will call Selection changed event which will call onSelectionChanged
312   // To clear mySelection, myListControl and storeValue()
313   // So, we don't need to call it
314   myWorkshop->setSelected(anEmptyList);
315 }
316
317 //********************************************************************
318 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
319 {
320   QList<ModuleBase_ViewerPrs> aSelected = getSelectedEntitiesOrObjects(myWorkshop->selection());
321
322   DataPtr aData = myFeature->data();
323   AttributeSelectionListPtr aSelectionListAttr = 
324     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
325
326   aSelectionListAttr->clear();
327   if (aSelected.size() > 0) {
328     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
329       if (isValidSelection(aPrs)) {
330         setSelectionCustom(aPrs);
331       }
332     }
333   }
334   emit valuesChanged();
335   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
336   // calls validators for the feature and, as a result, updates the Apply button state.
337   updateObject(myFeature);
338 }
339
340 //********************************************************************
341 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
342 {
343   QString aShapeTypeName;
344   
345   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
346     aShapeTypeName = myTypeCombo->itemText(idx);
347     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
348     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
349       activateShapeSelection(false);
350       activateFilters(myWorkshop, false);
351       bool isBlocked = myTypeCombo->blockSignals(true);
352       myTypeCombo->setCurrentIndex(idx);
353       myTypeCombo->blockSignals(isBlocked);
354
355       activateShapeSelection(true);
356       activateFilters(myWorkshop, true);
357       break;
358     }
359   }
360 }
361
362 void ModuleBase_WidgetMultiSelector::activateShapeSelection(const bool isActivated)
363 {
364   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
365
366   if (isActivated) {
367     QString aNewType = myTypeCombo->currentText();
368     QIntList aList;
369     aList.append(ModuleBase_Tools::shapeType(aNewType));
370     myWorkshop->activateSubShapesSelection(aList);
371   } else {
372     myWorkshop->deactivateSubShapesSelection();
373   }
374 }
375
376 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getCurrentSelection() const
377 {
378   QList<ModuleBase_ViewerPrs> aSelected;
379   // Restore selection in the viewer by the attribute selection list
380   if(myFeature) {
381     DataPtr aData = myFeature->data();
382     AttributeSelectionListPtr aListAttr = 
383       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
384     if (aListAttr) {
385       for (int i = 0; i < aListAttr->size(); i++) {
386         AttributeSelectionPtr anAttr = aListAttr->value(i);
387         ResultPtr anObject = anAttr->context();
388         if (anObject.get()) {
389           TopoDS_Shape aShape;
390           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
391           if (aShapePtr.get()) {
392             aShape = aShapePtr->impl<TopoDS_Shape>();
393           }
394           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
395         }
396       }
397     }
398   }
399   return aSelected;
400 }
401
402 //********************************************************************
403 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
404 {
405   myListControl->clear();
406   for (int i = 0; i < theList->size(); i++) {
407     AttributeSelectionPtr aAttr = theList->value(i);
408     myListControl->addItem(aAttr->namingName().c_str());
409   }
410   // We have to call repaint because sometimes the List control is not updated
411   myListControl->repaint();
412 }
413
414 //********************************************************************
415 void ModuleBase_WidgetMultiSelector::onCopyItem()
416 {
417   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
418   QString aRes;
419   foreach(QListWidgetItem* aItem, aItems) {
420     if (!aRes.isEmpty())
421       aRes += "\n";
422     aRes += aItem->text();
423   }
424   if (!aRes.isEmpty()) {
425     QClipboard *clipboard = QApplication::clipboard();
426     clipboard->setText(aRes);
427   }
428 }
429
430 //********************************************************************
431 void ModuleBase_WidgetMultiSelector::onListSelection()
432 {
433   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
434   myCopyAction->setEnabled(!aItems.isEmpty());
435 }
436