Salome HOME
Merge Dev_2.1.0 with PythonAPI branch
[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   ModuleBase_Tools::setFocus(myListControl,
333                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
334 }
335
336 //********************************************************************
337 void ModuleBase_WidgetMultiSelector::updateSelectionName()
338 {
339 }
340
341 //********************************************************************
342 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
343 {
344   QIntList aShapeTypes;
345
346   if (myTypeCombo->count() > 1 && myIsUseChoice) {
347     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
348   }
349   else {
350     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
351       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
352       aShapeTypes.append(aType);
353       if (aType == TopAbs_SOLID)
354         aShapeTypes.append(TopAbs_COMPSOLID);
355     }
356   }
357   return aShapeTypes;
358 }
359
360 //********************************************************************
361 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
362 {
363   QString aShapeTypeName;
364   
365   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
366     aShapeTypeName = myTypeCombo->itemText(idx);
367     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
368     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
369       activateSelectionAndFilters(false);
370       bool isBlocked = myTypeCombo->blockSignals(true);
371       myTypeCombo->setCurrentIndex(idx);
372       myTypeCombo->blockSignals(isBlocked);
373
374       activateSelectionAndFilters(true);
375       break;
376     }
377   }
378 }
379
380 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
381 {
382   QList<ModuleBase_ViewerPrs> aSelected;
383   // Restore selection in the viewer by the attribute selection list
384   if(myFeature) {
385     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
386     if (aSelectionListAttr.get()) {
387       for (int i = 0; i < aSelectionListAttr->size(); i++) {
388         AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
389         ResultPtr anObject = anAttr->context();
390         if (anObject.get()) {
391           TopoDS_Shape aShape;
392           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
393           if (aShapePtr.get()) {
394             aShape = aShapePtr->impl<TopoDS_Shape>();
395           }
396           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
397         }
398       }
399     }
400     else {
401       AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
402       if (aRefListAttr.get()) {
403         for (int i = 0; i < aRefListAttr->size(); i++) {
404           ObjectPtr anObject = aRefListAttr->object(i);
405           if (anObject.get()) {
406             aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
407           }
408         }
409       }
410     }
411   }
412   return aSelected;
413 }
414
415 //********************************************************************
416 void ModuleBase_WidgetMultiSelector::updateSelectionList()
417 {
418   myListControl->clear();
419
420   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
421   if (aSelectionListAttr.get()) {
422     for (int i = 0; i < aSelectionListAttr->size(); i++) {
423       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
424       myListControl->addItem(aAttr->namingName().c_str());
425     }
426   }
427   else {
428     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
429     for (int i = 0; i < aRefListAttr->size(); i++) {
430       ObjectPtr anObject = aRefListAttr->object(i);
431       if (anObject.get())
432         myListControl->addItem(anObject->data()->name().c_str());
433     }
434   }
435   // We have to call repaint because sometimes the List control is not updated
436   myListControl->repaint();
437 }
438
439 //********************************************************************
440 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
441 {
442   std::string aType;
443
444   if (theType == "Vertices")
445     aType = "vertex";
446   else if (theType == "Edges")
447     aType = "edge";
448   else if (theType == "Faces")
449     aType = "face";
450   else if (theType == "Solids")
451     aType = "solid";
452
453   return aType;
454 }
455
456 //********************************************************************
457 void ModuleBase_WidgetMultiSelector::onCopyItem()
458 {
459   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
460   QString aRes;
461   foreach(QListWidgetItem* aItem, aItems) {
462     if (!aRes.isEmpty())
463       aRes += "\n";
464     aRes += aItem->text();
465   }
466   if (!aRes.isEmpty()) {
467     QClipboard *clipboard = QApplication::clipboard();
468     clipboard->setText(aRes);
469   }
470 }
471
472 //********************************************************************
473 void ModuleBase_WidgetMultiSelector::onListSelection()
474 {
475   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
476   myCopyAction->setEnabled(!aItems.isEmpty());
477 }