Salome HOME
Merge multi-study removal branch.
[samples/pyhello.git] / src / PYHELLOGUI / PYHELLOGUI.py
index 9f46497e82826f79308231cae5ee7eb4490c219b..d352ddf85b90e0506f8f264e0bc04d51637608ea 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #
 import traceback
 import os
-from PyQt4.QtGui import *
-from PyQt4.QtCore import *
+from qtsalome import *
 
 from PYHELLO_utils import *
 
+import salome
+
 ################################################
 # GUI context class
 # Used to store actions, menus, toolbars, etc...
@@ -108,10 +109,6 @@ class GUIcontext:
 # Global variables
 ################################################
 
-# study-to-context map
-__study2context__   = {}
-# current context
-__current_context__ = None
 # object counter
 __objectid__ = 0
 
@@ -131,27 +128,12 @@ sg = libSALOME_Swig.SALOMEGUI_Swig()
 # Internal methods
 ################################################
 
-###
-# get active study ID
-###
-def _getStudyId():
-    return sgPyQt.getStudyId()
-
-###
-# get active study
-###
-def _getStudy():
-    studyId = _getStudyId()
-    study = getStudyManager().GetStudyByID( studyId )
-    return study
-
 ###
 # returns True if object has children
 ###
 def _hasChildren( sobj ):
     if sobj:
-        study = _getStudy()
-        iter  = study.NewChildIterator( sobj )
+        iter  = salome.myStudy.NewChildIterator( sobj )
         while iter.More():
             name = iter.Value().GetName()
             if name:
@@ -161,25 +143,6 @@ def _hasChildren( sobj ):
         pass
     return False
 
-###
-# get current GUI context
-###
-def _getContext():
-    global __current_context__
-    return __current_context__
-
-###
-# set and return current GUI context
-# study ID is passed as parameter
-###
-def _setContext( studyID ):
-    global __study2context__, __current_context__
-    if not __study2context__.has_key(studyID):
-        __study2context__[studyID] = GUIcontext()
-        pass
-    __current_context__ = __study2context__[studyID]
-    return __current_context__
-
 ###
 # increment object counter in the map
 ###
@@ -195,7 +158,7 @@ def _getSelection():
     selcount = sg.SelectedCount()
     seltypes = {}
     for i in range( selcount ):
-        _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) )
+        _incObjToMap( seltypes, getObjectID( sg.getSelected( i ) ) )
         pass
     return selcount, seltypes
 
@@ -206,7 +169,7 @@ def _getSelection():
 # called when module is initialized
 # perform initialization actions
 def initialize():
-    if verbose() : print "PYHELLOGUI.initialize() : study : %d" % _getStudyId()
+    if verbose() : print "PYHELLOGUI.initialize()"
     # set default preferences values
     if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"):
         sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME )
@@ -219,7 +182,7 @@ def initialize():
 # called when module is initialized
 # return map of popup windows to be used by the module
 def windows():
-    if verbose() : print "PYHELLOGUI.windows() : study : %d" % _getStudyId()
+    if verbose() : print "PYHELLOGUI.windows()"
     wm = {}
     wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea
     wm[SalomePyQt.WT_PyConsole]     = Qt.BottomDockWidgetArea
@@ -228,13 +191,13 @@ def windows():
 # called when module is initialized
 # return list of 2d/3d views to be used ny the module
 def views():
-    if verbose() : print "PYHELLOGUI.views() : study : %d" % _getStudyId()
+    if verbose() : print "PYHELLOGUI.views()"
     return []
 
 # called when module is initialized
 # export module's preferences
 def createPreferences():
-    if verbose() : print "PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()
+    if verbose() : print "PYHELLOGUI.createPreferences()"
     gid = sgPyQt.addPreference( "General" )
     gid = sgPyQt.addPreference( "Object creation", gid )
     pid = sgPyQt.addPreference( "Default name",  gid, SalomePyQt.PT_String,   "PYHELLO", "def_obj_name" )
@@ -256,28 +219,19 @@ def createPreferences():
 # called when module is activated
 # returns True if activating is successfull and False otherwise
 def activate():
-    if verbose() : print "PYHELLOGUI.activate() : study : %d" % _getStudyId()
-    ctx = _setContext( _getStudyId() )
+    if verbose() : print "PYHELLOGUI.activate()"
+    GUIcontext()
     return True
 
 # called when module is deactivated
 def deactivate():
-    if verbose() : print "PYHELLOGUI.deactivate() : study : %d" % _getStudyId()
-    pass
-
-# called when active study is changed
-# active study ID is passed as parameter
-def activeStudyChanged( studyID ):
-    if verbose() : print "PYHELLOGUI.activeStudyChanged(): study : %d" % studyID
-    ctx = _setContext( _getStudyId() )
+    if verbose() : print "PYHELLOGUI.deactivate()"
     pass
 
 # called when popup menu is invoked
 # popup menu and menu context are passed as parameters
 def createPopupMenu( popup, context ):
     if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context
-    ctx = _setContext( _getStudyId() )
-    study = _getStudy()
     selcount, selected = _getSelection()
     if verbose() : print selcount, selected
     if selcount == 1:
@@ -398,7 +352,7 @@ class MyDialog( QDialog ):
         self.setModal( modal )
         self.setWindowTitle( "HELLO!" )
         vb = QVBoxLayout( self )
-        vb.setMargin( 8 )
+        vb.setContentsMargins( 8, 8, 8, 8 )
 
         hb0 = QHBoxLayout( self )
         label = QLabel( "Prenom: ", self )
@@ -411,16 +365,15 @@ class MyDialog( QDialog ):
         hb1 = QHBoxLayout( self )
         bOk = QPushButton( "&OK", self )
         bOk.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_HANDSHAKE' ) )
-        self.connect( bOk, SIGNAL( 'clicked()' ), self, SLOT( 'accept()' ) )
+        bOk.clicked.connect(self.accept)
         hb1.addWidget( bOk )
         
         hb1.addStretch( 10 )
         
         bCancel = QPushButton( "&Cancel", self )
         bCancel.setIcon( sgPyQt.loadIcon( 'PYHELLO', 'ICO_STOP' ) )
-        self.connect( bCancel, SIGNAL( 'clicked()' ), self, SLOT( 'close()' ) )
+        bCancel.clicked.connect(self.close)
         hb1.addWidget( bCancel )
-        
         vb.addLayout( hb1 )
         pass
     
@@ -450,7 +403,7 @@ def ShowHELLO():
 ###
 def CreateObject():
     global __objectid__
-    default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ).trimmed() )
+    default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ) ).strip()
     try:
         if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked():
             # request object name from the user
@@ -460,7 +413,7 @@ def CreateObject():
                                              QLineEdit.Normal,
                                              default_name )
             if not ok: return
-            name = str( name.trimmed() )
+            name = str( name ).strip()
         elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked():
             # generate object name
             __objectid__  = __objectid__ + 1
@@ -475,25 +428,24 @@ def CreateObject():
         name = "%s %d" % ( default_name, __objectid__ )
         pass
     if not name: return
-    getEngine().createObject( _getStudy(), name )
-    sg.updateObjBrowser( True )
+    getEngine().createObject( name )
+    sg.updateObjBrowser()
     pass
 
 ###
 # Delete all objects
 ###
 def DeleteAll():
-    study = _getStudy()
-    father = study.FindComponent( moduleName() )
+    father = salome.myStudy.FindComponent( moduleName() )
     if father:
-        iter = study.NewChildIterator( father )
-        builder = study.NewBuilder()
+        iter = salome.myStudy.NewChildIterator( father )
+        builder = salome.myStudy.NewBuilder()
         while iter.More():
             sobj = iter.Value()
             iter.Next()
             builder.RemoveObjectWithChildren( sobj )
             pass
-        sg.updateObjBrowser( True )
+        sg.updateObjBrowser()
         pass
     pass
 
@@ -501,10 +453,9 @@ def DeleteAll():
 # Show object's name
 ###
 def ShowMe():
-    study = _getStudy()
     entry = sg.getSelected( 0 )
     if entry != '':
-        sobj = study.FindObjectID( entry )
+        sobj = salome.myStudy.FindObjectID( entry )
         if ( sobj ):
             test, attr = sobj.FindAttribute( "AttributeName" )
             if test:
@@ -518,41 +469,39 @@ def ShowMe():
 # Delete selected object(s)
 ###
 def Delete():
-    study = _getStudy()
-    builder = study.NewBuilder()
+    builder = salome.myStudy.NewBuilder()
     if sg.SelectedCount() <= 0: return
     for i in range( sg.SelectedCount() ):
         entry = sg.getSelected( i )
         if entry != '':
-            sobj = study.FindObjectID( entry )
+            sobj = salome.myStudy.FindObjectID( entry )
             if ( sobj ):
                 builder.RemoveObject( sobj )
                 pass
             pass
         pass
-    sg.updateObjBrowser( True )
+    sg.updateObjBrowser()
     pass
 
 ###
 # Rename selected object
 ###
 def Rename():
-    study = _getStudy()
-    builder = study.NewBuilder()
+    builder = salome.myStudy.NewBuilder()
     entry = sg.getSelected( 0 )
     if entry != '':
-        sobj = study.FindObjectID( entry )
+        sobj = salome.myStudy.FindObjectID( entry )
         if ( sobj ):
             name, ok = QInputDialog.getText( sgPyQt.getDesktop(),
                                              "Object name",
                                              "Enter object name:",
                                              QLineEdit.Normal,
                                              sobj.GetName() )
-            name = str( name.trimmed() )
+            name = str( name ).strip()
             if not ok or not name: return
             attr = builder.FindOrCreateAttribute( sobj, "AttributeName" )
             attr.SetValue( name )
-            sg.updateObjBrowser( True )
+            sg.updateObjBrowser()
             pass
         pass
     pass
@@ -561,7 +510,7 @@ def Rename():
 # Display password stored in the preferences
 ###
 def Password():
-  passwd = str( sgPyQt.stringSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD ).trimmed() )
+  passwd = str( sgPyQt.stringSetting( "PYHELLO", "Password", GUIcontext.DEFAULT_PASSWD ) ).strip()
   QMessageBox.information(sgPyQt.getDesktop(),
                           "Password",
                           passwd)