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