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>
43 #include <ModelAPI_ResultField.h>
45 #include <GeomAPI_Shape.h>
47 #include <ModuleBase_ViewerPrs.h>
48 #include <ModuleBase_Tools.h>
50 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopTools_MapOfShape.hxx>
56 #include <inspector/VInspectorAPI_CallBack.hxx>
59 #define OPTIMIZATION_LEVEL 50
62 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
66 mySelection = new XGUI_Selection(myWorkshop);
69 XGUI_SelectionMgr::~XGUI_SelectionMgr()
74 //**************************************************************
75 void XGUI_SelectionMgr::connectViewers()
77 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
78 SLOT(onObjectBrowserSelection()));
80 //Connect to other viewers
81 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
84 //**************************************************************
85 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
88 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
89 if (!aContext.IsNull()) {
90 /// previous selection should be cleared, else there will be decomposition of selections:
91 /// as AddOrRemoveSelected inverts current selection
92 aContext->ClearSelected(false);
94 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
95 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
97 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
99 if (myWorkshop->displayer()->getCallBack())
100 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
106 //**************************************************************
107 void XGUI_SelectionMgr::onObjectBrowserSelection()
109 myLastSelectionPlace = ModuleBase_ISelection::Browser;
110 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
111 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
112 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
113 if (!myWorkshop->operationMgr()->hasOperation()) {
117 // Select all results of a selected feature in viewer
118 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
119 aObject = aPrs->object();
121 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
122 if (aFeature.get()) {
123 std::list<ResultPtr> allRes;
124 ModelAPI_Tools::allResults(aFeature, allRes);
125 std::list<ResultPtr>::iterator aRes;
126 for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
127 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
128 new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
134 aDisplayer->setSelected(aSelectedPrs);
135 myWorkshop->updateColorScaleVisibility();
136 emit selectionChanged();
139 //**************************************************************
140 void XGUI_SelectionMgr::onViewerSelection()
142 myLastSelectionPlace = ModuleBase_ISelection::Viewer;
143 QList<ModuleBase_ViewerPrsPtr> aValues;
144 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
145 if (!aContext.IsNull())
146 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
148 QObjectPtrList anObjects;
149 convertToObjectBrowserSelection(aValues, anObjects);
150 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
152 emit selectionChanged();
155 //**************************************************************
156 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
158 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
160 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
161 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
162 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
163 if (anOwner.IsNull()) // TODO: check why it is possible
165 if (anOwner->Selectable() == theObject && anOwner->IsSelected())
166 aResultOwners.Append(anOwner);
168 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
169 Handle(SelectMgr_EntityOwner) anOwner;
170 for (; anOwnersIt.More(); anOwnersIt.Next()) {
171 anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
172 if (!anOwner.IsNull())
173 aContext->AddOrRemoveSelected(anOwner, false);
177 //**************************************************************
178 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
180 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
181 myWorkshop->selector()->selection()->getSelected(thePlace);
182 if (thePlace == ModuleBase_ISelection::Browser) {
183 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
184 aDisplayer->setSelected(aSelectedPrs);
188 //**************************************************************
189 void XGUI_SelectionMgr::clearSelection()
191 QObjectPtrList aFeatures;
192 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
194 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
195 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
197 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
198 aDisplayer->setSelected(aSelectedPrs);
200 emit selectionChanged();
202 //**************************************************************
203 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
205 // update selection in Viewer
206 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
207 aDisplayer->setSelected(theValues);
209 // update selection in Object Browser
210 QObjectPtrList anObjects;
211 convertToObjectBrowserSelection(theValues, anObjects);
212 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
215 //**************************************************************
216 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
217 const QList<ModuleBase_ViewerPrsPtr>& theValues,
218 QObjectPtrList& theObjects)
224 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
225 SessionPtr aMgr = ModelAPI_Session::get();
226 DocumentPtr anActiveDocument = aMgr->activeDocument();
228 TopTools_MapOfShape aShapeMap;
229 bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
232 TopoDS_Shape aTShape;
233 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
234 if (aPrs->object().get()) {
235 if (!theObjects.contains(aPrs->object()))
236 theObjects.append(aPrs->object());
237 if (aPrs->shape().get() && (!aHasOperation)) {
238 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
240 aShape = aPrs->shape();
241 aTShape = aShape->impl<TopoDS_Shape>();
243 if (!aShapeMap.Contains(aTShape)) {
244 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
245 if (aFeature.get()) {
246 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
247 foreach(TopoDS_Shape aShape, aResList) {
248 if (!aShapeMap.Contains(aShape))
249 aShapeMap.Add(aShape);
255 aFeature = anActiveDocument->producedByFeature(aResult, aShape);
257 if (aFeature.get() && (!theObjects.contains(aFeature)))
258 theObjects.append(aFeature);
265 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
267 std::list<FeaturePtr> aFeatures;
268 QObjectPtrList aObjects = selection()->selectedObjects();
269 if (aObjects.isEmpty())
273 foreach(ObjectPtr aObj, aObjects) {
274 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
275 if (aFeature.get()) {
276 ResultPtr aRes = aFeature->firstResult();
277 isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
279 aFeatures.push_back(aFeature);
285 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
287 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
288 GeomShapePtr aResShape = theResult->shape();
289 TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
290 QList<TopoDS_Shape> aResult;
291 foreach(int aShapeType, aModes) {
292 if (aShapeType < TopAbs_SHAPE) {
293 TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
294 for (; aExp.More(); aExp.Next()) {
295 aResult.append(aExp.Current());