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