Back to index

salome-gui  6.5.0
helper.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 # Author: Guillaume Boulant (EDF/R&D)
00022 
00023 # ==================================================================
00024 # This file provides helper functions to drive some gui features of a
00025 # SALOME Application (the selection in the object browser for now,
00026 # further development coming soon). Note that some of these helper
00027 # functions are specialized for a GEOM or SMESH context (see the
00028 # guihelper.py modules comming with the packages salome.geom and
00029 # salome.smesh.
00030 # ==================================================================
00031 
00032 #
00033 # ==================================================================
00034 # Definition of usefull objects and functions to deal with the SALOME
00035 # technical context.
00036 # ==================================================================
00037 #
00038 
00039 from salome.kernel import services
00040 
00041 #
00042 # Get SALOME PyQt interface to manipulate the GUI context
00043 # (Get the active study for example. Note that the active study is a
00044 # GUI concept only: it is the study that is currently active in the
00045 # desktop)
00046 #
00047 import SalomePyQt
00048 sgPyQt=SalomePyQt.SalomePyQt()
00049 
00050 #
00051 # Get SALOME Swig interface to manipulate the GUI widget
00052 #
00053 import libSALOME_Swig
00054 SalomeGUI = libSALOME_Swig.SALOMEGUI_Swig()
00055 
00056 #
00057 # ==================================================================
00058 # General helper function for GUI programming actions
00059 # ==================================================================
00060 # 
00061 # Get the active study
00062 #
00063 def getActiveStudy():
00064     """
00065     This returns a study object that corresponds to the active
00066     study. The active study is a GUI concept: it's the study currently
00067     active on the desktop.
00068     """
00069     studyId = sgPyQt.getStudyId()
00070     study = services.getStudyManager().GetStudyByID( studyId )
00071     return study
00072 
00073 #
00074 # ==================================================================
00075 # Functions to manipulate the objects in the browser (generic)
00076 # ==================================================================
00077 #
00078 def getSObjectSelected():
00079     '''
00080     Returns the sobject and the entry of the item currently selected
00081     in the objects browser. Returns "None, None" if no item is
00082     selected. If several objects are selected, it returns the first
00083     one of the list.
00084     '''
00085     sobj = None
00086     entry = None
00087     study = getActiveStudy()
00088     if SalomeGUI.SelectedCount() == 1:
00089         # We only considere the first element of the list. If you need
00090         # something else, create another function in your own context.
00091         entry = SalomeGUI.getSelected( 0 )
00092         if entry != '':
00093             sobj = study.FindObjectID( entry )
00094     return sobj, entry
00095 
00096 def showSObjectSelected():
00097     '''
00098     This function print the attributes of the selected object.
00099     (this function is  only for test purpose)
00100     '''
00101     sobj, entry = getSObjectSelected()
00102     if ( sobj ):
00103         test, attr = sobj.FindAttribute( "AttributeName" )
00104         if test:
00105             message = "My name is '%s'" % attr.Value()
00106             print message
00107     pass
00108 
00109 def deleteSObjectSelected(): 
00110     '''
00111     This function deletes the selected object.
00112     '''
00113     sobj, entry = getSObjectSelected()
00114     if ( sobj ):
00115         study = getActiveStudy()
00116         builder = study.NewBuilder()
00117         builder.RemoveObject( sobj )
00118         SalomeGUI.updateObjBrowser(True)
00119     pass
00120 
00121 #
00122 # ==================================================================
00123 # Use cases and demo functions
00124 # ==================================================================
00125 #
00126 
00127 # CAUTION: Before running this test functions, you first have to
00128 # create (or import) an object and select this object in the objects
00129 # browser.
00130 
00131 #
00132 # Definitions:
00133 # - the SObject is an item in the active study (Study Object).
00134 # - the entry is the identifier of an item.
00135 # - the object (geom object or smesh object) is a CORBA servant
00136 #   embedded in the SALOME component container and with a reference in
00137 #   the SALOME study, so that it can be retrieved.
00138 #
00139 
00140 def TEST_getSObjectSelected():
00141     mySObject, myEntry = getSObjectSelected()
00142     myName = mySObject.GetName()
00143     print "The name of the selected object is %s"%myName
00144 
00145 def TEST_showSObjectSelected():
00146     showSObjectSelected()
00147 
00148 if __name__ == "__main__":
00149     TEST_getSObjectSelected()
00150     TEST_showSObjectSelected()