1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_SelectionMgr.h"
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"
13 #include <AppElements_MainWindow.h>
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>
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_Tools.h>
27 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
29 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
33 mySelection = new XGUI_Selection(myWorkshop);
36 XGUI_SelectionMgr::~XGUI_SelectionMgr()
41 //**************************************************************
42 void XGUI_SelectionMgr::connectViewers()
44 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
45 SLOT(onObjectBrowserSelection()));
47 //Connect to other viewers
48 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
51 //**************************************************************
52 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
55 SelectMgr_IndexedMapOfOwner aSelectedOwners;
56 selection()->selectedOwners(aSelectedOwners);
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)
65 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
68 ModuleBase_Tools::selectionInfo(aContext,
69 "XGUI_SelectionMgr::setSelectedOwners -- AddOrRemoveSelected");
72 //**************************************************************
73 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
75 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
76 if (aContext.IsNull())
79 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
81 SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
83 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
84 for (; anIt.More(); anIt.Next()) {
85 Handle(SelectMgr_Filter) aFilter = anIt.Value();
86 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
87 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
88 if (!aFilter->IsOk(anOwner))
89 anOwnersToDeselect.Add(aContext->SelectedOwner());
93 setSelectedOwners(anOwnersToDeselect, false);
96 aContext->UpdateCurrentViewer();
99 //**************************************************************
100 void XGUI_SelectionMgr::onObjectBrowserSelection()
102 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
103 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
105 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
108 foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
109 aObject = aPrs->object();
111 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
112 if (aFeature.get()) {
113 const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
115 ResultCompSolidPtr aCompSolid;
116 std::list<ResultPtr>::const_iterator aIt;
117 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
119 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
120 new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
121 aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
122 if (aCompSolid.get()) {
123 for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
124 ResultBodyPtr aResult = aCompSolid->subResult(i);
125 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
126 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
133 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
134 aDisplayer->setSelected(aSelectedPrs);
135 emit selectionChanged();
138 //**************************************************************
139 void XGUI_SelectionMgr::onViewerSelection()
141 SessionPtr aMgr = ModelAPI_Session::get();
142 DocumentPtr anActiveDocument = aMgr->activeDocument();
143 QObjectPtrList aFeatures;
146 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
147 if (!aContext.IsNull()) {
148 QList<ModuleBase_ViewerPrsPtr> aPresentations =
149 selection()->getSelected(ModuleBase_ISelection::Viewer);
150 foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
151 if (aPrs->object().get()) {
152 if (!aFeatures.contains(aPrs->object()))
153 aFeatures.append(aPrs->object());
154 if (aPrs->shape().get()) {
155 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
157 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
158 if (aFeature.get() && (!aFeatures.contains(aFeature)))
159 aFeatures.append(aFeature);
165 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
166 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
167 myWorkshop->objectBrowser()->blockSignals(aBlocked);
169 emit selectionChanged();
172 //**************************************************************
173 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
175 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
176 myWorkshop->selector()->selection()->getSelected(thePlace);
177 if (thePlace == ModuleBase_ISelection::Browser) {
178 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
179 aDisplayer->setSelected(aSelectedPrs);
183 //**************************************************************
184 void XGUI_SelectionMgr::clearSelection()
186 QObjectPtrList aFeatures;
187 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
188 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
189 myWorkshop->objectBrowser()->blockSignals(aBlocked);
191 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
192 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
194 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
195 aDisplayer->setSelected(aSelectedPrs);
197 emit selectionChanged();