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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_SelectionMgr.h"
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"
32 #include <AppElements_MainWindow.h>
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>
43 #include <ModuleBase_ViewerPrs.h>
44 #include <ModuleBase_Tools.h>
46 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
49 #include <inspector/VInspectorAPI_CallBack.hxx>
52 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
56 mySelection = new XGUI_Selection(myWorkshop);
59 XGUI_SelectionMgr::~XGUI_SelectionMgr()
64 //**************************************************************
65 void XGUI_SelectionMgr::connectViewers()
67 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
68 SLOT(onObjectBrowserSelection()));
70 //Connect to other viewers
71 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
74 //**************************************************************
75 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
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);
84 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
85 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
87 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
89 if (myWorkshop->displayer()->getCallBack())
90 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
96 //**************************************************************
97 void XGUI_SelectionMgr::onObjectBrowserSelection()
99 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
100 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
102 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
105 foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
106 aObject = aPrs->object();
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();
112 ResultCompSolidPtr aCompSolid;
113 std::list<ResultPtr>::const_iterator aIt;
114 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++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)));
130 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
131 aDisplayer->setSelected(aSelectedPrs);
132 emit selectionChanged();
135 //**************************************************************
136 void XGUI_SelectionMgr::onViewerSelection()
138 QList<ModuleBase_ViewerPrsPtr> aValues;
139 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
140 if (!aContext.IsNull())
141 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
143 QObjectPtrList anObjects;
144 convertToObjectBrowserSelection(aValues, anObjects);
146 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
147 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
148 myWorkshop->objectBrowser()->blockSignals(aBlocked);
150 emit selectionChanged();
153 //**************************************************************
154 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
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);
164 //**************************************************************
165 void XGUI_SelectionMgr::clearSelection()
167 QObjectPtrList aFeatures;
168 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
169 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
170 myWorkshop->objectBrowser()->blockSignals(aBlocked);
172 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
173 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
175 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
176 aDisplayer->setSelected(aSelectedPrs);
178 emit selectionChanged();
180 //**************************************************************
181 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
183 // update selection in Viewer
184 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
185 aDisplayer->setSelected(theValues);
187 // update selection in Object Browser
188 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
189 QObjectPtrList anObjects;
190 convertToObjectBrowserSelection(theValues, anObjects);
192 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
193 myWorkshop->objectBrowser()->blockSignals(aBlocked);
195 //**************************************************************
196 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
197 const QList<ModuleBase_ViewerPrsPtr>& theValues,
198 QObjectPtrList& theObjects)
204 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
205 SessionPtr aMgr = ModelAPI_Session::get();
206 DocumentPtr anActiveDocument = aMgr->activeDocument();
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());
215 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
216 if (aFeature.get() && (!theObjects.contains(aFeature)))
217 theObjects.append(aFeature);
224 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
226 std::list<FeaturePtr> aFeatures;
227 QObjectPtrList aObjects = selection()->selectedObjects();
228 if (aObjects.isEmpty())
231 foreach(ObjectPtr aObj, aObjects) {
232 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
234 aFeatures.push_back(aFeature);