Salome HOME
Merge multi-study removal branch. Before_python3_27062017
authorrnv <rnv@opencascade.com>
Thu, 8 Jun 2017 16:47:57 +0000 (19:47 +0300)
committerrnv <rnv@opencascade.com>
Thu, 8 Jun 2017 16:48:06 +0000 (19:48 +0300)
src/PYLIGHTGUI/PYLIGHTGUI.py

index 511281c327c85e974237d2538bcfe9967941e6a7..03e8dbd5d94f8899300a6ff1b4bf0fcf3c52bdbb 100644 (file)
@@ -40,10 +40,14 @@ sg = libSALOME_Swig.SALOMEGUI_Swig()
 # Used to store actions, menus, toolbars, etc...
 ################################################
 
+# data model
+__data_model__ = None
+
 class GUIcontext:
 
     # constructor
     def __init__( self ):
+        global __data_model__
         # Load File action
         sgPyQt.createAction(dict_actions["loadfile"], "Load text File", "Load text file")
         # Save File action
@@ -87,8 +91,10 @@ class GUIcontext:
         sgPyQt.createMenu( dict_actions["eraseLine"],   menuPyLight, 10 );
 
         # Create DataModel
-        self.DM = PYLIGHT_DataModel.PYLIGHT_DataModel()
-
+        if __data_model__ is None:
+            __data_model__ = PYLIGHT_DataModel.PYLIGHT_DataModel()
+            pass
+            
         pass # def __init__( self )
 
     pass # class GUIcontext
@@ -97,12 +103,6 @@ class GUIcontext:
 # Global variables and functions
 ################################################
 
-###
-# get active study ID
-###
-def _getStudyId():
-    return sgPyQt.getStudyId()
-
 # verbosity level
 __verbose__ = None
 
@@ -120,30 +120,6 @@ def verbose():
         pass
     return __verbose__
 
-# study-to-context map
-__study2context__   = {}
-# current context
-__current_context__ = None
-
-###
-# 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__
-
 ################################################
 
 # Create actions and menus
@@ -154,20 +130,13 @@ def initialize():
 # called when module is activated
 # returns True if activating is successfull and False otherwise
 def activate():
-    if verbose() : print "PYLIGHTGUI.activate() : study : %d" % _getStudyId()
-    ctx = _setContext( _getStudyId() )
+    if verbose() : print "PYLIGHTGUI.activate()"
+    GUIcontext()
     return True
 
 # called when module is deactivated
 def deactivate():
-    if verbose() : print "PYLIGHTGUI.deactivate() : study : %d" % _getStudyId()
-    pass
-
-# called when active study is changed
-# active study ID is passed as parameter
-def activeStudyChanged( studyID ):
-    if verbose() : print "PYLIGHTGUI.activeStudyChanged(): study : %d" % studyID
-    ctx = _setContext( _getStudyId() )
+    if verbose() : print "PYLIGHTGUI.deactivate()"
     pass
 
 # Process GUI action
@@ -184,17 +153,16 @@ def OnGUIEvent(commandID):
 
 # Customize popup menu
 def createPopupMenu(popup, context):
+    global __data_model__
     if verbose() : print "PYLIGHTGUI.createPopupMenu(): context = %s" % context
 
     if context != 'ObjectBrowser':
         return
     
-    studyId = _getStudyId()
-    ctx = _setContext( studyId )
     selcount = sg.SelectedCount()
     if selcount == 1:
         entry = sg.getSelected( 0 )
-        obj = ctx.DM.getObject(entry)
+        obj = __data_model__.getObject(entry)
         if obj is not None:
             if obj.getText() != "\n":
                 # Line is selected
@@ -219,25 +187,25 @@ def createPopupMenu(popup, context):
 
 # For saving data in the study
 def saveFiles(prefix):
+    global __data_model__
     if verbose(): print "PYLIGHTGUI::saveFile()"
-    ctx = _setContext( _getStudyId() )
     postfix = "PYLIGHT.txt"
     filename = prefix+postfix
-    ctx.DM.saveFile(filename)
+    __data_model__.saveFile(filename)
     return postfix
 
 # For restore data from the study
 def openFiles(filelist):
+    global __data_model__
     if verbose(): print "PYLIGHTGUI::openFile()"
-    ctx = _setContext( _getStudyId() )
     filename =  filelist[0]
     filename.append(filelist[1])
-    ctx.DM.loadFile(filename)
+    __data_model__.loadFile(filename)
     return True
 
 # Loading a text file
 def loadfile():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     aFilter = "Text files (*.txt)"
     filename = QFileDialog.getOpenFileName(sgPyQt.getDesktop(), "Open text file", "", aFilter, "Choose a text file to open")
 
@@ -250,18 +218,18 @@ def loadfile():
         return
     
     if os.access(str(filename),os.R_OK):
-        ctx.DM.loadFile(filename)
+        __data_model__.loadFile(filename)
     else:
         QMessageBox.warning(sgPyQt.getDesktop(),
                             "Error!",
                             "Can not read file:\n%s"%(filename))
         pass
-    sg.updateObjBrowser(True)
+    sg.updateObjBrowser()
     pass
 
 # Saving a text file
 def savefile():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     aFilter = "Text files (*.txt)"
     filename = QFileDialog.getSaveFileName(sgPyQt.getDesktop(),"Save text file", "", aFilter, "Choose a text file to save")
 
@@ -277,7 +245,7 @@ def savefile():
     fn = filename
     # Get directory name and check access
     if os.access(str(fn[:fn.rindex(os.path.sep)]), os.W_OK):
-        ctx.DM.saveFile(filename)
+        __data_model__.saveFile(filename)
     else:
         QMessageBox.warning(sgPyQt.getDesktop(),
                             "Error!",
@@ -289,7 +257,7 @@ def insertLine():
     '''
     Insert new line in the selected paragraph.
     '''
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     #Get result
     res = QInputDialog.getText(sgPyQt.getDesktop(),
                                "Add new line",
@@ -302,20 +270,20 @@ def insertLine():
     # Nb selected objects
     selcount = sg.SelectedCount()
     # Nb object in the Data Model
-    paragrCount = len(ctx.DM.getParagraphs())
+    paragrCount = len(__data_model__.getParagraphs())
 
     # Create first paragraph
     if paragrCount == 0:
-        ctx.DM.createObject()
+        __data_model__.createObject()
         # If line not empty create first line
         if text != "\n":
-            ctx.DM.createObject(text,ctx.DM.getParagraphs()[0])
-        sg.updateObjBrowser(True)
+            __data_model__.createObject(text,__data_model__.getParagraphs()[0])
+        sg.updateObjBrowser()
         return
     # Create paragraph
     if text == "\n":
-        ctx.DM.createObject()
-        sg.updateObjBrowser(True)
+        __data_model__.createObject()
+        sg.updateObjBrowser()
         return
     else:
         if selcount == 0:
@@ -325,12 +293,12 @@ def insertLine():
             return
         if selcount == 1:
             entry = sg.getSelected( 0 )
-            obj = ctx.DM.getObject(entry)
+            obj = __data_model__.getObject(entry)
             if obj is not None:
                 # Create line
                 if(obj.getText() == "\n"):
-                    ctx.DM.createObject(text,entry)
-                    sg.updateObjBrowser(True);
+                    __data_model__.createObject(text,entry)
+                    sg.updateObjBrowser();
                     return
                 else:
                     QMessageBox.warning(sgPyQt.getDesktop(),
@@ -345,10 +313,10 @@ def insertLine():
         
 # Edit selected line
 def editLine():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     if sg.SelectedCount() == 1:
         entry = sg.getSelected( 0 )
-        obj = ctx.DM.getObject(entry)
+        obj = __data_model__.getObject(entry)
         if(obj.getText() != "\n"):
             #Get text line
             res = QInputDialog.getText(sgPyQt.getDesktop(),
@@ -369,19 +337,19 @@ def editLine():
         QMessageBox.information(sgPyQt.getDesktop(),
                                 'Info',
                                 'Please, select one line!')
-    sg.updateObjBrowser(True);
+    sg.updateObjBrowser();
     pass
 
 # Remove selected lines
 def removeLine():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     selcount = sg.SelectedCount()
     onlyLines = True
     lines = []
     while selcount != 0:
         entry = sg.getSelected( selcount - 1)
         #Check what only lines selected
-        if ctx.DM.getObject(entry).getText() == "\n":
+        if __data_model__.getObject(entry).getText() == "\n":
             onlyLines = False
             break
         lines.append(entry)
@@ -397,18 +365,18 @@ def removeLine():
                 renderer.RemoveActor(actor)
                 pass
             pass
-        ctx.DM.removeObjects(lines)
-        sg.updateObjBrowser(True)
+        __data_model__.removeObjects(lines)
+        sg.updateObjBrowser()
         pass
     pass
 
 # Remove all lines from all paragraphs
 def clearAll():
-    ctx = _setContext( _getStudyId() )
-    paragraphs = ctx.DM.getParagraphs()
+    global __data_model__
+    paragraphs = __data_model__.getParagraphs()
     for paragr in paragraphs:
         lines = sgPyQt.getChildren(paragr)
-        ctx.DM.removeObjects(lines)
+        __data_model__.removeObjects(lines)
         renderer=libSalomePy.getRenderer()
         for l in lines:
             actor = getActor(l)
@@ -416,16 +384,16 @@ def clearAll():
                 renderer.RemoveActor(actor)
                 pass
             pass
-    sg.updateObjBrowser(True)
+    sg.updateObjBrowser()
     pass
 
 # Display the selected line
 def displayLine():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     if sg.SelectedCount() != 1:
         return
     entry = sg.getSelected(0)
-    text = ctx.DM.getObject(entry).getText()
+    text = __data_model__.getObject(entry).getText()
     if text == "\n":
         return
     renderer=libSalomePy.getRenderer()
@@ -447,19 +415,19 @@ def displayLine():
 
 # Clear remove all lines under selected paragraph
 def clearParagraph():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     lines = sgPyQt.getChildren(sg.getSelected(0))
-    ctx.DM.removeObjects(lines)
-    sg.updateObjBrowser(True)
+    __data_model__.removeObjects(lines)
+    sg.updateObjBrowser()
     pass
 
 # Erase the selected line
 def eraseLine():
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
     if sg.SelectedCount() != 1:
         return
     entry = sg.getSelected(0)
-    text = ctx.DM.getObject(entry).getText()
+    text = __data_model__.getObject(entry).getText()
     if text == "\n":
         return
     renderer=libSalomePy.getRenderer()