Salome HOME
Fix too long line (>100 characters)
[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
44 #include <GeomAPI_Shape.h>
45
46 #include <ModuleBase_ViewerPrs.h>
47 #include <ModuleBase_Tools.h>
48
49 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopTools_MapOfShape.hxx>
53
54 #ifdef TINSPECTOR
55 #include <inspector/VInspectorAPI_CallBack.hxx>
56 #endif
57
58 #define OPTIMIZATION_LEVEL 50
59
60
61 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
62     : QObject(theParent),
63       myWorkshop(theParent)
64 {
65   mySelection = new XGUI_Selection(myWorkshop);
66 }
67
68 XGUI_SelectionMgr::~XGUI_SelectionMgr()
69 {
70   delete mySelection;
71 }
72
73 //**************************************************************
74 void XGUI_SelectionMgr::connectViewers()
75 {
76   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
77           SLOT(onObjectBrowserSelection()));
78
79   //Connect to other viewers
80   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
81 }
82
83 //**************************************************************
84 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
85                                           bool isUpdateViewer)
86 {
87   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
88   if (!aContext.IsNull()) {
89     /// previous selection should be cleared, else there will be decomposition of selections:
90     /// as AddOrRemoveSelected inverts current selection
91     aContext->ClearSelected(false);
92
93     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
94       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
95
96       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
97       #ifdef TINSPECTOR
98       if (myWorkshop->displayer()->getCallBack())
99         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
100       #endif
101     }
102   }
103 }
104
105 //**************************************************************
106 void XGUI_SelectionMgr::onObjectBrowserSelection()
107 {
108   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
109     myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
110   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
111   if (!myWorkshop->operationMgr()->hasOperation()) {
112
113     ObjectPtr aObject;
114     FeaturePtr aFeature;
115     // Select all results of a selected feature in viewer
116     foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
117       aObject = aPrs->object();
118       if (aObject.get()) {
119         aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
120         if (aFeature.get()) {
121           std::list<ResultPtr> allRes;
122           ModelAPI_Tools::allResults(aFeature, allRes);
123           std::list<ResultPtr>::iterator aRes;
124           for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
125             aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
126               new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
127           }
128         }
129       }
130     }
131   }
132   aDisplayer->setSelected(aSelectedPrs);
133   emit selectionChanged();
134 }
135
136 //**************************************************************
137 void XGUI_SelectionMgr::onViewerSelection()
138 {
139   QList<ModuleBase_ViewerPrsPtr> aValues;
140   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
141   if (!aContext.IsNull())
142     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
143
144   QObjectPtrList anObjects;
145   convertToObjectBrowserSelection(aValues, anObjects);
146   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
147
148   emit selectionChanged();
149 }
150
151 //**************************************************************
152 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
153 {
154   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
155
156   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
157   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
158     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
159     if (anOwner.IsNull()) // TODO: check why it is possible
160       continue;
161     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
162       aResultOwners.Append(anOwner);
163   }
164   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
165   Handle(SelectMgr_EntityOwner) anOwner;
166   for (; anOwnersIt.More(); anOwnersIt.Next()) {
167     anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
168     if (!anOwner.IsNull())
169       aContext->AddOrRemoveSelected(anOwner, false);
170   }
171 }
172
173 //**************************************************************
174 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
175 {
176   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
177                myWorkshop->selector()->selection()->getSelected(thePlace);
178   if (thePlace == ModuleBase_ISelection::Browser) {
179     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
180     aDisplayer->setSelected(aSelectedPrs);
181   }
182
183 }
184 //**************************************************************
185 void XGUI_SelectionMgr::clearSelection()
186 {
187   QObjectPtrList aFeatures;
188   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
189
190   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
191              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
192
193   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
194   aDisplayer->setSelected(aSelectedPrs);
195
196   emit selectionChanged();
197 }
198 //**************************************************************
199 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
200 {
201   // update selection in Viewer
202   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203   aDisplayer->setSelected(theValues);
204
205   // update selection in Object Browser
206   QObjectPtrList anObjects;
207   convertToObjectBrowserSelection(theValues, anObjects);
208   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
209 }
210
211 //**************************************************************
212 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
213                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
214                                    QObjectPtrList& theObjects)
215 {
216   theObjects.clear();
217
218   ResultPtr aResult;
219   FeaturePtr aFeature;
220   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
221   SessionPtr aMgr = ModelAPI_Session::get();
222   DocumentPtr anActiveDocument = aMgr->activeDocument();
223
224   TopTools_MapOfShape aShapeMap;
225   bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
226
227   GeomShapePtr aShape;
228   TopoDS_Shape aTShape;
229   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
230     if (aPrs->object().get()) {
231       if (!theObjects.contains(aPrs->object()))
232         theObjects.append(aPrs->object());
233       if (aPrs->shape().get() && (!aHasOperation)) {
234         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
235         if (aResult.get()) {
236           aShape = aPrs->shape();
237           aTShape = aShape->impl<TopoDS_Shape>();
238           if (aToOptimize) {
239             if (!aShapeMap.Contains(aTShape)) {
240               aFeature = anActiveDocument->producedByFeature(aResult, aShape);
241               if (aFeature.get()) {
242                 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
243                 foreach(TopoDS_Shape aShape, aResList) {
244                   if (!aShapeMap.Contains(aShape))
245                     aShapeMap.Add(aShape);
246                 }
247               }
248             }
249           }
250           else {
251             aFeature = anActiveDocument->producedByFeature(aResult, aShape);
252           }
253           if (aFeature.get() && (!theObjects.contains(aFeature)))
254             theObjects.append(aFeature);
255         }
256       }
257     }
258   }
259 }
260
261 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
262 {
263   std::list<FeaturePtr> aFeatures;
264   QObjectPtrList aObjects = selection()->selectedObjects();
265   if (aObjects.isEmpty())
266     return aFeatures;
267
268   bool isPart = false;
269   foreach(ObjectPtr aObj, aObjects) {
270     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
271     if (aFeature.get()) {
272       ResultPtr aRes = aFeature->firstResult();
273       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
274       if (!isPart)
275         aFeatures.push_back(aFeature);
276     }
277   }
278   return aFeatures;
279 }
280
281 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
282 {
283   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
284   GeomShapePtr aResShape = theResult->shape();
285   TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
286   QList<TopoDS_Shape> aResult;
287   foreach(int aShapeType, aModes) {
288     if (aShapeType < TopAbs_SHAPE) {
289       TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
290       for (; aExp.More(); aExp.Next()) {
291         aResult.append(aExp.Current());
292       }
293     }
294   }
295   return aResult;
296 }