Disabling location definition for enumerated and structure variables
authorlaurent
Wed, 07 Sep 2011 15:53:28 +0200
changeset 552 a387f258814a
parent 551 ad74e44f55d9
child 553 1e1b632c7a41
Disabling location definition for enumerated and structure variables
Adding support for selecting enumerated variable initial value using choice cell editor
PLCControler.py
VariablePanel.py
plcopen/plcopen.py
--- a/PLCControler.py	Fri Sep 02 18:16:58 2011 +0200
+++ b/PLCControler.py	Wed Sep 07 15:53:28 2011 +0200
@@ -1298,7 +1298,7 @@
         return blocktypes
 
     # Return Data Types checking for recursion
-    def GetDataTypes(self, tagname = "", basetypes = True, debug = False):
+    def GetDataTypes(self, tagname = "", basetypes = True, only_locatables = False, debug = False):
         if basetypes:
             datatypes = self.GetBaseTypes()
         else:
@@ -1309,7 +1309,7 @@
             words = tagname.split("::")
             if words[0] in ["D"]:
                 name = words[1]
-            datatypes.extend(project.GetCustomDataTypes(name))
+            datatypes.extend(project.GetCustomDataTypes(name, only_locatables))
         return datatypes
 
     # Return Base Type of given possible derived type
@@ -1345,6 +1345,23 @@
             return not type.startswith("ANY")
         return True
 
+    def IsLocatableType(self, type, debug = False):
+        project = self.GetProject(debug)
+        if project is not None:
+            datatype = project.getdataType(type)
+            if datatype is not None:
+                return project.IsLocatableType(datatype)
+        return True
+    
+    def IsEnumeratedType(self, type, debug = False):
+        project = self.GetProject(debug)
+        if project is not None:
+            datatype = project.getdataType(type)
+            if datatype is not None:
+                basetype_content = datatype.baseType.getcontent()
+                return basetype_content["name"] == "enum"
+        return False
+
     def GetDataTypeRange(self, type, debug = False):
         project = self.GetProject(debug)
         if project is not None:
@@ -1361,10 +1378,10 @@
         return []
     
     # Return Enumerated Values
-    def GetEnumeratedDataValues(self, debug = False):
-        project = self.GetProject(debug)
-        if project is not None:
-            return project.GetEnumeratedDataTypeValues()
+    def GetEnumeratedDataValues(self, type = None, debug = False):
+        project = self.GetProject(debug)
+        if project is not None:
+            return project.GetEnumeratedDataTypeValues(type)
         return []
 
 #-------------------------------------------------------------------------------
@@ -1440,7 +1457,7 @@
                     for dimension in basetype_content["value"].getdimension():
                         infos["dimensions"].append((dimension.getlower(), dimension.getupper()))
                     base_type = basetype_content["value"].baseType.getcontent()
-                    if base_type["value"] is None or element_type["name"] in ["string", "wstring"]:
+                    if base_type["value"] is None or base_type["name"] in ["string", "wstring"]:
                         infos["base_type"] = base_type["name"].upper()
                     else:
                         infos["base_type"] = base_type["value"].getname()
--- a/VariablePanel.py	Fri Sep 02 18:16:58 2011 +0200
+++ b/VariablePanel.py	Wed Sep 07 15:53:28 2011 +0200
@@ -199,6 +199,7 @@
         """
         for row in range(self.GetNumberRows()):
             var_class = self.GetValueByName(row, "Class")
+            var_type = self.GetValueByName(row, "Type")
             for col in range(self.GetNumberCols()):
                 editor = None
                 renderer = None
@@ -222,12 +223,16 @@
                             renderer = wx.grid.GridCellStringRenderer()
                     elif colname == "Initial Value":
                         if var_class != "External":
-                            editor = wx.grid.GridCellTextEditor()
+                            if self.Parent.Controler.IsEnumeratedType(var_type):
+                                editor = wx.grid.GridCellChoiceEditor()
+                                editor.SetParameters(",".join(self.Parent.Controler.GetEnumeratedDataValues(var_type)))
+                            else:
+                                editor = wx.grid.GridCellTextEditor()
                             renderer = wx.grid.GridCellStringRenderer()
                         else:
                             grid.SetReadOnly(row, col, True)
                     elif colname == "Location":
-                        if var_class in ["Local", "Global"]:
+                        if var_class in ["Local", "Global"] and self.Parent.Controler.IsLocatableType(var_type):
                             editor = LocationCellEditor(self, self.Parent.Controler)
                             renderer = wx.grid.GridCellStringRenderer()
                         else:
@@ -240,7 +245,7 @@
                             excluded = []
                             if self.Parent.PouIsUsed:
                                 excluded.extend(["Input","Output","InOut"])
-                            if self.Parent.IsFunctionBlockType(self.data[row]["Type"]):
+                            if self.Parent.IsFunctionBlockType(var_type):
                                 excluded.extend(["Local","Temp"])
                             editor.SetParameters(",".join([_(choice) for choice in self.Parent.ClassList if choice not in excluded]))
                 elif colname != "Documentation":
--- a/plcopen/plcopen.py	Fri Sep 02 18:16:58 2011 +0200
+++ b/plcopen/plcopen.py	Wed Sep 07 15:53:28 2011 +0200
@@ -649,15 +649,34 @@
     setattr(cls, "GetCustomBlockResource", GetCustomBlockResource)
 
     # Return Data Types checking for recursion
-    def GetCustomDataTypes(self, exclude = ""):
+    def GetCustomDataTypes(self, exclude = "", only_locatable = False):
         customdatatypes = []
         for customdatatype in self.getdataTypes():
-            customdatatype_name = customdatatype.getname()
-            if customdatatype_name != exclude and not self.ElementIsUsedBy(exclude, customdatatype_name):
-                customdatatypes.append(customdatatype_name)
+            if not only_locatable or self.IsLocatableType(customdatatype):
+                customdatatype_name = customdatatype.getname()
+                if customdatatype_name != exclude and not self.ElementIsUsedBy(exclude, customdatatype_name):
+                    customdatatypes.append(customdatatype_name)
         return customdatatypes
     setattr(cls, "GetCustomDataTypes", GetCustomDataTypes)
 
+    # Return if Data Type can be used for located variables
+    def IsLocatableType(self, datatype):
+        basetype_content = datatype.baseType.getcontent()
+        if basetype_content["name"] in ["enum", "struct"]:
+            return False
+        elif basetype_content["name"] == "derived":
+            base_type = self.getdataType(basetype_content["value"].getname())
+            if base_type is not None:
+                return self.IsLocatableType(base_type)
+        elif basetype_content["name"] == "array":
+            array_base_type = basetype_content["value"].baseType.getcontent()
+            if array_base_type["value"] is not None and array_base_type["name"] not in ["string", "wstring"]:
+                base_type = self.getdataType(array_base_type["value"].getname())
+                if base_type is not None:
+                    return self.IsLocatableType(base_type)
+        return True
+    setattr(cls, "IsLocatableType", IsLocatableType)
+
 cls = PLCOpenClasses.get("project_fileHeader", None)
 if cls:
     cls.singleLineAttributes = False