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
21 # File : PYHELLOGUI.py
22 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
28 from PyQt4.QtGui import *
29 from PyQt4.QtCore import *
31 from omniORB import CORBA
32 from SALOME_NamingServicePy import *
33 from LifeCycleCORBA import *
35 import SALOMEDS_Attributes_idl
39 ################################################
41 # Used to store actions, menus, toolbars, etc...
42 ################################################
46 MODULE_NAME = "PYHELLO"
48 MODULE_PIXMAP = "PYHELLO_small.png"
53 # menus/toolbars/actions IDs
56 CREATE_OBJECT_ID = 942
67 DEFAULT_NAME = "Object"
71 # create top-level menu
72 mid = sgPyQt.createMenu( "PyHello", -1, GUIcontext.PYHELLO_MENU_ID, sgPyQt.defaultMenuGroup() )
74 tid = sgPyQt.createTool( "PyHello" )
75 # create actions and fill menu and toolbar with actions
76 a = sgPyQt.createAction( GUIcontext.HELLO_ID, "Hello", "Hello", "Show hello dialog box", "ExecPYHELLO.png" )
77 sgPyQt.createMenu( a, mid )
78 sgPyQt.createTool( a, tid )
79 a = sgPyQt.createSeparator()
80 sgPyQt.createMenu( a, mid )
81 a = sgPyQt.createAction( GUIcontext.CREATE_OBJECT_ID, "Create object", "Create object", "Create object" )
82 sgPyQt.createMenu( a, mid )
83 a = sgPyQt.createSeparator()
84 sgPyQt.createMenu( a, mid )
86 ag = sgPyQt.createActionGroup( GUIcontext.OPTIONS_ID )
87 a = sgPyQt.createAction( GUIcontext.OPTION_1_ID, "Default name", "Default name", "Use default name for the objects" )
88 a.setToggleAction( True )
90 a = sgPyQt.createAction( GUIcontext.OPTION_2_ID, "Generate name", "Generate name", "Generate name for the objects" )
91 a.setToggleAction( True )
93 a = sgPyQt.createAction( GUIcontext.OPTION_3_ID, "Ask name", "Ask name", "Request object name from the user" )
94 a.setToggleAction( True )
96 sgPyQt.createMenu( ag, mid )
97 default_mode = sgPyQt.integerSetting( "PYHELLO", "creation_mode", 0 )
98 sgPyQt.action( GUIcontext.OPTION_1_ID + default_mode ).setOn( True )
101 # the following action are used in context popup
102 a = sgPyQt.createAction( GUIcontext.DELETE_ALL_ID, "Delete all", "Delete all", "Delete all objects" )
103 a = sgPyQt.createAction( GUIcontext.SHOW_ME_ID, "Show", "Show", "Show object name" )
104 a = sgPyQt.createAction( GUIcontext.DELETE_ME_ID, "Delete", "Delete", "Remove object" )
105 a = sgPyQt.createAction( GUIcontext.RENAME_ME_ID, "Rename", "Rename", "Rename object" )
109 ################################################
111 ################################################
113 # study-to-context map
114 __study2context__ = {}
116 __current_context__ = None
120 ################################################
122 # Get SALOME PyQt interface
124 sgPyQt = SalomePyQt.SalomePyQt()
126 # Get SALOME Swig interface
127 import libSALOME_Swig
128 sg = libSALOME_Swig.SALOMEGUI_Swig()
130 ################################################
133 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
135 # create naming service instance
136 naming_service = SALOME_NamingServicePy_i( orb )
138 # create life cycle CORBA instance
139 lcc = LifeCycleCORBA( orb )
142 obj = naming_service.Resolve( '/myStudyManager' )
143 studyManager = obj._narrow( SALOMEDS.StudyManager )
145 ################################################
147 ################################################
155 if __verbose__ is None:
157 __verbose__ = int( os.getenv('SALOME_VERBOSE', 0) )
168 engine = lcc.FindOrLoadComponent( "FactoryServerPy", GUIcontext.MODULE_NAME )
172 # get active study ID
175 return sgPyQt.getStudyId()
181 studyId = _getStudyId()
182 study = studyManager.GetStudyByID( studyId )
186 # returns True if object has children
188 def _hasChildren( sobj ):
191 iter = study.NewChildIterator( sobj )
193 name = iter.Value().GetName()
202 # finds or creates component object
204 def _findOrCreateComponent():
206 father = study.FindComponent( GUIcontext.MODULE_NAME )
208 builder = study.NewBuilder()
209 father = builder.NewComponent( GUIcontext.MODULE_NAME )
210 attr = builder.FindOrCreateAttribute( father, "AttributeName" )
211 attr.SetValue( GUIcontext.MODULE_NAME )
212 attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
213 attr.SetPixMap( GUIcontext.MODULE_PIXMAP )
214 attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
215 attr.SetValue( GUIcontext.MODULE_ID )
217 builder.DefineComponentInstance( father, _getEngine() )
225 # get current GUI context
228 global __current_context__
229 return __current_context__
232 # set and return current GUI context
233 # study ID is passed as parameter
235 def _setContext( studyID ):
236 global __study2context__, __current_context__
237 if not __study2context__.has_key(studyID):
238 __study2context__[studyID] = GUIcontext()
240 __current_context__ = __study2context__[studyID]
241 return __current_context__
244 # increment object counter in the map
246 def _incObjToMap( m, id ):
247 if id not in m: m[id] = 0
255 selcount = sg.SelectedCount()
258 for i in range( selcount ):
259 entry = sg.getSelected( i )
261 sobj = study.FindObjectID( entry )
263 test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
265 ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
267 _incObjToMap( seltypes, ID )
272 _incObjToMap( seltypes, GUIcontext.FOREIGN_ID )
274 return selcount, seltypes
276 ################################################
278 ################################################
280 # called when module is initialized
281 # perform initialization actions
283 if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
284 # set default preferences values
285 if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
286 sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
287 if not sgPyQt.hasSetting( "PYHELLO", "creation_mode"):
288 sgPyQt.addSetting( "PYHELLO", "creation_mode", 0 )
291 # called when module is initialized
292 # return map of popup windows to be used by the module
294 if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
296 wm[SalomePyQt.WT_ObjectBrowser] = Qt.DockLeft
297 wm[SalomePyQt.WT_PyConsole] = Qt.DockBottom
300 # called when module is initialized
301 # return list of 2d/3d views to be used ny the module
303 if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
306 # called when module is initialized
307 # export module's preferences
308 def createPreferences():
309 if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
310 gid = sgPyQt.addPreference( "General" )
311 gid = sgPyQt.addPreference( "Object creation", gid )
312 pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" )
313 pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" )
314 sgPyQt.addPreferenceProperty( pid, "strings", 0, QVariant( "Default name" ) )
315 sgPyQt.addPreferenceProperty( pid, "indexes", 0, QVariant( 0 ) )
316 sgPyQt.addPreferenceProperty( pid, "strings", 1, QVariant( "Generate name" ) )
317 sgPyQt.addPreferenceProperty( pid, "indexes", 1, QVariant( 1 ) )
318 sgPyQt.addPreferenceProperty( pid, "strings", 2, QVariant( "Ask name" ) )
319 sgPyQt.addPreferenceProperty( pid, "indexes", 2, QVariant( 2 ) )
322 # called when module is activated
323 # returns True if activating is successfull and False otherwise
325 if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
326 ctx = _setContext( _getStudyId() )
329 # called when module is deactivated
331 if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
334 # called when active study is changed
335 # active study ID is passed as parameter
336 def activeStudyChanged( studyID ):
337 if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
338 ctx = _setContext( _getStudyId() )
341 # called when popup menu is invoked
342 # popup menu and menu context are passed as parameters
343 def createPopupMenu( popup, context ):
344 if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
345 ctx = _setContext( _getStudyId() )
347 selcount, selected = _getSelection()
348 print selcount, selected
350 # one object is selected
351 if GUIcontext.MODULE_ID in selected:
353 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
354 elif GUIcontext.OBJECT_ID in selected:
356 popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
357 popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
359 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
363 # several objects are selected
364 if len( selected ) == 1:
365 if GUIcontext.MODULE_ID in selected:
367 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
368 elif GUIcontext.OBJECT_ID in selected:
369 # menu for list of objects
370 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
376 # called when GUI action is activated
377 # action ID is passed as parameter
378 def OnGUIEvent( commandID ):
379 if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID
380 if dict_command.has_key( commandID ):
382 dict_command[commandID]()
384 traceback.print_exc()
386 if verbose() : print "The command is not implemented: %d" % commandID
389 # called when module's preferences are changed
390 # preference's resources section and setting name are passed as parameters
391 def preferenceChanged( section, setting ):
392 if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )
395 # called when active view is changed
396 # view ID is passed as parameter
397 def activeViewChanged( viewID ):
398 if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID
401 # called when active view is cloned
402 # cloned view ID is passed as parameter
403 def viewCloned( viewID ):
404 if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID
407 # called when active view is viewClosed
408 # view ID is passed as parameter
409 def viewClosed( viewID ):
410 if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID
413 ################################################
414 # GUI actions implementation
415 ################################################
420 class MyDialog( QDialog ):
422 def __init__( self, parent = None, modal = 0):
423 QDialog.__init__( self, parent )
424 self.setObjectName( "MyDialog" )
425 self.setModal( modal )
426 self.setWindowTitle( "HELLO!" )
427 vb = QVBoxLayout( self )
430 hb0 = QHBoxLayout( self )
431 label = QLabel( "Prenom: ", self )
432 hb0.addWidget( label )
433 self.entry = QLineEdit( self )
434 self.entry.setMinimumWidth( 200 )
435 hb0.addWidget( self.entry )
438 hb1 = QHBoxLayout( self )
439 bOk = QPushButton( "&OK", self )
440 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
445 bCancel = QPushButton( "&Cancel", self )
446 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
447 hb1.addWidget( bCancel )
454 name = str( self.entry.text() )
456 banner = _getEngine().makeBanner( name )
457 QMessageBox.information( self, 'Info', banner )
460 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
464 # Show 'HELLO' dialog box
468 d = MyDialog( sgPyQt.getDesktop(), 1 )
477 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
479 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isOn():
480 # request object name from the user
481 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
483 "Enter object name:",
487 name = str( name.trimmed() )
488 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isOn():
489 # generate object name
492 name = "%s %d" % ( default_name, __id__ )
498 # generate object name
501 name = "%s %d" % ( default_name, __id__ )
505 builder = study.NewBuilder()
506 father = _findOrCreateComponent()
507 object = builder.NewObject( father )
508 attr = builder.FindOrCreateAttribute( object, "AttributeName" )
509 attr.SetValue( name )
510 attr = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
511 attr.SetValue( GUIcontext.OBJECT_ID )
512 sg.updateObjBrowser( True )
520 father = study.FindComponent( GUIcontext.MODULE_NAME )
522 iter = study.NewChildIterator( father )
523 builder = study.NewBuilder()
527 builder.RemoveObjectWithChildren( sobj )
529 sg.updateObjBrowser( True )
538 entry = sg.getSelected( 0 )
540 sobj = study.FindObjectID( entry )
542 test, attr = sobj.FindAttribute( "AttributeName" )
544 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
551 # Delete selected object(s)
555 builder = study.NewBuilder()
556 if sg.SelectedCount() <= 0: return
557 for i in range( sg.SelectedCount() ):
558 entry = sg.getSelected( i )
560 sobj = study.FindObjectID( entry )
562 builder.RemoveObject( sobj )
566 sg.updateObjBrowser( True )
570 # Rename selected object
574 builder = study.NewBuilder()
575 entry = sg.getSelected( 0 )
577 sobj = study.FindObjectID( entry )
579 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
581 "Enter object name:",
584 name = str( name.trimmed() )
585 if not ok or not name: return
586 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
587 attr.SetValue( name )
588 sg.updateObjBrowser( True )
594 # Commands dictionary
597 GUIcontext.HELLO_ID : ShowHELLO,
598 GUIcontext.CREATE_OBJECT_ID : CreateObject,
599 GUIcontext.DELETE_ALL_ID : DeleteAll,
600 GUIcontext.SHOW_ME_ID : ShowMe,
601 GUIcontext.DELETE_ME_ID : Delete,
602 GUIcontext.RENAME_ME_ID : Rename,