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