1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Guillaume Boulant (EDF/R&D)
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
30 # ==================================================================
33 # ==================================================================
34 # Definition of usefull objects and functions to deal with the SALOME
36 # ==================================================================
39 from salome.kernel import services
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
48 sgPyQt=SalomePyQt.SalomePyQt()
51 # Get SALOME Swig interface to manipulate the GUI widget
54 SalomeGUI = libSALOME_Swig.SALOMEGUI_Swig()
57 # ==================================================================
58 # General helper function for GUI programming actions
59 # ==================================================================
61 # Get the active study
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.
69 studyId = sgPyQt.getStudyId()
70 study = services.getStudyManager().GetStudyByID( studyId )
74 # ==================================================================
75 # Functions to manipulate the objects in the browser (generic)
76 # ==================================================================
78 def getSObjectSelected():
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
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 )
93 sobj = study.FindObjectID( entry )
96 def showSObjectSelected():
98 This function print the attributes of the selected object.
99 (this function is only for test purpose)
101 sobj, entry = getSObjectSelected()
103 test, attr = sobj.FindAttribute( "AttributeName" )
105 message = "My name is '%s'" % attr.Value()
109 def deleteSObjectSelected():
111 This function deletes the selected object.
113 sobj, entry = getSObjectSelected()
115 study = getActiveStudy()
116 builder = study.NewBuilder()
117 builder.RemoveObject( sobj )
118 SalomeGUI.updateObjBrowser(True)
122 # ==================================================================
123 # Use cases and demo functions
124 # ==================================================================
127 # CAUTION: Before running this test functions, you first have to
128 # create (or import) an object and select this object in the objects
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.
140 def TEST_getSObjectSelected():
141 mySObject, myEntry = getSObjectSelected()
142 myName = mySObject.GetName()
143 print "The name of the selected object is %s"%myName
145 def TEST_showSObjectSelected():
146 showSObjectSelected()
148 if __name__ == "__main__":
149 TEST_getSObjectSelected()
150 TEST_showSObjectSelected()