]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_SelectionMgr.cpp
Salome HOME
Initial implementation of support of any level of hierarchy in Result Bodies.
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "XGUI_SelectionMgr.h"
22
23 #include "XGUI_Workshop.h"
24 #include "XGUI_ObjectsBrowser.h"
25 #include "XGUI_SalomeConnector.h"
26 #include "XGUI_ViewerProxy.h"
27 #include "XGUI_Displayer.h"
28 #include "XGUI_Selection.h"
29 #include "XGUI_OperationMgr.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
44 #include <ModuleBase_ViewerPrs.h>
45 #include <ModuleBase_Tools.h>
46
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48
49 #ifdef TINSPECTOR
50 #include <inspector/VInspectorAPI_CallBack.hxx>
51 #endif
52
53 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
54     : QObject(theParent),
55       myWorkshop(theParent)
56 {
57   mySelection = new XGUI_Selection(myWorkshop);
58 }
59
60 XGUI_SelectionMgr::~XGUI_SelectionMgr()
61 {
62   delete mySelection;
63 }
64
65 //**************************************************************
66 void XGUI_SelectionMgr::connectViewers()
67 {
68   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
69           SLOT(onObjectBrowserSelection()));
70
71   //Connect to other viewers
72   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
73 }
74
75 //**************************************************************
76 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
77                                           bool isUpdateViewer)
78 {
79   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
80   if (!aContext.IsNull()) {
81     /// previous selection should be cleared, else there will be decomposition of selections:
82     /// as AddOrRemoveSelected inverts current selection
83     aContext->ClearSelected(false);
84
85     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
86       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
87
88       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
89       #ifdef TINSPECTOR
90       if (myWorkshop->displayer()->getCallBack())
91         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
92       #endif
93     }
94   }
95 }
96
97 //**************************************************************
98 void XGUI_SelectionMgr::onObjectBrowserSelection()
99 {
100   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
101              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
102
103   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
104   ObjectPtr aObject;
105   FeaturePtr aFeature;
106   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
107     aObject = aPrs->object();
108     if (aObject.get()) {
109       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
110       if (aFeature.get()) {
111         std::list<ResultPtr> allRes;
112         ModelAPI_Tools::allResults(aFeature, allRes);
113         for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
114           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
115             new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
116         }
117       }
118     }
119   }
120   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
121   aDisplayer->setSelected(aSelectedPrs);
122   emit selectionChanged();
123 }
124
125 //**************************************************************
126 void XGUI_SelectionMgr::onViewerSelection()
127 {
128   QList<ModuleBase_ViewerPrsPtr> aValues;
129   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
130   if (!aContext.IsNull())
131     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
132
133   QObjectPtrList anObjects;
134   convertToObjectBrowserSelection(aValues, anObjects);
135
136   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
137   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
138   myWorkshop->objectBrowser()->blockSignals(aBlocked);
139
140   emit selectionChanged();
141 }
142
143 //**************************************************************
144 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
145 {
146   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
147
148   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
149   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
150     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
151     if (anOwner.IsNull()) // TODO: check why it is possible
152       continue;
153     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
154       aResultOwners.Append(anOwner);
155   }
156   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
157   Handle(SelectMgr_EntityOwner) anOwner;
158   for (; anOwnersIt.More(); anOwnersIt.Next()) {
159     anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
160     if (!anOwner.IsNull())
161       aContext->AddOrRemoveSelected(anOwner, false);
162   }
163 }
164
165 //**************************************************************
166 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
167 {
168   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
169                myWorkshop->selector()->selection()->getSelected(thePlace);
170   if (thePlace == ModuleBase_ISelection::Browser) {
171     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
172     aDisplayer->setSelected(aSelectedPrs);
173   }
174
175 }
176 //**************************************************************
177 void XGUI_SelectionMgr::clearSelection()
178 {
179   QObjectPtrList aFeatures;
180   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
181   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
182   myWorkshop->objectBrowser()->blockSignals(aBlocked);
183
184   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
185              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
186
187   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
188   aDisplayer->setSelected(aSelectedPrs);
189
190   emit selectionChanged();
191 }
192 //**************************************************************
193 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
194 {
195   // update selection in Viewer
196   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
197   aDisplayer->setSelected(theValues);
198
199   // update selection in Object Browser
200   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
201   QObjectPtrList anObjects;
202   convertToObjectBrowserSelection(theValues, anObjects);
203
204   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
205   myWorkshop->objectBrowser()->blockSignals(aBlocked);
206 }
207 //**************************************************************
208 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
209                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
210                                    QObjectPtrList& theObjects)
211 {
212   theObjects.clear();
213
214   ResultPtr aResult;
215   FeaturePtr aFeature;
216   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
217   SessionPtr aMgr = ModelAPI_Session::get();
218   DocumentPtr anActiveDocument = aMgr->activeDocument();
219
220   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
221     if (aPrs->object().get()) {
222       if (!theObjects.contains(aPrs->object()))
223         theObjects.append(aPrs->object());
224       if (aPrs->shape().get() && (!aHasOperation)) {
225         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
226         if (aResult.get()) {
227           aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
228           if (aFeature.get() && (!theObjects.contains(aFeature)))
229             theObjects.append(aFeature);
230         }
231       }
232     }
233   }
234 }
235
236 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
237 {
238   std::list<FeaturePtr> aFeatures;
239   QObjectPtrList aObjects = selection()->selectedObjects();
240   if (aObjects.isEmpty())
241     return aFeatures;
242
243   bool isPart = false;
244   foreach(ObjectPtr aObj, aObjects) {
245     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
246     if (aFeature.get()) {
247       ResultPtr aRes = aFeature->firstResult();
248       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
249       if (!isPart)
250         aFeatures.push_back(aFeature);
251     }
252   }
253   return aFeatures;
254 }