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   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
25
26   QList<ModuleBase_ViewerPrs> aPresentations;
27   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
28
29   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
30   if (aContext->HasOpenedContext()) {
31     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
32       ModuleBase_ViewerPrs aPrs;
33       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
34       if (aSelectedIds.contains((long)anIO.Access()))
35         continue;
36     
37       aSelectedIds.append((long)anIO.Access());
38       aPrs.setInteractive(anIO);
39
40       ObjectPtr aFeature = aDisplayer->getObject(anIO);
41       // we should not check the appearance of this feature because there can be some selected shapes
42       // for one feature
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       aPrs.setFeature(aFeature);
49       aPresentations.append(aPrs);
50     }
51   } else {
52     for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent()) {
53       ModuleBase_ViewerPrs aPrs;
54       Handle(AIS_InteractiveObject) anIO = aContext->Current();
55       if (aSelectedIds.contains((long)anIO.Access()))
56         continue;
57     
58       aSelectedIds.append((long)anIO.Access());
59       aPrs.setInteractive(anIO);
60
61       ObjectPtr aFeature = aDisplayer->getObject(anIO);
62       aPrs.setFeature(aFeature);
63       aPresentations.append(aPrs);
64     }
65   }
66   return aPresentations;
67 }
68
69 QList<ModuleBase_ViewerPrs> XGUI_Selection::getHighlighted(int theShapeTypeToSkip) const
70 {
71   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
72   QList<ModuleBase_ViewerPrs> aPresentations;
73   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
74
75   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
76   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
77     ModuleBase_ViewerPrs aPrs;
78     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
79     if (aSelectedIds.contains((long)anIO.Access()))
80       continue;
81     
82     aSelectedIds.append((long)anIO.Access());
83     aPrs.setInteractive(anIO);
84
85     ObjectPtr aResult = aDisplayer->getObject(anIO);
86     // we should not check the appearance of this feature because there can be some selected shapes
87     // for one feature
88     aPrs.setFeature(aResult);
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 }