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