1 # Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 # File : PYHELLOGUI.py
25 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
30 from PyQt4.QtGui import *
31 from PyQt4.QtCore import *
33 from PYHELLO_utils import *
35 ################################################
37 # Used to store actions, menus, toolbars, etc...
38 ################################################
41 # menus/toolbars/actions IDs
44 CREATE_OBJECT_ID = 942
56 DEFAULT_NAME = "Object"
58 DEFAULT_PASSWD = "Passwd"
62 # create top-level menu
63 mid = sgPyQt.createMenu( "PyHello", -1, GUIcontext.PYHELLO_MENU_ID, sgPyQt.defaultMenuGroup() )
65 tid = sgPyQt.createTool( "PyHello" )
66 # create actions and fill menu and toolbar with actions
67 a = sgPyQt.createAction( GUIcontext.HELLO_ID, "Hello", "Hello", "Show hello dialog box", "ExecPYHELLO.png" )
68 sgPyQt.createMenu( a, mid )
69 sgPyQt.createTool( a, tid )
70 a = sgPyQt.createSeparator()
71 sgPyQt.createMenu( a, mid )
72 a = sgPyQt.createAction( GUIcontext.CREATE_OBJECT_ID, "Create object", "Create object", "Create object" )
73 sgPyQt.createMenu( a, mid )
74 a = sgPyQt.createSeparator()
75 sgPyQt.createMenu( a, mid )
77 ag = sgPyQt.createActionGroup( GUIcontext.OPTIONS_ID )
78 ag.setText( "Creation mode" )
79 ag.setUsesDropDown(True)
80 a = sgPyQt.createAction( GUIcontext.OPTION_1_ID, "Default name", "Default name", "Use default name for the objects" )
81 a.setCheckable( True )
83 a = sgPyQt.createAction( GUIcontext.OPTION_2_ID, "Generate name", "Generate name", "Generate name for the objects" )
84 a.setCheckable( True )
86 a = sgPyQt.createAction( GUIcontext.OPTION_3_ID, "Ask name", "Ask name", "Request object name from the user" )
87 a.setCheckable( True )
89 sgPyQt.createMenu( ag, mid )
90 sgPyQt.createTool( ag, tid )
91 default_mode = sgPyQt.integerSetting( "PYHELLO", "creation_mode", 0 )
92 sgPyQt.action( GUIcontext.OPTION_1_ID + default_mode ).setChecked( True )
95 a = sgPyQt.createSeparator()
96 a = sgPyQt.createAction( GUIcontext.PASSWORD_ID, "Display password", "Display password", "Display password" )
97 sgPyQt.createMenu( a, mid )
99 # the following action are used in context popup
100 a = sgPyQt.createAction( GUIcontext.DELETE_ALL_ID, "Delete all", "Delete all", "Delete all objects" )
101 a = sgPyQt.createAction( GUIcontext.SHOW_ME_ID, "Show", "Show", "Show object name" )
102 a = sgPyQt.createAction( GUIcontext.DELETE_ME_ID, "Delete", "Delete", "Remove object" )
103 a = sgPyQt.createAction( GUIcontext.RENAME_ME_ID, "Rename", "Rename", "Rename object" )
107 ################################################
109 ################################################
111 # study-to-context map
112 __study2context__ = {}
114 __current_context__ = None
118 ################################################
120 # Get SALOME PyQt interface
122 sgPyQt = SalomePyQt.SalomePyQt()
124 # Get SALOME Swig interface
125 import libSALOME_Swig
126 sg = libSALOME_Swig.SALOMEGUI_Swig()
128 ################################################
130 ################################################
132 ################################################
135 # get active study ID
138 return sgPyQt.getStudyId()
144 studyId = _getStudyId()
145 study = getStudyManager().GetStudyByID( studyId )
149 # returns True if object has children
151 def _hasChildren( sobj ):
154 iter = study.NewChildIterator( sobj )
156 name = iter.Value().GetName()
165 # get current GUI context
168 global __current_context__
169 return __current_context__
172 # set and return current GUI context
173 # study ID is passed as parameter
175 def _setContext( studyID ):
176 global __study2context__, __current_context__
177 if not __study2context__.has_key(studyID):
178 __study2context__[studyID] = GUIcontext()
180 __current_context__ = __study2context__[studyID]
181 return __current_context__
184 # increment object counter in the map
186 def _incObjToMap( m, id ):
187 if id not in m: m[id] = 0
195 selcount = sg.SelectedCount()
197 for i in range( selcount ):
198 _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) )
200 return selcount, seltypes
202 ################################################
204 ################################################
206 # called when module is initialized
207 # perform initialization actions
209 if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
210 # set default preferences values
211 if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
212 sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
213 if not sgPyQt.hasSetting( "PYHELLO", "creation_mode"):
214 sgPyQt.addSetting( "PYHELLO", "creation_mode", 0 )
215 if not sgPyQt.hasSetting( "PYHELLO", "Password"):
216 sgPyQt.addSetting( "PYHELLO", "Password", DEFAULT_PASSWD )
219 # called when module is initialized
220 # return map of popup windows to be used by the module
222 if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
224 wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
225 wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea
228 # called when module is initialized
229 # return list of 2d/3d views to be used ny the module
231 if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
234 # called when module is initialized
235 # export module's preferences
236 def createPreferences():
237 if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
238 gid = sgPyQt.addPreference( "General" )
239 gid = sgPyQt.addPreference( "Object creation", gid )
240 pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" )
241 pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" )
242 strings = QStringList()
243 strings.append( "Default name" )
244 strings.append( "Generate name" )
245 strings.append( "Ask name" )
247 indexes.append( QVariant(0) )
248 indexes.append( QVariant(1) )
249 indexes.append( QVariant(2) )
250 sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) )
251 sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) )
252 pid = sgPyQt.addPreference( "Password", gid, SalomePyQt.PT_String, "PYHELLO", "Password" )
253 sgPyQt.setPreferenceProperty( pid, "echo", QVariant( 2 ) )
256 # called when module is activated
257 # returns True if activating is successfull and False otherwise
259 if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
260 ctx = _setContext( _getStudyId() )
263 # called when module is deactivated
265 if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
268 # called when active study is changed
269 # active study ID is passed as parameter
270 def activeStudyChanged( studyID ):
271 if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
272 ctx = _setContext( _getStudyId() )
275 # called when popup menu is invoked
276 # popup menu and menu context are passed as parameters
277 def createPopupMenu( popup, context ):
278 if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
279 ctx = _setContext( _getStudyId() )
281 selcount, selected = _getSelection()
282 if verbose() : print selcount, selected
284 # one object is selected
285 if moduleID() in selected:
287 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
288 elif objectID() in selected:
290 popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
291 popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
293 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
297 # several objects are selected
298 if len( selected ) == 1:
299 if moduleID() in selected:
301 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
302 elif objectID() in selected:
303 # menu for list of objects
304 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
310 # called when GUI action is activated
311 # action ID is passed as parameter
312 def OnGUIEvent( commandID ):
313 if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID
314 if dict_command.has_key( commandID ):
316 dict_command[commandID]()
318 traceback.print_exc()
320 if verbose() : print "The command is not implemented: %d" % commandID
323 # called when module's preferences are changed
324 # preference's resources section and setting name are passed as parameters
325 def preferenceChanged( section, setting ):
326 if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )
329 # called when active view is changed
330 # view ID is passed as parameter
331 def activeViewChanged( viewID ):
332 if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID
335 # called when active view is cloned
336 # cloned view ID is passed as parameter
337 def viewCloned( viewID ):
338 if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID
341 # called when active view is viewClosed
342 # view ID is passed as parameter
343 def viewClosed( viewID ):
344 if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID
347 # called when study is opened
350 if verbose() : print "PYHELLOGUI.engineIOR()"
351 return getEngineIOR()
353 ################################################
354 # GUI actions implementation
355 ################################################
360 class MyDialog( QDialog ):
362 def __init__( self, parent = None, modal = 0):
363 QDialog.__init__( self, parent )
364 self.setObjectName( "MyDialog" )
365 self.setModal( modal )
366 self.setWindowTitle( "HELLO!" )
367 vb = QVBoxLayout( self )
370 hb0 = QHBoxLayout( self )
371 label = QLabel( "Prenom: ", self )
372 hb0.addWidget( label )
373 self.entry = QLineEdit( self )
374 self.entry.setMinimumWidth( 200 )
375 hb0.addWidget( self.entry )
378 hb1 = QHBoxLayout( self )
379 bOk = QPushButton( "&OK", self )
380 bOk.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_HANDSHAKE' ) )
381 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
386 bCancel = QPushButton( "&Cancel", self )
387 bCancel.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_STOP' ) )
388 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
389 hb1.addWidget( bCancel )
396 name = str( self.entry.text() )
398 banner = getEngine().makeBanner( name )
399 QMessageBox.information( self, 'Info', banner )
402 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
406 # Show 'HELLO' dialog box
410 d = MyDialog( sgPyQt.getDesktop(), 1 )
420 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
422 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
423 # request object name from the user
424 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
426 "Enter object name:",
430 name = str( name.trimmed() )
431 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
432 # generate object name
433 __objectid__ = __objectid__ + 1
434 name = "%s %d" % ( default_name, __objectid__ )
440 # generate object name
441 __objectid__ = __objectid__ + 1
442 name = "%s %d" % ( default_name, __objectid__ )
445 getEngine().createObject( _getStudy(), name )
446 sg.updateObjBrowser( True )
454 father = study.FindComponent( moduleName() )
456 iter = study.NewChildIterator( father )
457 builder = study.NewBuilder()
461 builder.RemoveObjectWithChildren( sobj )
463 sg.updateObjBrowser( True )
472 entry = sg.getSelected( 0 )
474 sobj = study.FindObjectID( entry )
476 test, attr = sobj.FindAttribute( "AttributeName" )
478 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
485 # Delete selected object(s)
489 builder = study.NewBuilder()
490 if sg.SelectedCount() <= 0: return
491 for i in range( sg.SelectedCount() ):
492 entry = sg.getSelected( i )
494 sobj = study.FindObjectID( entry )
496 builder.RemoveObject( sobj )
500 sg.updateObjBrowser( True )
504 # Rename selected object
508 builder = study.NewBuilder()
509 entry = sg.getSelected( 0 )
511 sobj = study.FindObjectID( entry )
513 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
515 "Enter object name:",
518 name = str( name.trimmed() )
519 if not ok or not name: return
520 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
521 attr.SetValue( name )
522 sg.updateObjBrowser( True )
528 # Display password stored in the preferences
531 passwd = str( sgPyQt.stringSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD ).trimmed() )
532 QMessageBox.information(sgPyQt.getDesktop(),
537 # Commands dictionary
540 GUIcontext.HELLO_ID : ShowHELLO,
541 GUIcontext.CREATE_OBJECT_ID : CreateObject,
542 GUIcontext.DELETE_ALL_ID : DeleteAll,
543 GUIcontext.SHOW_ME_ID : ShowMe,
544 GUIcontext.DELETE_ME_ID : Delete,
545 GUIcontext.RENAME_ME_ID : Rename,
546 GUIcontext.PASSWORD_ID : Password,