Salome HOME
Support of Steps visualization from the data model level.
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_SelectionMgr.h"
21
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"
30
31 #ifndef HAVE_SALOME
32 #include <AppElements_MainWindow.h>
33 #endif
34
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>
44 #include <ModuleBase_IStepPrs.h>
45
46 #include <GeomAPI_Shape.h>
47
48 #include <ModuleBase_ViewerPrs.h>
49 #include <ModuleBase_Tools.h>
50
51 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopTools_MapOfShape.hxx>
55
56 #ifdef TINSPECTOR
57 #include <inspector/VInspectorAPI_CallBack.hxx>
58 #endif
59
60 #define OPTIMIZATION_LEVEL 50
61
62
63 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
64     : QObject(theParent),
65       myWorkshop(theParent)
66 {
67   mySelection = new XGUI_Selection(myWorkshop);
68 }
69
70 XGUI_SelectionMgr::~XGUI_SelectionMgr()
71 {
72   delete mySelection;
73 }
74
75 //**************************************************************
76 void XGUI_SelectionMgr::connectViewers()
77 {
78   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
79           SLOT(onObjectBrowserSelection()));
80
81   //Connect to other viewers
82   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
83 }
84
85 //**************************************************************
86 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
87                                           bool isUpdateViewer)
88 {
89   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
90   if (!aContext.IsNull()) {
91     /// previous selection should be cleared, else there will be decomposition of selections:
92     /// as AddOrRemoveSelected inverts current selection
93     aContext->ClearSelected(false);
94
95     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
96       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
97
98       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
99       #ifdef TINSPECTOR
100       if (myWorkshop->displayer()->getCallBack())
101         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
102       #endif
103     }
104   }
105 }
106
107 //**************************************************************
108 void XGUI_SelectionMgr::onObjectBrowserSelection()
109 {
110   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
111     myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
112   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
113   if (!myWorkshop->operationMgr()->hasOperation()) {
114
115     ObjectPtr aObject;
116     FeaturePtr aFeature;
117     // Select all results of a selected feature in viewer
118     foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
119       aObject = aPrs->object();
120       if (aObject.get()) {
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)));
129           }
130         }
131       }
132     }
133   }
134   aDisplayer->setSelected(aSelectedPrs);
135   myWorkshop->viewer()->setColorScaleShown(false);
136   if (aSelectedPrs.size() == 1) {
137     FieldStepPtr aStep =
138       std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>
139       (aSelectedPrs.first()->object());
140     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
141     if (aStep.get() && aDisplayer->isVisible(aStep)) {
142       XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
143       AISObjectPtr aAisPtr = aDisplayer->getAISObject(aStep);
144       Handle(AIS_InteractiveObject) aIO = aAisPtr->impl<Handle(AIS_InteractiveObject)>();
145       ModuleBase_IStepPrs* aPrs = dynamic_cast<ModuleBase_IStepPrs*>(aIO.get());
146       if (aPrs) {
147         ModelAPI_AttributeTables::ValueType aType = aPrs->dataType();
148         if ((aType == ModelAPI_AttributeTables::DOUBLE) ||
149           (aType == ModelAPI_AttributeTables::INTEGER) ||
150           (aType == ModelAPI_AttributeTables::BOOLEAN)) {
151           aViewer->setupColorScale();
152           if (aType == ModelAPI_AttributeTables::BOOLEAN) {
153             aViewer->setColorScaleIntervals(2);
154             aViewer->setColorScaleRange(0., 1.);
155           }
156           else {
157             double aMin, aMax;
158             aPrs->dataRange(aMin, aMax);
159             aViewer->setColorScaleRange(aMin, aMax);
160           }
161           aViewer->setColorScaleTitle(aStep->name().c_str());
162           aViewer->setColorScaleShown(true);
163         }
164       }
165     }
166   }
167   emit selectionChanged();
168 }
169
170 //**************************************************************
171 void XGUI_SelectionMgr::onViewerSelection()
172 {
173   QList<ModuleBase_ViewerPrsPtr> aValues;
174   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
175   if (!aContext.IsNull())
176     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
177
178   QObjectPtrList anObjects;
179   convertToObjectBrowserSelection(aValues, anObjects);
180   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
181
182   emit selectionChanged();
183 }
184
185 //**************************************************************
186 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
187 {
188   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
189
190   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
191   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
192     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
193     if (anOwner.IsNull()) // TODO: check why it is possible
194       continue;
195     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
196       aResultOwners.Append(anOwner);
197   }
198   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
199   Handle(SelectMgr_EntityOwner) anOwner;
200   for (; anOwnersIt.More(); anOwnersIt.Next()) {
201     anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
202     if (!anOwner.IsNull())
203       aContext->AddOrRemoveSelected(anOwner, false);
204   }
205 }
206
207 //**************************************************************
208 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
209 {
210   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
211                myWorkshop->selector()->selection()->getSelected(thePlace);
212   if (thePlace == ModuleBase_ISelection::Browser) {
213     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
214     aDisplayer->setSelected(aSelectedPrs);
215   }
216
217 }
218 //**************************************************************
219 void XGUI_SelectionMgr::clearSelection()
220 {
221   QObjectPtrList aFeatures;
222   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
223
224   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
225              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
226
227   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
228   aDisplayer->setSelected(aSelectedPrs);
229
230   emit selectionChanged();
231 }
232 //**************************************************************
233 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
234 {
235   // update selection in Viewer
236   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
237   aDisplayer->setSelected(theValues);
238
239   // update selection in Object Browser
240   QObjectPtrList anObjects;
241   convertToObjectBrowserSelection(theValues, anObjects);
242   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
243 }
244
245 //**************************************************************
246 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
247                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
248                                    QObjectPtrList& theObjects)
249 {
250   theObjects.clear();
251
252   ResultPtr aResult;
253   FeaturePtr aFeature;
254   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
255   SessionPtr aMgr = ModelAPI_Session::get();
256   DocumentPtr anActiveDocument = aMgr->activeDocument();
257
258   TopTools_MapOfShape aShapeMap;
259   bool aToOptimize = (theValues.size() > OPTIMIZATION_LEVEL);
260
261   GeomShapePtr aShape;
262   TopoDS_Shape aTShape;
263   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
264     if (aPrs->object().get()) {
265       if (!theObjects.contains(aPrs->object()))
266         theObjects.append(aPrs->object());
267       if (aPrs->shape().get() && (!aHasOperation)) {
268         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
269         if (aResult.get()) {
270           aShape = aPrs->shape();
271           aTShape = aShape->impl<TopoDS_Shape>();
272           if (aToOptimize) {
273             if (!aShapeMap.Contains(aTShape)) {
274               aFeature = anActiveDocument->producedByFeature(aResult, aShape);
275               if (aFeature.get()) {
276                 QList<TopoDS_Shape> aResList = findAllShapes(aResult);
277                 foreach(TopoDS_Shape aShape, aResList) {
278                   if (!aShapeMap.Contains(aShape))
279                     aShapeMap.Add(aShape);
280                 }
281               }
282             }
283           }
284           else {
285             aFeature = anActiveDocument->producedByFeature(aResult, aShape);
286           }
287           if (aFeature.get() && (!theObjects.contains(aFeature)))
288             theObjects.append(aFeature);
289         }
290       }
291     }
292   }
293 }
294
295 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
296 {
297   std::list<FeaturePtr> aFeatures;
298   QObjectPtrList aObjects = selection()->selectedObjects();
299   if (aObjects.isEmpty())
300     return aFeatures;
301
302   bool isPart = false;
303   foreach(ObjectPtr aObj, aObjects) {
304     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
305     if (aFeature.get()) {
306       ResultPtr aRes = aFeature->firstResult();
307       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
308       if (!isPart)
309         aFeatures.push_back(aFeature);
310     }
311   }
312   return aFeatures;
313 }
314
315 QList<TopoDS_Shape> XGUI_SelectionMgr::findAllShapes(const ResultPtr& theResult) const
316 {
317   QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
318   GeomShapePtr aResShape = theResult->shape();
319   TopoDS_Shape aShape = aResShape->impl<TopoDS_Shape>();
320   QList<TopoDS_Shape> aResult;
321   foreach(int aShapeType, aModes) {
322     if (aShapeType < TopAbs_SHAPE) {
323       TopExp_Explorer aExp(aShape, (TopAbs_ShapeEnum)aShapeType);
324       for (; aExp.More(); aExp.Next()) {
325         aResult.append(aExp.Current());
326       }
327     }
328   }
329   return aResult;
330 }