1 # Copyright (C) 2005 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 from omniORB import CORBA
23 from SALOME_NamingServicePy import *
24 from LifeCycleCORBA import *
26 import SALOMEDS_Attributes_idl
30 ################################################
32 __MODULE_NAME__ = "PYHELLO"
35 ################################################
37 # Get SALOME PyQt interface
39 sgPyQt=SalomePyQt.SalomePyQt()
41 # Get SALOME Swig interface
43 sg = libSALOME_Swig.SALOMEGUI_Swig()
45 ################################################
48 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
50 # create naming service instance
51 naming_service = SALOME_NamingServicePy_i( orb )
53 # create life cycle CORBA instance
54 lcc = LifeCycleCORBA( orb )
57 obj = naming_service.Resolve( '/myStudyManager' )
58 studyManager = obj._narrow( SALOMEDS.StudyManager )
60 ################################################
63 # --- get PYHELLO engine ---
65 engine = lcc.FindOrLoadComponent( "FactoryServerPy", __MODULE_NAME__ )
68 # --- get active study ---
70 studyId = sgPyQt.getStudyId()
71 study = studyManager.GetStudyByID( studyId )
74 # --- returns 1 if object has children ---
75 def _hasChildren( sobj ):
78 iter = study.NewChildIterator( sobj )
80 name = iter.Value().GetName()
86 # --- finds or creates component object ---
87 def _findOrCreateComponent():
89 father = study.FindComponent( __MODULE_NAME__ )
91 builder = study.NewBuilder()
92 father = builder.NewComponent( __MODULE_NAME__ )
93 attr = builder.FindOrCreateAttribute( father, "AttributeName" )
94 attr.SetValue( __MODULE_NAME__ )
95 attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
96 attr.SetValue( __MODULE_ID__ )
98 builder.DefineComponentInstance( father, _getEngine() )
104 ################################################
107 # set workspace (obsolete method, not used)
108 def setWorkSpace( pyws ):
109 print "PYHELLOGUI::setWorkSpace : ", pyws
112 # called when module is activated
114 print "PYHELLOGUI::setSettings"
117 # called when active study is changed
118 def activeStudyChanged( studyID ):
119 print "PYHELLOGUI::activeStudyChanged: study ID =", studyID
123 def definePopup( context, object, parent ):
128 if sg.SelectedCount() == 1:
129 entry = sg.getSelected( 0 )
131 sobj = study.FindObjectID( entry )
133 test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
135 id = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
138 print "PYHELLOGUI::definePopup :", context, object, parent
139 return context, object, parent
141 # customize popup menu
142 def customPopup( popup, context, object, parent ):
143 print "PYHELLOGUI::customPopup :", context, object, parent
146 if id == __MODULE_ID__:
148 if sg.SelectedCount() == 1:
149 entry = sg.getSelected( 0 )
151 sobj = study.FindObjectID( entry )
152 if sobj and not _hasChildren( sobj ):
153 popup.removeItem( 951 ) # remove 'Delete All' command
159 def OnGUIEvent(commandID) :
160 print "PYHELLOGUI::OnGUIEvent : commandID =",commandID
161 if dict_command.has_key( commandID ):
163 dict_command[commandID]()
165 traceback.print_exc()
167 print "The command is not implemented: ",commandID
169 ################################################
170 # GUI actions implementation
172 # ----------------------- #
174 # ----------------------- #
175 class MyDialog( QDialog ):
177 def __init__( self, parent = None, modal = 0):
178 QDialog.__init__( self, parent, "MyDialog", modal )
179 self.setCaption( "HELLO!" )
180 vb = QVBoxLayout( self, 8 )
183 label = QLabel( "Prenom: ", hb0 )
184 self.entry = QLineEdit( hb0 )
185 self.entry.setMinimumWidth( 200 )
188 bOk = QPushButton( "&OK", hb1 )
189 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
190 dummy = QWidget( hb1 )
191 bCancel = QPushButton( "&Cancel", hb1 )
192 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
193 hb1.setStretchFactor( dummy, 10 )
198 name = str( self.entry.text() )
200 banner = _getEngine().makeBanner( name )
201 QMessageBox.information( self, 'Info', banner )
204 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
207 # ----------------------- #
210 d = MyDialog( sgPyQt.getDesktop(), 1 )
216 # ----------------------- #
220 builder = study.NewBuilder()
221 father = _findOrCreateComponent()
222 object = builder.NewObject( father )
223 attr = builder.FindOrCreateAttribute( object, "AttributeName" )
225 attr.SetValue( "Object " + str( __id__ ) )
226 attr = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
227 attr.SetValue( __OBJECT_ID__ )
228 sg.updateObjBrowser(True)
231 # ----------------------- #
234 father = study.FindComponent( __MODULE_NAME__ )
236 iter = study.NewChildIterator( father )
237 builder = study.NewBuilder()
241 builder.RemoveObjectWithChildren( sobj )
242 sg.updateObjBrowser(True)
245 # ----------------------- #
248 entry = sg.getSelected( 0 )
250 sobj = study.FindObjectID( entry )
252 test, attr = sobj.FindAttribute( "AttributeName" )
254 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
258 # ----------------------- #
261 entry = sg.getSelected( 0 )
263 sobj = study.FindObjectID( entry )
265 builder = study.NewBuilder()
266 builder.RemoveObject( sobj )
267 sg.updateObjBrowser(True)
270 # ----------------------- #