Salome HOME
Synchronize adm files
[modules/gui.git] / src / GuiHelpers / SALOME_GuiServices.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_GuiServices.hxx"
23 #include <SUIT_Session.h>
24
25 namespace GUI {
26   /*!
27    * Get the SALOME application, i.e. the main GUI framework of
28    * SALOME. This function returns a pointer to the singleton instance
29    * of the SalomeApp_Application.
30    * 
31    * The SALOME application can be used to get reference to
32    * the SALOME KERNEL services such that the naming service, the
33    * lifeCycleCORBA, ... Theses services can be retrieved using static
34    * functions (not required to get the singleton instance):
35    *
36    *   SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
37    *
38    * Please note that this usage can be done only from within the
39    * SALOME session server process, i.e. the process that embed the
40    * SALOME application.
41    */
42   SalomeApp_Application * getSalomeApplication() {
43     static SalomeApp_Application * app;
44     if ( app == NULL ) {
45       app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
46     }
47     return app;
48   }
49
50   /*!
51    * Get the selection manager of the SALOME application. The
52    * selection manager can be used to get the selected items in the
53    * objects browser that is a GUI display of the active study.
54    * The function returns a pointer to the selectionMgr attribute of
55    * the SalomeApp_Application singleton instance.
56    */
57   LightApp_SelectionMgr * getSelectionManager() {
58     SalomeApp_Application* anApp = GUI::getSalomeApplication();
59     if ( anApp == NULL ) return NULL;
60     return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
61   }
62
63
64   SUIT_ResourceMgr * getResourcesManager() {
65     return SUIT_Session::session()->resourceMgr();
66   }
67   // Note that the resource manager can be also retrieved from the
68   // SALOME application using the resourceMgr() method:
69   // 
70
71   /**
72    * This returns the current active study id if an active study is
73    * defined in the SALOME session, returns -1 otherwise. Note that
74    * the active study doesn't make sense outside of the GUI SALOME
75    * process, i.e. the SALOME_SessionServer embedding the
76    * SalomeApp_Application.
77    */
78   int getActiveStudyId() {
79     SALOME::Session_var aSession = KERNEL::getSalomeSession();
80     if ( CORBA::is_nil(aSession) ) {
81       INFOS("ERR: can't request for active study because the session is NULL");
82       return -1;
83     }
84     return aSession->GetActiveStudyId();
85   }
86
87   /**
88    * This returns the current active study if an active study is
89    * defined in the SALOME session, returns null otherwise.
90    */
91   SALOMEDS::Study_ptr getActiveStudy() {
92     return KERNEL::getStudyById(getActiveStudyId());
93   }
94
95
96   // __GBO__ Question: what is the difference between a
97   // SALOMEDS::Study and a SalomeApp_Study?
98   SalomeApp_Study* getSalomeAppActiveStudy() {
99     SUIT_Application* app = getSalomeApplication();
100     if( app )
101       return dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
102     else
103       return NULL;
104   }
105
106
107   SALOMEDS::SObject_ptr IObjectToSObject(const Handle(SALOME_InteractiveObject)& iobject) {
108     if (!iobject.IsNull()) {
109       if (iobject->hasEntry()) {
110         SalomeApp_Study* appStudy = getSalomeAppActiveStudy();
111         if ( appStudy != NULL ) {
112           //
113           // IMPORTANT NOTE:
114           //
115           // Note that the SalomeApp_Study give acces to shared
116           // pointer (i.e. _PTR(<BaseClassName>)) that points to proxy
117           // objects (i.e. SALOMEDSClien_<BaseClassName>) that embeds
118           // the CORBA servants (i.e. SALOMEDS::<BaseClassName>_ptr).
119           // Then to retrieve the corba servant, a method is to
120           // retrieve the SALOMEDS::Study servant first and the to
121           // request this servant to get the SObject given its entry.
122           //
123           _PTR(Study) studyClient = appStudy->studyDS();
124           SALOMEDS::Study_var study = KERNEL::getStudyManager()->GetStudyByID(studyClient->StudyId());
125           SALOMEDS::SObject_ptr sobject = study->FindObjectID(iobject->getEntry());
126           return sobject;
127         }
128       }
129     }
130     return SALOMEDS::SObject::_nil();
131   }
132
133 }
134