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