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