SFCViewer.py
changeset 9 b29105e29081
parent 5 f8652b073e84
child 27 dae55dd9ee14
--- a/SFCViewer.py	Wed Apr 11 17:26:07 2007 +0200
+++ b/SFCViewer.py	Thu Apr 12 17:26:07 2007 +0200
@@ -329,7 +329,10 @@
 #-------------------------------------------------------------------------------
 
     def AddInitialStep(self, pos):
-        dialog = wxTextEntryDialog(self.Parent, "Add a new initial step", "Please enter step name", "", wxOK|wxCANCEL)
+        dialog = StepNameDialog(self.Parent, "Add a new initial step", "Please enter step name", "", wxOK|wxCANCEL)
+        dialog.SetPouNames(self.Controler.GetProjectPouNames())
+        dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
+        dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
         if dialog.ShowModal() == wxID_OK:
             id = self.GetNewId()
             name = dialog.GetValue()
@@ -348,7 +351,10 @@
 
     def AddStep(self):
         if self.SelectedElement in self.Wires or isinstance(self.SelectedElement, SFC_Step):
-            dialog = wxTextEntryDialog(self.Parent, "Add a new step", "Please enter step name", "", wxOK|wxCANCEL)
+            dialog = StepNameDialog(self.Parent, "Add a new step", "Please enter step name", "", wxOK|wxCANCEL)
+            dialog.SetPouNames(self.Controler.GetProjectPouNames())
+            dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
+            dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
             if dialog.ShowModal() == wxID_OK:
                 name = dialog.GetValue()
                 if self.SelectedElement in self.Wires:
@@ -688,7 +694,10 @@
         dialog.Destroy()
 
     def EditStepContent(self, step):
-        dialog = wxTextEntryDialog(self.Parent, "Edit step name", "Please enter step name", step.GetName(), wxOK|wxCANCEL)
+        dialog = StepNameDialog(self.Parent, "Edit step name", "Please enter step name", step.GetName(), wxOK|wxCANCEL)
+        dialog.SetPouNames(self.Controler.GetProjectPouNames())
+        dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
+        dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step) and block.GetName() != step.GetName()])
         if dialog.ShowModal() == wxID_OK:
             value = dialog.GetValue()
             step.SetName(value)
@@ -1069,7 +1078,7 @@
 
     def __init__(self, parent):
         self._init_ctrls(parent)
-        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
+        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
         
         EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
@@ -1221,7 +1230,7 @@
 
     def __init__(self, parent):
         self._init_ctrls(parent)
-        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
+        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
         
         self.Divergence = None
@@ -1508,7 +1517,7 @@
 
     def __init__(self, parent):
         self._init_ctrls(parent)
-        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
+        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
         
         self.DefaultValue = {"Qualifier" : "N", "Duration" : "", "Type" : "Action", "Value" : "", "Indicator" : ""}
@@ -1609,3 +1618,58 @@
                 action["indicator"] = data["Indicator"]
             values.append(action)
         return values
+
+
+#-------------------------------------------------------------------------------
+#                          Edit Step Name Dialog
+#-------------------------------------------------------------------------------
+
+class StepNameDialog(wxTextEntryDialog):
+
+    def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
+                       style = wxOK|wxCANCEL|wxCENTRE, pos = wxDefaultPosition):
+        wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
+        
+        self.PouNames = []
+        self.Variables = []
+        self.StepNames = []
+        
+        EVT_BUTTON(self, self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId(), self.OnOK)
+        
+    def OnOK(self, event):
+        step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
+        if step_name == "":
+            message = wxMessageDialog(self, "You must type a name!", "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif not TestIdentifier(step_name):
+            message = wxMessageDialog(self, "\"%s\" is not a valid identifier!"%step_name, "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif step_name.upper() in IEC_KEYWORDS:
+            message = wxMessageDialog(self, "\"%s\" is a keyword. It can't be used!"%step_name, "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif step_name.upper() in self.PouNames:
+            message = wxMessageDialog(self, "A pou with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif step_name.upper() in self.Variables:
+            message = wxMessageDialog(self, "A variable with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif step_name.upper() in self.StepNames:
+            message = wxMessageDialog(self, "\"%s\" step already exists!"%step_name, "Error", wxOK|wxICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        else:
+            self.EndModal(wxID_OK)
+
+    def SetPouNames(self, pou_names):
+        self.PouNames = [pou_name.upper() for pou_name in pou_names]
+
+    def SetVariables(self, variables):
+        self.Variables = [var["Name"].upper() for var in variables]
+
+    def SetStepNames(self, step_names):
+        self.StepNames = [step_name.upper() for step_name in step_names]