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