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