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, "XGUI_SelectionMgr::setSelectedOwners -- AddOrRemoveSelected");
71 //**************************************************************
72 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
74 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
75 if (aContext.IsNull())
78 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
80 SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
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());
92 setSelectedOwners(anOwnersToDeselect, false);
95 aContext->UpdateCurrentViewer();
98 //**************************************************************
99 void XGUI_SelectionMgr::onObjectBrowserSelection()
101 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
102 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
104 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
107 foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
108 aObject = aPrs->object();
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();
114 ResultCompSolidPtr aCompSolid;
115 std::list<ResultPtr>::const_iterator aIt;
116 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++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)));
132 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
133 aDisplayer->setSelected(aSelectedPrs);
134 emit selectionChanged();
137 //**************************************************************
138 void XGUI_SelectionMgr::onViewerSelection()
140 SessionPtr aMgr = ModelAPI_Session::get();
141 DocumentPtr anActiveDocument = aMgr->activeDocument();
142 QObjectPtrList aFeatures;
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());
155 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
156 if (aFeature.get() && (!aFeatures.contains(aFeature)))
157 aFeatures.append(aFeature);
163 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
164 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
165 myWorkshop->objectBrowser()->blockSignals(aBlocked);
167 emit selectionChanged();
170 //**************************************************************
171 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
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);
181 //**************************************************************
182 void XGUI_SelectionMgr::clearSelection()
184 QObjectPtrList aFeatures;
185 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
186 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
187 myWorkshop->objectBrowser()->blockSignals(aBlocked);
189 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
190 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
192 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193 aDisplayer->setSelected(aSelectedPrs);
195 emit selectionChanged();