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