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 ag.setText( "Creation mode" )
88 ag.setUsesDropDown(True)
89 a = sgPyQt.createAction( GUIcontext.OPTION_1_ID, "Default name", "Default name", "Use default name for the objects" )
90 a.setCheckable( True )
92 a = sgPyQt.createAction( GUIcontext.OPTION_2_ID, "Generate name", "Generate name", "Generate name for the objects" )
93 a.setCheckable( True )
95 a = sgPyQt.createAction( GUIcontext.OPTION_3_ID, "Ask name", "Ask name", "Request object name from the user" )
96 a.setCheckable( True )
98 sgPyQt.createMenu( ag, mid )
99 sgPyQt.createTool( ag, tid )
100 default_mode = sgPyQt.integerSetting( "PYHELLO", "creation_mode", 0 )
101 sgPyQt.action( GUIcontext.OPTION_1_ID + default_mode ).setChecked( True )
104 # the following action are used in context popup
105 a = sgPyQt.createAction( GUIcontext.DELETE_ALL_ID, "Delete all", "Delete all", "Delete all objects" )
106 a = sgPyQt.createAction( GUIcontext.SHOW_ME_ID, "Show", "Show", "Show object name" )
107 a = sgPyQt.createAction( GUIcontext.DELETE_ME_ID, "Delete", "Delete", "Remove object" )
108 a = sgPyQt.createAction( GUIcontext.RENAME_ME_ID, "Rename", "Rename", "Rename object" )
112 ################################################
114 ################################################
116 # study-to-context map
117 __study2context__ = {}
119 __current_context__ = None
123 ################################################
125 # Get SALOME PyQt interface
127 sgPyQt = SalomePyQt.SalomePyQt()
129 # Get SALOME Swig interface
130 import libSALOME_Swig
131 sg = libSALOME_Swig.SALOMEGUI_Swig()
133 ################################################
136 orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
138 # create naming service instance
139 naming_service = SALOME_NamingServicePy_i( orb )
141 # create life cycle CORBA instance
142 lcc = LifeCycleCORBA( orb )
145 obj = naming_service.Resolve( '/myStudyManager' )
146 studyManager = obj._narrow( SALOMEDS.StudyManager )
148 ################################################
150 ################################################
158 if __verbose__ is None:
160 __verbose__ = int( os.getenv('SALOME_VERBOSE', 0) )
171 engine = lcc.FindOrLoadComponent( "FactoryServerPy", GUIcontext.MODULE_NAME )
175 # get active study ID
178 return sgPyQt.getStudyId()
184 studyId = _getStudyId()
185 study = studyManager.GetStudyByID( studyId )
189 # returns True if object has children
191 def _hasChildren( sobj ):
194 iter = study.NewChildIterator( sobj )
196 name = iter.Value().GetName()
205 # finds or creates component object
207 def _findOrCreateComponent():
209 father = study.FindComponent( GUIcontext.MODULE_NAME )
211 builder = study.NewBuilder()
212 father = builder.NewComponent( GUIcontext.MODULE_NAME )
213 attr = builder.FindOrCreateAttribute( father, "AttributeName" )
214 attr.SetValue( GUIcontext.MODULE_NAME )
215 attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
216 attr.SetPixMap( GUIcontext.MODULE_PIXMAP )
217 attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
218 attr.SetValue( GUIcontext.MODULE_ID )
220 builder.DefineComponentInstance( father, _getEngine() )
228 # get current GUI context
231 global __current_context__
232 return __current_context__
235 # set and return current GUI context
236 # study ID is passed as parameter
238 def _setContext( studyID ):
239 global __study2context__, __current_context__
240 if not __study2context__.has_key(studyID):
241 __study2context__[studyID] = GUIcontext()
243 __current_context__ = __study2context__[studyID]
244 return __current_context__
247 # increment object counter in the map
249 def _incObjToMap( m, id ):
250 if id not in m: m[id] = 0
258 selcount = sg.SelectedCount()
261 for i in range( selcount ):
262 entry = sg.getSelected( i )
264 sobj = study.FindObjectID( entry )
266 test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
268 ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
270 _incObjToMap( seltypes, ID )
275 _incObjToMap( seltypes, GUIcontext.FOREIGN_ID )
277 return selcount, seltypes
279 ################################################
281 ################################################
283 # called when module is initialized
284 # perform initialization actions
286 if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
287 # set default preferences values
288 if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
289 sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
290 if not sgPyQt.hasSetting( "PYHELLO", "creation_mode"):
291 sgPyQt.addSetting( "PYHELLO", "creation_mode", 0 )
294 # called when module is initialized
295 # return map of popup windows to be used by the module
297 if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
299 wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
300 wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea
303 # called when module is initialized
304 # return list of 2d/3d views to be used ny the module
306 if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
309 # called when module is initialized
310 # export module's preferences
311 def createPreferences():
312 if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
313 gid = sgPyQt.addPreference( "General" )
314 gid = sgPyQt.addPreference( "Object creation", gid )
315 pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" )
316 pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" )
317 strings = QStringList()
318 strings.append( "Default name" )
319 strings.append( "Generate name" )
320 strings.append( "Ask name" )
322 indexes.append( QVariant(0) )
323 indexes.append( QVariant(1) )
324 indexes.append( QVariant(2) )
325 sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) )
326 sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) )
329 # called when module is activated
330 # returns True if activating is successfull and False otherwise
332 if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
333 ctx = _setContext( _getStudyId() )
336 # called when module is deactivated
338 if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
341 # called when active study is changed
342 # active study ID is passed as parameter
343 def activeStudyChanged( studyID ):
344 if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
345 ctx = _setContext( _getStudyId() )
348 # called when popup menu is invoked
349 # popup menu and menu context are passed as parameters
350 def createPopupMenu( popup, context ):
351 if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
352 ctx = _setContext( _getStudyId() )
354 selcount, selected = _getSelection()
355 print selcount, selected
357 # one object is selected
358 if GUIcontext.MODULE_ID in selected:
360 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
361 elif GUIcontext.OBJECT_ID in selected:
363 popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
364 popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
366 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
370 # several objects are selected
371 if len( selected ) == 1:
372 if GUIcontext.MODULE_ID in selected:
374 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
375 elif GUIcontext.OBJECT_ID in selected:
376 # menu for list of objects
377 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
383 # called when GUI action is activated
384 # action ID is passed as parameter
385 def OnGUIEvent( commandID ):
386 if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID
387 if dict_command.has_key( commandID ):
389 dict_command[commandID]()
391 traceback.print_exc()
393 if verbose() : print "The command is not implemented: %d" % commandID
396 # called when module's preferences are changed
397 # preference's resources section and setting name are passed as parameters
398 def preferenceChanged( section, setting ):
399 if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )
402 # called when active view is changed
403 # view ID is passed as parameter
404 def activeViewChanged( viewID ):
405 if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID
408 # called when active view is cloned
409 # cloned view ID is passed as parameter
410 def viewCloned( viewID ):
411 if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID
414 # called when active view is viewClosed
415 # view ID is passed as parameter
416 def viewClosed( viewID ):
417 if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID
420 ################################################
421 # GUI actions implementation
422 ################################################
427 class MyDialog( QDialog ):
429 def __init__( self, parent = None, modal = 0):
430 QDialog.__init__( self, parent )
431 self.setObjectName( "MyDialog" )
432 self.setModal( modal )
433 self.setWindowTitle( "HELLO!" )
434 vb = QVBoxLayout( self )
437 hb0 = QHBoxLayout( self )
438 label = QLabel( "Prenom: ", self )
439 hb0.addWidget( label )
440 self.entry = QLineEdit( self )
441 self.entry.setMinimumWidth( 200 )
442 hb0.addWidget( self.entry )
445 hb1 = QHBoxLayout( self )
446 bOk = QPushButton( "&OK", self )
447 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
452 bCancel = QPushButton( "&Cancel", self )
453 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
454 hb1.addWidget( bCancel )
461 name = str( self.entry.text() )
463 banner = _getEngine().makeBanner( name )
464 QMessageBox.information( self, 'Info', banner )
467 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
471 # Show 'HELLO' dialog box
475 d = MyDialog( sgPyQt.getDesktop(), 1 )
484 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
486 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
487 # request object name from the user
488 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
490 "Enter object name:",
494 name = str( name.trimmed() )
495 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
496 # generate object name
499 name = "%s %d" % ( default_name, __id__ )
505 # generate object name
508 name = "%s %d" % ( default_name, __id__ )
512 builder = study.NewBuilder()
513 father = _findOrCreateComponent()
514 object = builder.NewObject( father )
515 attr = builder.FindOrCreateAttribute( object, "AttributeName" )
516 attr.SetValue( name )
517 attr = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
518 attr.SetValue( GUIcontext.OBJECT_ID )
519 sg.updateObjBrowser( True )
527 father = study.FindComponent( GUIcontext.MODULE_NAME )
529 iter = study.NewChildIterator( father )
530 builder = study.NewBuilder()
534 builder.RemoveObjectWithChildren( sobj )
536 sg.updateObjBrowser( True )
545 entry = sg.getSelected( 0 )
547 sobj = study.FindObjectID( entry )
549 test, attr = sobj.FindAttribute( "AttributeName" )
551 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
558 # Delete selected object(s)
562 builder = study.NewBuilder()
563 if sg.SelectedCount() <= 0: return
564 for i in range( sg.SelectedCount() ):
565 entry = sg.getSelected( i )
567 sobj = study.FindObjectID( entry )
569 builder.RemoveObject( sobj )
573 sg.updateObjBrowser( True )
577 # Rename selected object
581 builder = study.NewBuilder()
582 entry = sg.getSelected( 0 )
584 sobj = study.FindObjectID( entry )
586 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
588 "Enter object name:",
591 name = str( name.trimmed() )
592 if not ok or not name: return
593 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
594 attr.SetValue( name )
595 sg.updateObjBrowser( True )
601 # Commands dictionary
604 GUIcontext.HELLO_ID : ShowHELLO,
605 GUIcontext.CREATE_OBJECT_ID : CreateObject,
606 GUIcontext.DELETE_ALL_ID : DeleteAll,
607 GUIcontext.SHOW_ME_ID : ShowMe,
608 GUIcontext.DELETE_ME_ID : Delete,
609 GUIcontext.RENAME_ME_ID : Rename,