Salome HOME
Union of validator and filter functionalities.
[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 <SelectMgr_Selection.hxx>
18 #include <SelectMgr_EntityOwner.hxx>
19
20 #include <SelectBasics_SensitiveEntity.hxx>
21 #include <TColStd_ListIteratorOfListOfInteger.hxx>
22 #include <StdSelect_BRepOwner.hxx>
23
24 #include <set>
25
26 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
27     : myWorkshop(theWorkshop)
28 {
29 }
30
31 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected() const
32 {
33   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
34
35   QList<ModuleBase_ViewerPrs> aPresentations;
36   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
37
38   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
39   if (aContext.IsNull())
40     return aPresentations;
41
42   if (aContext->HasOpenedContext()) {
43     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
44       ModuleBase_ViewerPrs aPrs;
45       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
46
47       if (aSelectedIds.contains((long)anOwner.Access()))
48         continue;
49       aSelectedIds.append((long)anOwner.Access());
50
51       fillPresentation(aPrs, anOwner);
52
53       aPresentations.append(aPrs);
54     }
55   }
56   /* else {
57     for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent()) {
58       ModuleBase_ViewerPrs aPrs;
59       Handle(AIS_InteractiveObject) anIO = aContext->Current();
60       if (aSelectedIds.contains((long)anIO.Access()))
61         continue;
62     
63       aSelectedIds.append((long)anIO.Access());
64       aPrs.setInteractive(anIO);
65
66       ObjectPtr aFeature = aDisplayer->getObject(anIO);
67       aPrs.setFeature(aFeature);
68       aPresentations.append(aPrs);
69     }
70   }*/
71   return aPresentations;
72 }
73
74 void XGUI_Selection::fillPresentation(ModuleBase_ViewerPrs& thePrs,
75                                       const Handle(SelectMgr_EntityOwner)& theOwner) const
76 {
77   thePrs.setOwner(theOwner);
78
79   Handle(AIS_InteractiveObject) anIO = 
80                            Handle(AIS_InteractiveObject)::DownCast(theOwner->Selectable());
81   thePrs.setInteractive(anIO);
82
83   // we should not check the appearance of this feature because there can be some selected shapes
84   // for one feature
85   Handle(StdSelect_BRepOwner) aBRO = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
86   if( !aBRO.IsNull() ) {
87     // the located method is called in the context to obtain the shape by the SelectedShape() method,
88     // so the shape is located by the same rules
89     TopoDS_Shape aShape = aBRO->Shape().Located (aBRO->Location() * aBRO->Shape().Location());
90     if (!aShape.IsNull())
91       thePrs.setShape(aShape);
92   }      
93      
94   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
95   ObjectPtr aFeature = aDisplayer->getObject(anIO);
96   thePrs.setFeature(aFeature);
97 }
98
99 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted() const
100 {
101   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
102   QList<ModuleBase_ViewerPrs> aPresentations;
103   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
104
105   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
106   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
107     ModuleBase_ViewerPrs aPrs;
108     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
109     if (aSelectedIds.contains((long)anIO.Access()))
110       continue;
111     
112     aSelectedIds.append((long)anIO.Access());
113     aPrs.setInteractive(anIO);
114
115     ObjectPtr aResult = aDisplayer->getObject(anIO);
116     // we should not check the appearance of this feature because there can be some selected shapes
117     // for one feature
118     aPrs.setFeature(aResult);
119     if (aContext->HasOpenedContext()) {
120       TopoDS_Shape aShape = aContext->DetectedShape();
121       if (!aShape.IsNull())
122         aPrs.setShape(aShape);
123     }
124     aPresentations.push_back(aPrs);
125   }
126   return aPresentations;
127 }
128
129 QObjectPtrList XGUI_Selection::selectedObjects() const
130 {
131   return myWorkshop->objectBrowser()->selectedObjects();
132 }
133
134 QObjectPtrList XGUI_Selection::selectedPresentations() const
135 {
136   QObjectPtrList aSelectedList;
137
138   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
139   if (!aContext.IsNull()) {
140     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
141       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
142       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
143       if (aResult)
144         aSelectedList.append(aResult);
145     }
146   }
147   return aSelectedList;
148 }
149
150 //**************************************************************
151 QModelIndexList XGUI_Selection::selectedIndexes() const
152 {
153   return myWorkshop->objectBrowser()->selectedIndexes();
154 }
155
156 //**************************************************************
157 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
158 {
159   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
160   theList.Clear();
161   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
162     theList.Append(aContext->SelectedInteractive());
163 }
164
165 //**************************************************************
166 ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const
167 {
168   ObjectPtr anObject;
169
170   Handle(SelectMgr_EntityOwner) aEO = theOwner;
171   if (!aEO.IsNull()) {
172     Handle(AIS_InteractiveObject) anObj = 
173       Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
174     anObject = myWorkshop->displayer()->getObject(anObj);
175   }
176   return anObject;
177 }
178
179 //**************************************************************
180 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList, 
181                                     std::list<ObjectPtr>& theOwners) const
182 {
183   theList.Clear();
184   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
185   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
186     TopoDS_Shape aShape = aContext->SelectedShape();
187     if (!aShape.IsNull()) {
188       theList.Append(aShape);
189       Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
190       if (!aEO.IsNull()) {
191         Handle(AIS_InteractiveObject) anObj = 
192           Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
193         ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
194         theOwners.push_back(anObject);
195       }
196     }
197   }
198 }
199
200 //**************************************************************
201 void XGUI_Selection::selectedOwners(SelectMgr_IndexedMapOfOwner& theSelectedOwners) const
202 {
203   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
204
205   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
206     theSelectedOwners.Add(aContext->SelectedOwner());
207   }
208 }
209
210 //**************************************************************
211 void XGUI_Selection::entityOwners(const Handle(AIS_InteractiveObject)& theObject,
212                                   SelectMgr_IndexedMapOfOwner& theOwners) const
213 {
214   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
215
216   if (theObject.IsNull() || aContext.IsNull())
217     return;
218
219   TColStd_ListOfInteger aModes;
220   aContext->ActivatedModes(theObject, aModes);
221
222   TColStd_ListIteratorOfListOfInteger anIt(aModes);
223   for (; anIt.More(); anIt.Next()) {
224     int aMode = anIt.Value();
225     if (!theObject->HasSelection(aMode))
226       continue;
227
228     Handle(SelectMgr_Selection) aSelection = theObject->Selection(aMode);
229     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
230       Handle(SelectBasics_SensitiveEntity) anEntity = aSelection->Sensitive();
231       if (anEntity.IsNull())
232         continue;
233       Handle(SelectMgr_EntityOwner) anOwner =
234         Handle(SelectMgr_EntityOwner)::DownCast(anEntity->OwnerId());
235       if (!anOwner.IsNull())
236         theOwners.Add(anOwner);
237     }
238   }
239 }