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