Salome HOME
Task 5.1.7: To be able to export a part to a file and import it into an existing...
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_SelectionMgr.h"
21
22 #include "XGUI_Workshop.h"
23 #include "XGUI_ObjectsBrowser.h"
24 #include "XGUI_SalomeConnector.h"
25 #include "XGUI_ViewerProxy.h"
26 #include "XGUI_Displayer.h"
27 #include "XGUI_Selection.h"
28 #include "XGUI_OperationMgr.h"
29 #include "XGUI_SelectionActivate.h"
30
31 #ifndef HAVE_SALOME
32 #include <AppElements_MainWindow.h>
33 #endif
34
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_AttributeDocRef.h>
38 #include <ModelAPI_Data.h>
39 #include <ModelAPI_Result.h>
40 #include <ModelAPI_Object.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_Tools.h>
43 #include <ModelAPI_ResultField.h>
44
45 #include <GeomAPI_Shape.h>
46
47 #include <ModuleBase_ViewerPrs.h>
48 #include <ModuleBase_Tools.h>
49
50 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopTools_MapOfShape.hxx>
54
55 #ifdef TINSPECTOR
56 #include <inspector/VInspectorAPI_CallBack.hxx>
57 #endif
58
59 #define OPTIMIZATION_LEVEL 50
60
61
62 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
63     : QObject(theParent),
64       myWorkshop(theParent)
65 {
66   mySelection = new XGUI_Selection(myWorkshop);
67 }
68
69 XGUI_SelectionMgr::~XGUI_SelectionMgr()
70 {
71   delete mySelection;
72 }
73
74 //**************************************************************
75 void XGUI_SelectionMgr::connectViewers()
76 {
77   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
78           SLOT(onObjectBrowserSelection()));
79
80   //Connect to other viewers
81   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
82 }
83
84 //**************************************************************
85 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
86                                           bool isUpdateViewer)
87 {
88   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
89   if (!aContext.IsNull()) {
90     /// previous selection should be cleared, else there will be decomposition of selections:
91     /// as AddOrRemoveSelected inverts current selection
92     aContext->ClearSelected(false);
93
94     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
95       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
96
97       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
98       #ifdef TINSPECTOR
99       if (myWorkshop->displayer()->getCallBack())
100         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
101       #endif
102     }
103   }
104 }
105
106 //**************************************************************
107 void XGUI_SelectionMgr::onObjectBrowserSelection()
108 {
109   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
110     myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
111   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
112   if (!myWorkshop->operationMgr()->hasOperation()) {
113
114     ObjectPtr aObject;
115     FeaturePtr aFeature;
116     // Select all results of a selected feature in viewer
117     foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
118       aObject = aPrs->object();
119       if (aObject.get()) {
120         aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
121         if (aFeature.get()) {
122           std::list<ResultPtr> allRes;
123           ModelAPI_Tools::allResults(aFeature, allRes);
124           std::list<ResultPtr>::iterator aRes;
125           for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
126             aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
127               new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
128           }
129         }
130       }
131     }
132   }
133   aDisplayer->setSelected(aSelectedPrs);
134   myWorkshop->updateColorScaleVisibility();
135   emit selectionChanged();
136 }
137
138 //**************************************************************
139 void XGUI_SelectionMgr::onViewerSelection()
140 {
141   QList<ModuleBase_ViewerPrsPtr> aValues;
142   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
143   if (!aContext.IsNull())
144     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
145
146   QObjectPtrList anObjects;
147   convertToObjectBrowserSelection(aValues, anObjects);
148   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
149
150   emit selectionChanged();
151 }
152
153 //**************************************************************
154 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
155 {
156   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
157
158   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
159   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
160     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
161     if (anOwner.IsNull()) // TODO: check why it is possible
162       continue;
163     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
164       aResultOwners.Append(anOwner);
165   }
166   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
167   Handle(SelectMgr_EntityOwner) anOwner;
168   for (; anOwnersIt.More(); anOwnersIt.Next()) {
169     anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
170     if (!anOwner.IsNull())
171       aContext->AddOrRemoveSelected(anOwner, false);
172   }
173 }
174
175 //**************************************************************
176 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
177 {
178   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
179                myWorkshop->selector()->selection()->getSelected(thePlace);
180   if (thePlace == ModuleBase_ISelection::Browser) {
181     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182     aDisplayer->setSelected(aSelectedPrs);
183   }
184
185 }
186 //**************************************************************
187 void XGUI_SelectionMgr::clearSelection()
188 {
189   QObjectPtrList aFeatures;
190   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
191
192   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
193              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
194
195   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
196   aDisplayer->setSelected(aSelectedPrs);
197
198   emit selectionChanged();
199 }
200 //**************************************************************
201 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
202 {
203   // update selection in Viewer
204   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
205   aDisplayer->setSelected(theValues);
206
207   // update selection in Object Browser
208   QObjectPtrList anObjects;
209   convertToObjectBrowserSelection(theValues, anObjects);
210   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
211 }
212
213 //**************************************************************
214 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
215                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
216                                    QObjectPtrList& theObjects)
217 {
218   theObjects.clear();
219
220   ResultPtr aResult;
221   FeaturePtr aFeature;
222   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
223   SessionPtr aMgr = ModelAPI_Session::get();
224   DocumentPtr anActiveDocument = aMgr->activeDocument();
225
226   TopTools_MapOfShape aShapeMap;
227   bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
228
229   GeomShapePtr aShape;
230   TopoDS_Shape aTShape;
231   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
232     if (aPrs->object().get()) {
233       if (!theObjects.contains(aPrs->object()))
234         theObjects.append(aPrs->object());
235       if (aPrs->shape().get() && (!aHasOperation)) {
236         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
237         if (aResult.get()) {
238           aShape = aPrs->shape();
239           aTShape = aShape->impl<TopoDS_Shape>();
240           if (aToOptimize) {
241             if (!aShapeMap.Contains(aTShape)) {
242               aFeature = anActiveDocument->producedByFeature(aResult, aShape);
243               if (aFeature.get()) {
244                 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
245                 foreach(TopoDS_Shape aShape, aResList) {
246                   if (!aShapeMap.Contains(aShape))
247                     aShapeMap.Add(aShape);
248                 }
249               }
250             }
251           }
252           else {
253             aFeature = anActiveDocument->producedByFeature(aResult, aShape);
254           }
255           if (aFeature.get() && (!theObjects.contains(aFeature)))
256             theObjects.append(aFeature);
257         }
258       }
259     }
260   }
261 }
262
263 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
264 {
265   std::list<FeaturePtr> aFeatures;
266   QObjectPtrList aObjects = selection()->selectedObjects();
267   if (aObjects.isEmpty())
268     return aFeatures;
269
270   bool isPart = false;
271   foreach(ObjectPtr aObj, aObjects) {
272     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
273     if (aFeature.get()) {
274       ResultPtr aRes = aFeature->firstResult();
275       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
276       if (!isPart)
277         aFeatures.push_back(aFeature);
278     }
279   }
280   return aFeatures;
281 }
282
283 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
284 {
285   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
286   GeomShapePtr aResShape = theResult->shape();
287   TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
288   QList<TopoDS_Shape> aResult;
289   foreach(int aShapeType, aModes) {
290     if (aShapeType < TopAbs_SHAPE) {
291       TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
292       for (; aExp.More(); aExp.Next()) {
293         aResult.append(aExp.Current());
294       }
295     }
296   }
297   return aResult;
298 }