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