Salome HOME
edc1136839354ccbd72ab41671ae32acd2e2f885
[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() && aContext->HasOpenedContext()) {
68     QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
69     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
70       ModuleBase_ViewerPrs aPrs;
71       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
72
73       if (aSelectedIds.contains((long)anOwner.Access()))
74         continue;
75       aSelectedIds.append((long)anOwner.Access());
76
77       fillPresentation(aPrs, anOwner);
78
79       if (!thePresentations.contains(aPrs)) // TODO: check whether the presentation in a list
80         thePresentations.append(aPrs);
81     }
82   }
83 }
84
85 void XGUI_Selection::getSelectedInBrowser(QList<ModuleBase_ViewerPrs>& thePresentations) const
86 {
87   // collect the objects  of the parameter presentation to avoid a repeted objects in the result
88   QObjectPtrList aPresentationObjects;
89   QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = thePresentations.begin(),
90                                               aPrsLast = thePresentations.end();
91   for (; aPrsIt != aPrsLast; aPrsIt++) {
92     aPresentationObjects.push_back((*aPrsIt).object());
93   }
94
95   QObjectPtrList anObjects = selectedObjects();
96   QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
97   for (; anIt != aLast; anIt++) {
98     ObjectPtr anObject = *anIt;
99     if (anObject.get() != NULL && !aPresentationObjects.contains(anObject)) {
100       thePresentations.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
101     }
102   }
103 }
104
105 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
106                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
107 {
108   thePrs.setOwner(theOwner);
109
110   Handle(AIS_InteractiveObject) anIO = 
111                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
112   thePrs.setInteractive(anIO);
113
114   // we should not check the appearance of this feature because there can be some selected shapes
115   // for one feature
116   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
117   if( !aBRO.IsNull() && aBRO->HasShape() ) {
118     // the located method is called in the context to obtain the shape by the SelectedShape() method,
119     // so the shape is located by the same rules
120     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
121     if (!aShape.IsNull())
122       thePrs.setShape(aShape);
123   }      
124      
125   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
126   ObjectPtr aFeature = aDisplayer->getObject(anIO);
127   thePrs.setObject(aFeature);
128 }
129
130 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
131 {
132   QList<ModuleBase_ViewerPrs> aPresentations;
133   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
134   if (aContext.IsNull())
135     return aPresentations;
136
137   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
138   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
139   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
140     ModuleBase_ViewerPrs aPrs;
141     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
142     if (aSelectedIds.contains((long)anIO.Access()))
143       continue;
144     
145     aSelectedIds.append((long)anIO.Access());
146     aPrs.setInteractive(anIO);
147
148     ObjectPtr aResult = aDisplayer->getObject(anIO);
149     // we should not check the appearance of this feature because there can be some selected shapes
150     // for one feature
151     aPrs.setObject(aResult);
152     if (aContext->HasOpenedContext()) {
153       TopoDS_Shape aShape = aContext->DetectedShape();
154       if (!aShape.IsNull())
155         aPrs.setShape(aShape);
156     }
157     aPresentations.push_back(aPrs);
158   }
159   return aPresentations;
160 }
161
162 QObjectPtrList XGUI_Selection::selectedObjects() const
163 {
164   return myWorkshop->objectBrowser()->selectedObjects();
165 }
166
167 void XGUI_Selection::setSelectedObjects( const QObjectPtrList& theObjects ) const
168 {
169   return myWorkshop->objectBrowser()->setObjectsSelected( theObjects );
170 }
171
172 QObjectPtrList XGUI_Selection::selectedPresentations() const
173 {
174   QObjectPtrList aSelectedList;
175
176   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
177   if (!aContext.IsNull()) {
178     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
179       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
180       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
181       if (aResult)
182         aSelectedList.append(aResult);
183     }
184   }
185   return aSelectedList;
186 }
187
188 //**************************************************************
189 QModelIndexList XGUI_Selection::selectedIndexes() const
190 {
191   return myWorkshop->objectBrowser()->selectedIndexes();
192 }
193
194 //**************************************************************
195 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
196 {
197   theList.Clear();
198
199   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
200   if (!aContext.IsNull()) {
201     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
202       theList.Append(aContext->SelectedInteractive());
203   }
204 }
205
206 //**************************************************************
207 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
208 {
209   ObjectPtr anObject;
210
211   Handle(SelectMgr_EntityOwner) aEO = theOwner;
212   if (!aEO.IsNull()) {
213     Handle(AIS_InteractiveObject) anObj = 
214       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
215     anObject = myWorkshop->displayer()->getObject(anObj);
216   }
217   return anObject;
218 }
219
220 //**************************************************************
221 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList, 
222                                     std::list<ObjectPtr>& theOwners) const
223 {
224   theList.Clear();
225   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
226   if (aContext.IsNull())
227     return;
228
229   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
230     TopoDS_Shape aShape = aContext->SelectedShape();
231     if (!aShape.IsNull()) {
232       theList.Append(aShape);
233       Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
234       if (!aEO.IsNull()) {
235         Handle(AIS_InteractiveObject) anObj = 
236           Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
237         ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
238         theOwners.push_back(anObject);
239       }
240     }
241   }
242 }
243
244 //**************************************************************
245 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
246 {
247   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
248   if (!aContext.IsNull()) {
249     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
250       theSelectedOwners.Add(aContext->SelectedOwner());
251     }
252   }
253 }
254
255 //**************************************************************
256 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
257                                   SelectMgr_IndexedMapOfOwner& theOwners) const
258 {
259   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
260   if (aContext.IsNull() || theObject.IsNull())
261     return;
262
263   TColStd_ListOfInteger aModes;
264   aContext->ActivatedModes(theObject, aModes);
265
266   TColStd_ListIteratorOfListOfInteger anIt(aModes);
267   for (; anIt.More(); anIt.Next()) {
268     int aMode = anIt.Value();
269     if (!theObject->HasSelection(aMode))
270       continue;
271
272     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
273     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
274       Handle(SelectMgr_SensitiveEntity) anEntity = aSelection->Sensitive();
275       if (anEntity.IsNull())
276         continue;
277       Handle(SelectMgr_EntityOwner) anOwner =
278         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->BaseSensitive()->OwnerId());
279       if (!anOwner.IsNull())
280         theOwners.Add(anOwner);
281     }
282   }
283 }