Salome HOME
Issue #800 Crash on call of pop-up menu of Part result
[modules/shaper.git] / src / XGUI / XGUI_Selection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_Selection.cpp
4 // Created:     8 July 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "XGUI_Selection.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_ViewerProxy.h"
11 #include "XGUI_ObjectsBrowser.h"
12
13 #include <ModelAPI_Feature.h>
14
15 #include <AIS_InteractiveContext.hxx>
16
17 #include <TColStd_ListIteratorOfListOfInteger.hxx>
18 #include <StdSelect_BRepOwner.hxx>
19
20 #include <set>
21
22 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
23     : myWorkshop(theWorkshop)
24 {
25 }
26
27 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected(const SelectionPlace& thePlace) const
28 {
29   QList<ModuleBase_ViewerPrs> aPresentations;
30
31   switch (thePlace) {
32     case Browser:
33       getSelectedInBrowser(aPresentations);
34     break;
35     case Viewer:
36       getSelectedInViewer(aPresentations);
37     break;
38   case AllControls:
39       getSelectedInViewer(aPresentations);
40       getSelectedInBrowser(aPresentations);
41     break;
42   }
43   return aPresentations;
44 }
45
46 Handle(AIS_InteractiveObject) XGUI_Selection::getIO(const ModuleBase_ViewerPrs& thePrs)
47 {
48   Handle(AIS_InteractiveObject) anIO = thePrs.interactive();
49   if (anIO.IsNull()) {
50     Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
51     if (!anOwner.IsNull())
52       anIO = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
53
54     if (anIO.IsNull() && thePrs.object()) {
55       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
56       AISObjectPtr anAISObject = aDisplayer->getAISObject(thePrs.object());
57       if (anAISObject.get())
58         anIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
59     }
60   }
61   return anIO;
62 }
63
64 void XGUI_Selection::getSelectedInViewer(QList<ModuleBase_ViewerPrs>& thePresentations) const
65 {
66   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
67   if (aContext.IsNull())
68     return;
69
70   if (aContext->HasOpenedContext()) {
71     QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
72     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
73       ModuleBase_ViewerPrs aPrs;
74       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
75
76       if (aSelectedIds.contains((long)anOwner.Access()))
77         continue;
78       aSelectedIds.append((long)anOwner.Access());
79
80       fillPresentation(aPrs, anOwner);
81
82       if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
83         thePresentations.append(aPrs);
84     }
85   }
86 }
87
88 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
89 {
90   // collect the objects  of the parameter presentation to avoid a repeted objects in the result
91   QObjectPtrList aPresentationObjects;
92   QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
93                                               aPrsLast = thePresentations.end();
94   for (; aPrsIt != aPrsLast; aPrsIt++) {
95     aPresentationObjects.push_back((*aPrsIt).object());
96   }
97
98   QObjectPtrList anObjects = selectedObjects();
99   QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
100   for (; anIt != aLast; anIt++) {
101     ObjectPtr anObject = *anIt;
102     if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
103       thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
104     }
105   }
106 }
107
108 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
109                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
110 {
111   thePrs.setOwner(theOwner);
112
113   Handle(AIS_InteractiveObject) anIO = 
114                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
115   thePrs.setInteractive(anIO);
116
117   // we should not check the appearance of this feature because there can be some selected shapes
118   // for one feature
119   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
120   if( !aBRO.IsNull() && aBRO->HasShape() ) {
121     // the located method is called in the context to obtain the shape by the SelectedShape() method,
122     // so the shape is located by the same rules
123     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
124     if (!aShape.IsNull())
125       thePrs.setShape(aShape);
126   }      
127      
128   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
129   ObjectPtr aFeature = aDisplayer->getObject(anIO);
130   thePrs.setObject(aFeature);
131 }
132
133 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
134 {
135   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
136   QList<ModuleBase_ViewerPrs> aPresentations;
137   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
138
139   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
140   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
141     ModuleBase_ViewerPrs aPrs;
142     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
143     if (aSelectedIds.contains((long)anIO.Access()))
144       continue;
145     
146     aSelectedIds.append((long)anIO.Access());
147     aPrs.setInteractive(anIO);
148
149     ObjectPtr aResult = aDisplayer->getObject(anIO);
150     // we should not check the appearance of this feature because there can be some selected shapes
151     // for one feature
152     aPrs.setObject(aResult);
153     if (aContext->HasOpenedContext()) {
154       TopoDS_Shape aShape = aContext->DetectedShape();
155       if (!aShape.IsNull())
156         aPrs.setShape(aShape);
157     }
158     aPresentations.push_back(aPrs);
159   }
160   return aPresentations;
161 }
162
163 QObjectPtrList XGUI_Selection::selectedObjects() const
164 {
165   return myWorkshop->objectBrowser()->selectedObjects();
166 }
167
168 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
169 {
170   return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
171 }
172
173 QObjectPtrList XGUI_Selection::selectedPresentations() const
174 {
175   QObjectPtrList aSelectedList;
176
177   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
178   if (!aContext.IsNull()) {
179     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
180       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
181       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
182       if (aResult)
183         aSelectedList.append(aResult);
184     }
185   }
186   return aSelectedList;
187 }
188
189 //**************************************************************
190 QModelIndexList XGUI_Selection::selectedIndexes() const
191 {
192   return myWorkshop->objectBrowser()->selectedIndexes();
193 }
194
195 //**************************************************************
196 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
197 {
198   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
199   theList.Clear();
200   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
201     theList.Append(aContext->SelectedInteractive());
202 }
203
204 //**************************************************************
205 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
206 {
207   ObjectPtr anObject;
208
209   Handle(SelectMgr_EntityOwner) aEO = theOwner;
210   if (!aEO.IsNull()) {
211     Handle(AIS_InteractiveObject) anObj = 
212       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
213     anObject = myWorkshop->displayer()->getObject(anObj);
214   }
215   return anObject;
216 }
217
218 //**************************************************************
219 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList, 
220                                     std::list<ObjectPtr>& theOwners) const
221 {
222   theList.Clear();
223   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
224   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
225     TopoDS_Shape aShape = aContext->SelectedShape();
226     if (!aShape.IsNull()) {
227       theList.Append(aShape);
228       Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
229       if (!aEO.IsNull()) {
230         Handle(AIS_InteractiveObject) anObj = 
231           Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
232         ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
233         theOwners.push_back(anObject);
234       }
235     }
236   }
237 }
238
239 //**************************************************************
240 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
241 {
242   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
243
244   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
245     theSelectedOwners.Add(aContext->SelectedOwner());
246   }
247 }
248
249 //**************************************************************
250 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
251                                   SelectMgr_IndexedMapOfOwner& theOwners) const
252 {
253   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
254
255   if (theObject.IsNull() || aContext.IsNull())
256     return;
257
258   TColStd_ListOfInteger aModes;
259   aContext->ActivatedModes(theObject, aModes);
260
261   TColStd_ListIteratorOfListOfInteger anIt(aModes);
262   for (; anIt.More(); anIt.Next()) {
263     int aMode = anIt.Value();
264     if (!theObject->HasSelection(aMode))
265       continue;
266
267     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
268     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
269       Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
270       if (anEntity.IsNull())
271         continue;
272       Handle(SelectMgr_EntityOwner) anOwner =
273         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
274       if (!anOwner.IsNull())
275         theOwners.Add(anOwner);
276     }
277   }
278 }