]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Using validator in multi selector control for the "type_choice" key.
[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 <GeomValidators_ShapeType.h>
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Object.h>
22
23 #include <Config_WidgetAPI.h>
24
25 #include <QGridLayout>
26 #include <QLabel>
27 #include <QListWidget>
28 #include <QObject>
29 #include <QString>
30 #include <QComboBox>
31 #include <QEvent>
32 #include <QAction>
33 #include <QApplication>
34 #include <QClipboard>
35
36 #include <memory>
37 #include <string>
38
39 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
40                                                                ModuleBase_IWorkshop* theWorkshop,
41                                                                const Config_WidgetAPI* theData,
42                                                                const std::string& theParentId)
43     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
44       myWorkshop(theWorkshop)
45 {
46   QGridLayout* aMainLay = new QGridLayout(this);
47   ModuleBase_Tools::adjustMargins(aMainLay);
48
49   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
50   aMainLay->addWidget(aTypeLabel, 0, 0);
51
52   myTypeCombo = new QComboBox(this);
53   // There is no sence to paramerize list of types while we can not parametrize selection mode
54
55   myShapeValidator = new GeomValidators_ShapeType();
56
57   std::string aPropertyTypes = theData->getProperty("type_choice");
58   QString aTypesStr = aPropertyTypes.c_str();
59   QStringList aShapeTypes = aTypesStr.split(' ');
60
61   myTypeCombo->addItems(aShapeTypes);
62   aMainLay->addWidget(myTypeCombo, 0, 1);
63   // if the xml definition contains one type, the controls to select a type should not be shown
64   if (aShapeTypes.size() == 1) {
65     aTypeLabel->setVisible(false);
66     myTypeCombo->setVisible(false);
67   }
68
69   QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
70   aMainLay->addWidget(aListLabel, 1, 0);
71   // if the xml definition contains one type, an information label should be shown near to the latest
72   if (aShapeTypes.size() == 1) {
73     QString aLabelText = QString::fromStdString(theData->widgetLabel());
74     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
75     QLabel* aSelectedLabel = new QLabel(aLabelText, this);
76     if (!aLabelIcon.isEmpty())
77       aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
78     aMainLay->addWidget(aSelectedLabel, 1, 1);
79     aMainLay->setColumnStretch(2, 1);
80   }
81
82   myListControl = new QListWidget(this);
83   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
84   aMainLay->setRowStretch(2, 1);
85   aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
86   aMainLay->setRowMinimumHeight(3, 20);
87   this->setLayout(aMainLay);
88   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
89
90   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
91   myCopyAction->setShortcut(QKeySequence::Copy);
92   myCopyAction->setEnabled(false);
93   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
94   myListControl->addAction(myCopyAction);
95   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
96   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
97 }
98
99 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
100 {
101   activateShapeSelection(false);
102   activateFilters(myWorkshop, false);
103
104   delete myShapeValidator;
105 }
106
107 //********************************************************************
108 void ModuleBase_WidgetMultiSelector::activateCustom()
109 {
110   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
111   connect(myWorkshop, SIGNAL(selectionChanged()), 
112           this,       SLOT(onSelectionChanged()), 
113           Qt::UniqueConnection);
114
115   activateShapeSelection(true);
116
117   // Restore selection in the viewer by the attribute selection list
118   myWorkshop->setSelected(getAttributeSelection());
119
120   activateFilters(myWorkshop, true);
121 }
122
123 //********************************************************************
124 void ModuleBase_WidgetMultiSelector::deactivate()
125 {
126   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
127   activateShapeSelection(false);
128   activateFilters(myWorkshop, false);
129 }
130
131 //********************************************************************
132 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
133 {
134   // the value is stored on the selection changed signal processing 
135   // A rare case when plugin was not loaded. 
136   if(!myFeature)
137     return false;
138   DataPtr aData = myFeature->data();
139   AttributeSelectionListPtr aSelectionListAttr = 
140     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
141
142   if (aSelectionListAttr) {
143     // Store shapes type
144      TopAbs_ShapeEnum aCurrentType =
145            ModuleBase_Tools::shapeType(myTypeCombo->currentText());
146      aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
147   }   
148    return true;
149 }
150
151 //********************************************************************
152 bool ModuleBase_WidgetMultiSelector::restoreValue()
153 {
154   // A rare case when plugin was not loaded. 
155   if(!myFeature)
156     return false;
157   DataPtr aData = myFeature->data();
158   AttributeSelectionListPtr aSelectionListAttr = 
159     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
160
161   if (aSelectionListAttr) {
162     // Restore shape type
163     if (!aSelectionListAttr->selectionType().empty())
164       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
165     updateSelectionList(aSelectionListAttr);
166     return true;
167   }
168   return false;
169 }
170
171 //********************************************************************
172 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
173 {
174   DataPtr aData = myFeature->data();
175   AttributeSelectionListPtr aSelectionListAttr = 
176     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
177   if (aSelectionListAttr.get() == NULL)
178     return;
179
180   mySelectionType = aSelectionListAttr->selectionType();
181   mySelection.clear();
182   int aSize = aSelectionListAttr->size();
183   for (int i = 0; i < aSelectionListAttr->size(); i++) {
184     AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
185     mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
186   }
187 }
188
189 //********************************************************************
190 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
191 {
192   DataPtr aData = myFeature->data();
193   AttributeSelectionListPtr aSelectionListAttr = 
194     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
195   if (aSelectionListAttr.get() == NULL)
196     return;
197   aSelectionListAttr->clear();
198
199   // Store shapes type
200   aSelectionListAttr->setSelectionType(mySelectionType);
201
202   // Store selection in the attribute
203   int aSize = mySelection.size();
204   foreach (GeomSelection aSelec, mySelection) {
205     aSelectionListAttr->append(aSelec.first, aSelec.second);
206   }
207 }
208
209 //********************************************************************
210 void ModuleBase_WidgetMultiSelector::customValidators(
211                                         std::list<ModelAPI_Validator*>& theValidators,
212                                         std::list<std::list<std::string> >& theArguments) const
213 {
214   std::list<std::string> anArguments;
215
216   theValidators.push_back(myShapeValidator);
217   QString aType = myTypeCombo->currentText();
218   anArguments.push_back(aType.toStdString().c_str());
219   theArguments.push_back(anArguments);
220 }
221
222 //********************************************************************
223 bool ModuleBase_WidgetMultiSelector::setSelection(const QList<ModuleBase_ViewerPrs>& theValues,
224                                                   int& thePosition)
225 {
226   if (thePosition < 0)
227     return false;
228
229   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
230   bool isDone = false;
231   for (int i = thePosition; i < theValues.size(); i++) {
232     ModuleBase_ViewerPrs aValue = theValues[i];
233     thePosition++;
234     bool aProcessed = false;
235     if (isValidSelection(aValue)) {
236       aProcessed = setSelectionCustom(aValue);
237     }
238     // if there is at least one set, the result is true
239     isDone = isDone || aProcessed;
240     // when an object, which do not satisfy the validating process, stop set selection
241     if (!aProcessed)
242       break;
243   }
244   if (isDone) {
245     updateObject(myFeature);
246     // this emit is necessary to call store/restore method an restore type of selection
247     emit valuesChanged();
248   }
249   return isDone;
250 }
251
252 //********************************************************************
253 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
254 {
255   TopoDS_Shape aShape = thePrs.shape();
256   if ((myTypeCombo->count() > 1) && (!aShape.IsNull())) {
257     TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
258     if (aShape.ShapeType() != aType)
259       return false;
260   }
261   ResultPtr aResult;
262   if (!thePrs.owner().IsNull()) {
263     ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
264     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
265   }
266   else {
267     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
268   }
269
270
271   if (myFeature) {
272     // We can not select a result of our feature
273     const std::list<ResultPtr>& aResList = myFeature->results();
274     std::list<ResultPtr>::const_iterator aIt;
275     bool isSkipSelf = false;
276     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
277       if ((*aIt) == aResult) {
278         isSkipSelf = true;
279         break;
280       }
281     }
282     if(isSkipSelf)
283       return false;
284   }
285
286   // if the result has the similar shap as the parameter shape, just the context is set to the
287   // selection list attribute.
288   DataPtr aData = myFeature->data();
289   AttributeSelectionListPtr aSelectionListAttr = 
290     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
291
292   const TopoDS_Shape& aTDSShape = thePrs.shape();
293   // if only result is selected, an empty shape is set to the model
294   if (aTDSShape.IsNull()) {
295     aSelectionListAttr->append(aResult, GeomShapePtr());
296   }
297   else {
298     GeomShapePtr aShape(new GeomAPI_Shape());
299     aShape->setImpl(new TopoDS_Shape(aTDSShape));
300     // We can not select a result of our feature
301     if (aShape->isEqual(aResult->shape()))
302       aSelectionListAttr->append(aResult, GeomShapePtr());
303     else
304       aSelectionListAttr->append(aResult, aShape);
305   }
306   return true;
307 }
308
309 //********************************************************************
310 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
311 {
312   QList<QWidget*> result;
313   //result << myTypeCombo;
314   result << myListControl;
315   return result;
316 }
317
318 //********************************************************************
319 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
320 {
321   //TODO: Remove maybe?
322   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
323 }
324
325 //********************************************************************
326 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
327 {
328   activateShapeSelection(true);
329   activateFilters(myWorkshop, true);
330   QList<ModuleBase_ViewerPrs> anEmptyList;
331   // This method will call Selection changed event which will call onSelectionChanged
332   // To clear mySelection, myListControl and storeValue()
333   // So, we don't need to call it
334   myWorkshop->setSelected(anEmptyList);
335 }
336
337 //********************************************************************
338 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
339 {
340   QList<ModuleBase_ViewerPrs> aSelected = getSelectedEntitiesOrObjects(myWorkshop->selection());
341
342   DataPtr aData = myFeature->data();
343   AttributeSelectionListPtr aSelectionListAttr = 
344     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
345
346   aSelectionListAttr->clear();
347   if (aSelected.size() > 0) {
348     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
349       if (isValidSelection(aPrs)) {
350         setSelectionCustom(aPrs);
351       }
352     }
353   }
354   emit valuesChanged();
355   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
356   // calls validators for the feature and, as a result, updates the Apply button state.
357   updateObject(myFeature);
358
359   // Set focus to List control in order to make possible 
360   // to use Tab key for transfer the focus to next widgets
361   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
362   myListControl->setFocus();
363 }
364
365 //********************************************************************
366 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
367 {
368   QString aShapeTypeName;
369   
370   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
371     aShapeTypeName = myTypeCombo->itemText(idx);
372     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
373     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
374       activateShapeSelection(false);
375       activateFilters(myWorkshop, false);
376       bool isBlocked = myTypeCombo->blockSignals(true);
377       myTypeCombo->setCurrentIndex(idx);
378       myTypeCombo->blockSignals(isBlocked);
379
380       activateShapeSelection(true);
381       activateFilters(myWorkshop, true);
382       break;
383     }
384   }
385 }
386
387 void ModuleBase_WidgetMultiSelector::activateShapeSelection(const bool isActivated)
388 {
389   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
390
391   if (isActivated) {
392     QString aNewType = myTypeCombo->currentText();
393     QIntList aList;
394     aList.append(ModuleBase_Tools::shapeType(aNewType));
395     myWorkshop->activateSubShapesSelection(aList);
396   } else {
397     myWorkshop->deactivateSubShapesSelection();
398   }
399 }
400
401 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
402 {
403   QList<ModuleBase_ViewerPrs> aSelected;
404   // Restore selection in the viewer by the attribute selection list
405   if(myFeature) {
406     DataPtr aData = myFeature->data();
407     AttributeSelectionListPtr aListAttr = 
408       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
409     if (aListAttr) {
410       for (int i = 0; i < aListAttr->size(); i++) {
411         AttributeSelectionPtr anAttr = aListAttr->value(i);
412         ResultPtr anObject = anAttr->context();
413         if (anObject.get()) {
414           TopoDS_Shape aShape;
415           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
416           if (aShapePtr.get()) {
417             aShape = aShapePtr->impl<TopoDS_Shape>();
418           }
419           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
420         }
421       }
422     }
423   }
424   return aSelected;
425 }
426
427 //********************************************************************
428 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
429 {
430   myListControl->clear();
431   for (int i = 0; i < theList->size(); i++) {
432     AttributeSelectionPtr aAttr = theList->value(i);
433     myListControl->addItem(aAttr->namingName().c_str());
434   }
435   // We have to call repaint because sometimes the List control is not updated
436   myListControl->repaint();
437 }
438
439 //********************************************************************
440 void ModuleBase_WidgetMultiSelector::onCopyItem()
441 {
442   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
443   QString aRes;
444   foreach(QListWidgetItem* aItem, aItems) {
445     if (!aRes.isEmpty())
446       aRes += "\n";
447     aRes += aItem->text();
448   }
449   if (!aRes.isEmpty()) {
450     QClipboard *clipboard = QApplication::clipboard();
451     clipboard->setText(aRes);
452   }
453 }
454
455 //********************************************************************
456 void ModuleBase_WidgetMultiSelector::onListSelection()
457 {
458   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
459   myCopyAction->setEnabled(!aItems.isEmpty());
460 }
461