Salome HOME
Merge branch 'Pre_2.8.0_development'
[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_ResultCompSolid.h>
42
43 #include <ModuleBase_ViewerPrs.h>
44 #include <ModuleBase_Tools.h>
45
46 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
47
48 #ifdef TINSPECTOR
49 #include <VInspectorAPI_CallBack.hxx>
50 #endif
51
52 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
53     : QObject(theParent),
54       myWorkshop(theParent)
55 {
56   mySelection = new XGUI_Selection(myWorkshop);
57 }
58
59 XGUI_SelectionMgr::~XGUI_SelectionMgr()
60 {
61   delete mySelection;
62 }
63
64 //**************************************************************
65 void XGUI_SelectionMgr::connectViewers()
66 {
67   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
68           SLOT(onObjectBrowserSelection()));
69
70   //Connect to other viewers
71   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
72 }
73
74 //**************************************************************
75 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
76                                           bool isUpdateViewer)
77 {
78   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
79   if (!aContext.IsNull()) {
80     /// previous selection should be cleared, else there will be decomposition of selections:
81     /// as AddOrRemoveSelected inverts current selection
82     aContext->ClearSelected(false);
83
84     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
85       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
86
87       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
88       #ifdef TINSPECTOR
89       if (myWorkshop->displayer()->getCallBack())
90         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
91       #endif
92     }
93   }
94 }
95
96 //**************************************************************
97 void XGUI_SelectionMgr::onObjectBrowserSelection()
98 {
99   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
100              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
101
102   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
103   ObjectPtr aObject;
104   FeaturePtr aFeature;
105   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
106     aObject = aPrs->object();
107     if (aObject.get()) {
108       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
109       if (aFeature.get()) {
110         const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
111         ResultPtr aResult;
112         ResultCompSolidPtr aCompSolid;
113         std::list<ResultPtr>::const_iterator aIt;
114         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
115           aResult = (*aIt);
116           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
117             new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
118           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
119           if (aCompSolid.get()) {
120             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
121               ResultBodyPtr aResult = aCompSolid->subResult(i);
122               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
123                 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
124             }
125           }
126         }
127       }
128     }
129   }
130   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
131   aDisplayer->setSelected(aSelectedPrs);
132   emit selectionChanged();
133 }
134
135 //**************************************************************
136 void XGUI_SelectionMgr::onViewerSelection()
137 {
138   QList<ModuleBase_ViewerPrsPtr> aValues;
139   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
140   if (!aContext.IsNull())
141     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
142
143   QObjectPtrList anObjects;
144   convertToObjectBrowserSelection(aValues, anObjects);
145
146   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
147   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
148   myWorkshop->objectBrowser()->blockSignals(aBlocked);
149
150   emit selectionChanged();
151 }
152
153 //**************************************************************
154 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
155 {
156   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
157                myWorkshop->selector()->selection()->getSelected(thePlace);
158   if (thePlace == ModuleBase_ISelection::Browser) {
159     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
160     aDisplayer->setSelected(aSelectedPrs);
161   }
162
163 }
164 //**************************************************************
165 void XGUI_SelectionMgr::clearSelection()
166 {
167   QObjectPtrList aFeatures;
168   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
169   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
170   myWorkshop->objectBrowser()->blockSignals(aBlocked);
171
172   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
173              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
174
175   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
176   aDisplayer->setSelected(aSelectedPrs);
177
178   emit selectionChanged();
179 }
180 //**************************************************************
181 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
182 {
183   // update selection in Viewer
184   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
185   aDisplayer->setSelected(theValues);
186
187   // update selection in Object Browser
188   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
189   QObjectPtrList anObjects;
190   convertToObjectBrowserSelection(theValues, anObjects);
191
192   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
193   myWorkshop->objectBrowser()->blockSignals(aBlocked);
194 }
195 //**************************************************************
196 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
197                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
198                                    QObjectPtrList& theObjects)
199 {
200   theObjects.clear();
201
202   ResultPtr aResult;
203   FeaturePtr aFeature;
204   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
205   SessionPtr aMgr = ModelAPI_Session::get();
206   DocumentPtr anActiveDocument = aMgr->activeDocument();
207
208   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
209     if (aPrs->object().get()) {
210       if (!theObjects.contains(aPrs->object()))
211         theObjects.append(aPrs->object());
212       if (aPrs->shape().get() && (!aHasOperation)) {
213         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
214         if (aResult.get()) {
215           aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
216           if (aFeature.get() && (!theObjects.contains(aFeature)))
217             theObjects.append(aFeature);
218         }
219       }
220     }
221   }
222 }