1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "XGUI_SelectionMgr.h"
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"
31 #include <AppElements_MainWindow.h>
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>
42 #include <ModuleBase_ViewerPrs.h>
43 #include <ModuleBase_Tools.h>
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <VInspectorAPI_CallBack.hxx>
51 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
55 mySelection = new XGUI_Selection(myWorkshop);
58 XGUI_SelectionMgr::~XGUI_SelectionMgr()
63 //**************************************************************
64 void XGUI_SelectionMgr::connectViewers()
66 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
67 SLOT(onObjectBrowserSelection()));
69 //Connect to other viewers
70 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
73 //**************************************************************
74 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
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);
83 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
84 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
86 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
88 if (myWorkshop->displayer()->getCallBack())
89 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
95 //**************************************************************
96 void XGUI_SelectionMgr::onObjectBrowserSelection()
98 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
99 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
101 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
104 foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
105 aObject = aPrs->object();
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();
111 ResultCompSolidPtr aCompSolid;
112 std::list<ResultPtr>::const_iterator aIt;
113 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++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)));
129 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
130 aDisplayer->setSelected(aSelectedPrs);
131 emit selectionChanged();
134 //**************************************************************
135 void XGUI_SelectionMgr::onViewerSelection()
137 QList<ModuleBase_ViewerPrsPtr> aValues;
138 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
139 if (!aContext.IsNull())
140 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
142 QObjectPtrList anObjects;
143 convertToObjectBrowserSelection(aValues, anObjects);
145 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
146 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
147 myWorkshop->objectBrowser()->blockSignals(aBlocked);
149 emit selectionChanged();
152 //**************************************************************
153 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
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);
163 //**************************************************************
164 void XGUI_SelectionMgr::clearSelection()
166 QObjectPtrList aFeatures;
167 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
168 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
169 myWorkshop->objectBrowser()->blockSignals(aBlocked);
171 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
172 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
174 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
175 aDisplayer->setSelected(aSelectedPrs);
177 emit selectionChanged();
179 //**************************************************************
180 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
182 // update selection in Viewer
183 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
184 aDisplayer->setSelected(theValues);
186 // update selection in Object Browser
187 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
188 QObjectPtrList anObjects;
189 convertToObjectBrowserSelection(theValues, anObjects);
191 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
192 myWorkshop->objectBrowser()->blockSignals(aBlocked);
194 //**************************************************************
195 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
196 const QList<ModuleBase_ViewerPrsPtr>& theValues,
197 QObjectPtrList& theObjects)
203 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
204 SessionPtr aMgr = ModelAPI_Session::get();
205 DocumentPtr anActiveDocument = aMgr->activeDocument();
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());
214 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
215 if (aFeature.get() && (!theObjects.contains(aFeature)))
216 theObjects.append(aFeature);