1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_SelectionMgr.h"
22 #include "XGUI_Workshop.h"
23 #include "XGUI_ObjectsBrowser.h"
24 #include "XGUI_SalomeConnector.h"
25 #include "XGUI_ViewerProxy.h"
26 #include "XGUI_Displayer.h"
27 #include "XGUI_Selection.h"
28 #include "XGUI_OperationMgr.h"
29 #include "XGUI_SelectionActivate.h"
32 #include <AppElements_MainWindow.h>
35 #include <ModelAPI_Feature.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_AttributeDocRef.h>
38 #include <ModelAPI_Data.h>
39 #include <ModelAPI_Result.h>
40 #include <ModelAPI_Object.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_Tools.h>
44 #include <GeomAPI_Shape.h>
46 #include <ModuleBase_ViewerPrs.h>
47 #include <ModuleBase_Tools.h>
49 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopTools_MapOfShape.hxx>
55 #include <inspector/VInspectorAPI_CallBack.hxx>
58 #define OPTIMIZATION_LEVEL 50
61 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
65 mySelection = new XGUI_Selection(myWorkshop);
68 XGUI_SelectionMgr::~XGUI_SelectionMgr()
73 //**************************************************************
74 void XGUI_SelectionMgr::connectViewers()
76 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
77 SLOT(onObjectBrowserSelection()));
79 //Connect to other viewers
80 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
83 //**************************************************************
84 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
87 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
88 if (!aContext.IsNull()) {
89 /// previous selection should be cleared, else there will be decomposition of selections:
90 /// as AddOrRemoveSelected inverts current selection
91 aContext->ClearSelected(false);
93 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
94 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
96 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
98 if (myWorkshop->displayer()->getCallBack())
99 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
105 //**************************************************************
106 void XGUI_SelectionMgr::onObjectBrowserSelection()
108 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
109 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
110 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
111 if (!myWorkshop->operationMgr()->hasOperation()) {
115 // Select all results of a selected feature in viewer
116 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
117 aObject = aPrs->object();
119 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
120 if (aFeature.get()) {
121 std::list<ResultPtr> allRes;
122 ModelAPI_Tools::allResults(aFeature, allRes);
123 std::list<ResultPtr>::iterator aRes;
124 for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
125 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
126 new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
132 aDisplayer->setSelected(aSelectedPrs);
133 emit selectionChanged();
136 //**************************************************************
137 void XGUI_SelectionMgr::onViewerSelection()
139 QList<ModuleBase_ViewerPrsPtr> aValues;
140 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
141 if (!aContext.IsNull())
142 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
144 QObjectPtrList anObjects;
145 convertToObjectBrowserSelection(aValues, anObjects);
146 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
148 emit selectionChanged();
151 //**************************************************************
152 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
154 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
156 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
157 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
158 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
159 if (anOwner.IsNull()) // TODO: check why it is possible
161 if (anOwner->Selectable() == theObject && anOwner->IsSelected())
162 aResultOwners.Append(anOwner);
164 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
165 Handle(SelectMgr_EntityOwner) anOwner;
166 for (; anOwnersIt.More(); anOwnersIt.Next()) {
167 anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
168 if (!anOwner.IsNull())
169 aContext->AddOrRemoveSelected(anOwner, false);
173 //**************************************************************
174 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
176 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
177 myWorkshop->selector()->selection()->getSelected(thePlace);
178 if (thePlace == ModuleBase_ISelection::Browser) {
179 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
180 aDisplayer->setSelected(aSelectedPrs);
184 //**************************************************************
185 void XGUI_SelectionMgr::clearSelection()
187 QObjectPtrList aFeatures;
188 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
190 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
191 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
193 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
194 aDisplayer->setSelected(aSelectedPrs);
196 emit selectionChanged();
198 //**************************************************************
199 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
201 // update selection in Viewer
202 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203 aDisplayer->setSelected(theValues);
205 // update selection in Object Browser
206 QObjectPtrList anObjects;
207 convertToObjectBrowserSelection(theValues, anObjects);
208 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
211 //**************************************************************
212 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
213 const QList<ModuleBase_ViewerPrsPtr>& theValues,
214 QObjectPtrList& theObjects)
220 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
221 SessionPtr aMgr = ModelAPI_Session::get();
222 DocumentPtr anActiveDocument = aMgr->activeDocument();
224 TopTools_MapOfShape aShapeMap;
225 bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
228 TopoDS_Shape aTShape;
229 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
230 if (aPrs->object().get()) {
231 if (!theObjects.contains(aPrs->object()))
232 theObjects.append(aPrs->object());
233 if (aPrs->shape().get() && (!aHasOperation)) {
234 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
236 aShape = aPrs->shape();
237 aTShape = aShape->impl<TopoDS_Shape>();
239 if (!aShapeMap.Contains(aTShape)) {
240 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
241 if (aFeature.get()) {
242 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
243 foreach(TopoDS_Shape aShape, aResList) {
244 if (!aShapeMap.Contains(aShape))
245 aShapeMap.Add(aShape);
251 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
253 if (aFeature.get() && (!theObjects.contains(aFeature)))
254 theObjects.append(aFeature);
261 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
263 std::list<FeaturePtr> aFeatures;
264 QObjectPtrList aObjects = selection()->selectedObjects();
265 if (aObjects.isEmpty())
269 foreach(ObjectPtr aObj, aObjects) {
270 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
271 if (aFeature.get()) {
272 ResultPtr aRes = aFeature->firstResult();
273 isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
275 aFeatures.push_back(aFeature);
281 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
283 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
284 GeomShapePtr aResShape = theResult->shape();
285 TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
286 QList<TopoDS_Shape> aResult;
287 foreach(int aShapeType, aModes) {
288 if (aShapeType < TopAbs_SHAPE) {
289 TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
290 for (; aExp.More(); aExp.Next()) {
291 aResult.append(aExp.Current());