Salome HOME
Merge branch 'V7_main' of salome:modules/gui.git into V7_main
[modules/gui.git] / src / GUI_PY / helper.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2014  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 # General helper function for GUI programming actions
59 # ==================================================================
60
61 # Get the active study
62 #
63 def getActiveStudy():
64     """
65     This returns a study object that corresponds to the active
66     study. The active study is a GUI concept: it's the study currently
67     active on the desktop.
68     """
69     studyId = sgPyQt.getStudyId()
70     study = services.getStudyManager().GetStudyByID( studyId )
71     return study
72
73 #
74 # ==================================================================
75 # Functions to manipulate the objects in the browser (generic)
76 # ==================================================================
77 #
78 def getSObjectSelected():
79     '''
80     Returns the sobject and the entry of the item currently selected
81     in the objects browser. Returns "None, None" if no item is
82     selected. If several objects are selected, it returns the first
83     one of the list.
84     '''
85     sobj = None
86     entry = None
87     study = getActiveStudy()
88     if SalomeGUI.SelectedCount() == 1:
89         # We only considere the first element of the list. If you need
90         # something else, create another function in your own context.
91         entry = SalomeGUI.getSelected( 0 )
92         if entry != '':
93             sobj = study.FindObjectID( entry )
94     return sobj, entry
95
96 def showSObjectSelected():
97     '''
98     This function print the attributes of the selected object.
99     (this function is  only for test purpose)
100     '''
101     sobj, entry = getSObjectSelected()
102     if ( sobj ):
103         test, attr = sobj.FindAttribute( "AttributeName" )
104         if test:
105             message = "My name is '%s'" % attr.Value()
106             print message
107     pass
108
109 def deleteSObjectSelected(): 
110     '''
111     This function deletes the selected object.
112     '''
113     sobj, entry = getSObjectSelected()
114     if ( sobj ):
115         study = getActiveStudy()
116         builder = study.NewBuilder()
117         builder.RemoveObject( sobj )
118         SalomeGUI.updateObjBrowser(True)
119     pass
120
121 #
122 # ==================================================================
123 # Use cases and demo functions
124 # ==================================================================
125 #
126
127 # CAUTION: Before running this test functions, you first have to
128 # create (or import) an object and select this object in the objects
129 # browser.
130
131 #
132 # Definitions:
133 # - the SObject is an item in the active study (Study Object).
134 # - the entry is the identifier of an item.
135 # - the object (geom object or smesh object) is a CORBA servant
136 #   embedded in the SALOME component container and with a reference in
137 #   the SALOME study, so that it can be retrieved.
138 #
139
140 def TEST_getSObjectSelected():
141     mySObject, myEntry = getSObjectSelected()
142     myName = mySObject.GetName()
143     print "The name of the selected object is %s"%myName
144
145 def TEST_showSObjectSelected():
146     showSObjectSelected()
147
148 if __name__ == "__main__":
149     TEST_getSObjectSelected()
150     TEST_showSObjectSelected()