Salome HOME
Merge branch 'BR_PYTHON_PLUGIN' of newgeom:newgeom.git 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 <set>
18
19 XGUI_Selection::XGUI_Selection(XGUI_Workshop* theWorkshop)
20     : myWorkshop(theWorkshop)
21 {
22 }
23
24 QList<ModuleBase_ViewerPrs> XGUI_Selection::getSelected(int theShapeTypeToSkip) const
25 {
26   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
27
28   QList<ModuleBase_ViewerPrs> aPresentations;
29   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
30
31   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
32   if (aContext->HasOpenedContext()) {
33     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
34       ModuleBase_ViewerPrs aPrs;
35       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
36       if (aSelectedIds.contains((long)anIO.Access()))
37         continue;
38     
39       aSelectedIds.append((long)anIO.Access());
40       aPrs.setInteractive(anIO);
41
42       ObjectPtr aFeature = aDisplayer->getObject(anIO);
43       // we should not check the appearance of this feature because there can be some selected shapes
44       // for one feature
45       TopoDS_Shape aShape = aContext->SelectedShape();
46       if (!aShape.IsNull() && (aShape.ShapeType() != theShapeTypeToSkip))
47         aPrs.setShape(aShape);
48       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
49       aPrs.setOwner(anOwner);
50       aPrs.setFeature(aFeature);
51       aPresentations.append(aPrs);
52     }
53   } else {
54     for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent()) {
55       ModuleBase_ViewerPrs aPrs;
56       Handle(AIS_InteractiveObject) anIO = aContext->Current();
57       if (aSelectedIds.contains((long)anIO.Access()))
58         continue;
59     
60       aSelectedIds.append((long)anIO.Access());
61       aPrs.setInteractive(anIO);
62
63       ObjectPtr aFeature = aDisplayer->getObject(anIO);
64       aPrs.setFeature(aFeature);
65       aPresentations.append(aPrs);
66     }
67   }
68   return aPresentations;
69 }
70
71 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted(int theShapeTypeToSkip) const
72 {
73   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
74   QList<ModuleBase_ViewerPrs> aPresentations;
75   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
76
77   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
78   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
79     ModuleBase_ViewerPrs aPrs;
80     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
81     if (aSelectedIds.contains((long)anIO.Access()))
82       continue;
83     
84     aSelectedIds.append((long)anIO.Access());
85     aPrs.setInteractive(anIO);
86
87     ObjectPtr aResult = aDisplayer->getObject(anIO);
88     // we should not check the appearance of this feature because there can be some selected shapes
89     // for one feature
90     aPrs.setFeature(aResult);
91     if (aContext->HasOpenedContext()) {
92       TopoDS_Shape aShape = aContext->DetectedShape();
93       if (!aShape.IsNull() && aShape.ShapeType() != theShapeTypeToSkip)
94         aPrs.setShape(aShape);
95     }
96     aPresentations.push_back(aPrs);
97   }
98   return aPresentations;
99 }
100
101 QObjectPtrList XGUI_Selection::selectedObjects() const
102 {
103   return myWorkshop->objectBrowser()->selectedObjects();
104 }
105
106 QObjectPtrList XGUI_Selection::selectedPresentations() const
107 {
108   QObjectPtrList aSelectedList;
109
110   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
111   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
112     Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
113     ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
114     if (aResult)
115       aSelectedList.append(aResult);
116   }
117   return aSelectedList;
118 }
119
120 //**************************************************************
121 QModelIndexList XGUI_Selection::selectedIndexes() const
122 {
123   return myWorkshop->objectBrowser()->selectedIndexes();
124 }
125
126 //**************************************************************
127 void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const
128 {
129   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
130   theList.Clear();
131   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
132     theList.Append(aContext->SelectedInteractive());
133 }
134
135 //**************************************************************
136 void XGUI_Selection::selectedShapes(NCollection_List<TopoDS_Shape>& theList, 
137                                     std::list<ObjectPtr>& theOwners) const
138 {
139   theList.Clear();
140   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
141   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
142     TopoDS_Shape aShape = aContext->SelectedShape();
143     if (!aShape.IsNull()) {
144       theList.Append(aShape);
145       Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner();
146       if (!aEO.IsNull()) {
147         Handle(AIS_InteractiveObject) anObj = 
148           Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable());
149         ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj);
150         theOwners.push_back(anObject);
151       }
152     }
153   }
154 }