Salome HOME
Fix obvious warnings
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-2020  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   myLastSelectionPlace = ModuleBase_ISelection::Browser;
110   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
111     myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
112   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
113   if (!myWorkshop->operationMgr()->hasOperation()) {
114
115     ObjectPtr aObject;
116     FeaturePtr aFeature;
117     // Select all results of a selected feature in viewer
118     foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
119       aObject = aPrs->object();
120       if (aObject.get()) {
121         aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
122         if (aFeature.get()) {
123           std::list<ResultPtr> allRes;
124           ModelAPI_Tools::allResults(aFeature, allRes);
125           std::list<ResultPtr>::iterator aRes;
126           for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
127             aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
128               new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
129           }
130         }
131       }
132     }
133   }
134   aDisplayer->setSelected(aSelectedPrs);
135   myWorkshop->updateColorScaleVisibility();
136   emit selectionChanged();
137 }
138
139 //**************************************************************
140 void XGUI_SelectionMgr::onViewerSelection()
141 {
142   myLastSelectionPlace = ModuleBase_ISelection::Viewer;
143   QList<ModuleBase_ViewerPrsPtr> aValues;
144   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
145   if (!aContext.IsNull()) {
146     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
147     // Update is necessary for OCCT 7.4.0: when it is clears selection it doesn't updates viewer
148 #if OCC_VERSION_HEX == 0x070400
149     if (aValues.isEmpty())
150       aContext->UpdateCurrentViewer();
151 #endif
152   }
153   QObjectPtrList anObjects;
154   convertToObjectBrowserSelection(aValues, anObjects);
155   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
156
157   emit selectionChanged();
158 }
159
160 //**************************************************************
161 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
162 {
163   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
164
165   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
166   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
167     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
168     if (anOwner.IsNull()) // TODO: check why it is possible
169       continue;
170     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
171       aResultOwners.Append(anOwner);
172   }
173   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
174   Handle(SelectMgr_EntityOwner) anOwner;
175   for (; anOwnersIt.More(); anOwnersIt.Next()) {
176     anOwner = anOwnersIt.Value();
177     if (!anOwner.IsNull())
178       aContext->AddOrRemoveSelected(anOwner, false);
179   }
180 }
181
182 //**************************************************************
183 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
184 {
185   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
186                myWorkshop->selector()->selection()->getSelected(thePlace);
187   if (thePlace == ModuleBase_ISelection::Browser) {
188     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
189     aDisplayer->setSelected(aSelectedPrs);
190   }
191
192 }
193 //**************************************************************
194 void XGUI_SelectionMgr::clearSelection()
195 {
196   QObjectPtrList aFeatures;
197   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
198
199   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
200              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
201
202   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203   aDisplayer->setSelected(aSelectedPrs);
204
205   emit selectionChanged();
206 }
207 //**************************************************************
208 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
209 {
210   // update selection in Viewer
211   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
212   aDisplayer->setSelected(theValues);
213
214   // update selection in Object Browser
215   QObjectPtrList anObjects;
216   convertToObjectBrowserSelection(theValues, anObjects);
217   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
218 }
219
220 //**************************************************************
221 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
222                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
223                                    QObjectPtrList& theObjects)
224 {
225   theObjects.clear();
226
227   ResultPtr aResult;
228   FeaturePtr aFeature;
229   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
230   SessionPtr aMgr = ModelAPI_Session::get();
231   DocumentPtr anActiveDocument = aMgr->activeDocument();
232
233   TopTools_MapOfShape aShapeMap;
234   bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
235
236   GeomShapePtr aShape;
237   TopoDS_Shape aTShape;
238   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
239     if (aPrs->object().get()) {
240       if (!theObjects.contains(aPrs->object()))
241         theObjects.append(aPrs->object());
242       if (aPrs->shape().get() && (!aHasOperation)) {
243         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
244         if (aResult.get()) {
245           aShape = aPrs->shape();
246           aTShape = aShape->impl<TopoDS_Shape>();
247           if (aToOptimize) {
248             if (!aShapeMap.Contains(aTShape)) {
249               aFeature = anActiveDocument->producedByFeature(aResult, aShape);
250               if (aFeature.get()) {
251                 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
252                 foreach(TopoDS_Shape aShape, aResList) {
253                   if (!aShapeMap.Contains(aShape))
254                     aShapeMap.Add(aShape);
255                 }
256               }
257             }
258           }
259           else {
260             aFeature = anActiveDocument->producedByFeature(aResult, aShape);
261           }
262           if (aFeature.get() && (!theObjects.contains(aFeature)))
263             theObjects.append(aFeature);
264         }
265       }
266     }
267   }
268 }
269
270 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
271 {
272   std::list<FeaturePtr> aFeatures;
273   QObjectPtrList aObjects = selection()->selectedObjects();
274   if (aObjects.isEmpty())
275     return aFeatures;
276
277   bool isPart = false;
278   foreach(ObjectPtr aObj, aObjects) {
279     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
280     if (aFeature.get()) {
281       ResultPtr aRes = aFeature->firstResult();
282       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
283       if (!isPart)
284         aFeatures.push_back(aFeature);
285     }
286   }
287   return aFeatures;
288 }
289
290 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
291 {
292   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
293   GeomShapePtr aResShape = theResult->shape();
294   TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
295   QList<TopoDS_Shape> aResult;
296   foreach(int aShapeType, aModes) {
297     if (aShapeType < TopAbs_SHAPE) {
298       TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
299       for (; aExp.More(); aExp.Next()) {
300         aResult.append(aExp.Current());
301       }
302     }
303   }
304   return aResult;
305 }