Salome HOME
082cf4dbb79d3cfaedcbcef4d60c98d42999615d
[modules/gui.git] / src / GuiHelpers / SALOME_GuiServices.cxx
1 // Copyright (C) 2007-2022  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 if an active study is
73    * defined in the SALOME session, returns null otherwise.
74    */
75   SALOMEDS::Study_ptr getStudyServant() {
76     return SALOMEDS::Study::_duplicate(KERNEL::getStudyServant());
77   }
78
79   // __GBO__ Question: what is the difference between a
80   // SALOMEDS::Study and a SalomeApp_Study?
81   SalomeApp_Study* getSalomeAppActiveStudy() {
82     SUIT_Application* app = getSalomeApplication();
83     if( app )
84       return dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
85     else
86       return NULL;
87   }
88
89
90   SALOMEDS::SObject_ptr IObjectToSObject(const Handle(SALOME_InteractiveObject)& iobject) {
91     if (!iobject.IsNull()) {
92       if (iobject->hasEntry()) {
93         SalomeApp_Study* appStudy = getSalomeAppActiveStudy();
94         if ( appStudy != NULL ) {
95           //
96           // IMPORTANT NOTE:
97           //
98           // Note that the SalomeApp_Study give acces to shared
99           // pointer (i.e. _PTR(<BaseClassName>)) that points to proxy
100           // objects (i.e. SALOMEDSClien_<BaseClassName>) that embeds
101           // the CORBA servants (i.e. SALOMEDS::<BaseClassName>_ptr).
102           // Then to retrieve the corba servant, a method is to
103           // retrieve the SALOMEDS::Study servant first and the to
104           // request this servant to get the SObject given its entry.
105           //
106           SALOMEDS::SObject_ptr sobject = KERNEL::getStudyServant()->FindObjectID(iobject->getEntry());
107           return sobject;
108         }
109       }
110     }
111     return SALOMEDS::SObject::_nil();
112   }
113
114 }
115