Salome HOME
Synchronize adm files
[modules/gui.git] / src / GuiHelpers / SALOME_AppStudyEditor.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: Guillaume Boulant (EDF/R&D)
21
22 #include "SALOME_AppStudyEditor.hxx"
23
24 #include <SalomeApp_Study.h>
25 #include <SALOME_ListIO.hxx>
26 #include <LightApp_SelectionMgr.h>
27 #include <SALOME_ListIteratorOfListIO.hxx>
28
29 SALOME_AppStudyEditor::SALOME_AppStudyEditor(SalomeApp_Application * salomeApp)
30   : SALOME_StudyEditor()
31 {
32   _salomeApp = salomeApp;
33   updateActiveStudy();
34 }
35
36 /**
37  * This updates the editor with the current active study. If the
38  * active study id is identical to the study id currently associated
39  * to this object, then no update is performed.
40  */
41 int SALOME_AppStudyEditor::updateActiveStudy() {
42   int activeStudyId = SALOME_AppStudyEditor::getActiveStudyId(_salomeApp);
43   if ( activeStudyId != this->getStudyId() ) {
44     this->setStudyById(activeStudyId);
45   }
46   return activeStudyId;
47 }
48
49 // GUI context only
50 int SALOME_AppStudyEditor::getActiveStudyId(SalomeApp_Application * salomeApp) {
51   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*> (salomeApp->activeStudy());
52   _PTR(Study) aCStudy = appStudy->studyDS();
53   int studyId = aCStudy->StudyId();
54   return studyId;
55 }
56
57 SALOMEDS::SObject_ptr SALOME_AppStudyEditor::IObjectToSObject(const Handle(SALOME_InteractiveObject)& iobject) {
58   if (!iobject.IsNull()) {
59     if (iobject->hasEntry()) {
60       SALOMEDS::SObject_var sobject = _study->FindObjectID(iobject->getEntry());
61       return sobject._retn();
62     }
63   }
64   return SALOMEDS::SObject::_nil();
65 }
66
67 /**
68  * This function creates a list of all the "study objects" (SObject)
69  * that map with the selection in the object browser (the "interactive
70  * objects", IObjects). Please note that the objects belongs to the
71  * active study.
72  */
73 SALOME_StudyEditor::SObjectList * SALOME_AppStudyEditor::getSelectedObjects() {
74
75   // _GBO_: please note that the use of this function can be
76   // underoptimal in the case where the number of selected objects is
77   // high, because we create a vector list of SObjects while the list
78   // of IObject can be processed directly. In the case where a high
79   // number of objects is selected (~1000), it's certainly better to
80   // use directly the SALOME_ListIO and iterators on it.
81
82   LightApp_SelectionMgr* aSelectionMgr = _salomeApp->selectionMgr();
83   SALOME_ListIO selectedIObjects;
84   aSelectionMgr->selectedObjects(selectedIObjects);
85   /* 
86    * This returns a list containing the selected objects of the objects
87    * browser. Please note that the objects of the browser ARE NOT the
88    * objects of the study. What is returned by this function is a list
89    * of "interactive objects" (IObject) which are objects of the
90    * graphical context. Each of this IObject is characterized by an
91    * entry string that corresponds to the entry string of an associated
92    * "study object" (SObject) in the active study. The mapping can be
93    * done using the function IObjectToSObject.
94    */
95
96   SObjectList * listOfSObject = new SObjectList();
97   SALOME_ListIteratorOfListIO It (selectedIObjects);
98   for (; It.More(); It.Next()) {
99     SALOMEDS::SObject_ptr sobject = this->IObjectToSObject(It.Value());
100     if (!sobject->_is_nil()) {
101       listOfSObject->push_back(sobject);
102     }
103   }
104
105   return listOfSObject;
106 }