Salome HOME
Merge Python 3 porting.
[modules/gui.git] / src / GUI_PY / helper.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2016  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 from salome.kernel import services
40
41 #
42 # Get SALOME PyQt interface to manipulate the GUI context
43 # (Get the active study for example. Note that the active study is a
44 # GUI concept only: it is the study that is currently active in the
45 # desktop)
46 #
47 import SalomePyQt
48 sgPyQt=SalomePyQt.SalomePyQt()
49
50 #
51 # Get SALOME Swig interface to manipulate the GUI widget
52 #
53 import libSALOME_Swig
54 SalomeGUI = libSALOME_Swig.SALOMEGUI_Swig()
55
56 #
57 # ==================================================================
58 # Functions to manipulate the objects in the browser (generic)
59 # ==================================================================
60 #
61 def getSObjectSelected():
62     '''
63     Returns the sobject and the entry of the item currently selected
64     in the objects browser. Returns "None, None" if no item is
65     selected. If several objects are selected, it returns the first
66     one of the list.
67     '''
68     sobj = None
69     entry = None
70     study = salome.myStudy
71     if SalomeGUI.SelectedCount() == 1:
72         # We only considere the first element of the list. If you need
73         # something else, create another function in your own context.
74         entry = SalomeGUI.getSelected( 0 )
75         if entry != '':
76             sobj = study.FindObjectID( entry )
77     return sobj, entry
78
79 def showSObjectSelected():
80     '''
81     This function print the attributes of the selected object.
82     (this function is  only for test purpose)
83     '''
84     sobj, entry = getSObjectSelected()
85     if ( sobj ):
86         test, attr = sobj.FindAttribute( "AttributeName" )
87         if test:
88             message = "My name is '%s'" % attr.Value()
89             print(message)
90     pass
91
92 def deleteSObjectSelected(): 
93     '''
94     This function deletes the selected object.
95     '''
96     sobj, entry = getSObjectSelected()
97     if ( sobj ):
98         study = salome.myStudy
99         builder = study.NewBuilder()
100         builder.RemoveObject( sobj )
101         SalomeGUI.updateObjBrowser()
102     pass
103
104 #
105 # ==================================================================
106 # Use cases and demo functions
107 # ==================================================================
108 #
109
110 # CAUTION: Before running this test functions, you first have to
111 # create (or import) an object and select this object in the objects
112 # browser.
113
114 #
115 # Definitions:
116 # - the SObject is an item in the study (Study Object).
117 # - the entry is the identifier of an item.
118 # - the object (geom object or smesh object) is a CORBA servant
119 #   embedded in the SALOME component container and with a reference in
120 #   the SALOME study, so that it can be retrieved.
121 #
122
123 def TEST_getSObjectSelected():
124     mySObject, myEntry = getSObjectSelected()
125     myName = mySObject.GetName()
126     print("The name of the selected object is %s"%myName)
127
128 def TEST_showSObjectSelected():
129     showSObjectSelected()
130
131 if __name__ == "__main__":
132     TEST_getSObjectSelected()
133     TEST_showSObjectSelected()