1 # Copyright (C) 2007-2008 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
23 # File : PYHELLOGUI.py
24 # 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
55 DEFAULT_NAME = "Object"
59 # create top-level menu
60 mid = sgPyQt.createMenu( "PyHello", -1, GUIcontext.PYHELLO_MENU_ID, sgPyQt.defaultMenuGroup() )
62 tid = sgPyQt.createTool( "PyHello" )
63 # create actions and fill menu and toolbar with actions
64 a = sgPyQt.createAction( GUIcontext.HELLO_ID, "Hello", "Hello", "Show hello dialog box", "ExecPYHELLO.png" )
65 sgPyQt.createMenu( a, mid )
66 sgPyQt.createTool( a, tid )
67 a = sgPyQt.createSeparator()
68 sgPyQt.createMenu( a, mid )
69 a = sgPyQt.createAction( GUIcontext.CREATE_OBJECT_ID, "Create object", "Create object", "Create object" )
70 sgPyQt.createMenu( a, mid )
71 a = sgPyQt.createSeparator()
72 sgPyQt.createMenu( a, mid )
74 ag = sgPyQt.createActionGroup( GUIcontext.OPTIONS_ID )
75 ag.setText( "Creation mode" )
76 ag.setUsesDropDown(True)
77 a = sgPyQt.createAction( GUIcontext.OPTION_1_ID, "Default name", "Default name", "Use default name for the objects" )
78 a.setCheckable( True )
80 a = sgPyQt.createAction( GUIcontext.OPTION_2_ID, "Generate name", "Generate name", "Generate name for the objects" )
81 a.setCheckable( True )
83 a = sgPyQt.createAction( GUIcontext.OPTION_3_ID, "Ask name", "Ask name", "Request object name from the user" )
84 a.setCheckable( True )
86 sgPyQt.createMenu( ag, mid )
87 sgPyQt.createTool( ag, tid )
88 default_mode = sgPyQt.integerSetting( "PYHELLO", "creation_mode", 0 )
89 sgPyQt.action( GUIcontext.OPTION_1_ID + default_mode ).setChecked( True )
92 # the following action are used in context popup
93 a = sgPyQt.createAction( GUIcontext.DELETE_ALL_ID, "Delete all", "Delete all", "Delete all objects" )
94 a = sgPyQt.createAction( GUIcontext.SHOW_ME_ID, "Show", "Show", "Show object name" )
95 a = sgPyQt.createAction( GUIcontext.DELETE_ME_ID, "Delete", "Delete", "Remove object" )
96 a = sgPyQt.createAction( GUIcontext.RENAME_ME_ID, "Rename", "Rename", "Rename object" )
100 ################################################
102 ################################################
104 # study-to-context map
105 __study2context__ = {}
107 __current_context__ = None
111 ################################################
113 # Get SALOME PyQt interface
115 sgPyQt = SalomePyQt.SalomePyQt()
117 # Get SALOME Swig interface
118 import libSALOME_Swig
119 sg = libSALOME_Swig.SALOMEGUI_Swig()
121 ################################################
123 ################################################
125 ################################################
128 # get active study ID
131 return sgPyQt.getStudyId()
137 studyId = _getStudyId()
138 study = getStudyManager().GetStudyByID( studyId )
142 # returns True if object has children
144 def _hasChildren( sobj ):
147 iter = study.NewChildIterator( sobj )
149 name = iter.Value().GetName()
158 # get current GUI context
161 global __current_context__
162 return __current_context__
165 # set and return current GUI context
166 # study ID is passed as parameter
168 def _setContext( studyID ):
169 global __study2context__, __current_context__
170 if not __study2context__.has_key(studyID):
171 __study2context__[studyID] = GUIcontext()
173 __current_context__ = __study2context__[studyID]
174 return __current_context__
177 # increment object counter in the map
179 def _incObjToMap( m, id ):
180 if id not in m: m[id] = 0
188 selcount = sg.SelectedCount()
190 for i in range( selcount ):
191 _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) )
193 return selcount, seltypes
195 ################################################
197 ################################################
199 # called when module is initialized
200 # perform initialization actions
202 if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
203 # set default preferences values
204 if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
205 sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
206 if not sgPyQt.hasSetting( "PYHELLO", "creation_mode"):
207 sgPyQt.addSetting( "PYHELLO", "creation_mode", 0 )
210 # called when module is initialized
211 # return map of popup windows to be used by the module
213 if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
215 wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
216 wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea
219 # called when module is initialized
220 # return list of 2d/3d views to be used ny the module
222 if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
225 # called when module is initialized
226 # export module's preferences
227 def createPreferences():
228 if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
229 gid = sgPyQt.addPreference( "General" )
230 gid = sgPyQt.addPreference( "Object creation", gid )
231 pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" )
232 pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" )
233 strings = QStringList()
234 strings.append( "Default name" )
235 strings.append( "Generate name" )
236 strings.append( "Ask name" )
238 indexes.append( QVariant(0) )
239 indexes.append( QVariant(1) )
240 indexes.append( QVariant(2) )
241 sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) )
242 sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) )
245 # called when module is activated
246 # returns True if activating is successfull and False otherwise
248 if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
249 ctx = _setContext( _getStudyId() )
252 # called when module is deactivated
254 if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
257 # called when active study is changed
258 # active study ID is passed as parameter
259 def activeStudyChanged( studyID ):
260 if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
261 ctx = _setContext( _getStudyId() )
264 # called when popup menu is invoked
265 # popup menu and menu context are passed as parameters
266 def createPopupMenu( popup, context ):
267 if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
268 ctx = _setContext( _getStudyId() )
270 selcount, selected = _getSelection()
271 if verbose() : print selcount, selected
273 # one object is selected
274 if moduleID() in selected:
276 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
277 elif objectID() in selected:
279 popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
280 popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
282 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
286 # several objects are selected
287 if len( selected ) == 1:
288 if moduleID() in selected:
290 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
291 elif objectID() in selected:
292 # menu for list of objects
293 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
299 # called when GUI action is activated
300 # action ID is passed as parameter
301 def OnGUIEvent( commandID ):
302 if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID
303 if dict_command.has_key( commandID ):
305 dict_command[commandID]()
307 traceback.print_exc()
309 if verbose() : print "The command is not implemented: %d" % commandID
312 # called when module's preferences are changed
313 # preference's resources section and setting name are passed as parameters
314 def preferenceChanged( section, setting ):
315 if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )
318 # called when active view is changed
319 # view ID is passed as parameter
320 def activeViewChanged( viewID ):
321 if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID
324 # called when active view is cloned
325 # cloned view ID is passed as parameter
326 def viewCloned( viewID ):
327 if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID
330 # called when active view is viewClosed
331 # view ID is passed as parameter
332 def viewClosed( viewID ):
333 if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID
336 # called when study is opened
339 if verbose() : print "PYHELLOGUI.engineIOR()"
340 return getEngineIOR()
342 ################################################
343 # GUI actions implementation
344 ################################################
349 class MyDialog( QDialog ):
351 def __init__( self, parent = None, modal = 0):
352 QDialog.__init__( self, parent )
353 self.setObjectName( "MyDialog" )
354 self.setModal( modal )
355 self.setWindowTitle( "HELLO!" )
356 vb = QVBoxLayout( self )
359 hb0 = QHBoxLayout( self )
360 label = QLabel( "Prenom: ", self )
361 hb0.addWidget( label )
362 self.entry = QLineEdit( self )
363 self.entry.setMinimumWidth( 200 )
364 hb0.addWidget( self.entry )
367 hb1 = QHBoxLayout( self )
368 bOk = QPushButton( "&OK", self )
369 bOk.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_HANDSHAKE' ) )
370 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
375 bCancel = QPushButton( "&Cancel", self )
376 bCancel.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_STOP' ) )
377 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
378 hb1.addWidget( bCancel )
385 name = str( self.entry.text() )
387 banner = getEngine().makeBanner( name )
388 QMessageBox.information( self, 'Info', banner )
391 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
395 # Show 'HELLO' dialog box
399 d = MyDialog( sgPyQt.getDesktop(), 1 )
409 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
411 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
412 # request object name from the user
413 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
415 "Enter object name:",
419 name = str( name.trimmed() )
420 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
421 # generate object name
422 __objectid__ = __objectid__ + 1
423 name = "%s %d" % ( default_name, __objectid__ )
429 # generate object name
430 __objectid__ = __objectid__ + 1
431 name = "%s %d" % ( default_name, __objectid__ )
434 getEngine().createObject( _getStudy(), name )
435 sg.updateObjBrowser( True )
443 father = study.FindComponent( moduleName() )
445 iter = study.NewChildIterator( father )
446 builder = study.NewBuilder()
450 builder.RemoveObjectWithChildren( sobj )
452 sg.updateObjBrowser( True )
461 entry = sg.getSelected( 0 )
463 sobj = study.FindObjectID( entry )
465 test, attr = sobj.FindAttribute( "AttributeName" )
467 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
474 # Delete selected object(s)
478 builder = study.NewBuilder()
479 if sg.SelectedCount() <= 0: return
480 for i in range( sg.SelectedCount() ):
481 entry = sg.getSelected( i )
483 sobj = study.FindObjectID( entry )
485 builder.RemoveObject( sobj )
489 sg.updateObjBrowser( True )
493 # Rename selected object
497 builder = study.NewBuilder()
498 entry = sg.getSelected( 0 )
500 sobj = study.FindObjectID( entry )
502 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
504 "Enter object name:",
507 name = str( name.trimmed() )
508 if not ok or not name: return
509 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
510 attr.SetValue( name )
511 sg.updateObjBrowser( True )
517 # Commands dictionary
520 GUIcontext.HELLO_ID : ShowHELLO,
521 GUIcontext.CREATE_OBJECT_ID : CreateObject,
522 GUIcontext.DELETE_ALL_ID : DeleteAll,
523 GUIcontext.SHOW_ME_ID : ShowMe,
524 GUIcontext.DELETE_ME_ID : Delete,
525 GUIcontext.RENAME_ME_ID : Rename,