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