Salome HOME
Issue #956 Fatal error as result of action without OCC scene
[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 #include <AppElements_MainWindow.h>
13
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_AttributeDocRef.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Result.h>
19 #include <ModelAPI_Object.h>
20
21 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
22
23 XGUI_SelectionMgr::XGUI_SelectionMgr(XGUI_Workshop* theParent)
24     : QObject(theParent),
25       myWorkshop(theParent)
26 {
27   mySelection = new XGUI_Selection(myWorkshop);
28 }
29
30 XGUI_SelectionMgr::~XGUI_SelectionMgr()
31 {
32   delete mySelection;
33 }
34
35 //**************************************************************
36 void XGUI_SelectionMgr::connectViewers()
37 {
38   connect(myWorkshop->objectBrowser(), SIGNAL(selectionChanged()), this,
39           SLOT(onObjectBrowserSelection()));
40
41   //Connect to other viewers
42   connect(myWorkshop->viewer(), SIGNAL(selectionChanged()), this, SLOT(onViewerSelection()));
43 }
44
45 //**************************************************************
46 void XGUI_SelectionMgr::setSelectedOwners(const SelectMgr_IndexedMapOfOwner& theSelectedOwners,
47                                           bool isUpdateViewer)
48 {
49   SelectMgr_IndexedMapOfOwner aSelectedOwners;
50   selection()->selectedOwners(aSelectedOwners);
51
52   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
53   if (!aContext.IsNull()) {
54     for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
55       Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
56       if (aSelectedOwners.FindIndex(anOwner) > 0)
57         continue;
58
59       aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
60     }
61   }
62 }
63
64 //**************************************************************
65 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
66 {
67   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
68   if (aContext.IsNull())
69     return;
70
71   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
72
73   SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
74
75   SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
76   for (; anIt.More(); anIt.Next()) {
77     Handle(SelectMgr_Filter) aFilter = anIt.Value();
78     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
79       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
80       if (!aFilter->IsOk(anOwner))
81         anOwnersToDeselect.Add(aContext->SelectedOwner());
82     }
83   }
84
85   setSelectedOwners(anOwnersToDeselect, false);
86
87   if (isUpdateViewer)
88     aContext->UpdateCurrentViewer();
89 }
90
91 //**************************************************************
92 void XGUI_SelectionMgr::onObjectBrowserSelection()
93 {
94   QList<ModuleBase_ViewerPrs> aSelectedPrs =
95              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
96
97   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
98   aDisplayer->setSelected(aSelectedPrs);
99   emit selectionChanged();
100 }
101
102 //**************************************************************
103 void XGUI_SelectionMgr::onViewerSelection()
104 {
105   QObjectPtrList aFeatures;
106   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
107   if (!aContext.IsNull()) {
108     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
109       Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
110       ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
111       if (aResult)
112         aFeatures.append(aResult);
113     }
114   }
115   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
116   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
117   myWorkshop->objectBrowser()->blockSignals(aBlocked);
118
119   emit selectionChanged();
120 }
121
122 //**************************************************************
123 void XGUI_SelectionMgr::clearSelection()
124 {
125   QObjectPtrList aFeatures;
126   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
127   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
128   myWorkshop->objectBrowser()->blockSignals(aBlocked);
129   
130   QList<ModuleBase_ViewerPrs> aSelectedPrs =
131              myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Browser);
132
133   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
134   aDisplayer->setSelected(aSelectedPrs);
135
136   emit selectionChanged();
137 }