]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
[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
21 #include <Config_WidgetAPI.h>
22
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QListWidget>
26 #include <QObject>
27 #include <QString>
28 #include <QComboBox>
29 #include <QEvent>
30 #include <QAction>
31 #include <QApplication>
32 #include <QClipboard>
33
34 #include <memory>
35 #include <string>
36
37 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
38                                                                ModuleBase_IWorkshop* theWorkshop,
39                                                                const Config_WidgetAPI* theData,
40                                                                const std::string& theParentId)
41     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
42       myWorkshop(theWorkshop)
43 {
44   QGridLayout* aMainLay = new QGridLayout(this);
45   ModuleBase_Tools::adjustMargins(aMainLay);
46
47   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
48   aMainLay->addWidget(aTypeLabel, 0, 0);
49
50   myTypeCombo = new QComboBox(this);
51   // There is no sence to paramerize list of types while we can not parametrize selection mode
52
53   std::string aPropertyTypes = theData->getProperty("type_choice");
54   QString aTypesStr = aPropertyTypes.c_str();
55   QStringList aShapeTypes = aTypesStr.split(' ');
56
57   myTypeCombo->addItems(aShapeTypes);
58   aMainLay->addWidget(myTypeCombo, 0, 1);
59   // if the xml definition contains one type, the controls to select a type should not be shown
60   if (aShapeTypes.size() == 1) {
61     aTypeLabel->setVisible(false);
62     myTypeCombo->setVisible(false);
63   }
64
65   QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
66   aMainLay->addWidget(aListLabel, 1, 0);
67   // if the xml definition contains one type, an information label should be shown near to the latest
68   if (aShapeTypes.size() == 1) {
69     QString aLabelText = QString::fromStdString(theData->widgetLabel());
70     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
71     QLabel* aSelectedLabel = new QLabel(aLabelText, this);
72     if (!aLabelIcon.isEmpty())
73       aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
74     aMainLay->addWidget(aSelectedLabel, 1, 1);
75     aMainLay->setColumnStretch(2, 1);
76   }
77
78   myListControl = new QListWidget(this);
79   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
80   aMainLay->setRowStretch(2, 1);
81   aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
82   aMainLay->setRowMinimumHeight(3, 20);
83   this->setLayout(aMainLay);
84   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
85
86   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
87   myCopyAction->setShortcut(QKeySequence::Copy);
88   myCopyAction->setEnabled(false);
89   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
90   myListControl->addAction(myCopyAction);
91   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
92   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
93 }
94
95 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
96 {
97   activateShapeSelection(false);
98 }
99
100 //********************************************************************
101 void ModuleBase_WidgetMultiSelector::activateCustom()
102 {
103   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
104   connect(myWorkshop, SIGNAL(selectionChanged()), 
105           this,       SLOT(onSelectionChanged()), 
106           Qt::UniqueConnection);
107
108   activateShapeSelection(true);
109
110   QObjectPtrList anObjects;
111   // Restore selection in the viewer by the attribute selection list
112   if(myFeature) {
113     DataPtr aData = myFeature->data();
114     AttributeSelectionListPtr aListAttr = 
115       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
116     if (aListAttr) {
117       for (int i = 0; i < aListAttr->size(); i++) {
118         AttributeSelectionPtr anAttr = aListAttr->value(i);
119         ResultPtr anObject = anAttr->context();
120         if (anObject.get())
121           anObjects.append(anObject);
122       }
123     }
124   }
125   myWorkshop->setSelected(anObjects);
126 }
127
128 //********************************************************************
129 void ModuleBase_WidgetMultiSelector::deactivate()
130 {
131   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
132   activateShapeSelection(false);
133 }
134
135 //********************************************************************
136 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
137 {
138   // the value is stored on the selection changed signal processing 
139   // A rare case when plugin was not loaded. 
140   if(!myFeature)
141     return false;
142   DataPtr aData = myFeature->data();
143   AttributeSelectionListPtr aSelectionListAttr = 
144     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
145
146   if (aSelectionListAttr) {
147     // Store shapes type
148      TopAbs_ShapeEnum aCurrentType =
149            ModuleBase_Tools::shapeType(myTypeCombo->currentText());
150      aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
151   }   
152    return true;
153 }
154
155 //********************************************************************
156 bool ModuleBase_WidgetMultiSelector::restoreValue()
157 {
158   // A rare case when plugin was not loaded. 
159   if(!myFeature)
160     return false;
161   DataPtr aData = myFeature->data();
162   AttributeSelectionListPtr aSelectionListAttr = 
163     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
164
165   if (aSelectionListAttr) {
166     // Restore shape type
167     if (!aSelectionListAttr->selectionType().empty())
168       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
169     updateSelectionList(aSelectionListAttr);
170     return true;
171   }
172   return false;
173 }
174
175 //********************************************************************
176 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
177 {
178   DataPtr aData = myFeature->data();
179   AttributeSelectionListPtr aSelectionListAttr = 
180     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
181   if (aSelectionListAttr.get() == NULL)
182     return;
183
184   mySelectionType = aSelectionListAttr->selectionType();
185   mySelection.clear();
186   int aSize = aSelectionListAttr->size();
187   for (int i = 0; i < aSelectionListAttr->size(); i++) {
188     AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
189     mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
190   }
191 }
192
193 //********************************************************************
194 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
195 {
196   DataPtr aData = myFeature->data();
197   AttributeSelectionListPtr aSelectionListAttr = 
198     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
199   if (aSelectionListAttr.get() == NULL)
200     return;
201   aSelectionListAttr->clear();
202
203   // Store shapes type
204   aSelectionListAttr->setSelectionType(mySelectionType);
205
206   // Store selection in the attribute
207   int aSize = mySelection.size();
208   foreach (GeomSelection aSelec, mySelection) {
209     aSelectionListAttr->append(aSelec.first, aSelec.second);
210   }
211 }
212
213 //********************************************************************
214 bool ModuleBase_WidgetMultiSelector::setSelection(const QList<ModuleBase_ViewerPrs>& theValues,
215                                                   int& thePosition)
216 {
217   if (thePosition < 0)
218     return false;
219
220   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
221   bool isDone = false;
222   for (int i = thePosition; i < theValues.size(); i++) {
223     ModuleBase_ViewerPrs aValue = theValues[i];
224     thePosition++;
225     bool aProcessed = false;
226     if (isValidSelection(aValue)) {
227       aProcessed = setSelectionCustom(aValue);
228     }
229     // if there is at least one set, the result is true
230     isDone = isDone || aProcessed;
231     // when an object, which do not satisfy the validating process, stop set selection
232     if (!aProcessed)
233       break;
234   }
235   if (isDone) {
236     updateObject(myFeature);
237     emit valuesChanged();
238   }
239   return isDone;
240 }
241
242 //********************************************************************
243 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
244 {
245   TopoDS_Shape aShape = thePrs.shape();
246   if ((myTypeCombo->count() > 1) && (!aShape.IsNull())) {
247     TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
248     if (aShape.ShapeType() != aType)
249       return false;
250   }
251   ResultPtr aResult;
252   if (!thePrs.owner().IsNull()) {
253     ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
254     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
255   }
256   else {
257     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
258   }
259
260
261   if (myFeature) {
262     // We can not select a result of our feature
263     const std::list<ResultPtr>& aResList = myFeature->results();
264     std::list<ResultPtr>::const_iterator aIt;
265     bool isSkipSelf = false;
266     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
267       if ((*aIt) == aResult) {
268         isSkipSelf = true;
269         break;
270       }
271     }
272     if(isSkipSelf)
273       return false;
274   }
275
276   // if the result has the similar shap as the parameter shape, just the context is set to the
277   // selection list attribute.
278   DataPtr aData = myFeature->data();
279   AttributeSelectionListPtr aSelectionListAttr = 
280     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
281
282   const TopoDS_Shape& aTDSShape = thePrs.shape();
283   // if only result is selected, an empty shape is set to the model
284   if (aTDSShape.IsNull()) {
285     aSelectionListAttr->append(aResult, GeomShapePtr());
286   }
287   else {
288     GeomShapePtr aShape(new GeomAPI_Shape());
289     aShape->setImpl(new TopoDS_Shape(aTDSShape));
290     // We can not select a result of our feature
291     if (aShape->isEqual(aResult->shape()))
292       aSelectionListAttr->append(aResult, GeomShapePtr());
293     else
294       aSelectionListAttr->append(aResult, aShape);
295   }
296   return true;
297 }
298
299 //********************************************************************
300 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
301 {
302   QList<QWidget*> result;
303   //result << myTypeCombo;
304   result << myListControl;
305   return result;
306 }
307
308 //********************************************************************
309 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
310 {
311   //TODO: Remove maybe?
312   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
313 }
314
315 //********************************************************************
316 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
317 {
318   activateShapeSelection(true);
319   QObjectPtrList anEmptyList;
320   // This method will call Selection changed event which will call onSelectionChanged
321   // To clear mySelection, myListControl and storeValue()
322   // So, we don't need to call it
323   myWorkshop->setSelected(anEmptyList);
324 }
325
326 //********************************************************************
327 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
328 {
329   QList<ModuleBase_ViewerPrs> aSelected = getSelectedEntitiesOrObjects(myWorkshop->selection());
330
331   DataPtr aData = myFeature->data();
332   AttributeSelectionListPtr aSelectionListAttr = 
333     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
334
335   aSelectionListAttr->clear();
336   if (aSelected.size() > 0) {
337     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
338       if (isValidSelection(aPrs)) {
339         setSelectionCustom(aPrs);
340       }
341     }
342   }
343   emit valuesChanged();
344   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
345   // calls validators for the feature and, as a result, updates the Apply button state.
346   updateObject(myFeature);
347 }
348
349 //********************************************************************
350 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
351 {
352   QString aShapeTypeName;
353   
354   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
355     aShapeTypeName = myTypeCombo->itemText(idx);
356     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
357     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
358       activateShapeSelection(false);
359       bool isBlocked = myTypeCombo->blockSignals(true);
360       myTypeCombo->setCurrentIndex(idx);
361       myTypeCombo->blockSignals(isBlocked);
362       activateShapeSelection(true);
363       break;
364     }
365   }
366 }
367
368 void ModuleBase_WidgetMultiSelector::activateShapeSelection(const bool isActivated)
369 {
370   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
371
372   if (isActivated) {
373     QString aNewType = myTypeCombo->currentText();
374     QIntList aList;
375     aList.append(ModuleBase_Tools::shapeType(aNewType));
376     myWorkshop->activateSubShapesSelection(aList);
377   } else {
378     myWorkshop->deactivateSubShapesSelection();
379   }
380
381   activateFilters(myWorkshop, isActivated);
382 }
383
384 //********************************************************************
385 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
386 {
387   myListControl->clear();
388   for (int i = 0; i < theList->size(); i++) {
389     AttributeSelectionPtr aAttr = theList->value(i);
390     myListControl->addItem(aAttr->namingName().c_str());
391   }
392   // We have to call repaint because sometimes the List control is not updated
393   myListControl->repaint();
394 }
395
396 //********************************************************************
397 void ModuleBase_WidgetMultiSelector::onCopyItem()
398 {
399   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
400   QString aRes;
401   foreach(QListWidgetItem* aItem, aItems) {
402     if (!aRes.isEmpty())
403       aRes += "\n";
404     aRes += aItem->text();
405   }
406   if (!aRes.isEmpty()) {
407     QClipboard *clipboard = QApplication::clipboard();
408     clipboard->setText(aRes);
409   }
410 }
411
412 //********************************************************************
413 void ModuleBase_WidgetMultiSelector::onListSelection()
414 {
415   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
416   myCopyAction->setEnabled(!aItems.isEmpty());
417 }
418