Salome HOME
updated copyright message
[modules/gui.git] / src / GUI_PY / helper.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # Author: Guillaume Boulant (EDF/R&D)
22
23 # ==================================================================
24 # This file provides helper functions to drive some gui features of a
25 # SALOME Application (the selection in the object browser for now,
26 # further development coming soon). Note that some of these helper
27 # functions are specialized for a GEOM or SMESH context (see the
28 # guihelper.py modules comming with the packages salome.geom and
29 # salome.smesh.
30 # ==================================================================
31
32 #
33 # ==================================================================
34 # Definition of usefull objects and functions to deal with the SALOME
35 # technical context.
36 # ==================================================================
37 #
38
39 import salome
40 from salome.kernel import services
41
42 #
43 # Get SALOME PyQt interface to manipulate the GUI context
44 # (Get the active study for example. Note that the active study is a
45 # GUI concept only: it is the study that is currently active in the
46 # desktop)
47 #
48 import SalomePyQt
49 sgPyQt=SalomePyQt.SalomePyQt()
50
51 #
52 # Get SALOME Swig interface to manipulate the GUI widget
53 #
54 import libSALOME_Swig
55 SalomeGUI = libSALOME_Swig.SALOMEGUI_Swig()
56
57 #
58 # ==================================================================
59 # Functions to manipulate the objects in the browser (generic)
60 # ==================================================================
61 #
62 def getSObjectSelected():
63     '''
64     Returns the sobject and the entry of the item currently selected
65     in the objects browser. Returns "None, None" if no item is
66     selected. If several objects are selected, it returns the first
67     one of the list.
68     '''
69     sobj = None
70     entry = None
71     study = salome.myStudy
72     if SalomeGUI.SelectedCount() == 1:
73         # We only considere the first element of the list. If you need
74         # something else, create another function in your own context.
75         entry = SalomeGUI.getSelected( 0 )
76         if entry != '':
77             sobj = study.FindObjectID( entry )
78     return sobj, entry
79
80 def showSObjectSelected():
81     '''
82     This function print the attributes of the selected object.
83     (this function is  only for test purpose)
84     '''
85     sobj, entry = getSObjectSelected()
86     if ( sobj ):
87         test, attr = sobj.FindAttribute( "AttributeName" )
88         if test:
89             message = "My name is '%s'" % attr.Value()
90             print(message)
91     pass
92
93 def deleteSObjectSelected(): 
94     '''
95     This function deletes the selected object.
96     '''
97     sobj, entry = getSObjectSelected()
98     if ( sobj ):
99         study = salome.myStudy
100         builder = study.NewBuilder()
101         builder.RemoveObject( sobj )
102         SalomeGUI.updateObjBrowser()
103     pass
104
105 #
106 # ==================================================================
107 # Use cases and demo functions
108 # ==================================================================
109 #
110
111 # CAUTION: Before running this test functions, you first have to
112 # create (or import) an object and select this object in the objects
113 # browser.
114
115 #
116 # Definitions:
117 # - the SObject is an item in the study (Study Object).
118 # - the entry is the identifier of an item.
119 # - the object (geom object or smesh object) is a CORBA servant
120 #   embedded in the SALOME component container and with a reference in
121 #   the SALOME study, so that it can be retrieved.
122 #
123
124 def TEST_getSObjectSelected():
125     mySObject, myEntry = getSObjectSelected()
126     myName = mySObject.GetName()
127     print("The name of the selected object is %s"%myName)
128
129 def TEST_showSObjectSelected():
130     showSObjectSelected()
131
132 if __name__ == "__main__":
133     TEST_getSObjectSelected()
134     TEST_showSObjectSelected()