Salome HOME
Merge V9_dev branch into master SHAPER_V9_1_0RC1
authorrnv <rnv@opencascade.com>
Thu, 7 Jun 2018 12:50:15 +0000 (15:50 +0300)
committerrnv <rnv@opencascade.com>
Thu, 14 Jun 2018 11:53:42 +0000 (14:53 +0300)
CMakeLists.txt
src/PYLIGHTGUI/PYLIGHTGUI.py
src/PYLIGHTGUI/PYLIGHT_DataModel.py

index f05d114cb795b490b0fbeb86b6133d424e10e7c4..6baba0a4fc30294b5ccfdd6d952cb9889b7e94aa 100755 (executable)
@@ -32,7 +32,7 @@ SET(${PROJECT_NAME_UC}_MINOR_VERSION 5)
 SET(${PROJECT_NAME_UC}_PATCH_VERSION 0)
 SET(${PROJECT_NAME_UC}_VERSION
   ${${PROJECT_NAME_UC}_MAJOR_VERSION}.${${PROJECT_NAME_UC}_MINOR_VERSION}.${${PROJECT_NAME_UC}_PATCH_VERSION})
-SET(${PROJECT_NAME_UC}_VERSION_DEV 1)
+SET(${PROJECT_NAME_UC}_VERSION_DEV 0)
 
 # Common CMake macros
 # ===================
index 511281c327c85e974237d2538bcfe9967941e6a7..54e5744512e4c1d4f4c54afcade3efd1a6ee0f4a 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,81 +120,49 @@ 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
 def initialize():
-    if verbose(): print "PYLIGHTGUI::initialize()"
+    if verbose(): print("PYLIGHTGUI::initialize()")
     return
 
 # 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
 def OnGUIEvent(commandID):
-    if verbose() : print "PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID
-    if dict_command.has_key( commandID ):
+    if verbose() : print("PYLIGHTGUI::OnGUIEvent : commandID = %d" % commandID)
+    if commandID in dict_command:
         try:
             dict_command[commandID]()
         except:
             traceback.print_exc()
     else:
-       if verbose() : print "The command is not implemented: %d" % commandID
+       if verbose() : print("The command is not implemented: %d" % commandID)
     pass
 
 # Customize popup menu
 def createPopupMenu(popup, context):
-    if verbose() : print "PYLIGHTGUI.createPopupMenu(): context = %s" % 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,57 +187,53 @@ def createPopupMenu(popup, context):
 
 # For saving data in the study
 def saveFiles(prefix):
-    if verbose(): print "PYLIGHTGUI::saveFile()"
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
+    if verbose(): print("PYLIGHTGUI::saveFile()")
     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):
-    if verbose(): print "PYLIGHTGUI::openFile()"
-    ctx = _setContext( _getStudyId() )
+    global __data_model__
+    if verbose(): print("PYLIGHTGUI::openFile()")
     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")
 
     if isinstance(filename,tuple) and len(filename) >=2:
        filename = filename[0]
 
-    filename = unicode(filename)
-
     if len(filename) == 0:
         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")
 
     if isinstance(filename,tuple) and len(filename) >=2:
         filename = filename[0]
 
-    filename = unicode(filename)
-
     if filename.endswith(".txt") == 0:
         filename+=".txt"
         pass
@@ -277,7 +241,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 +253,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 +266,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 +289,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,11 +309,11 @@ 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)
-        if(obj.getText() != "\n"):
+        obj = __data_model__.getObject(entry)
+        if(obj is not None and obj.getText() != "\n"):
             #Get text line
             res = QInputDialog.getText(sgPyQt.getDesktop(),
                                        "Edit line",
@@ -369,19 +333,22 @@ 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":
+        obj = __data_model__.getObject(entry)
+        if obj is None:
+            continue
+        if obj.getText() == "\n":
             onlyLines = False
             break
         lines.append(entry)
@@ -397,18 +364,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 +383,22 @@ 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()
+    obj = ctx.DM.getObject(entry)
+    if obj is None:
+        QMessageBox.information(sgPyQt.getDesktop(),
+                                'Info',
+                                'Please, select line!')
+        return
+    text = obj.getText()
     if text == "\n":
         return
     renderer=libSalomePy.getRenderer()
@@ -440,26 +413,32 @@ def displayLine():
     txtPr = vtk.vtkTextProperty()
     txtPr.SetFontSize(30)
     actor.SetTextProperty(txtPr)
-    for act in  dict_actors.values():
+    for act in  list(dict_actors.values()):
         renderer.RemoveActor(act)
     renderer.AddActor(actor)
     pass
 
 # 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()
+    obj = __data_model__.getObject(entry)
+    if obj is None:
+        QMessageBox.information(sgPyQt.getDesktop(),
+                                'Info',
+                                'Please, select line!')
+        return
+    text = obj.getText()
     if text == "\n":
         return
     renderer=libSalomePy.getRenderer()
@@ -472,7 +451,7 @@ def eraseLine():
 # Return vtkActor by entry
 def getActor(entry):
     entry = str(entry)
-    if dict_actors.has_key(entry):
+    if entry in dict_actors:
         return dict_actors[entry]
     return None
 
index c1b52797e5c8c6c56513ff1445767b0cd7859027..37ba1b3132e90372b7c7c0230b60ab5546070866 100644 (file)
@@ -85,32 +85,32 @@ class PYLIGHT_DataModel:
         '''
         Read text file and publish it.
         '''
-        aFile = open(str(filename),"r")
-        lines = aFile.readlines()
-        if(lines[0] != "\n"):
-            paragr = self.createObject()
-        for line in lines:
-            if line == "\n":
+        with open(str(filename), "r") as aFile:
+            lines = aFile.readlines()
+            if(lines[0] != "\n"):
                 paragr = self.createObject()
+            for line in lines:
+                if line == "\n":
+                    paragr = self.createObject()
 
-            else:
-                self.createObject(processText(line), paragr)
+                else:
+                    self.createObject(processText(line), paragr)
+                    pass
                 pass
             pass
-        aFile.close()
         pass
     
     def saveFile(self, filename):
-        aFile = open(str(filename),"w")
-        paragrs = self.getParagraphs()
-        for paragr in paragrs:
-            aFile.write("\n")
-            lines = sgPyQt.getChildren(paragr)
-            for line in lines:
-                aFile.write(str(sgPyQt.getName(line))+"\n")
+        with open(str(filename), "w") as aFile:
+            paragrs = self.getParagraphs()
+            for paragr in paragrs:
+                aFile.write("\n")
+                lines = sgPyQt.getChildren(paragr)
+                for line in lines:
+                    aFile.write(str(sgPyQt.getName(line)) + "\n")
+                    pass
                 pass
             pass
-        aFile.close()
         pass
     pass