PLCControler.py
changeset 56 7187e1c00975
parent 53 4988262d03e3
child 57 9bf197698af0
--- a/PLCControler.py	Wed Jul 25 10:06:29 2007 +0200
+++ b/PLCControler.py	Thu Jul 26 17:23:21 2007 +0200
@@ -162,6 +162,7 @@
         self.VerifyXML = False
         self.Project = None
         self.ProjectBuffer = None
+        self.Buffering = False
         self.FilePath = ""
         self.FileName = ""
         self.ProgramFilePath = ""
@@ -187,16 +188,10 @@
         # Create the project
         self.Project = plcopen.project()
         self.Project.setName(name)
+        self.SetFilePath("")
         # Initialize the project buffer
-        #self.ProjectBuffer = UndoBuffer(self.Copy(self.Project))
-    
-    # Change project name
-    def SetProjectName(self, name):
-        self.Project.setName(name)
-    
-    # Return project name
-    def GetProjectName(self):
-        return self.Project.getName()
+        self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), False)
+        self.Buffering = False
     
     # Return project pou names
     def GetProjectPouNames(self):
@@ -219,6 +214,10 @@
                         variables.append(action.getName())
         return variables
     
+    # Return if project is saved
+    def ProjectIsSaved(self):
+        return self.ProjectBuffer.IsCurrentSaved()
+    
     # Return file path if project is an open file
     def GetFilePath(self):
         return self.FilePath
@@ -244,12 +243,19 @@
             self.FileName = os.path.splitext(os.path.basename(filepath))[0]
     
     # Change project properties
-    def SetProjectProperties(self, values):
-        self.Project.setFileHeader(values)
-    
+    def SetProjectProperties(self, name = None, properties = None):
+        if name != None:
+            self.Project.setName(name)
+        if properties != None:
+            self.Project.setFileHeader(properties)
+        if name != None or properties != None:
+            self.BufferProject()
+            
     # Return project properties
     def GetProjectProperties(self):
-        return self.Project.getFileHeader()
+        properties = self.Project.getFileHeader()
+        properties["projectName"] = self.Project.getName()
+        return properties
     
     # Return project informations
     def GetProjectInfos(self):
@@ -376,15 +382,15 @@
 
     def GenerateProgram(self, filepath):
         if self.Project:
-            #try:
-            program = GenerateCurrentProgram(self.Project)
-            programfile = open(filepath, "w")
-            programfile.write(program)
-            programfile.close()
-            self.ProgramFilePath = filepath
-            return True
-            #except:
-            #    pass
+            try:
+                program = GenerateCurrentProgram(self.Project)
+                programfile = open(filepath, "w")
+                programfile.write(program)
+                programfile.close()
+                self.ProgramFilePath = filepath
+                return True
+            except:
+                pass
         return False
 
 #-------------------------------------------------------------------------------
@@ -397,6 +403,7 @@
         self.Project.appendPou(name, pou_type, body_type)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Remove a pou from project
     def ProjectRemovePou(self, name):
@@ -418,40 +425,47 @@
         self.Project.removePou(name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Add a configuration to Project
     def ProjectAddConfiguration(self, name):
         self.Project.addConfiguration(name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Remove a configuration from project
     def ProjectRemoveConfiguration(self, name):
         self.Project.removeConfiguration(name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Add a resource to a configuration of the Project
     def ProjectAddConfigurationResource(self, config, name):
         self.Project.addConfigurationResource(config, name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Remove a resource from a configuration of the project
     def ProjectRemoveConfigurationResource(self, config, name):
         self.Project.removeConfigurationResource(config, name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Add a Transition to a Project Pou
     def ProjectAddPouTransition(self, pou_name, transition_name, transition_type):
         pou = self.Project.getPou(pou_name)
         pou.addTransition(transition_name, transition_type)
+        self.BufferProject()
     
     # Add a Transition to a Project Pou
     def ProjectAddPouAction(self, pou_name, action_name, action_type):
         pou = self.Project.getPou(pou_name)
         pou.addAction(action_name, action_type)
+        self.BufferProject()
         
     # Change the name of a pou
     def ChangePouName(self, old_name, new_name):
@@ -464,6 +478,7 @@
             self.ElementsOpened[idx] = new_name
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Change the name of a pou transition
     def ChangePouTransitionName(self, pou_name, old_name, new_name):
@@ -477,6 +492,7 @@
         if old_computedname in self.ElementsOpened:
             idx = self.ElementsOpened.index(old_computedname)
             self.ElementsOpened[idx] = new_computedname
+        self.BufferProject()
     
     # Change the name of a pou action
     def ChangePouActionName(self, pou_name, old_name, new_name):
@@ -490,7 +506,8 @@
         if old_computedname in self.ElementsOpened:
             idx = self.ElementsOpened.index(old_computedname)
             self.ElementsOpened[idx] = new_computedname
-
+        self.BufferProject()
+    
     # Change the name of a pou action
     def ChangePouVariableName(self, pou_name, old_name, new_name):
         # Found the pou action corresponding to old name and change its name to new name
@@ -500,6 +517,7 @@
                 if var.getName() == old_name:
                     var.setName(new_name)
         self.RefreshBlockTypes()
+        self.BufferProject()
         
     # Change the name of a configuration
     def ChangeConfigurationName(self, old_name, new_name):
@@ -509,7 +527,8 @@
         # If configuration is currently opened, change its name in the list of opened elements
         for idx, element in enumerate(self.ElementsOpened):
             self.ElementsOpened[idx] = element.replace(old_name, new_name)
-
+        self.BufferProject()
+    
     # Change the name of a configuration resource
     def ChangeConfigurationResourceName(self, config_name, old_name, new_name):
         # Found the resource corresponding to old name and change its name to new name
@@ -521,6 +540,7 @@
         if old_computedname in self.ElementsOpened:
             idx = self.ElementsOpened.index(old_computedname)
             self.ElementsOpened[idx] = new_computedname
+        self.BufferProject()
     
     # Return the type of the pou given by its name
     def GetPouType(self, name):
@@ -579,11 +599,13 @@
     def ProjectRemovePouTransition(self, pou_name, transition_name):
         pou = self.Project.getPou(pou_name)
         pou.removeTransition(transition_name)
+        self.BufferProject()
     
     # Add a Transition to a Project Pou
     def ProjectRemovePouAction(self, pou_name, action_name):
         pou = self.Project.getPou(pou_name)
         pou.removeAction(action_name)
+        self.BufferProject()
     
     # Extract varlists from a list of vars
     def ExtractVarLists(self, vars):
@@ -627,7 +649,8 @@
             for vartype, varlist in self.ExtractVarLists(vars):
                 configuration.globalVars.append(varlist)
         self.RefreshBlockTypes()
-
+        self.BufferProject()
+    
     # Return the configuration globalvars
     def GetConfigurationGlobalVars(self, name):
         vars = []
@@ -669,7 +692,8 @@
             for vartype, varlist in self.ExtractVarLists(vars):
                 resource.globalVars.append(varlist)
         self.RefreshBlockTypes()
-
+        self.BufferProject()
+    
     # Return the resource globalvars
     def GetConfigurationResourceGlobalVars(self, config_name, name):
         vars = []
@@ -745,7 +769,8 @@
         # Set Pou interface
         pou.setVars(self.ExtractVarLists(vars))
         self.RefreshBlockTypes()
-
+        self.BufferProject()
+    
     # Replace the return type of the pou given by its name (only for functions)
     def SetPouInterfaceReturnType(self, name, type):
         pou = self.Project.getPou(name)
@@ -759,6 +784,7 @@
         # Change return type
         return_type.setValue(type)
         self.RefreshBlockTypes()
+        self.BufferProject()
     
     # Return the return type of the pou given by its name
     def GetPouInterfaceReturnTypeByName(self, name):
@@ -1951,8 +1977,9 @@
         
         self.Project = plcopen.project()
         self.Project.loadXMLTree(tree.childNodes[0])
-        self.UndoBuffer = UndoBuffer(self.Copy(self.Project), True)
         self.SetFilePath(filepath)
+        self.ProjectBuffer = UndoBuffer(self.Copy(self.Project), True)
+        self.Buffering = False
         self.ElementsOpened = []
         self.CurrentElementEditing = None
         self.RefreshPouUsingTree()
@@ -1982,7 +2009,7 @@
                 xmlfile = open(self.FilePath,"w")
             xmlfile.write(text)
             xmlfile.close()
-            #self.ProjectBuffer.CurrentSaved()
+            self.ProjectBuffer.CurrentSaved()
             if filepath:
                 self.SetFilePath(filepath)
             return True
@@ -1998,18 +2025,25 @@
         return cPickle.loads(cPickle.dumps(model))
 
     def BufferProject(self):
-        self.ProjectBuffer.Buffering(self.Copy(self))
+        self.ProjectBuffer.Buffering(self.Copy(self.Project))
+
+    def StartBuffering(self):
+        self.ProjectBuffer.Buffering(self.Project)
+        self.Buffering = True
+        
+    def EndBuffering(self):
+        if self.Buffering:
+            self.Project = self.Copy(self.Project)
+            self.Buffering = False
 
     def ProjectIsSaved(self):
         return self.ProjectBuffer.IsCurrentSaved()
 
     def LoadPrevious(self):
         self.Project = self.Copy(self.ProjectBuffer.Previous())
-        self.RefreshElementsOpened()
     
     def LoadNext(self):
         self.Project = self.Copy(self.ProjectBuffer.Next())
-        self.RefreshElementsOpened()
     
     def GetBufferState(self):
         first = self.ProjectBuffer.IsFirst()