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