Salome HOME
8fc20d6b94e1bd76b76f13f82d4f26c8ce5b487f
[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_FilterNoDegeneratedEdge.h>
13 #include <ModuleBase_ISelection.h>
14 #include <ModuleBase_IWorkshop.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_Tools.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), myIsActive(false)
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   myIsActive = false;
98   activateShapeSelection();
99 }
100
101 //********************************************************************
102 void ModuleBase_WidgetMultiSelector::activateCustom()
103 {
104   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
105   connect(myWorkshop, SIGNAL(selectionChanged()), 
106           this,       SLOT(onSelectionChanged()), 
107           Qt::UniqueConnection);
108
109   myIsActive = true;
110   activateShapeSelection();
111 }
112
113 //********************************************************************
114 void ModuleBase_WidgetMultiSelector::deactivate()
115 {
116   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
117   myIsActive = false;
118   activateShapeSelection();
119 }
120
121 //********************************************************************
122 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
123 {
124   // the value is stored on the selection changed signal processing 
125   return true;
126 }
127
128 //********************************************************************
129 bool ModuleBase_WidgetMultiSelector::restoreValue()
130 {
131   // A rare case when plugin was not loaded. 
132   if(!myFeature)
133     return false;
134   DataPtr aData = myFeature->data();
135   AttributeSelectionListPtr aSelectionListAttr = 
136     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
137
138   if (aSelectionListAttr) {
139     // Restore shape type
140     setCurrentShapeType(
141       ModuleBase_WidgetShapeSelector::shapeType(aSelectionListAttr->selectionType().c_str()));
142     updateSelectionList(aSelectionListAttr);
143     return true;
144   }
145   return false;
146 }
147
148 //********************************************************************
149 void ModuleBase_WidgetMultiSelector::backupAttributeValue(const bool isBackup)
150 {
151   DataPtr aData = myFeature->data();
152   AttributeSelectionListPtr aSelectionListAttr = 
153     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
154   if (aSelectionListAttr.get() == NULL)
155     return;
156
157   if (isBackup) {
158     mySelectionType = aSelectionListAttr->selectionType();
159     mySelection.clear();
160     for (int i = 0; i < aSelectionListAttr->size(); i++) {
161       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
162       mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
163     }
164   }
165   else {
166     aSelectionListAttr->clear();
167     // Store shapes type
168     aSelectionListAttr->setSelectionType(mySelectionType);
169
170     // Store selection in the attribute
171     foreach (GeomSelection aSelec, mySelection) {
172       aSelectionListAttr->append(aSelec.first, aSelec.second);
173     }
174   }
175 }
176
177 //********************************************************************
178 bool ModuleBase_WidgetMultiSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
179 {
180   ModuleBase_ViewerPrs aPrs;
181   ModuleBase_ISelection* aSelection = myWorkshop->selection();
182   aSelection->fillPresentation(aPrs, theOwner);
183
184   const TopoDS_Shape& aTDSShape = aPrs.shape();
185   if (aTDSShape.IsNull())
186     return false;
187   GeomShapePtr aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
188   aShape->setImpl(new TopoDS_Shape(aTDSShape));
189
190   ObjectPtr anObject = aSelection->getSelectableObject(theOwner);
191   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
192   if (myFeature) {
193     // We can not select a result of our feature
194     const std::list<ResultPtr>& aResList = myFeature->results();
195     std::list<ResultPtr>::const_iterator aIt;
196     bool isSkipSelf = false;
197     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
198       if ((*aIt) == aResult) {
199         isSkipSelf = true;
200         break;
201       }
202     }
203     if(isSkipSelf)
204       return false;
205   }
206
207   // if the result has the similar shap as the parameter shape, just the context is set to the
208   // selection list attribute.
209   DataPtr aData = myFeature->data();
210   AttributeSelectionListPtr aSelectionListAttr = 
211     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
212   if (aShape->isEqual(aResult->shape()))
213     aSelectionListAttr->append(aResult, NULL);
214   else
215     aSelectionListAttr->append(aResult, aShape);
216
217   return true;
218 }
219
220 //********************************************************************
221 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
222 {
223   QList<QWidget*> result;
224   //result << myTypeCombo;
225   result << myListControl;
226   return result;
227 }
228
229 //********************************************************************
230 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
231 {
232   //TODO: Remove maybe?
233   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
234 }
235
236 //********************************************************************
237 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
238 {
239   activateShapeSelection();
240   QObjectPtrList anEmptyList;
241   myWorkshop->setSelected(anEmptyList);
242   // Clear mySelection, myListControl and storeValue()
243   onSelectionChanged();
244 }
245
246 //********************************************************************
247 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
248 {
249   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
250
251   DataPtr aData = myFeature->data();
252   AttributeSelectionListPtr aSelectionListAttr = 
253     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
254
255   aSelectionListAttr->clear();
256   if (aSelected.size() > 0) {
257     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
258       Handle(SelectMgr_EntityOwner) anOwner = aPrs.owner();
259       if (isValid(anOwner)) {
260         setSelection(anOwner);
261       }
262     }
263   }
264   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
265   // calls validators for the feature and, as a result, updates the Apply button state.
266   updateObject(myFeature);
267
268   emit valuesChanged();
269 }
270
271 //********************************************************************
272 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
273 {
274   QString aShapeTypeName;
275   
276   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
277     aShapeTypeName = myTypeCombo->itemText(idx);
278     TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
279     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
280       myIsActive = false;
281       activateShapeSelection();
282       bool isBlocked = myTypeCombo->blockSignals(true);
283       myTypeCombo->setCurrentIndex(idx);
284       myIsActive = true;
285       myTypeCombo->blockSignals(isBlocked);
286       activateShapeSelection();
287       break;
288     }
289   }
290 }
291
292 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
293 {
294   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
295
296   if (myIsActive) {
297     QString aNewType = myTypeCombo->currentText();
298     QIntList aList;
299     aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
300     myWorkshop->activateSubShapesSelection(aList);
301
302     // it is necessary to filter the selected edges to be non-degenerated
303     // it is not possible to build naming name for such edges
304     if (aNewType == "Edges") {
305       myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge();
306       aViewer->addSelectionFilter(myEdgesTypeFilter);
307     }
308     else {
309       aViewer->removeSelectionFilter(myEdgesTypeFilter);
310     }
311
312   } else {
313     myWorkshop->deactivateSubShapesSelection();
314     aViewer->removeSelectionFilter(myEdgesTypeFilter);
315   }
316
317   activateFilters(myWorkshop, myIsActive);
318 }
319
320 //********************************************************************
321 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
322 {
323   myListControl->clear();
324   for (int i = 0; i < theList->size(); i++) {
325     AttributeSelectionPtr aAttr = theList->value(i);
326     myListControl->addItem(aAttr->namingName().c_str());
327   }
328 }
329
330 //********************************************************************
331 void ModuleBase_WidgetMultiSelector::onCopyItem()
332 {
333   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
334   QString aRes;
335   foreach(QListWidgetItem* aItem, aItems) {
336     if (!aRes.isEmpty())
337       aRes += "\n";
338     aRes += aItem->text();
339   }
340   if (!aRes.isEmpty()) {
341     QClipboard *clipboard = QApplication::clipboard();
342     clipboard->setText(aRes);
343   }
344 }
345
346 //********************************************************************
347 void ModuleBase_WidgetMultiSelector::onListSelection()
348 {
349   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
350   myCopyAction->setEnabled(!aItems.isEmpty());
351 }
352