1 # Copyright (C) 2007-2014 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, or (at your option) any later version.
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", GUIcontext.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 # called to check if object can be dragged
354 # returns True if drag operation is allowed for this object
355 def isDraggable(what):
356 if verbose() : print "PYHELLOGUI.isDraggable()"
357 # return True if object is draggable
360 # called to check if object allows dropping on it
361 # returns True if drop operation is allowed for this object
362 def isDropAccepted(where):
363 if verbose() : print "PYHELLOGUI.isDropAccepted()"
364 # return True if object accept drops
367 # called when drag and drop operation is finished
368 # performs corresponding data re-arrangement if allowed
369 def dropObjects(what, where, row, action):
371 print "PYHELLOGUI.dropObjects()"
372 # 'what' is a list of entries of objects being dropped
373 for i in what: print "- dropped:", i
374 # 'where' is a parent object's entry
375 print "- dropping on:", where
376 # 'row' is an position in the parent's children list;
377 # -1 if appending to the end of children list is performed
378 print "- dropping position:", row
379 # 'action' is a dropping action being performed:
380 # - 0x01 (Qt::CopyAction) for copy
381 # - 0x02 (Qt::MoveAction) for move
382 print "- drop action:", action
386 ################################################
387 # GUI actions implementation
388 ################################################
393 class MyDialog( QDialog ):
395 def __init__( self, parent = None, modal = 0):
396 QDialog.__init__( self, parent )
397 self.setObjectName( "MyDialog" )
398 self.setModal( modal )
399 self.setWindowTitle( "HELLO!" )
400 vb = QVBoxLayout( self )
403 hb0 = QHBoxLayout( self )
404 label = QLabel( "Prenom: ", self )
405 hb0.addWidget( label )
406 self.entry = QLineEdit( self )
407 self.entry.setMinimumWidth( 200 )
408 hb0.addWidget( self.entry )
411 hb1 = QHBoxLayout( self )
412 bOk = QPushButton( "&OK", self )
413 bOk.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_HANDSHAKE' ) )
414 self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
419 bCancel = QPushButton( "&Cancel", self )
420 bCancel.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_STOP' ) )
421 self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
422 hb1.addWidget( bCancel )
429 name = str( self.entry.text() )
431 banner = getEngine().makeBanner( name )
432 QMessageBox.information( self, 'Info', banner )
435 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
439 # Show 'HELLO' dialog box
443 d = MyDialog( sgPyQt.getDesktop(), 1 )
453 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
455 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
456 # request object name from the user
457 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
459 "Enter object name:",
463 name = str( name.trimmed() )
464 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
465 # generate object name
466 __objectid__ = __objectid__ + 1
467 name = "%s %d" % ( default_name, __objectid__ )
473 # generate object name
474 __objectid__ = __objectid__ + 1
475 name = "%s %d" % ( default_name, __objectid__ )
478 getEngine().createObject( _getStudy(), name )
479 sg.updateObjBrowser( True )
487 father = study.FindComponent( moduleName() )
489 iter = study.NewChildIterator( father )
490 builder = study.NewBuilder()
494 builder.RemoveObjectWithChildren( sobj )
496 sg.updateObjBrowser( True )
505 entry = sg.getSelected( 0 )
507 sobj = study.FindObjectID( entry )
509 test, attr = sobj.FindAttribute( "AttributeName" )
511 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
518 # Delete selected object(s)
522 builder = study.NewBuilder()
523 if sg.SelectedCount() <= 0: return
524 for i in range( sg.SelectedCount() ):
525 entry = sg.getSelected( i )
527 sobj = study.FindObjectID( entry )
529 builder.RemoveObject( sobj )
533 sg.updateObjBrowser( True )
537 # Rename selected object
541 builder = study.NewBuilder()
542 entry = sg.getSelected( 0 )
544 sobj = study.FindObjectID( entry )
546 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
548 "Enter object name:",
551 name = str( name.trimmed() )
552 if not ok or not name: return
553 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
554 attr.SetValue( name )
555 sg.updateObjBrowser( True )
561 # Display password stored in the preferences
564 passwd = str( sgPyQt.stringSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD ).trimmed() )
565 QMessageBox.information(sgPyQt.getDesktop(),
570 # Commands dictionary
573 GUIcontext.HELLO_ID : ShowHELLO,
574 GUIcontext.CREATE_OBJECT_ID : CreateObject,
575 GUIcontext.DELETE_ALL_ID : DeleteAll,
576 GUIcontext.SHOW_ME_ID : ShowMe,
577 GUIcontext.DELETE_ME_ID : Delete,
578 GUIcontext.RENAME_ME_ID : Rename,
579 GUIcontext.PASSWORD_ID : Password,