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);
102 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
103 if (!myWorkshop->operationMgr()->hasOperation()) {
105 QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
108 // Select all results of a selected feature in viewer
109 foreach(ModuleBase_ViewerPrsPtr aPrs, aSelectedPrs) {
110 aObject = aPrs->object();
112 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
113 if (aFeature.get()) {
114 std::list<ResultPtr> allRes;
115 ModelAPI_Tools::allResults(aFeature, allRes);
116 for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
117 aTmpList.append(std::shared_ptr<ModuleBase_ViewerPrs>(
118 new ModuleBase_ViewerPrs(*aRes, GeomShapePtr(), NULL)));
123 aDisplayer->setSelected(aTmpList);
125 aDisplayer->setSelected(aSelectedPrs);
127 emit selectionChanged();
130 //**************************************************************
131 void XGUI_SelectionMgr::onViewerSelection()
133 QList<ModuleBase_ViewerPrsPtr> aValues;
134 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
135 if (!aContext.IsNull())
136 aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
138 QObjectPtrList anObjects;
139 convertToObjectBrowserSelection(aValues, anObjects);
141 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
142 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
143 myWorkshop->objectBrowser()->blockSignals(aBlocked);
145 emit selectionChanged();
148 //**************************************************************
149 void XGUI_SelectionMgr::deselectPresentation(const Handle(AIS_InteractiveObject) theObject)
151 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
153 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
154 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
155 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
156 if (anOwner.IsNull()) // TODO: check why it is possible
158 if (anOwner->Selectable() == theObject && anOwner->IsSelected())
159 aResultOwners.Append(anOwner);
161 NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (aResultOwners);
162 Handle(SelectMgr_EntityOwner) anOwner;
163 for (; anOwnersIt.More(); anOwnersIt.Next()) {
164 anOwner = Handle(SelectMgr_EntityOwner)::DownCast(anOwnersIt.Value());
165 if (!anOwner.IsNull())
166 aContext->AddOrRemoveSelected(anOwner, false);
170 //**************************************************************
171 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
173 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
174 myWorkshop->selector()->selection()->getSelected(thePlace);
175 if (thePlace == ModuleBase_ISelection::Browser) {
176 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
177 aDisplayer->setSelected(aSelectedPrs);
181 //**************************************************************
182 void XGUI_SelectionMgr::clearSelection()
184 QObjectPtrList aFeatures;
185 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
186 myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
187 myWorkshop->objectBrowser()->blockSignals(aBlocked);
189 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
190 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
192 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193 aDisplayer->setSelected(aSelectedPrs);
195 emit selectionChanged();
197 //**************************************************************
198 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
200 // update selection in Viewer
201 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
202 aDisplayer->setSelected(theValues);
204 // update selection in Object Browser
205 bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
206 QObjectPtrList anObjects;
207 convertToObjectBrowserSelection(theValues, anObjects);
209 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
210 myWorkshop->objectBrowser()->blockSignals(aBlocked);
212 //**************************************************************
213 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
214 const QList<ModuleBase_ViewerPrsPtr>& theValues,
215 QObjectPtrList& theObjects)
221 bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
222 SessionPtr aMgr = ModelAPI_Session::get();
223 DocumentPtr anActiveDocument = aMgr->activeDocument();
225 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
226 if (aPrs->object().get()) {
227 if (!theObjects.contains(aPrs->object()))
228 theObjects.append(aPrs->object());
229 if (aPrs->shape().get() && (!aHasOperation)) {
230 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
232 aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
233 if (aFeature.get() && (!theObjects.contains(aFeature)))
234 theObjects.append(aFeature);
241 std::list<FeaturePtr> XGUI_SelectionMgr::getSelectedFeatures()
243 std::list<FeaturePtr> aFeatures;
244 QObjectPtrList aObjects = selection()->selectedObjects();
245 if (aObjects.isEmpty())
249 foreach(ObjectPtr aObj, aObjects) {
250 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
251 if (aFeature.get()) {
252 ResultPtr aRes = aFeature->firstResult();
253 isPart = (aRes.get() && (aRes->groupName() == ModelAPI_ResultPart::group()));
255 aFeatures.push_back(aFeature);