Salome HOME
Selection in Sketch with SHIFT pressed.
[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   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
60   if (!aContext.IsNull()) {
61     /// previous selection should be cleared, else there will be decomposition of selections:
62     /// as AddOrRemoveSelected inverts current selection
63     aContext->ClearSelected(false);
64
65     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
66       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
67
68       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
69       #ifdef VINSPECTOR
70       if (myWorkshop->displayer()->getCallBack())
71         myWorkshop->displayer()->getCallBack()->AddOrRemoveSelected(anOwner);
72       #endif
73     }
74   }
75 }
76
77 //**************************************************************
78 void XGUI_SelectionMgr::onObjectBrowserSelection()
79 {
80   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
81              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
82
83   QList<ModuleBase_ViewerPrsPtr> aTmpList = aSelectedPrs;
84   ObjectPtr aObject;
85   FeaturePtr aFeature;
86   foreach(ModuleBase_ViewerPrsPtr aPrs, aTmpList) {
87     aObject = aPrs->object();
88     if (aObject.get()) {
89       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
90       if (aFeature.get()) {
91         const std::list<std::shared_ptr<ModelAPI_Result>> aResList = aFeature->results();
92         ResultPtr aResult;
93         ResultCompSolidPtr aCompSolid;
94         std::list<ResultPtr>::const_iterator aIt;
95         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
96           aResult = (*aIt);
97           aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
98             new ModuleBase_ViewerPrs(aResult, GeomShapePtr(), NULL)));
99           aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
100           if (aCompSolid.get()) {
101             for (int i = 0; i < aCompSolid->numberOfSubs(); i++) {
102               ResultBodyPtr aResult = aCompSolid->subResult(i);
103               aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
104                 new ModuleBase_ViewerPrs(aResult, aResult->shape(), NULL)));
105             }
106           }
107         }
108       }
109     }
110   }
111   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
112   aDisplayer->setSelected(aSelectedPrs);
113   emit selectionChanged();
114 }
115
116 //**************************************************************
117 void XGUI_SelectionMgr::onViewerSelection()
118 {
119   SessionPtr aMgr = ModelAPI_Session::get();
120   DocumentPtr anActiveDocument = aMgr->activeDocument();
121   QObjectPtrList aFeatures;
122   ResultPtr aResult;
123   FeaturePtr aFeature;
124   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
125   if (!aContext.IsNull()) {
126     QList<ModuleBase_ViewerPrsPtr> aPresentations =
127       selection()->getSelected(ModuleBase_ISelection::Viewer);
128     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
129       if (aPrs->object().get()) {
130         if (!aFeatures.contains(aPrs->object()))
131           aFeatures.append(aPrs->object());
132         if (aPrs->shape().get()) {
133           aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
134           if (aResult.get()) {
135             aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
136             if (aFeature.get() && (!aFeatures.contains(aFeature)))
137               aFeatures.append(aFeature);
138           }
139         }
140       }
141     }
142   }
143   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
144   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
145   myWorkshop->objectBrowser()->blockSignals(aBlocked);
146
147   emit selectionChanged();
148 }
149
150 //**************************************************************
151 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
152 {
153   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
154                myWorkshop->selector()->selection()->getSelected(thePlace);
155   if (thePlace == ModuleBase_ISelection::Browser) {
156     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
157     aDisplayer->setSelected(aSelectedPrs);
158   }
159
160 }
161 //**************************************************************
162 void XGUI_SelectionMgr::clearSelection()
163 {
164   QObjectPtrList aFeatures;
165   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
166   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
167   myWorkshop->objectBrowser()->blockSignals(aBlocked);
168
169   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
170              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
171
172   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
173   aDisplayer->setSelected(aSelectedPrs);
174
175   emit selectionChanged();
176 }