1 # Copyright (C) 2007-2016 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 qtsalome import *
32 from PYHELLO_utils import *
34 ################################################
36 # Used to store actions, menus, toolbars, etc...
37 ################################################
40 # menus/toolbars/actions IDs
43 CREATE_OBJECT_ID = 942
55 DEFAULT_NAME = "Object"
57 DEFAULT_PASSWD = "Passwd"
61 # create top-level menu
62 mid = sgPyQt.createMenu( "PyHello", -1, GUIcontext.PYHELLO_MENU_ID, sgPyQt.defaultMenuGroup() )
64 tid = sgPyQt.createTool( "PyHello" )
65 # create actions and fill menu and toolbar with actions
66 a = sgPyQt.createAction( GUIcontext.HELLO_ID, "Hello", "Hello", "Show hello dialog box", "ExecPYHELLO.png" )
67 sgPyQt.createMenu( a, mid )
68 sgPyQt.createTool( a, tid )
69 a = sgPyQt.createSeparator()
70 sgPyQt.createMenu( a, mid )
71 a = sgPyQt.createAction( GUIcontext.CREATE_OBJECT_ID, "Create object", "Create object", "Create object" )
72 sgPyQt.createMenu( a, mid )
73 a = sgPyQt.createSeparator()
74 sgPyQt.createMenu( a, mid )
76 ag = sgPyQt.createActionGroup( GUIcontext.OPTIONS_ID )
77 ag.setText( "Creation mode" )
78 ag.setUsesDropDown(True)
79 a = sgPyQt.createAction( GUIcontext.OPTION_1_ID, "Default name", "Default name", "Use default name for the objects" )
80 a.setCheckable( True )
82 a = sgPyQt.createAction( GUIcontext.OPTION_2_ID, "Generate name", "Generate name", "Generate name for the objects" )
83 a.setCheckable( True )
85 a = sgPyQt.createAction( GUIcontext.OPTION_3_ID, "Ask name", "Ask name", "Request object name from the user" )
86 a.setCheckable( True )
88 sgPyQt.createMenu( ag, mid )
89 sgPyQt.createTool( ag, tid )
90 default_mode = sgPyQt.integerSetting( "PYHELLO", "creation_mode", 0 )
91 sgPyQt.action( GUIcontext.OPTION_1_ID + default_mode ).setChecked( True )
94 a = sgPyQt.createSeparator()
95 a = sgPyQt.createAction( GUIcontext.PASSWORD_ID, "Display password", "Display password", "Display password" )
96 sgPyQt.createMenu( a, mid )
98 # the following action are used in context popup
99 a = sgPyQt.createAction( GUIcontext.DELETE_ALL_ID, "Delete all", "Delete all", "Delete all objects" )
100 a = sgPyQt.createAction( GUIcontext.SHOW_ME_ID, "Show", "Show", "Show object name" )
101 a = sgPyQt.createAction( GUIcontext.DELETE_ME_ID, "Delete", "Delete", "Remove object" )
102 a = sgPyQt.createAction( GUIcontext.RENAME_ME_ID, "Rename", "Rename", "Rename object" )
106 ################################################
108 ################################################
110 # study-to-context map
111 __study2context__ = {}
113 __current_context__ = None
117 ################################################
119 # Get SALOME PyQt interface
121 sgPyQt = SalomePyQt.SalomePyQt()
123 # Get SALOME Swig interface
124 import libSALOME_Swig
125 sg = libSALOME_Swig.SALOMEGUI_Swig()
127 ################################################
129 ################################################
131 ################################################
134 # get active study ID
137 return sgPyQt.getStudyId()
143 studyId = _getStudyId()
144 study = getStudyManager().GetStudyByID( studyId )
148 # returns True if object has children
150 def _hasChildren( sobj ):
153 iter = study.NewChildIterator( sobj )
155 name = iter.Value().GetName()
164 # get current GUI context
167 global __current_context__
168 return __current_context__
171 # set and return current GUI context
172 # study ID is passed as parameter
174 def _setContext( studyID ):
175 global __study2context__, __current_context__
176 if not __study2context__.has_key(studyID):
177 __study2context__[studyID] = GUIcontext()
179 __current_context__ = __study2context__[studyID]
180 return __current_context__
183 # increment object counter in the map
185 def _incObjToMap( m, id ):
186 if id not in m: m[id] = 0
194 selcount = sg.SelectedCount()
196 for i in range( selcount ):
197 _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) )
199 return selcount, seltypes
201 ################################################
203 ################################################
205 # called when module is initialized
206 # perform initialization actions
208 if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
209 # set default preferences values
210 if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
211 sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
212 if not sgPyQt.hasSetting( "PYHELLO", "creation_mode"):
213 sgPyQt.addSetting( "PYHELLO", "creation_mode", 0 )
214 if not sgPyQt.hasSetting( "PYHELLO", "Password"):
215 sgPyQt.addSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD )
218 # called when module is initialized
219 # return map of popup windows to be used by the module
221 if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
223 wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
224 wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea
227 # called when module is initialized
228 # return list of 2d/3d views to be used ny the module
230 if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
233 # called when module is initialized
234 # export module's preferences
235 def createPreferences():
236 if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
237 gid = sgPyQt.addPreference( "General" )
238 gid = sgPyQt.addPreference( "Object creation", gid )
239 pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" )
240 pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" )
241 strings = QStringList()
242 strings.append( "Default name" )
243 strings.append( "Generate name" )
244 strings.append( "Ask name" )
246 indexes.append( QVariant(0) )
247 indexes.append( QVariant(1) )
248 indexes.append( QVariant(2) )
249 sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) )
250 sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) )
251 pid = sgPyQt.addPreference( "Password", gid, SalomePyQt.PT_String, "PYHELLO", "Password" )
252 sgPyQt.setPreferenceProperty( pid, "echo", QVariant( 2 ) )
255 # called when module is activated
256 # returns True if activating is successfull and False otherwise
258 if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
259 ctx = _setContext( _getStudyId() )
262 # called when module is deactivated
264 if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
267 # called when active study is changed
268 # active study ID is passed as parameter
269 def activeStudyChanged( studyID ):
270 if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
271 ctx = _setContext( _getStudyId() )
274 # called when popup menu is invoked
275 # popup menu and menu context are passed as parameters
276 def createPopupMenu( popup, context ):
277 if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
278 ctx = _setContext( _getStudyId() )
280 selcount, selected = _getSelection()
281 if verbose() : print selcount, selected
283 # one object is selected
284 if moduleID() in selected:
286 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
287 elif objectID() in selected:
289 popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
290 popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
292 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
296 # several objects are selected
297 if len( selected ) == 1:
298 if moduleID() in selected:
300 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
301 elif objectID() in selected:
302 # menu for list of objects
303 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
309 # called when GUI action is activated
310 # action ID is passed as parameter
311 def OnGUIEvent( commandID ):
312 if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID
313 if dict_command.has_key( commandID ):
315 dict_command[commandID]()
317 traceback.print_exc()
319 if verbose() : print "The command is not implemented: %d" % commandID
322 # called when module's preferences are changed
323 # preference's resources section and setting name are passed as parameters
324 def preferenceChanged( section, setting ):
325 if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )
328 # called when active view is changed
329 # view ID is passed as parameter
330 def activeViewChanged( viewID ):
331 if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID
334 # called when active view is cloned
335 # cloned view ID is passed as parameter
336 def viewCloned( viewID ):
337 if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID
340 # called when active view is viewClosed
341 # view ID is passed as parameter
342 def viewClosed( viewID ):
343 if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID
346 # called when study is opened
349 if verbose() : print "PYHELLOGUI.engineIOR()"
350 return getEngineIOR()
352 # called to check if object can be dragged
353 # returns True if drag operation is allowed for this object
354 def isDraggable(what):
355 if verbose() : print "PYHELLOGUI.isDraggable()"
356 # return True if object is draggable
359 # called to check if object allows dropping on it
360 # returns True if drop operation is allowed for this object
361 def isDropAccepted(where):
362 if verbose() : print "PYHELLOGUI.isDropAccepted()"
363 # return True if object accept drops
366 # called when drag and drop operation is finished
367 # performs corresponding data re-arrangement if allowed
368 def dropObjects(what, where, row, action):
370 print "PYHELLOGUI.dropObjects()"
371 # 'what' is a list of entries of objects being dropped
372 for i in what: print "- dropped:", i
373 # 'where' is a parent object's entry
374 print "- dropping on:", where
375 # 'row' is an position in the parent's children list;
376 # -1 if appending to the end of children list is performed
377 print "- dropping position:", row
378 # 'action' is a dropping action being performed:
379 # - 0x01 (Qt::CopyAction) for copy
380 # - 0x02 (Qt::MoveAction) for move
381 print "- drop action:", action
385 ################################################
386 # GUI actions implementation
387 ################################################
392 class MyDialog( QDialog ):
394 def __init__( self, parent = None, modal = 0):
395 QDialog.__init__( self, parent )
396 self.setObjectName( "MyDialog" )
397 self.setModal( modal )
398 self.setWindowTitle( "HELLO!" )
399 vb = QVBoxLayout( self )
400 vb.setContentsMargins( 8, 8, 8, 8 )
402 hb0 = QHBoxLayout( self )
403 label = QLabel( "Prenom: ", self )
404 hb0.addWidget( label )
405 self.entry = QLineEdit( self )
406 self.entry.setMinimumWidth( 200 )
407 hb0.addWidget( self.entry )
410 hb1 = QHBoxLayout( self )
411 bOk = QPushButton( "&OK", self )
412 bOk.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_HANDSHAKE' ) )
413 bOk.clicked.connect(self.accept)
418 bCancel = QPushButton( "&Cancel", self )
419 bCancel.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_STOP' ) )
420 bCancel.clicked.connect(self.close)
421 hb1.addWidget( bCancel )
427 name = str( self.entry.text() )
429 banner = getEngine().makeBanner( name )
430 QMessageBox.information( self, 'Info', banner )
433 QMessageBox.warning( self, 'Error!', 'Please, enter the name!' )
437 # Show 'HELLO' dialog box
441 d = MyDialog( sgPyQt.getDesktop(), 1 )
451 default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ) ).strip()
453 if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
454 # request object name from the user
455 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
457 "Enter object name:",
461 name = str( name ).strip()
462 elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
463 # generate object name
464 __objectid__ = __objectid__ + 1
465 name = "%s %d" % ( default_name, __objectid__ )
471 # generate object name
472 __objectid__ = __objectid__ + 1
473 name = "%s %d" % ( default_name, __objectid__ )
476 getEngine().createObject( _getStudy(), name )
477 sg.updateObjBrowser( True )
485 father = study.FindComponent( moduleName() )
487 iter = study.NewChildIterator( father )
488 builder = study.NewBuilder()
492 builder.RemoveObjectWithChildren( sobj )
494 sg.updateObjBrowser( True )
503 entry = sg.getSelected( 0 )
505 sobj = study.FindObjectID( entry )
507 test, attr = sobj.FindAttribute( "AttributeName" )
509 QMessageBox.information( sgPyQt.getDesktop(), 'Info', "My name is '%s'" % attr.Value() )
516 # Delete selected object(s)
520 builder = study.NewBuilder()
521 if sg.SelectedCount() <= 0: return
522 for i in range( sg.SelectedCount() ):
523 entry = sg.getSelected( i )
525 sobj = study.FindObjectID( entry )
527 builder.RemoveObject( sobj )
531 sg.updateObjBrowser( True )
535 # Rename selected object
539 builder = study.NewBuilder()
540 entry = sg.getSelected( 0 )
542 sobj = study.FindObjectID( entry )
544 name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
546 "Enter object name:",
549 name = str( name ).strip()
550 if not ok or not name: return
551 attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
552 attr.SetValue( name )
553 sg.updateObjBrowser( True )
559 # Display password stored in the preferences
562 passwd = str( sgPyQt.stringSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD ) ).strip()
563 QMessageBox.information(sgPyQt.getDesktop(),
568 # Commands dictionary
571 GUIcontext.HELLO_ID : ShowHELLO,
572 GUIcontext.CREATE_OBJECT_ID : CreateObject,
573 GUIcontext.DELETE_ALL_ID : DeleteAll,
574 GUIcontext.SHOW_ME_ID : ShowMe,
575 GUIcontext.DELETE_ME_ID : Delete,
576 GUIcontext.RENAME_ME_ID : Rename,
577 GUIcontext.PASSWORD_ID : Password,