Salome HOME
Issue #2154: Do not synchronize selection for result and features if any operation...
[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   SessionPtr aMgr = ModelAPI_Session::get();
121   DocumentPtr anActiveDocument = aMgr->activeDocument();
122   QObjectPtrList aFeatures;
123   ResultPtr aResult;
124   FeaturePtr aFeature;
125   bool aHasOperation = (myWorkshop->operationMgr()->currentOperation() != 0);
126   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
127   if (!aContext.IsNull()) {
128     QList<ModuleBase_ViewerPrsPtr> aPresentations =
129       selection()->getSelected(ModuleBase_ISelection::Viewer);
130     foreach(ModuleBase_ViewerPrsPtr aPrs, aPresentations) {
131       if (aPrs->object().get()) {
132         if (!aFeatures.contains(aPrs->object()))
133           aFeatures.append(aPrs->object());
134         if (aPrs->shape().get() && (!aHasOperation)) {
135           aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
136           if (aResult.get()) {
137             aFeature = anActiveDocument->producedByFeature(aResult, aPrs->shape());
138             if (aFeature.get() && (!aFeatures.contains(aFeature)))
139               aFeatures.append(aFeature);
140           }
141         }
142       }
143     }
144   }
145   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
146   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
147   myWorkshop->objectBrowser()->blockSignals(aBlocked);
148
149   emit selectionChanged();
150 }
151
152 //**************************************************************
153 void XGUI_SelectionMgr::updateSelectionBy(const ModuleBase_ISelection::SelectionPlace& thePlace)
154 {
155   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
156                myWorkshop->selector()->selection()->getSelected(thePlace);
157   if (thePlace == ModuleBase_ISelection::Browser) {
158     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
159     aDisplayer->setSelected(aSelectedPrs);
160   }
161
162 }
163 //**************************************************************
164 void XGUI_SelectionMgr::clearSelection()
165 {
166   QObjectPtrList aFeatures;
167   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
168   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
169   myWorkshop->objectBrowser()->blockSignals(aBlocked);
170
171   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs =
172              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
173
174   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
175   aDisplayer->setSelected(aSelectedPrs);
176
177   emit selectionChanged();
178 }