]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_SelectionMgr.cpp
Salome HOME
Issue #2626: Do not use non-initialized attribute for default value definition
[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   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
103   if (!myWorkshop->operationMgr()->hasOperation()) {
104
105     QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
106     ObjectPtr aObject;
107     FeaturePtr aFeature;
108     // Select all results of a selected feature in viewer
109     foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
110       aObject = aPrs->object();
111       if (aObject.get()) {
112         aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
113         if (aFeature.get()) {
114         std::list<ResultPtr> allRes;
115         ModelAPI_Tools::allResults(aFeature, allRes);
116         for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
117           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
118             new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
119           }
120         }
121       }
122     }
123     aDisplayer->setSelected(aTmpList);
124   } else {
125     aDisplayer->setSelected(aSelectedPrs);
126   }
127   emit selectionChanged();
128 }
129
130 //**************************************************************
131 void XGUI_SelectionMgr::onViewerSelection()
132 {
133   QList<ModuleBase_ViewerPrsPtr> aValues;
134   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
135   if (!aContext.IsNull())
136     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
137
138   QObjectPtrList anObjects;
139   convertToObjectBrowserSelection(aValues, anObjects);
140
141   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
142   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
143   myWorkshop->objectBrowser()->blockSignals(aBlocked);
144
145   emit selectionChanged();
146 }
147
148 //**************************************************************
149 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
150 {
151   NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
152
153   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
154   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
155     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
156     if (anOwner.IsNull()) // TODO: check why it is possible
157       continue;
158     if (anOwner->Selectable() == theObject && anOwner->IsSelected())
159       aResultOwners.Append(anOwner);
160   }
161   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
162   Handle(SelectMgr_EntityOwner) anOwner;
163   for (; anOwnersIt.More(); anOwnersIt.Next()) {
164     anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
165     if (!anOwner.IsNull())
166       aContext->AddOrRemoveSelected(anOwner, false);
167   }
168 }
169
170 //**************************************************************
171 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
172 {
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);
178   }
179
180 }
181 //**************************************************************
182 void XGUI_SelectionMgr::clearSelection()
183 {
184   QObjectPtrList aFeatures;
185   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
186   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
187   myWorkshop->objectBrowser()->blockSignals(aBlocked);
188
189   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
190              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
191
192   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193   aDisplayer->setSelected(aSelectedPrs);
194
195   emit selectionChanged();
196 }
197 //**************************************************************
198 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
199 {
200   // update selection in Viewer
201   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
202   aDisplayer->setSelected(theValues);
203
204   // update selection in Object Browser
205   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
206   QObjectPtrList anObjects;
207   convertToObjectBrowserSelection(theValues, anObjects);
208
209   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
210   myWorkshop->objectBrowser()->blockSignals(aBlocked);
211 }
212 //**************************************************************
213 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
214                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
215                                    QObjectPtrList& theObjects)
216 {
217   theObjects.clear();
218
219   ResultPtr aResult;
220   FeaturePtr aFeature;
221   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
222   SessionPtr aMgr = ModelAPI_Session::get();
223   DocumentPtr anActiveDocument = aMgr->activeDocument();
224
225   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
226     if (aPrs->object().get()) {
227       if (!theObjects.contains(aPrs->object()))
228         theObjects.append(aPrs->object());
229       if (aPrs->shape().get() && (!aHasOperation)) {
230         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
231         if (aResult.get()) {
232           aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
233           if (aFeature.get() && (!theObjects.contains(aFeature)))
234             theObjects.append(aFeature);
235         }
236       }
237     }
238   }
239 }
240
241 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
242 {
243   std::list<FeaturePtr> aFeatures;
244   QObjectPtrList aObjects = selection()->selectedObjects();
245   if (aObjects.isEmpty())
246     return aFeatures;
247
248   bool isPart = false;
249   foreach(ObjectPtr aObj, aObjects) {
250     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
251     if (aFeature.get()) {
252       ResultPtr aRes = aFeature->firstResult();
253       isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
254       if (!isPart)
255         aFeatures.push_back(aFeature);
256     }
257   }
258   return aFeatures;
259 }