Salome HOME
Porting to SALOME_8.2.0 : Selection with SHIFT in sketch, stayed selection of point...
[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 #include <ModuleBase_Tools.h>
26
27 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
28
29 #ifdef VINSPECTOR
30 #include <VInspectorAPI_CallBack.h>
31 #endif
32
33 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
34     : QObject(theParent),
35       myWorkshop(theParent)
36 {
37   mySelection = new XGUI_Selection(myWorkshop);
38 }
39
40 XGUI_SelectionMgr::~XGUI_SelectionMgr()
41 {
42   delete mySelection;
43 }
44
45 //**************************************************************
46 void XGUI_SelectionMgr::connectViewers()
47 {
48   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
49           SLOT(onObjectBrowserSelection()));
50
51   //Connect to other viewers
52   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
53 }
54
55 //**************************************************************
56 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
57                                           bool isUpdateViewer)
58 {
59   SelectMgr_IndexedMapOfOwner aSelectedOwners;
60   selection()->selectedOwners(aSelectedOwners);
61
62   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
63   if (!aContext.IsNull()) {
64     /// previous selection should be cleared, else there will be decomposition of selections:
65     /// as AddOrRemoveSelected inverts current selection
66     aContext->ClearSelected(false);
67
68     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
69       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
70       if (aSelectedOwners.FindIndex(anOwner) > 0)
71         continue;
72
73       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
74       #ifdef VINSPECTOR
75       if (myWorkshop->displayer()->getCallBack())
76         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
77       #endif
78     }
79   }
80 }
81
82 //**************************************************************
83 void XGUI_SelectionMgr::onObjectBrowserSelection()
84 {
85   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
86              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
87
88   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
89   ObjectPtr aObject;
90   FeaturePtr aFeature;
91   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
92     aObject = aPrs->object();
93     if (aObject.get()) {
94       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
95       if (aFeature.get()) {
96         const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
97         ResultPtr aResult;
98         ResultCompSolidPtr aCompSolid;
99         std::list<ResultPtr>::const_iterator aIt;
100         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
101           aResult = (*aIt);
102           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
103             new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
104           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
105           if (aCompSolid.get()) {
106             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
107               ResultBodyPtr aResult = aCompSolid->subResult(i);
108               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
109                 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
110             }
111           }
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 =
132       selection()->getSelected(ModuleBase_ISelection::Viewer);
133     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
134       if (aPrs->object().get()) {
135         if (!aFeatures.contains(aPrs->object()))
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 }