]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Processing composilid like a solid in selection.
[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 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeRefList.h>
24
25 #include <Config_WidgetAPI.h>
26
27 #include <QGridLayout>
28 #include <QLabel>
29 #include <QListWidget>
30 #include <QObject>
31 #include <QString>
32 #include <QComboBox>
33 #include <QEvent>
34 #include <QAction>
35 #include <QApplication>
36 #include <QClipboard>
37
38 #include <memory>
39 #include <string>
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    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
138   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
139   if (aSelectionListAttr.get()) {
140      aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
141   }   
142    return true;
143 }
144
145 //********************************************************************
146 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
147 {
148   // A rare case when plugin was not loaded. 
149   if (!myFeature)
150     return false;
151
152   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
153   if (aSelectionListAttr.get()) {
154     // Restore shape type
155     if (!aSelectionListAttr->selectionType().empty())
156       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
157   }
158   updateSelectionList();
159   return true;
160 }
161
162 //********************************************************************
163 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
164 {
165   ModuleBase_WidgetValidated::storeAttributeValue();
166
167   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
168   if (aSelectionListAttr.get()) {
169     mySelectionType = aSelectionListAttr->selectionType();
170     mySelectionCount = aSelectionListAttr->size();
171   }
172   else {
173     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
174     mySelectionCount = aRefListAttr->size();
175   }
176 }
177
178 //********************************************************************
179 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
180 {
181   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
182
183   // Store shape type
184   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
185   if (aSelectionListAttr.get()) {
186     aSelectionListAttr->setSelectionType(mySelectionType);
187
188     // restore selection in the attribute. Indeed there is only one stored object
189     int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
190     for (int i = 0; i < aCountAppened; i++)
191       aSelectionListAttr->removeLast();
192   }
193   else {
194     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
195     // restore objects in the attribute. Indeed there is only one stored object
196     int aCountAppened = aRefListAttr->size() - mySelectionCount;
197     for (int i = 0; i < aCountAppened; i++)
198       aRefListAttr->removeLast();
199   }
200 }
201
202 //********************************************************************
203 void ModuleBase_WidgetMultiSelector::clearAttribute()
204 {
205   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
206   if (aSelectionListAttr.get())
207     aSelectionListAttr->clear();
208   else {
209     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
210     aRefListAttr->clear();
211   }
212 }
213
214 //********************************************************************
215 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
216                                                GeomShapePtr theShape)
217 {
218   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
219   if (aSelectionListAttr.get()) {
220     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
221     aSelectionListAttr->append(aResult, theShape, myIsInValidate);
222   }
223   else {
224     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
225     aRefListAttr->append(theSelectedObject);
226   }
227 }
228
229 //********************************************************************
230 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
231                                                   const bool theToValidate)
232 {
233   QList<ModuleBase_ViewerPrs> aSkippedValues;
234
235   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
236   bool isDone = false;
237   for (; anIt != aLast; anIt++) {
238     ModuleBase_ViewerPrs aValue = *anIt;
239     bool aProcessed = false;
240     if (!theToValidate || isValidInFilters(aValue)) {
241       aProcessed = setSelectionCustom(aValue);
242     }
243     else
244       aSkippedValues.append(aValue);
245     // if there is at least one set, the result is true
246     isDone = isDone || aProcessed;
247   }
248   // updateObject - to update/redisplay feature
249   // it is commented in order to perfom it outside the method
250   //if (isDone) {
251     //updateObject(myFeature);
252     // this emit is necessary to call store/restore method an restore type of selection
253     //emit valuesChanged();
254   //}
255   theValues.clear();
256   if (!aSkippedValues.empty())
257     theValues.append(aSkippedValues);
258
259   return isDone;
260 }
261
262 //********************************************************************
263 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
264 {
265   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
266   if (aValid) {
267     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
268     aValid = aResult.get() != NULL;
269     if (aValid) {
270       if (myFeature) {
271         // We can not select a result of our feature
272         const std::list<ResultPtr>& aResList = myFeature->results();
273         std::list<ResultPtr>::const_iterator aIt;
274         bool isSkipSelf = false;
275         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
276           if ((*aIt) == aResult) {
277             isSkipSelf = true;
278             break;
279           }
280         }
281         if (isSkipSelf)
282           aValid = false;
283       }
284     }
285   }
286   return aValid;
287 }
288
289 //********************************************************************
290 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
291 {
292   QList<QWidget*> result;
293   //result << myTypeCombo;
294   result << myListControl;
295   return result;
296 }
297
298 //********************************************************************
299 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
300 {
301   activateSelection(true);
302   activateFilters(true);
303   QList<ModuleBase_ViewerPrs> anEmptyList;
304   // This method will call Selection changed event which will call onSelectionChanged
305   // To clear mySelection, myListControl and storeValue()
306   // So, we don't need to call it
307   myWorkshop->setSelected(anEmptyList);
308 }
309
310 void ModuleBase_WidgetMultiSelector::updateFocus()
311 {
312   // Set focus to List control in order to make possible 
313   // to use Tab key for transfer the focus to next widgets
314   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
315   myListControl->setFocus();
316 }
317
318 //********************************************************************
319 void ModuleBase_WidgetMultiSelector::updateSelectionName()
320 {
321 }
322
323 //********************************************************************
324 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
325 {
326   QIntList aShapeTypes;
327
328   if (myTypeCombo->count() > 1 && myIsUseChoice) {
329     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
330   }
331   else {
332     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
333       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
334       aShapeTypes.append(aType);
335       if (aType == TopAbs_SOLID)
336         aShapeTypes.append(TopAbs_COMPSOLID);
337     }
338   }
339   return aShapeTypes;
340 }
341
342 //********************************************************************
343 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
344 {
345   QString aShapeTypeName;
346   
347   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
348     aShapeTypeName = myTypeCombo->itemText(idx);
349     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
350     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
351       activateSelection(false);
352       activateFilters(false);
353       bool isBlocked = myTypeCombo->blockSignals(true);
354       myTypeCombo->setCurrentIndex(idx);
355       myTypeCombo->blockSignals(isBlocked);
356
357       activateSelection(true);
358       activateFilters(true);
359       break;
360     }
361   }
362 }
363
364 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
365 {
366   QList<ModuleBase_ViewerPrs> aSelected;
367   // Restore selection in the viewer by the attribute selection list
368   if(myFeature) {
369     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
370     if (aSelectionListAttr.get()) {
371       for (int i = 0; i < aSelectionListAttr->size(); i++) {
372         AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
373         ResultPtr anObject = anAttr->context();
374         if (anObject.get()) {
375           TopoDS_Shape aShape;
376           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
377           if (aShapePtr.get()) {
378             aShape = aShapePtr->impl<TopoDS_Shape>();
379           }
380           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
381         }
382       }
383     }
384     else {
385       AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
386       if (aRefListAttr.get()) {
387         for (int i = 0; i < aRefListAttr->size(); i++) {
388           ObjectPtr anObject = aRefListAttr->object(i);
389           if (anObject.get()) {
390             aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
391           }
392         }
393       }
394     }
395   }
396   return aSelected;
397 }
398
399 //********************************************************************
400 void ModuleBase_WidgetMultiSelector::updateSelectionList()
401 {
402   myListControl->clear();
403
404   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
405   if (aSelectionListAttr.get()) {
406     for (int i = 0; i < aSelectionListAttr->size(); i++) {
407       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
408       myListControl->addItem(aAttr->namingName().c_str());
409     }
410   }
411   else {
412     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
413     for (int i = 0; i < aRefListAttr->size(); i++) {
414       ObjectPtr anObject = aRefListAttr->object(i);
415       if (anObject.get())
416         myListControl->addItem(anObject->data()->name().c_str());
417     }
418   }
419   // We have to call repaint because sometimes the List control is not updated
420   myListControl->repaint();
421 }
422
423 //********************************************************************
424 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
425 {
426   std::string aType;
427
428   if (theType == "Vertices")
429     aType = "vertex";
430   else if (theType == "Edges")
431     aType = "edge";
432   else if (theType == "Faces")
433     aType = "face";
434   else if (theType == "Solids")
435     aType = "solid";
436
437   return aType;
438 }
439
440 //********************************************************************
441 void ModuleBase_WidgetMultiSelector::onCopyItem()
442 {
443   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
444   QString aRes;
445   foreach(QListWidgetItem* aItem, aItems) {
446     if (!aRes.isEmpty())
447       aRes += "\n";
448     aRes += aItem->text();
449   }
450   if (!aRes.isEmpty()) {
451     QClipboard *clipboard = QApplication::clipboard();
452     clipboard->setText(aRes);
453   }
454 }
455
456 //********************************************************************
457 void ModuleBase_WidgetMultiSelector::onListSelection()
458 {
459   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
460   myCopyAction->setEnabled(!aItems.isEmpty());
461 }