Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.1
[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   for  (Standard_Integer i = 1, n = theSelectedOwners.Extent(); i <= n; i++)  {
54     Handle(SelectMgr_EntityOwner) anOwner = theSelectedOwners(i);
55     if (aSelectedOwners.FindIndex(anOwner) > 0)
56       continue;
57
58     aContext->AddOrRemoveSelected(anOwner, isUpdateViewer);
59   }
60 }
61
62 //**************************************************************
63 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
64 void XGUI_SelectionMgr::updateSelectedOwners(bool isUpdateViewer)
65 {
66   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
67   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
68
69   SelectMgr_IndexedMapOfOwner anOwnersToDeselect;
70
71   SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
72   for (; anIt.More(); anIt.Next()) {
73     Handle(SelectMgr_Filter) aFilter = anIt.Value();
74     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
75       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
76       if (!aFilter->IsOk(anOwner))
77         anOwnersToDeselect.Add(aContext->SelectedOwner());
78     }
79   }
80
81   setSelectedOwners(anOwnersToDeselect, false);
82
83   if (isUpdateViewer)
84     aContext->UpdateCurrentViewer();
85 }
86
87 //**************************************************************
88 void XGUI_SelectionMgr::onObjectBrowserSelection()
89 {
90   QObjectPtrList aObjects = myWorkshop->objectBrowser()->selectedObjects();
91   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
92   aDisplayer->setSelected(aObjects);
93   emit selectionChanged();
94 }
95
96 //**************************************************************
97 void XGUI_SelectionMgr::onViewerSelection()
98 {
99   QObjectPtrList aFeatures;
100   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
101   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
102     Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
103     ObjectPtr aResult = myWorkshop->displayer()->getObject(anIO);
104     if (aResult)
105       aFeatures.append(aResult);
106   }
107   bool aBlocked = myWorkshop->objectBrowser()->blockSignals(true);
108   myWorkshop->objectBrowser()->setObjectsSelected(aFeatures);
109   myWorkshop->objectBrowser()->blockSignals(aBlocked);
110
111   emit selectionChanged();
112 }
113
114 //**************************************************************
115 /*QFeatureList XGUI_SelectionMgr::selectedFeatures() const 
116  { 
117  return myWorkshop->objectBrowser()->selectedFeatures(); 
118  }
119
120  //**************************************************************
121  QModelIndexList XGUI_SelectionMgr::selectedIndexes() const 
122  { 
123  return myWorkshop->objectBrowser()->selectedIndexes();
124  }
125
126  //**************************************************************
127  void XGUI_SelectionMgr::selectedAISObjects(AIS_ListOfInteractive& theList) const
128  {
129  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
130  theList.Clear();
131  for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
132  theList.Append(aContext->SelectedInteractive());
133  }
134
135  //**************************************************************
136  void XGUI_SelectionMgr::selectedShapes(NCollection_List<TopoDS_Shape>& theList) const
137  {
138  theList.Clear();
139  Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
140  for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
141  TopoDS_Shape aShape = aContext->SelectedShape();
142  if (!aShape.IsNull())
143  theList.Append(aShape);
144  }
145  }*/