Salome HOME
cf61433f600613749f4f94e4b8fa83be13eff558
[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 #include "XGUI_OperationMgr.h"
12
13 #ifndef HAVE_SALOME
14 #include <AppElements_MainWindow.h>
15 #endif
16
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_AttributeDocRef.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultCompSolid.h>
24
25 #include <ModuleBase_ViewerPrs.h>
26 #include <ModuleBase_Tools.h>
27
28 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
29
30 #ifdef TINSPECTOR
31 #include <VInspectorAPI_CallBack.hxx>
32 #endif
33
34 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
35     : QObject(theParent),
36       myWorkshop(theParent)
37 {
38   mySelection = new XGUI_Selection(myWorkshop);
39 }
40
41 XGUI_SelectionMgr::~XGUI_SelectionMgr()
42 {
43   delete mySelection;
44 }
45
46 //**************************************************************
47 void XGUI_SelectionMgr::connectViewers()
48 {
49   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
50           SLOT(onObjectBrowserSelection()));
51
52   //Connect to other viewers
53   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
54 }
55
56 //**************************************************************
57 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
58                                           bool isUpdateViewer)
59 {
60   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
61   if (!aContext.IsNull()) {
62     /// previous selection should be cleared, else there will be decomposition of selections:
63     /// as AddOrRemoveSelected inverts current selection
64     aContext->ClearSelected(false);
65
66     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
67       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
68
69       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
70       #ifdef TINSPECTOR
71       if (myWorkshop->displayer()->getCallBack())
72         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
73       #endif
74     }
75   }
76 }
77
78 //**************************************************************
79 void XGUI_SelectionMgr::onObjectBrowserSelection()
80 {
81   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
82              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
83
84   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
85   ObjectPtr aObject;
86   FeaturePtr aFeature;
87   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
88     aObject = aPrs->object();
89     if (aObject.get()) {
90       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
91       if (aFeature.get()) {
92         const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
93         ResultPtr aResult;
94         ResultCompSolidPtr aCompSolid;
95         std::list<ResultPtr>::const_iterator aIt;
96         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
97           aResult = (*aIt);
98           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
99             new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
100           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
101           if (aCompSolid.get()) {
102             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
103               ResultBodyPtr aResult = aCompSolid->subResult(i);
104               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
105                 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
106             }
107           }
108         }
109       }
110     }
111   }
112   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
113   aDisplayer->setSelected(aSelectedPrs);
114   emit selectionChanged();
115 }
116
117 //**************************************************************
118 void XGUI_SelectionMgr::onViewerSelection()
119 {
120   QList<ModuleBase_ViewerPrsPtr> aValues;
121   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
122   if (!aContext.IsNull())
123     aValues = selection()->getSelected(ModuleBase_ISelection::Viewer);
124
125   QObjectPtrList anObjects;
126   convertToObjectBrowserSelection(aValues, anObjects);
127
128   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
129   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
130   myWorkshop->objectBrowser()->blockSignals(aBlocked);
131
132   emit selectionChanged();
133 }
134
135 //**************************************************************
136 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
137 {
138   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
139                myWorkshop->selector()->selection()->getSelected(thePlace);
140   if (thePlace == ModuleBase_ISelection::Browser) {
141     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
142     aDisplayer->setSelected(aSelectedPrs);
143   }
144
145 }
146 //**************************************************************
147 void XGUI_SelectionMgr::clearSelection()
148 {
149   QObjectPtrList aFeatures;
150   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
151   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
152   myWorkshop->objectBrowser()->blockSignals(aBlocked);
153
154   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
155              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
156
157   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
158   aDisplayer->setSelected(aSelectedPrs);
159
160   emit selectionChanged();
161 }
162 //**************************************************************
163 void XGUI_SelectionMgr::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
164 {
165   // update selection in Viewer
166   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
167   aDisplayer->setSelected(theValues);
168
169   // update selection in Object Browser
170   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
171   QObjectPtrList anObjects;
172   convertToObjectBrowserSelection(theValues, anObjects);
173
174   myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
175   myWorkshop->objectBrowser()->blockSignals(aBlocked);
176 }
177 //**************************************************************
178 void XGUI_SelectionMgr::convertToObjectBrowserSelection(
179                                    const QList<ModuleBase_ViewerPrsPtr>& theValues,
180                                    QObjectPtrList& theObjects)
181 {
182   theObjects.clear();
183
184   ResultPtr aResult;
185   FeaturePtr aFeature;
186   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
187   SessionPtr aMgr = ModelAPI_Session::get();
188   DocumentPtr anActiveDocument = aMgr->activeDocument();
189
190   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
191     if (aPrs->object().get()) {
192       if (!theObjects.contains(aPrs->object()))
193         theObjects.append(aPrs->object());
194       if (aPrs->shape().get() && (!aHasOperation)) {
195         aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
196         if (aResult.get()) {
197           aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
198           if (aFeature.get() && (!theObjects.contains(aFeature)))
199             theObjects.append(aFeature);
200         }
201       }
202     }
203   }
204 }