Salome HOME
Documentation update
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_SelectionMgr.h"
4
5 #include "XGUI_Workshop.h"
6 #include "XGUI_ObjectsBrowser.h"
7 #include "XGUI_SalomeConnector.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_Selection.h"
11
12 #ifndef HAVE_SALOME
13 #include <AppElements_MainWindow.h>
14 #endif
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_AttributeDocRef.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_ResultCompSolid.h>
23
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_Tools.h>
26
27 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
28
29 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
30     : QObject(theParent),
31       myWorkshop(theParent)
32 {
33   mySelection = new XGUI_Selection(myWorkshop);
34 }
35
36 XGUI_SelectionMgr::~XGUI_SelectionMgr()
37 {
38   delete mySelection;
39 }
40
41 //**************************************************************
42 void XGUI_SelectionMgr::connectViewers()
43 {
44   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
45           SLOT(onObjectBrowserSelection()));
46
47   //Connect to other viewers
48   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
49 }
50
51 //**************************************************************
52 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
53                                           bool isUpdateViewer)
54 {
55   SelectMgr_IndexedMapOfOwner aSelectedOwners;
56   selection()->selectedOwners(aSelectedOwners);
57
58   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
59   if (!aContext.IsNull()) {
60     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
61       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
62       if (aSelectedOwners.FindIndex(anOwner) > 0)
63         continue;
64
65       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
66     }
67   }
68   ModuleBase_Tools::selectionInfo(aContext, "XGUI_SelectionMgr::setSelectedOwners -- AddOrRemoveSelected");
69 }
70
71 //**************************************************************
72 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
73 {
74   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
75   if (aContext.IsNull())
76     return;
77
78   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
79
80   SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
81
82   SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
83   for (; anIt.More(); anIt.Next()) {
84     Handle(SelectMgr_Filter) aFilter = anIt.Value();
85     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
86       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
87       if (!aFilter->IsOk(anOwner))
88         anOwnersToDeselect.Add(aContext->SelectedOwner());
89     }
90   }
91
92   setSelectedOwners(anOwnersToDeselect, false);
93
94   if (isUpdateViewer)
95     aContext->UpdateCurrentViewer();
96 }
97
98 //**************************************************************
99 void XGUI_SelectionMgr::onObjectBrowserSelection()
100 {
101   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
102              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
103
104   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
105   ObjectPtr aObject;
106   FeaturePtr aFeature;
107   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
108     aObject = aPrs->object();
109     if (aObject.get()) {
110       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
111       if (aFeature.get()) {
112         const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
113         ResultPtr aResult;
114         ResultCompSolidPtr aCompSolid;
115         std::list<ResultPtr>::const_iterator aIt;
116         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
117           aResult = (*aIt);
118           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
119             new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
120           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
121           if (aCompSolid.get()) {
122             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
123               ResultBodyPtr aResult = aCompSolid->subResult(i);
124               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
125                 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
126             }
127           }
128         }
129       }
130     }
131   }
132   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
133   aDisplayer->setSelected(aSelectedPrs);
134   emit selectionChanged();
135 }
136
137 //**************************************************************
138 void XGUI_SelectionMgr::onViewerSelection()
139 {
140   SessionPtr aMgr = ModelAPI_Session::get();
141   DocumentPtr anActiveDocument = aMgr->activeDocument();
142   QObjectPtrList aFeatures;
143   ResultPtr aResult;
144   FeaturePtr aFeature;
145   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
146   if (!aContext.IsNull()) {
147     QList<ModuleBase_ViewerPrsPtr> aPresentations = selection()->getSelected(ModuleBase_ISelection::Viewer);
148     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
149       if (aPrs->object().get()) {
150         if (!aFeatures.contains(aPrs->object()))
151           aFeatures.append(aPrs->object());
152         if (aPrs->shape().get()) {
153           aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
154           if (aResult.get()) {
155             aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
156             if (aFeature.get() && (!aFeatures.contains(aFeature)))
157               aFeatures.append(aFeature);
158           }
159         }
160       }
161     }
162   }
163   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
164   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
165   myWorkshop->objectBrowser()->blockSignals(aBlocked);
166
167   emit selectionChanged();
168 }
169
170 //**************************************************************
171 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
172 {
173   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
174                myWorkshop->selector()->selection()->getSelected(thePlace);
175   if (thePlace == ModuleBase_ISelection::Browser) {
176     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
177     aDisplayer->setSelected(aSelectedPrs);
178   }
179
180 }
181 //**************************************************************
182 void XGUI_SelectionMgr::clearSelection()
183 {
184   QObjectPtrList aFeatures;
185   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
186   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
187   myWorkshop->objectBrowser()->blockSignals(aBlocked);
188   
189   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
190              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
191
192   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193   aDisplayer->setSelected(aSelectedPrs);
194
195   emit selectionChanged();
196 }