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