]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Source documentation updated. Obsolete files removed
[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   activateSelection(true);
308   activateFilters(true);
309   QList<ModuleBase_ViewerPrs> anEmptyList;
310   // This method will call Selection changed event which will call onSelectionChanged
311   // To clear mySelection, myListControl and storeValue()
312   // So, we don't need to call it
313   myWorkshop->setSelected(anEmptyList);
314 }
315
316 void ModuleBase_WidgetMultiSelector::updateFocus()
317 {
318   // Set focus to List control in order to make possible 
319   // to use Tab key for transfer the focus to next widgets
320   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
321   myListControl->setFocus();
322 }
323
324 //********************************************************************
325 void ModuleBase_WidgetMultiSelector::updateSelectionName()
326 {
327 }
328
329 //********************************************************************
330 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
331 {
332   QIntList aShapeTypes;
333
334   if (myTypeCombo->count() > 1 && myIsUseChoice) {
335     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
336   }
337   else {
338     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
339       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
340       aShapeTypes.append(aType);
341       if (aType == TopAbs_SOLID)
342         aShapeTypes.append(TopAbs_COMPSOLID);
343     }
344   }
345   return aShapeTypes;
346 }
347
348 //********************************************************************
349 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
350 {
351   QString aShapeTypeName;
352   
353   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
354     aShapeTypeName = myTypeCombo->itemText(idx);
355     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
356     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
357       activateSelection(false);
358       activateFilters(false);
359       bool isBlocked = myTypeCombo->blockSignals(true);
360       myTypeCombo->setCurrentIndex(idx);
361       myTypeCombo->blockSignals(isBlocked);
362
363       activateSelection(true);
364       activateFilters(true);
365       break;
366     }
367   }
368 }
369
370 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
371 {
372   QList<ModuleBase_ViewerPrs> aSelected;
373   // Restore selection in the viewer by the attribute selection list
374   if(myFeature) {
375     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
376     if (aSelectionListAttr.get()) {
377       for (int i = 0; i < aSelectionListAttr->size(); i++) {
378         AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
379         ResultPtr anObject = anAttr->context();
380         if (anObject.get()) {
381           TopoDS_Shape aShape;
382           std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
383           if (aShapePtr.get()) {
384             aShape = aShapePtr->impl<TopoDS_Shape>();
385           }
386           aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
387         }
388       }
389     }
390     else {
391       AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
392       if (aRefListAttr.get()) {
393         for (int i = 0; i < aRefListAttr->size(); i++) {
394           ObjectPtr anObject = aRefListAttr->object(i);
395           if (anObject.get()) {
396             aSelected.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
397           }
398         }
399       }
400     }
401   }
402   return aSelected;
403 }
404
405 //********************************************************************
406 void ModuleBase_WidgetMultiSelector::updateSelectionList()
407 {
408   myListControl->clear();
409
410   AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
411   if (aSelectionListAttr.get()) {
412     for (int i = 0; i < aSelectionListAttr->size(); i++) {
413       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
414       myListControl->addItem(aAttr->namingName().c_str());
415     }
416   }
417   else {
418     AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
419     for (int i = 0; i < aRefListAttr->size(); i++) {
420       ObjectPtr anObject = aRefListAttr->object(i);
421       if (anObject.get())
422         myListControl->addItem(anObject->data()->name().c_str());
423     }
424   }
425   // We have to call repaint because sometimes the List control is not updated
426   myListControl->repaint();
427 }
428
429 //********************************************************************
430 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
431 {
432   std::string aType;
433
434   if (theType == "Vertices")
435     aType = "vertex";
436   else if (theType == "Edges")
437     aType = "edge";
438   else if (theType == "Faces")
439     aType = "face";
440   else if (theType == "Solids")
441     aType = "solid";
442
443   return aType;
444 }
445
446 //********************************************************************
447 void ModuleBase_WidgetMultiSelector::onCopyItem()
448 {
449   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
450   QString aRes;
451   foreach(QListWidgetItem* aItem, aItems) {
452     if (!aRes.isEmpty())
453       aRes += "\n";
454     aRes += aItem->text();
455   }
456   if (!aRes.isEmpty()) {
457     QClipboard *clipboard = QApplication::clipboard();
458     clipboard->setText(aRes);
459   }
460 }
461
462 //********************************************************************
463 void ModuleBase_WidgetMultiSelector::onListSelection()
464 {
465   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
466   myCopyAction->setEnabled(!aItems.isEmpty());
467 }