merged rename_on_location_change branch
authorEdouard Tisserant
Thu, 04 Apr 2019 14:00:17 +0200
changeset 2576 c08bbfe7bbd4
parent 2572 9622418ac28c (current diff)
parent 2575 aee08bd135d8 (diff)
child 2578 eed31489c909
child 2581 20eb4e7a0647
merged rename_on_location_change branch
--- a/controls/LocationCellEditor.py	Thu Mar 28 14:06:53 2019 +0100
+++ b/controls/LocationCellEditor.py	Thu Apr 04 14:00:17 2019 +0200
@@ -60,6 +60,7 @@
         self.Controller = None
         self.VarType = None
         self.Default = False
+        self.VariableName = None
 
     def __del__(self):
         self.Controller = None
@@ -75,11 +76,16 @@
 
     def SetValue(self, value):
         self.Default = value
+        self.VariableName = None
+        self.VarType = None
         self.Location.SetValue(value)
 
     def GetValue(self):
         return self.Location.GetValue()
 
+    def GetName(self):
+        return self.VariableName
+    
     def OnSize(self, event):
         self.Layout()
 
@@ -118,8 +124,13 @@
                     location = "%M" + location
 
             self.Location.SetValue(location)
+            self.VariableName = infos["var_name"]
             self.VarType = infos["IEC_type"]
 
+            # when user selected something, end editing immediately
+            # so that changes over multiple colums appear
+            wx.CallAfter(self.Parent.Parent.CloseEditControl)
+
         self.Location.SetFocus()
 
     def OnLocationChar(self, event):
@@ -171,8 +182,22 @@
         loc = self.CellControl.GetValue()
         changed = loc != old_loc
         if changed:
+            name = self.CellControl.GetName()
+            if name is not None:
+                message = self.Table.Parent.CheckVariableName(name, row)
+                if message is not None:
+                    wx.CallAfter(self.Table.Parent.ShowErrorMessage, message)
+                    return None
+                old_name  = self.Table.GetValueByName(row, 'Name')
+                self.Table.SetValueByName(row, 'Name', name)
+                self.Table.Parent.OnVariableNameChange(old_name, name)
             self.Table.SetValueByName(row, 'Location', loc)
-            self.Table.SetValueByName(row, 'Type', self.CellControl.GetVarType())
+            var_type = self.CellControl.GetVarType()
+            if var_type is not None:
+                self.Table.SetValueByName(row, 'Type', var_type)
+        else:
+            wx.CallAfter(self.Table.Parent.ShowErrorMessage,
+                _("Selected location is identical to previous one"))
         self.CellControl.Disable()
         return changed
 
--- a/controls/VariablePanel.py	Thu Mar 28 14:06:53 2019 +0100
+++ b/controls/VariablePanel.py	Thu Apr 04 14:00:17 2019 +0200
@@ -34,6 +34,7 @@
 from six import string_types
 from six.moves import xrange
 
+
 from plcopen.structures import LOCATIONDATATYPES, TestIdentifier, IEC_KEYWORDS, DefaultType
 from plcopen.VariableInfoCollector import _VariableInfos
 from graphics.GraphicCommons import REFRESH_HIGHLIGHT_PERIOD, ERROR_HIGHLIGHT
@@ -150,6 +151,7 @@
             return value
 
     def SetValue(self, row, col, value):
+        print("SetValue",row, col, value)
         if col < len(self.colnames):
             colname = self.GetColLabelValue(col, False)
             if colname == "Name":
@@ -788,6 +790,27 @@
         dialog.ShowModal()
         dialog.Destroy()
 
+    def OnVariableNameChange(self, old_name, new_name):
+        """ propagate renaming of variable to the rest of the project """
+        if old_name != "":
+            self.Controler.UpdateEditedElementUsedVariable(self.TagName, old_name, new_name)
+        self.Controler.BufferProject()
+        wx.CallAfter(self.ParentWindow.RefreshView, False)
+        self.ParentWindow._Refresh(TITLE, FILEMENU, EDITMENU, PAGETITLES, POUINSTANCEVARIABLESPANEL, LIBRARYTREE)
+
+    def CheckVariableName(self, value, row):
+        if not TestIdentifier(value):
+            message = _("\"%s\" is not a valid identifier!") % value
+        elif value.upper() in IEC_KEYWORDS:
+            message = _("\"%s\" is a keyword. It can't be used!") % value
+        elif value.upper() in self.PouNames:
+            message = _("A POU named \"%s\" already exists!") % value
+        elif value.upper() in [var.Name.upper() for var in self.Values if var != self.Table.data[row]]:
+            message = _("A variable with \"%s\" as name already exists in this pou!") % value
+        else:
+            return None
+        return message
+
     def OnVariablesGridCellChange(self, event):
         row, col = event.GetRow(), event.GetCol()
         colname = self.Table.GetColLabelValue(col, False)
@@ -795,22 +818,11 @@
         message = None
 
         if colname == "Name" and value != "":
-            if not TestIdentifier(value):
-                message = _("\"%s\" is not a valid identifier!") % value
-            elif value.upper() in IEC_KEYWORDS:
-                message = _("\"%s\" is a keyword. It can't be used!") % value
-            elif value.upper() in self.PouNames:
-                message = _("A POU named \"%s\" already exists!") % value
-            elif value.upper() in [var.Name.upper() for var in self.Values if var != self.Table.data[row]]:
-                message = _("A variable with \"%s\" as name already exists in this pou!") % value
-            else:
+            message = self.CheckVariableName(value, row)
+            if message is None:
                 self.SaveValues(False)
                 old_value = self.Table.GetOldValue()
-                if old_value != "":
-                    self.Controler.UpdateEditedElementUsedVariable(self.TagName, old_value, value)
-                self.Controler.BufferProject()
-                wx.CallAfter(self.ParentWindow.RefreshView, False)
-                self.ParentWindow._Refresh(TITLE, FILEMENU, EDITMENU, PAGETITLES, POUINSTANCEVARIABLESPANEL, LIBRARYTREE)
+                self.OnVariableNameChange(old_value, value)
         else:
             self.SaveValues()
             if colname == "Class":
--- a/dialogs/BrowseLocationsDialog.py	Thu Mar 28 14:06:53 2019 +0100
+++ b/dialogs/BrowseLocationsDialog.py	Thu Apr 04 14:00:17 2019 +0200
@@ -71,7 +71,7 @@
 
         self.DIRFILTERCHOICE_OPTIONS = dict(
             [(_(option), filter) for option, filter in GetDirFilterChoiceOptions()])
-        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
+        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10)
         main_sizer.AddGrowableCol(0)
         main_sizer.AddGrowableRow(1)
 
@@ -91,6 +91,15 @@
         main_sizer.AddWindow(self.LocationsTree, border=20,
                              flag=wx.LEFT | wx.RIGHT | wx.GROW)
 
+        self.RenameCheckBox = wx.CheckBox(self, label=_("Rename variable to signal name"))
+        self.Config = wx.ConfigBase.Get()
+        default_checked = self.Config.Read("RenameVariableOnLocationChange") == "True"
+        self.RenameCheckBox.SetValue(default_checked)
+        self.do_rename = default_checked
+
+        main_sizer.AddWindow(self.RenameCheckBox, border=20,
+                             flag=wx.LEFT | wx.RIGHT | wx.GROW)
+
         button_gridsizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0)
         button_gridsizer.AddGrowableCol(1)
         button_gridsizer.AddGrowableCol(3)
@@ -217,9 +226,14 @@
 
     def GetValues(self):
         selected = self.LocationsTree.GetSelection()
-        return self.LocationsTree.GetPyData(selected)
+        infos = self.LocationsTree.GetPyData(selected)
+        if not self.do_rename:
+            infos["var_name"] = None
+        return infos
 
     def OnOK(self, event):
+        self.do_rename = self.RenameCheckBox.IsChecked()
+        self.Config.Write("RenameVariableOnLocationChange", str(self.do_rename))
         selected = self.LocationsTree.GetSelection()
         var_infos = None
         if selected.IsOk():