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