Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[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
23 #include <ModuleBase_ViewerPrs.h>
24
25 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
26
27 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
28     : QObject(theParent),
29       myWorkshop(theParent)
30 {
31   mySelection = new XGUI_Selection(myWorkshop);
32 }
33
34 XGUI_SelectionMgr::~XGUI_SelectionMgr()
35 {
36   delete mySelection;
37 }
38
39 //**************************************************************
40 void XGUI_SelectionMgr::connectViewers()
41 {
42   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
43           SLOT(onObjectBrowserSelection()));
44
45   //Connect to other viewers
46   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
47 }
48
49 //**************************************************************
50 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
51                                           bool isUpdateViewer)
52 {
53   SelectMgr_IndexedMapOfOwner aSelectedOwners;
54   selection()->selectedOwners(aSelectedOwners);
55
56   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
57   if (!aContext.IsNull()) {
58     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
59       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
60       if (aSelectedOwners.FindIndex(anOwner) > 0)
61         continue;
62
63       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
64     }
65   }
66 }
67
68 //**************************************************************
69 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
70 {
71   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
72   if (aContext.IsNull())
73     return;
74
75   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
76
77   SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
78
79   SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
80   for (; anIt.More(); anIt.Next()) {
81     Handle(SelectMgr_Filter) aFilter = anIt.Value();
82     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
83       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
84       if (!aFilter->IsOk(anOwner))
85         anOwnersToDeselect.Add(aContext->SelectedOwner());
86     }
87   }
88
89   setSelectedOwners(anOwnersToDeselect, false);
90
91   if (isUpdateViewer)
92     aContext->UpdateCurrentViewer();
93 }
94
95 //**************************************************************
96 void XGUI_SelectionMgr::onObjectBrowserSelection()
97 {
98   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
99              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
100
101   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
102   ObjectPtr aObject;
103   FeaturePtr aFeature;
104   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
105     aObject = aPrs->object();
106     if (aObject.get()) {
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();
110         std::list<ResultPtr>::const_iterator aIt;
111         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
112           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
113                new ModuleBase_ViewerPrs((*aIt), GeomShapePtr(), NULL)));
114         }
115       }
116     }
117   }
118   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
119   aDisplayer->setSelected(aSelectedPrs);
120   emit selectionChanged();
121 }
122
123 //**************************************************************
124 void XGUI_SelectionMgr::onViewerSelection()
125 {
126   SessionPtr aMgr = ModelAPI_Session::get();
127   DocumentPtr anActiveDocument = aMgr->activeDocument();
128   QObjectPtrList aFeatures;
129   ResultPtr aResult;
130   FeaturePtr aFeature;
131   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
132   if (!aContext.IsNull()) {
133     QList<ModuleBase_ViewerPrsPtr> aPresentations = selection()->getSelected(ModuleBase_ISelection::Viewer);
134     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
135       if (aPrs->object().get()) {
136         aFeatures.append(aPrs->object());
137         if (aPrs->shape().get()) {
138           aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
139           if (aResult.get()) {
140             aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
141             if (aFeature.get() && (!aFeatures.contains(aFeature)))
142               aFeatures.append(aFeature);
143           }
144         }
145       }
146     }
147   }
148   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
149   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
150   myWorkshop->objectBrowser()->blockSignals(aBlocked);
151
152   emit selectionChanged();
153 }
154
155 //**************************************************************
156 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
157 {
158   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
159                myWorkshop->selector()->selection()->getSelected(thePlace);
160   if (thePlace == ModuleBase_ISelection::Browser) {
161     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
162     aDisplayer->setSelected(aSelectedPrs);
163   }
164
165 }
166 //**************************************************************
167 void XGUI_SelectionMgr::clearSelection()
168 {
169   QObjectPtrList aFeatures;
170   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
171   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
172   myWorkshop->objectBrowser()->blockSignals(aBlocked);
173   
174   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
175              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
176
177   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
178   aDisplayer->setSelected(aSelectedPrs);
179
180   emit selectionChanged();
181 }