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