Salome HOME
98f45e37081cd3d54bb193b39feb7f3a44b59c44
[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 class CustomListWidget : public QListWidget
42 {
43 public:
44   CustomListWidget( QWidget* theParent )
45     : QListWidget( theParent )
46   {
47   }
48
49   virtual QSize sizeHint() const
50   {
51     int aHeight = 2*QFontMetrics( font() ).height();
52     QSize aSize = QListWidget::sizeHint();
53     return QSize( aSize.width(), aHeight );
54   }
55
56   virtual QSize minimumSizeHint() const
57   {
58     int aHeight = 2*QFontMetrics( font() ).height();
59     QSize aSize = QListWidget::minimumSizeHint();
60     return QSize( aSize.width(), aHeight );
61   }
62 };
63
64 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
65                                                                ModuleBase_IWorkshop* theWorkshop,
66                                                                const Config_WidgetAPI* theData,
67                                                                const std::string& theParentId)
68  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
69    mySelectionType(""), mySelectionCount(0)
70 {
71   QGridLayout* aMainLay = new QGridLayout(this);
72   ModuleBase_Tools::adjustMargins(aMainLay);
73
74   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
75   aMainLay->addWidget(aTypeLabel, 0, 0);
76
77   myTypeCombo = new QComboBox(this);
78   // There is no sense to parameterize list of types while we can not parameterize selection mode
79
80   std::string aPropertyTypes = theData->getProperty("type_choice");
81   QString aTypesStr = aPropertyTypes.c_str();
82   QStringList aShapeTypes = aTypesStr.split(' ');
83
84   myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
85
86   myTypeCombo->addItems(aShapeTypes);
87   aMainLay->addWidget(myTypeCombo, 0, 1);
88   // if the xml definition contains one type, the controls to select a type should not be shown
89   if (aShapeTypes.size() == 1 || !myIsUseChoice) {
90     aTypeLabel->setVisible(false);
91     myTypeCombo->setVisible(false);
92   }
93
94   std::string aLabelText = theData->getProperty("label");
95   QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
96                                                       : tr("Selected objects:"), this);
97   aMainLay->addWidget(aListLabel, 1, 0);
98   // if the xml definition contains one type, an information label should be shown near to the latest
99   if (aShapeTypes.size() == 1) {
100     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
101     if (!aLabelIcon.isEmpty()) {
102       QLabel* aSelectedLabel = new QLabel("", this);
103       aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
104       aMainLay->addWidget(aSelectedLabel, 1, 1);
105     }
106     aMainLay->setColumnStretch(2, 1);
107   }
108
109   myListControl = new CustomListWidget(this);
110   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
111   aMainLay->setRowStretch(2, 1);
112   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
113   //aMainLay->setRowMinimumHeight(3, 20);
114   //this->setLayout(aMainLay);
115   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
116
117   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
118   myCopyAction->setShortcut(QKeySequence::Copy);
119   myCopyAction->setEnabled(false);
120   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
121   myListControl->addAction(myCopyAction);
122   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
123   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
124 }
125
126 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
127 {
128 }
129
130 //********************************************************************
131 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
132 {
133   // the value is stored on the selection changed signal processing 
134   // A rare case when plugin was not loaded. 
135   if(!myFeature)
136     return false;
137   DataPtr aData = myFeature->data();
138   AttributeSelectionListPtr aSelectionListAttr = 
139     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
140
141   if (aSelectionListAttr) {
142     // Store shapes type
143      TopAbs_ShapeEnum aCurrentType =
144            ModuleBase_Tools::shapeType(myTypeCombo->currentText());
145      aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
146   }   
147    return true;
148 }
149
150 //********************************************************************
151 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
152 {
153   // A rare case when plugin was not loaded. 
154   if(!myFeature)
155     return false;
156   DataPtr aData = myFeature->data();
157   AttributeSelectionListPtr aSelectionListAttr = 
158     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
159
160   if (aSelectionListAttr) {
161     // Restore shape type
162     if (!aSelectionListAttr->selectionType().empty())
163       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
164     updateSelectionList(aSelectionListAttr);
165     return true;
166   }
167   return false;
168 }
169
170 //********************************************************************
171 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
172 {
173   DataPtr aData = myFeature->data();
174   AttributeSelectionListPtr aSelectionListAttr = 
175     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
176   if (aSelectionListAttr.get() == NULL)
177     return;
178
179   mySelectionType = aSelectionListAttr->selectionType();
180   mySelectionCount = aSelectionListAttr->size();
181
182   /*mySelection.clear();
183   int aSize = aSelectionListAttr->size();
184   for (int i = 0; i < aSelectionListAttr->size(); i++) {
185     AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
186     mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
187   }*/
188 }
189
190 //********************************************************************
191 void ModuleBase_WidgetMultiSelector::clearAttribute()
192 {
193   DataPtr aData = myFeature->data();
194   AttributeSelectionListPtr aSelectionListAttr =
195     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
196   aSelectionListAttr->clear();
197 }
198
199 //********************************************************************
200 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
201                                                GeomShapePtr theShape)
202 {
203   DataPtr aData = myFeature->data();
204   AttributeSelectionListPtr aSelectionListAttr = 
205     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
206
207   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
208   aSelectionListAttr->append(aResult, theShape);
209 }
210
211 //********************************************************************
212 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
213 {
214   //clearAttribute();
215
216   // Store shape type
217   DataPtr aData = myFeature->data();
218   AttributeSelectionListPtr aSelectionListAttr = 
219     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
220   aSelectionListAttr->setSelectionType(mySelectionType);
221
222   // Store selection in the attribute
223   //int aSize = mySelection.size();
224   //foreach (GeomSelection aSelec, mySelection) {
225   //  setObject(aSelec.first, aSelec.second);
226   //}
227
228   //int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
229   //for ( int i = 0; i < aCountAppened; i++)
230   //  aSelectionListAttr->removeLast();
231 }
232
233 //********************************************************************
234 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
235                                                   const bool theToValidate)
236 {
237   QList<ModuleBase_ViewerPrs> aSkippedValues;
238
239   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
240   bool isDone = false;
241   for (; anIt != aLast; anIt++) {
242     ModuleBase_ViewerPrs aValue = *anIt;
243     bool aProcessed = false;
244     if (!theToValidate || isValidInFilters(aValue)) {
245       aProcessed = setSelectionCustom(aValue);
246     }
247     else
248       aSkippedValues.append(aValue);
249     // if there is at least one set, the result is true
250     isDone = isDone || aProcessed;
251   }
252   // updateObject - to update/redisplay feature
253   // it is commented in order to perfom it outside the method
254   //if (isDone) {
255     //updateObject(myFeature);
256     // this emit is necessary to call store/restore method an restore type of selection
257     //emit valuesChanged();
258   //}
259   theValues.clear();
260   if (!aSkippedValues.empty())
261     theValues.append(aSkippedValues);
262
263   return isDone;
264 }
265
266 //********************************************************************
267 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
268 {
269   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
270   if (aValid) {
271     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
272     aValid = aResult.get() != NULL;
273     if (aValid) {
274       if (myFeature) {
275         // We can not select a result of our feature
276         const std::list<ResultPtr>& aResList = myFeature->results();
277         std::list<ResultPtr>::const_iterator aIt;
278         bool isSkipSelf = false;
279         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
280           if ((*aIt) == aResult) {
281             isSkipSelf = true;
282             break;
283           }
284         }
285         if (isSkipSelf)
286           aValid = false;
287       }
288     }
289   }
290   return aValid;
291 }
292
293 //********************************************************************
294 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
295 {
296   QList<QWidget*> result;
297   //result << myTypeCombo;
298   result << myListControl;
299   return result;
300 }
301
302 //********************************************************************
303 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
304 {
305   activateSelection(true);
306   activateFilters(true);
307   QList<ModuleBase_ViewerPrs> anEmptyList;
308   // This method will call Selection changed event which will call onSelectionChanged
309   // To clear mySelection, myListControl and storeValue()
310   // So, we don't need to call it
311   myWorkshop->setSelected(anEmptyList);
312 }
313
314 void ModuleBase_WidgetMultiSelector::updateFocus()
315 {
316   // Set focus to List control in order to make possible 
317   // to use Tab key for transfer the focus to next widgets
318   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
319   myListControl->setFocus();
320 }
321
322 //********************************************************************
323 void ModuleBase_WidgetMultiSelector::updateSelectionName()
324 {
325 }
326
327 //********************************************************************
328 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
329 {
330   QIntList aShapeTypes;
331
332   if (myTypeCombo->count() > 1 && myIsUseChoice) {
333     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
334   }
335   else {
336     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
337       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
338     }
339   }
340   return aShapeTypes;
341 }
342
343 //********************************************************************
344 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
345 {
346   QString aShapeTypeName;
347   
348   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
349     aShapeTypeName = myTypeCombo->itemText(idx);
350     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
351     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
352       activateSelection(false);
353       activateFilters(false);
354       bool isBlocked = myTypeCombo->blockSignals(true);
355       myTypeCombo->setCurrentIndex(idx);
356       myTypeCombo->blockSignals(isBlocked);
357
358       activateSelection(true);
359       activateFilters(true);
360       break;
361     }
362   }
363 }
364
365 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
366 {
367   QList<ModuleBase_ViewerPrs> aSelected;
368   // Restore selection in the viewer by the attribute selection list
369   if(myFeature) {
370     DataPtr aData = myFeature->data();
371     AttributeSelectionListPtr aListAttr = 
372       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
373     if (aListAttr) {
374       for (int i = 0; i < aListAttr->size(); i++) {
375         AttributeSelectionPtr anAttr = aListAttr->value(i);
376         ResultPtr anObject = anAttr->context();
377         if (anObject.get()) {
378           TopoDS_Shape aShape;
379           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
380           if (aShapePtr.get()) {
381             aShape = aShapePtr->impl<TopoDS_Shape>();
382           }
383           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
384         }
385       }
386     }
387   }
388   return aSelected;
389 }
390
391 //********************************************************************
392 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
393 {
394   myListControl->clear();
395   for (int i = 0; i < theList->size(); i++) {
396     AttributeSelectionPtr aAttr = theList->value(i);
397     myListControl->addItem(aAttr->namingName().c_str());
398   }
399   // We have to call repaint because sometimes the List control is not updated
400   myListControl->repaint();
401 }
402
403 //********************************************************************
404 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
405 {
406   std::string aType;
407
408   if (theType == "Vertices")
409     aType = "vertex";
410   else if (theType == "Edges")
411     aType = "edge";
412   else if (theType == "Faces")
413     aType = "face";
414   else if (theType == "Solids")
415     aType = "solid";
416
417   return aType;
418 }
419
420 //********************************************************************
421 void ModuleBase_WidgetMultiSelector::onCopyItem()
422 {
423   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
424   QString aRes;
425   foreach(QListWidgetItem* aItem, aItems) {
426     if (!aRes.isEmpty())
427       aRes += "\n";
428     aRes += aItem->text();
429   }
430   if (!aRes.isEmpty()) {
431     QClipboard *clipboard = QApplication::clipboard();
432     clipboard->setText(aRes);
433   }
434 }
435
436 //********************************************************************
437 void ModuleBase_WidgetMultiSelector::onListSelection()
438 {
439   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
440   myCopyAction->setEnabled(!aItems.isEmpty());
441 }