Adding support for disable interface variable change if a POU is used
authorlbessard
Thu, 12 Jul 2007 11:29:11 +0200
changeset 37 256eedd275d0
parent 36 e7d67b27877f
child 38 e7cc8bea78f3
Adding support for disable interface variable change if a POU is used
Bug on String code generation fixed
PLCGenerator.py
PLCOpenEditor.py
--- a/PLCGenerator.py	Thu Jul 12 11:27:43 2007 +0200
+++ b/PLCGenerator.py	Thu Jul 12 11:29:11 2007 +0200
@@ -462,7 +462,9 @@
                 program += ": %s"%var_type
                 if var_initial != None:
                     value = {"TRUE":"0","FALSE":"1"}.get(str(var_initial).upper(), str(var_initial))
-                    if var_type in ["STRING", "WSTRING"]:
+                    if var_type == "STRING":
+                        program += " := '%s'"%value
+                    elif var_type == "WSTRING":
                         program += " := \"%s\""%value
                     else:
                         program += " := %s"%value
@@ -493,7 +495,9 @@
                 if initial:
                     value = str(initial.getValue())
                     value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
-                    if var_type in ["STRING", "WSTRING"]:
+                    if var_type == "STRING":
+                        config += " := '%s'"%value
+                    elif var_type == "WSTRING":
                         config += " := \"%s\""%value
                     else:
                         config += " := %s"%value
@@ -524,7 +528,9 @@
                 if initial:
                     value = str(initial.getValue())
                     value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
-                    if var_type in ["STRING", "WSTRING"]:
+                    if var_type == "STRING":
+                        resrce += " := '%s'"%value
+                    elif var_type == "WSTRING":
                         resrce += " := \"%s\""%value
                     else:
                         resrce += " := %s"%value
--- a/PLCOpenEditor.py	Thu Jul 12 11:27:43 2007 +0200
+++ b/PLCOpenEditor.py	Thu Jul 12 11:29:11 2007 +0200
@@ -884,32 +884,27 @@
                 item_type = self.ProjectTree.GetPyData(item)
             item_name = self.ProjectTree.GetItemText(item)
             if item_type == ITEM_POU:
-                dialog = EditVariableDialog(self, item_name, self.Controler.GetPouType(item_name), name)
+                dialog = EditVariableDialog(self, item_name, self.Controler.GetPouType(item_name), self.Controler.PouIsUsed(item_name), name)
                 dialog.SetPouNames(self.Controler.GetProjectPouNames())
                 values = {}
                 values["returnType"] = self.Controler.GetPouInterfaceReturnTypeByName(item_name)
                 values["data"] = self.Controler.GetPouInterfaceVarsByName(item_name)
                 dialog.SetValues(values)
                 if dialog.ShowModal() == wxID_OK:
-                    if not self.Controler.PouIsUsed(item_name):
-                        new_values = dialog.GetValues()
-                        if "returnType" in new_values:
-                            self.Controler.SetPouInterfaceReturnType(item_name, new_values["returnType"])
-                        self.Controler.SetPouInterfaceVars(item_name, new_values["data"])
-                        pou_names = self.Controler.GetElementsOpenedNames()
-                        if item_name in pou_names:
-                            window = self.TabsOpened.GetPage(pou_names.index(item_name))
-                            if isinstance(window, TextViewer):
-                                varlist = []
-                                if "returnType" in new_values:
-                                    varlist.append(name)
-                                for var in new_values["data"]:
-                                    varlist.append(var["Name"])
-                                window.SetVariables(varlist)
-                    else:
-                        message = wxMessageDialog(self, "\"%s\" is used by one or more POUs. Its interface can't be changed!"%pou_name, "Error", wxOK|wxICON_ERROR)
-                        message.ShowModal()
-                        message.Destroy()
+                    new_values = dialog.GetValues()
+                    if "returnType" in new_values:
+                        self.Controler.SetPouInterfaceReturnType(item_name, new_values["returnType"])
+                    self.Controler.SetPouInterfaceVars(item_name, new_values["data"])
+                    pou_names = self.Controler.GetElementsOpenedNames()
+                    if item_name in pou_names:
+                        window = self.TabsOpened.GetPage(pou_names.index(item_name))
+                        if isinstance(window, TextViewer):
+                            varlist = []
+                            if "returnType" in new_values:
+                                varlist.append(name)
+                            for var in new_values["data"]:
+                                varlist.append(var["Name"])
+                            window.SetVariables(varlist)
                 dialog.Destroy()
                 self.RefreshProjectTree()
             elif item_type == ITEM_CONFIGURATION:
@@ -1906,18 +1901,30 @@
                 renderer = None
                 colname = self.GetColLabelValue(col)
                 grid.SetReadOnly(row, col, False)
-                if colname in ["Name","Initial Value","Location"]:
-                    editor = wxGridCellTextEditor()
-                    renderer = wxGridCellStringRenderer()    
-                elif colname == "Class":
-                    if len(self.Parent.ClassList) == 1:
+                if colname == "Name":
+                    if self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]:
                         grid.SetReadOnly(row, col, True)
                     else:
-                        editor = wxGridCellChoiceEditor()    
-                        editor.SetParameters(",".join(self.Parent.ClassList))
+                        editor = wxGridCellTextEditor()
+                        renderer = wxGridCellStringRenderer()
+                if colname in ["Initial Value","Location"]:
+                    editor = wxGridCellTextEditor()
+                    renderer = wxGridCellStringRenderer()
+                elif colname == "Class":
+                    if len(self.Parent.ClassList) == 1 or self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]:
+                        grid.SetReadOnly(row, col, True)
+                    else:
+                        editor = wxGridCellChoiceEditor()
+                        excluded = []
+                        if self.Parent.PouIsUsed:
+                            excluded.extend(["Input","Output","InOut"])    
+                        editor.SetParameters(",".join([choice for choice in self.Parent.ClassList if choice not in excluded]))
                 elif colname == "Type":
-                    editor = wxGridCellChoiceEditor()
-                    editor.SetParameters(self.Parent.TypeList)
+                    if self.Parent.PouIsUsed and self.GetValueByName(row, "Class") in ["Input", "Output", "InOut"]:
+                        grid.SetReadOnly(row, col, True)
+                    else:
+                        editor = wxGridCellChoiceEditor()
+                        editor.SetParameters(self.Parent.TypeList)
                 elif colname in ["Retain", "Constant"]:
                     editor = wxGridCellChoiceEditor()
                     editor.SetParameters(self.Parent.OptionList)
@@ -2018,6 +2025,7 @@
         self.VariablesGrid.DisableDragGridSize()
         self.VariablesGrid.EnableScrolling(False, True)
         self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnVariablesGridCellChange)
+        self.VariablesGrid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnVariablesGridSelectCell)
 
         self.AddButton = wx.Button(id=wxID_EDITVARIABLEDIALOGADDBUTTON, label='Add',
               name='AddButton', parent=self.MainPanel, pos=wx.Point(345, 340),
@@ -2041,19 +2049,20 @@
 
         self._init_sizers()
 
-    def __init__(self, parent, name, pou_type, filter = "All"):
+    def __init__(self, parent, name, pou_type, pou_is_used, filter = "All"):
         self._init_ctrls(parent, name)
         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
         self.Filter = filter
+        self.PouIsUsed = pou_is_used
         self.FilterChoices = []
         self.FilterChoiceTransfer = {"All" : "All", "Interface" : "Interface", 
-            "Input" : "   Input", "Output" : "   Output", "InOut" : "   InOut", 
-            "External" : "   External", "Variables" : "Variables", "Local" : "   Local",
-            "Temp" : "   Temp", "Global" : "Global", "Access" : "Access"}
+            "   Input" : "Input", "   Output" : "Output", "   InOut" : "InOut", 
+            "   External" : "External", "Variables" : "Variables", "   Local" : "Local",
+            "   Temp" : "Temp", "Global" : "Global", "Access" : "Access"}
         
         if pou_type:
-            self.DefaultTypes = {"All" : "Input", "Interface" : "Input", "Variables" : "Local"}
+            self.DefaultTypes = {"All" : "Local", "Interface" : "Input", "Variables" : "Local"}
             self.DefaultValue = {"Name" : "", "Class" : "", "Type" : "INT", "Location" : "", "Initial Value" : "", "Retain" : "No", "Constant" : "No"}
         else:
             self.DefaultTypes = {"All" : "Global"}
@@ -2073,9 +2082,12 @@
             self.ColAlignements = [wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_CENTER, wxALIGN_CENTER]
         for choice in self.FilterChoices:
             self.ClassFilter.Append(choice)
-        self.ClassFilter.SetStringSelection(self.FilterChoiceTransfer[self.Filter])
+        reverse_transfer = {}
+        for filter, choice in self.FilterChoiceTransfer.items():
+            reverse_transfer[choice] = filter
+        self.ClassFilter.SetStringSelection(reverse_transfer[self.Filter])
         self.RefreshTypeList()
-        self.RefreshUpDownButtons()
+        self.RefreshButtons()
 
         self.OptionList = "Yes,No"
         self.TypeList = ",".join([value for value, parent in TypeHierarchy_list if not value.startswith("ANY")])
@@ -2097,8 +2109,16 @@
 
         self.PouNames = []
 
+        if self.PouIsUsed:
+            wxCallAfter(self.WarningMessage, name)
+
         EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
     
+    def WarningMessage(self, name):
+        message = wxMessageDialog(self, "\"%s\" is used by one or more POUs. Its interface can't be changed!"%name, "WARNING", wxOK|wxICON_EXCLAMATION)
+        message.ShowModal()
+        message.Destroy()
+    
     def OnOK(self, event):
         self.VariablesGrid.SetGridCursor(0, 0)
         error = []
@@ -2120,18 +2140,15 @@
             self.EndModal(wxID_OK)
 
     def OnClassFilter(self, event):
-        reverse_transfer = {}
-        for filter, choice in self.FilterChoiceTransfer.items():
-            reverse_transfer[choice] = filter
-        self.Filter = reverse_transfer[self.ClassFilter.GetStringSelection()]
+        self.Filter = self.FilterChoiceTransfer[self.ClassFilter.GetStringSelection()]
         self.RefreshTypeList()
         self.RefreshValues()
-        self.RefreshUpDownButtons()
+        self.RefreshButtons()
         event.Skip()
 
     def RefreshTypeList(self):
         if self.Filter == "All":
-            self.ClassList = [choice for choice in self.FilterChoiceTransfer.keys() if choice not in ["All","Interface","Variables"]]
+            self.ClassList = [self.FilterChoiceTransfer[choice] for choice in self.FilterChoices if self.FilterChoiceTransfer[choice] not in ["All","Interface","Variables"]]
         elif self.Filter == "Interface":
             self.ClassList = ["Input","Output","InOut","External"]
         elif self.Filter == "Variables":
@@ -2139,14 +2156,17 @@
         else:
             self.ClassList = [self.Filter]
 
-    def RefreshUpDownButtons(self):
-        if self.Filter == "All":
-            self.UpButton.Enable(True)
-            self.DownButton.Enable(True)
-        else:
-            self.UpButton.Enable(False)
-            self.DownButton.Enable(False)
-
+    def RefreshButtons(self):
+        table_length = len(self.Table.data)
+        row_class = None
+        if table_length and self.PouIsUsed:
+            row = self.VariablesGrid.GetGridCursorRow()
+            row_class = self.Table.GetValueByName(row, "Class")
+        self.AddButton.Enable(not self.PouIsUsed or self.Filter not in ["Interface", "Input", "Output", "InOut"])
+        self.DeleteButton.Enable(table_length > 0 and row_class not in ["Input", "Output", "InOut"])
+        self.UpButton.Enable(table_length > 0 and self.Filter == "All" and row_class not in ["Input", "Output", "InOut"])
+        self.DownButton.Enable(table_length > 0 and self.Filter == "All" and row_class not in ["Input", "Output", "InOut"])
+    
     def OnAddButton(self, event):
         new_row = self.DefaultValue.copy()
         if self.Filter in self.DefaultTypes:
@@ -2155,20 +2175,24 @@
             new_row["Class"] = self.Filter
         self.Values.append(new_row)
         self.RefreshValues()
+        self.RefreshButtons()
         event.Skip()
 
     def OnDeleteButton(self, event):
         row = self.Table.GetRow(self.VariablesGrid.GetGridCursorRow())
         self.Values.remove(row)
         self.RefreshValues()
+        self.RefreshButtons()
         event.Skip()
 
     def OnUpButton(self, event):
         self.MoveValue(self.VariablesGrid.GetGridCursorRow(), -1)
+        self.RefreshButtons()
         event.Skip()
 
     def OnDownButton(self, event):
         self.MoveValue(self.VariablesGrid.GetGridCursorRow(), 1)
+        self.RefreshButtons()
         event.Skip()
 
     def OnVariablesGridCellChange(self, event):
@@ -2201,6 +2225,10 @@
         else:
             event.Skip()
 
+    def OnVariablesGridSelectCell(self, event):
+        wxCallAfter(self.RefreshButtons)
+        event.Skip()
+
     def SetPouNames(self, pou_names):
         self.PouNames = [pou_name.upper() for pou_name in pou_names]
 
@@ -2222,7 +2250,8 @@
             self.RefreshValues()
     
     def RefreshValues(self):
-        self.VariablesGrid.SetGridCursor(0, 0)
+        if len(self.Table.data) > 0:
+            self.VariablesGrid.SetGridCursor(0, 0)
         data = []
         for variable in self.Values:
             if variable["Class"] in self.ClassList: