Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[samples/pyhello.git] / src / PYHELLOGUI / PYHELLOGUI.py
1 #  Copyright (C) 2005  CEA/DEN, EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 #  See http://www.salome-platform.org/
18 #
19 from qt import *
20 import traceback
21
22 from omniORB import CORBA
23 from SALOME_NamingServicePy import *
24 from LifeCycleCORBA import *
25 import SALOMEDS
26 import SALOMEDS_Attributes_idl
27
28 ################################################
29 # module name
30 __MODULE_NAME__ = "PYHELLO"
31 __MODULE_ID__   = 1000
32 __OBJECT_ID__   = 1010
33 ################################################
34
35 # Get SALOME PyQt interface
36 import SalomePyQt
37 sgPyQt=SalomePyQt.SalomePyQt()
38
39 # Get SALOME Swig interface
40 import libSALOME_Swig
41 sg = libSALOME_Swig.SALOMEGUI_Swig()
42
43 ################################################
44
45 # init ORB
46 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
47
48 # create naming service instance
49 naming_service = SALOME_NamingServicePy_i( orb )
50
51 # create life cycle CORBA instance
52 lcc = LifeCycleCORBA( orb )
53
54 # get study manager
55 obj = naming_service.Resolve( '/myStudyManager' )
56 studyManager = obj._narrow( SALOMEDS.StudyManager )
57
58 ################################################
59 # Internal methods
60
61 # --- get PYHELLO engine ---
62 def _getEngine():
63     import PYHELLO_ORB
64     engine = lcc.FindOrLoadComponent( "FactoryServerPy", __MODULE_NAME__ )
65     return engine
66
67 # --- get active study ---
68 def _getStudy():
69     studyId = sgPyQt.getStudyId()
70     study = studyManager.GetStudyByID( studyId )
71     return study
72     
73 # --- returns 1 if object has children ---
74 def _hasChildren( sobj ):
75     if sobj:
76         study = _getStudy()
77         iter  = study.NewChildIterator( sobj )
78         while iter.More():
79             name = iter.Value().GetName()
80             if name:
81                 return 1
82             iter.Next()
83     return 0
84
85 # --- finds or creates component object ---
86 def _findOrCreateComponent():
87     study = _getStudy()
88     father = study.FindComponent( __MODULE_NAME__ )
89     if father is None:
90         builder = study.NewBuilder()
91         father = builder.NewComponent( __MODULE_NAME__ )
92         attr = builder.FindOrCreateAttribute( father, "AttributeName" )
93         attr.SetValue( __MODULE_NAME__ )
94         attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
95         attr.SetValue( __MODULE_ID__ )
96         try:
97             ### The following line is commented because it causes crashes ! ###
98             ### builder.DefineComponentInstance( father, _getEngine() )
99             pass
100         except:
101             pass
102     return father
103     
104 ################################################
105 # Callback functions
106
107 # set workspace (obsolete method, not used)
108 def setWorkSpace( pyws ):
109     print "PYHELLOGUI::setWorkSpace : ", pyws
110     pass
111
112 # called when module is activated
113 def setSettings():
114     print "PYHELLOGUI::setSettings"
115     pass
116
117 # called when active study is changed
118 def activeStudyChanged( studyID ):
119     print "PYHELLOGUI::activeStudyChanged: study ID =", studyID
120     pass
121
122 # define popup menu
123 def definePopup( context, object, parent ):
124     object = ""
125     parent = ""
126
127     study = _getStudy()
128     if sg.SelectedCount() == 1:
129         entry = sg.getSelected( 0 )
130         if entry != '':
131             sobj = study.FindObjectID( entry )
132             if sobj is not None:
133                 test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
134                 if test :
135                     id = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
136                     if ( id >= 0 ):
137                         object = str( id )
138     print "PYHELLOGUI::definePopup :", context, object, parent
139     return context, object, parent
140
141 # customize popup menu
142 def customPopup( popup, context, object, parent ):
143     print "PYHELLOGUI::customPopup :", context, object, parent
144     try:
145         id = int( object )
146         if id == __MODULE_ID__:
147             study = _getStudy()
148             if sg.SelectedCount() == 1:
149                 entry = sg.getSelected( 0 )
150                 if entry != '':
151                     sobj = study.FindObjectID( entry )
152                     if sobj and not _hasChildren( sobj ):
153                         popup.removeItem( 951 ) # remove 'Delete All' command
154     except:
155         pass
156     pass
157
158 # process GUI action
159 def OnGUIEvent(commandID) :
160     print "PYHELLOGUI::OnGUIEvent : commandID =",commandID
161     if dict_command.has_key( commandID ):
162         try:
163             dict_command[commandID]()
164         except:
165             traceback.print_exc()
166     else:
167        print "The command is not implemented: ",commandID
168
169 ################################################
170 # GUI actions implementation
171
172 # ----------------------- #
173 # Sample dialog box
174 # ----------------------- #
175 class MyDialog( QDialog ):
176     # constructor
177     def __init__( self, parent = None, modal = 0):
178         QDialog.__init__( self, parent, "MyDialog", modal )
179         self.setCaption( "HELLO!" )
180         vb = QVBoxLayout( self, 8 )
181         vb.setAutoAdd( 1 )
182         hb0 = QHBox( self )
183         label = QLabel( "Prenom: ", hb0 )
184         self.entry = QLineEdit( hb0 )
185         self.entry.setMinimumWidth( 200 )
186         
187         hb1 = QHBox( self )
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 )
194         pass
195     
196     # OK button slot
197     def accept( self ):
198         name = str( self.entry.text() )
199         if name != "":
200             banner = _getEngine().makeBanner( name )
201             QMessageBox.information( self, 'Info', banner )
202             self.close()
203         else:
204             QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
205         pass
206
207 # ----------------------- #
208 def ShowHELLO():
209     # create dialog box
210     d = MyDialog( sgPyQt.getDesktop(), 1 )
211     # show dialog box
212     d.exec_loop()
213
214 __id__ = 0
215
216 # ----------------------- #
217 def CreateObject():
218     global __id__
219     study   = _getStudy()
220     builder = study.NewBuilder()
221     father  = _findOrCreateComponent()
222     object  = builder.NewObject( father )
223     attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
224     __id__  = __id__ + 1
225     attr.SetValue( "Object " +  str( __id__ ) )
226     attr    = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
227     attr.SetValue( __OBJECT_ID__ )
228     sgPyQt.updateObjBrowser()
229     pass
230
231 # ----------------------- #
232 def DeleteAll():
233     study = _getStudy()
234     father = study.FindComponent( __MODULE_NAME__ )
235     if father:
236         iter = study.NewChildIterator( father )
237         builder = study.NewBuilder()
238         while iter.More():
239             sobj = iter.Value()
240             iter.Next()
241             builder.RemoveObjectWithChildren( sobj )
242         sgPyQt.updateObjBrowser()
243     pass
244
245 # ----------------------- #
246 def ShowMe():
247     study = _getStudy()
248     entry = sg.getSelected( 0 )
249     if entry != '':
250         sobj = study.FindObjectID( entry )
251         if ( sobj ):
252             test, attr = sobj.FindAttribute( "AttributeName" )
253             if test:
254                 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
255                 
256     pass
257
258 # ----------------------- #
259 def Delete():
260     study = _getStudy()
261     entry = sg.getSelected( 0 )
262     if entry != '':
263         sobj = study.FindObjectID( entry )
264         if ( sobj ):
265             builder = study.NewBuilder()
266             builder.RemoveObject( sobj )
267             sgPyQt.updateObjBrowser()
268     pass
269
270 # ----------------------- #
271 dict_command = {
272     941 : ShowHELLO,
273     942 : CreateObject,
274     951 : DeleteAll,
275     952 : ShowMe,
276     953 : Delete,
277     }