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