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_ResultBody.h>
42 #include <ModelAPI_Tools.h>
44 #include <ModuleBase_ViewerPrs.h>
45 #include <ModuleBase_Tools.h>
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
50 #include <inspector/VInspectorAPI_CallBack.hxx>
53 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
57 mySelection = new XGUI_Selection(myWorkshop);
60 XGUI_SelectionMgr::~XGUI_SelectionMgr()
65 //**************************************************************
66 void XGUI_SelectionMgr::connectViewers()
68 connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
69 SLOT(onObjectBrowserSelection()));
71 //Connect to other viewers
72 connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
75 //**************************************************************
76 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
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);
85 for (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++) {
86 Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
88 aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
90 if (myWorkshop->displayer()->getCallBack())
91 myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
97 //**************************************************************
98 void XGUI_SelectionMgr::onObjectBrowserSelection()
100 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
101 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
103 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
106 foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
107 aObject = aPrs->object();
109 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
110 if (aFeature.get()) {
111 std::list<ResultPtr> allRes;
112 ModelAPI_Tools::allResults(aFeature, allRes);
113 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
114 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
115 new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
120 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
121 aDisplayer->setSelected(aSelectedPrs);
122 emit selectionChanged();
125 //**************************************************************
126 void XGUI_SelectionMgr::onViewerSelection()
128 QList<ModuleBase_ViewerPrsPtr> aValues;
129 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
130 if (!aContext.IsNull())
131 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
133 QObjectPtrList anObjects;
134 convertToObjectBrowserSelection(aValues, anObjects);
136 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
137 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
138 myWorkshop->objectBrowser()->blockSignals(aBlocked);
140 emit selectionChanged();
143 //**************************************************************
144 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
146 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
148 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
149 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
150 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
151 if (anOwner.IsNull()) // TODO: check why it is possible
153 if (anOwner->Selectable() == theObject && anOwner->IsSelected())
154 aResultOwners.Append(anOwner);
156 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
157 Handle(SelectMgr_EntityOwner) anOwner;
158 for (; anOwnersIt.More(); anOwnersIt.Next()) {
159 anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
160 if (!anOwner.IsNull())
161 aContext->AddOrRemoveSelected(anOwner, false);
165 //**************************************************************
166 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
168 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
169 myWorkshop->selector()->selection()->getSelected(thePlace);
170 if (thePlace == ModuleBase_ISelection::Browser) {
171 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
172 aDisplayer->setSelected(aSelectedPrs);
176 //**************************************************************
177 void XGUI_SelectionMgr::clearSelection()
179 QObjectPtrList aFeatures;
180 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
181 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
182 myWorkshop->objectBrowser()->blockSignals(aBlocked);
184 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
185 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
187 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
188 aDisplayer->setSelected(aSelectedPrs);
190 emit selectionChanged();
192 //**************************************************************
193 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
195 // update selection in Viewer
196 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
197 aDisplayer->setSelected(theValues);
199 // update selection in Object Browser
200 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
201 QObjectPtrList anObjects;
202 convertToObjectBrowserSelection(theValues, anObjects);
204 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
205 myWorkshop->objectBrowser()->blockSignals(aBlocked);
207 //**************************************************************
208 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
209 const QList<ModuleBase_ViewerPrsPtr>& theValues,
210 QObjectPtrList& theObjects)
216 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
217 SessionPtr aMgr = ModelAPI_Session::get();
218 DocumentPtr anActiveDocument = aMgr->activeDocument();
220 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
221 if (aPrs->object().get()) {
222 if (!theObjects.contains(aPrs->object()))
223 theObjects.append(aPrs->object());
224 if (aPrs->shape().get() && (!aHasOperation)) {
225 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
227 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
228 if (aFeature.get() && (!theObjects.contains(aFeature)))
229 theObjects.append(aFeature);
236 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
238 std::list<FeaturePtr> aFeatures;
239 QObjectPtrList aObjects = selection()->selectedObjects();
240 if (aObjects.isEmpty())
244 foreach(ObjectPtr aObj, aObjects) {
245 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
246 if (aFeature.get()) {
247 ResultPtr aRes = aFeature->firstResult();
248 isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
250 aFeatures.push_back(aFeature);