Salome HOME
Issue #1037 : do not find model indices for the same result. If selection mode is...
[modules/shaper.git] / src / XGUI / XGUI_SelectionMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_SelectionMgr.h"
4
5 #include "XGUI_Workshop.h"
6 #include "XGUI_ObjectsBrowser.h"
7 #include "XGUI_SalomeConnector.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Displayer.h"
10 #include "XGUI_Selection.h"
11
12 #ifndef HAVE_SALOME
13 #include <AppElements_MainWindow.h>
14 #endif
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Session.h>
18 #include <ModelAPI_AttributeDocRef.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_ResultCompSolid.h>
23
24 #include <ModuleBase_ViewerPrs.h>
25
26 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
27
28 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
29     : QObject(theParent),
30       myWorkshop(theParent)
31 {
32   mySelection = new XGUI_Selection(myWorkshop);
33 }
34
35 XGUI_SelectionMgr::~XGUI_SelectionMgr()
36 {
37   delete mySelection;
38 }
39
40 //**************************************************************
41 void XGUI_SelectionMgr::connectViewers()
42 {
43   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
44           SLOT(onObjectBrowserSelection()));
45
46   //Connect to other viewers
47   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
48 }
49
50 //**************************************************************
51 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
52                                           bool isUpdateViewer)
53 {
54   SelectMgr_IndexedMapOfOwner aSelectedOwners;
55   selection()->selectedOwners(aSelectedOwners);
56
57   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
58   if (!aContext.IsNull()) {
59     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
60       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
61       if (aSelectedOwners.FindIndex(anOwner) > 0)
62         continue;
63
64       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
65     }
66   }
67 }
68
69 //**************************************************************
70 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
71 {
72   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
73   if (aContext.IsNull())
74     return;
75
76   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
77
78   SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
79
80   SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
81   for (; anIt.More(); anIt.Next()) {
82     Handle(SelectMgr_Filter) aFilter = anIt.Value();
83     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
84       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
85       if (!aFilter->IsOk(anOwner))
86         anOwnersToDeselect.Add(aContext->SelectedOwner());
87     }
88   }
89
90   setSelectedOwners(anOwnersToDeselect, false);
91
92   if (isUpdateViewer)
93     aContext->UpdateCurrentViewer();
94 }
95
96 //**************************************************************
97 void XGUI_SelectionMgr::onObjectBrowserSelection()
98 {
99   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
100              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
101
102   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
103   ObjectPtr aObject;
104   FeaturePtr aFeature;
105   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
106     aObject = aPrs->object();
107     if (aObject.get()) {
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();
111         ResultPtr aResult;
112         ResultCompSolidPtr aCompSolid;
113         std::list<ResultPtr>::const_iterator aIt;
114         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
115           aResult = (*aIt);
116           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
117           if (aCompSolid.get()) {
118             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
119               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
120                    new ModuleBase_ViewerPrs(aCompSolid->subResult(i), GeomShapePtr(), NULL)));
121             }
122           } else {
123             aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
124                  new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
125           }
126         }
127       }
128     }
129   }
130   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
131   aDisplayer->setSelected(aSelectedPrs);
132   emit selectionChanged();
133 }
134
135 //**************************************************************
136 void XGUI_SelectionMgr::onViewerSelection()
137 {
138   SessionPtr aMgr = ModelAPI_Session::get();
139   DocumentPtr anActiveDocument = aMgr->activeDocument();
140   QObjectPtrList aFeatures;
141   ResultPtr aResult;
142   FeaturePtr aFeature;
143   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
144   if (!aContext.IsNull()) {
145     QList<ModuleBase_ViewerPrsPtr> aPresentations = selection()->getSelected(ModuleBase_ISelection::Viewer);
146     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
147       if (aPrs->object().get()) {
148         if (!aFeatures.contains(aPrs->object()))
149           aFeatures.append(aPrs->object());
150         if (aPrs->shape().get()) {
151           aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
152           if (aResult.get()) {
153             aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
154             if (aFeature.get() && (!aFeatures.contains(aFeature)))
155               aFeatures.append(aFeature);
156           }
157         }
158       }
159     }
160   }
161   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
162   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
163   myWorkshop->objectBrowser()->blockSignals(aBlocked);
164
165   emit selectionChanged();
166 }
167
168 //**************************************************************
169 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
170 {
171   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
172                myWorkshop->selector()->selection()->getSelected(thePlace);
173   if (thePlace == ModuleBase_ISelection::Browser) {
174     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
175     aDisplayer->setSelected(aSelectedPrs);
176   }
177
178 }
179 //**************************************************************
180 void XGUI_SelectionMgr::clearSelection()
181 {
182   QObjectPtrList aFeatures;
183   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
184   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
185   myWorkshop->objectBrowser()->blockSignals(aBlocked);
186   
187   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
188              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
189
190   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
191   aDisplayer->setSelected(aSelectedPrs);
192
193   emit selectionChanged();
194 }