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_ModelWidget(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   QString aTypesStr("Vertices Edges Faces Solids");
53   QStringList aShapeTypes = aTypesStr.split(' ');
54   myTypeCombo->addItems(aShapeTypes);
55   aMainLay->addWidget(myTypeCombo, 0, 1);
56
57   QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
58   aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
59
60   myListControl = new QListWidget(this);
61   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
62   aMainLay->setRowStretch(2, 1);
63   aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
64   aMainLay->setRowMinimumHeight(3, 20);
65   this->setLayout(aMainLay);
66   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
67
68   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
69   myCopyAction->setShortcut(QKeySequence::Copy);
70   myCopyAction->setEnabled(false);
71   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
72   myListControl->addAction(myCopyAction);
73   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
74   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
75
76   activateSelection(true);
77 }
78
79 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
80 {
81   activateSelection(false);
82 }
83
84 //********************************************************************
85 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
86 {
87   // A rare case when plugin was not loaded. 
88   if(!myFeature)
89     return false;
90   DataPtr aData = myFeature->data();
91   AttributeSelectionListPtr aSelectionListAttr = 
92     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
93
94   if (aSelectionListAttr) {
95     aSelectionListAttr->clear();
96     // Store shapes type
97     TopAbs_ShapeEnum aCurrentType =
98           ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
99     aSelectionListAttr->setSelectionType((int) aCurrentType);
100     // Store selection in the attribute
101     foreach (GeomSelection aSelec, mySelection) {
102       aSelectionListAttr->append(aSelec.first, aSelec.second);
103     }
104     //updateSelectionList(aSelectionListAttr);
105     updateObject(myFeature);
106     return true;
107   }
108   return false;
109 }
110
111 //********************************************************************
112 bool ModuleBase_WidgetMultiSelector::restoreValue()
113 {
114   // A rare case when plugin was not loaded. 
115   if(!myFeature)
116     return false;
117   DataPtr aData = myFeature->data();
118   AttributeSelectionListPtr aSelectionListAttr = 
119     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
120
121   if (aSelectionListAttr) {
122     mySelection.clear();
123     // Restore shape type
124     TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType();
125     setCurrentShapeType(aShapeType);
126     // Restore selection in the list
127     for (int i = 0; i < aSelectionListAttr->size(); i++) {
128       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
129       mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
130     }
131     updateSelectionList(aSelectionListAttr);
132     return true;
133   }
134   return false;
135 }
136
137 //********************************************************************
138 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
139 {
140   QList<QWidget*> result;
141   //result << myTypeCombo;
142   result << myListControl;
143   return result;
144 }
145
146 //********************************************************************
147 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
148 {
149   //TODO: Remove maybe?
150   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
151 }
152
153 //********************************************************************
154 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
155 {
156   myIsActive = toActivate;
157   if (myIsActive) {
158     connect(myWorkshop, SIGNAL(selectionChanged()), 
159             this,       SLOT(onSelectionChanged()), 
160             Qt::UniqueConnection);
161     activateShapeSelection();
162   } else {
163     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
164     myWorkshop->deactivateSubShapesSelection();
165
166     myWorkshop->viewer()->removeSelectionFilter(myEdgesTypeFilter);
167   }
168 }
169
170 //********************************************************************
171 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
172 {
173   activateShapeSelection();
174   QObjectPtrList anEmptyList;
175   myWorkshop->setSelected(anEmptyList);
176   // Clear mySelection, myListControl and storeValue()
177   onSelectionChanged();
178 }
179
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
182 {
183   ModuleBase_ISelection* aSelection = myWorkshop->selection();
184   NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
185   std::list<ObjectPtr> aOwnersList;
186   aSelection->selectedShapes(aSelectedShapes, aOwnersList);
187
188   mySelection.clear();
189   std::list<ObjectPtr>::const_iterator aIt;
190   NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
191   GeomShapePtr aShape;
192   for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
193     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
194     if (myFeature) {
195       // We can not select a result of our feature
196       const std::list<ResultPtr>& aResList = myFeature->results();
197       std::list<ResultPtr>::const_iterator aIt;
198       bool isSkipSelf = false;
199       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
200         if ((*aIt) == aResult) {
201           isSkipSelf = true;
202           break;
203         }
204       }
205       if(isSkipSelf)
206         continue;
207     }
208     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
209     aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
210     mySelection.append(GeomSelection(aResult, aShape));
211   }
212   //updateSelectionList();
213   emit valuesChanged();
214 }
215
216 //********************************************************************
217 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
218                                                   NCollection_List<TopoDS_Shape>& theResult)
219 {
220   if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
221     return;
222   TopAbs_ShapeEnum aReferenceType =
223       ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
224   NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
225   for (; anIter.More(); anIter.Next()) {
226     TopoDS_Shape aShape = anIter.Value();
227     if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
228       continue;
229     theResult.Append(aShape);
230   }
231 }
232
233 //********************************************************************
234 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
235 {
236   QString aShapeTypeName;
237   
238   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
239     aShapeTypeName = myTypeCombo->itemText(idx);
240     TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
241     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
242       activateSelection(false);
243       bool isBlocked = myTypeCombo->blockSignals(true);
244       myTypeCombo->setCurrentIndex(idx);
245       myTypeCombo->blockSignals(isBlocked);
246       activateSelection(true);
247       break;
248     }
249   }
250 }
251
252 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
253 {
254   QString aNewType = myTypeCombo->currentText();
255   QIntList aList;
256   aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
257   myWorkshop->activateSubShapesSelection(aList);
258
259   // it is necessary to filter the selected edges to be non-degenerated
260   // it is not possible to build naming name for such edges
261   if (aNewType == "Edges") {
262     myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge();
263     ModuleBase_IViewer* aViewer = myWorkshop->viewer();
264     aViewer->addSelectionFilter(myEdgesTypeFilter);
265   }
266   else {
267     myWorkshop->viewer()->removeSelectionFilter(myEdgesTypeFilter);
268   }
269 }
270
271 //********************************************************************
272 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
273 {
274   myListControl->clear();
275   for (int i = 0; i < theList->size(); i++) {
276     AttributeSelectionPtr aAttr = theList->value(i);
277     myListControl->addItem(aAttr->namingName().c_str());
278   }
279 }
280
281 //********************************************************************
282 void ModuleBase_WidgetMultiSelector::onCopyItem()
283 {
284   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
285   QString aRes;
286   foreach(QListWidgetItem* aItem, aItems) {
287     if (!aRes.isEmpty())
288       aRes += "\n";
289     aRes += aItem->text();
290   }
291   if (!aRes.isEmpty()) {
292     QClipboard *clipboard = QApplication::clipboard();
293     clipboard->setText(aRes);
294   }
295 }
296
297 //********************************************************************
298 void ModuleBase_WidgetMultiSelector::onListSelection()
299 {
300   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
301   myCopyAction->setEnabled(!aItems.isEmpty());
302 }
303