SFCViewer.py
changeset 9 b29105e29081
parent 5 f8652b073e84
child 27 dae55dd9ee14
equal deleted inserted replaced
8:7ceec5c40d77 9:b29105e29081
   327 #-------------------------------------------------------------------------------
   327 #-------------------------------------------------------------------------------
   328 #                          Adding element functions
   328 #                          Adding element functions
   329 #-------------------------------------------------------------------------------
   329 #-------------------------------------------------------------------------------
   330 
   330 
   331     def AddInitialStep(self, pos):
   331     def AddInitialStep(self, pos):
   332         dialog = wxTextEntryDialog(self.Parent, "Add a new initial step", "Please enter step name", "", wxOK|wxCANCEL)
   332         dialog = StepNameDialog(self.Parent, "Add a new initial step", "Please enter step name", "", wxOK|wxCANCEL)
       
   333         dialog.SetPouNames(self.Controler.GetProjectPouNames())
       
   334         dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
       
   335         dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
   333         if dialog.ShowModal() == wxID_OK:
   336         if dialog.ShowModal() == wxID_OK:
   334             id = self.GetNewId()
   337             id = self.GetNewId()
   335             name = dialog.GetValue()
   338             name = dialog.GetValue()
   336             step = SFC_Step(self, name, True, id)
   339             step = SFC_Step(self, name, True, id)
   337             min_width, min_height = step.GetMinSize()
   340             min_width, min_height = step.GetMinSize()
   346             self.Refresh()
   349             self.Refresh()
   347         dialog.Destroy()
   350         dialog.Destroy()
   348 
   351 
   349     def AddStep(self):
   352     def AddStep(self):
   350         if self.SelectedElement in self.Wires or isinstance(self.SelectedElement, SFC_Step):
   353         if self.SelectedElement in self.Wires or isinstance(self.SelectedElement, SFC_Step):
   351             dialog = wxTextEntryDialog(self.Parent, "Add a new step", "Please enter step name", "", wxOK|wxCANCEL)
   354             dialog = StepNameDialog(self.Parent, "Add a new step", "Please enter step name", "", wxOK|wxCANCEL)
       
   355             dialog.SetPouNames(self.Controler.GetProjectPouNames())
       
   356             dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
       
   357             dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
   352             if dialog.ShowModal() == wxID_OK:
   358             if dialog.ShowModal() == wxID_OK:
   353                 name = dialog.GetValue()
   359                 name = dialog.GetValue()
   354                 if self.SelectedElement in self.Wires:
   360                 if self.SelectedElement in self.Wires:
   355                     self.SelectedElement.SetSelectedSegment(None)
   361                     self.SelectedElement.SetSelectedSegment(None)
   356                     previous = self.SelectedElement.EndConnected
   362                     previous = self.SelectedElement.EndConnected
   686             self.RefreshCommentModel(comment)
   692             self.RefreshCommentModel(comment)
   687             self.Refresh()
   693             self.Refresh()
   688         dialog.Destroy()
   694         dialog.Destroy()
   689 
   695 
   690     def EditStepContent(self, step):
   696     def EditStepContent(self, step):
   691         dialog = wxTextEntryDialog(self.Parent, "Edit step name", "Please enter step name", step.GetName(), wxOK|wxCANCEL)
   697         dialog = StepNameDialog(self.Parent, "Edit step name", "Please enter step name", step.GetName(), wxOK|wxCANCEL)
       
   698         dialog.SetPouNames(self.Controler.GetProjectPouNames())
       
   699         dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
       
   700         dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step) and block.GetName() != step.GetName()])
   692         if dialog.ShowModal() == wxID_OK:
   701         if dialog.ShowModal() == wxID_OK:
   693             value = dialog.GetValue()
   702             value = dialog.GetValue()
   694             step.SetName(value)
   703             step.SetName(value)
   695             min_size = step.GetMinSize()
   704             min_size = step.GetMinSize()
   696             size = step.GetSize()
   705             size = step.GetSize()
  1067 
  1076 
  1068         self._init_sizers()
  1077         self._init_sizers()
  1069 
  1078 
  1070     def __init__(self, parent):
  1079     def __init__(self, parent):
  1071         self._init_ctrls(parent)
  1080         self._init_ctrls(parent)
  1072         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
  1081         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
  1073         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1082         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1074         
  1083         
  1075         EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
  1084         EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
  1076     
  1085     
  1077     def OnOK(self, event):
  1086     def OnOK(self, event):
  1219 
  1228 
  1220         self._init_sizers()
  1229         self._init_sizers()
  1221 
  1230 
  1222     def __init__(self, parent):
  1231     def __init__(self, parent):
  1223         self._init_ctrls(parent)
  1232         self._init_ctrls(parent)
  1224         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
  1233         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
  1225         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1234         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1226         
  1235         
  1227         self.Divergence = None
  1236         self.Divergence = None
  1228         
  1237         
  1229         EVT_PAINT(self, self.OnPaint)
  1238         EVT_PAINT(self, self.OnPaint)
  1506 
  1515 
  1507         self._init_sizers()
  1516         self._init_sizers()
  1508 
  1517 
  1509     def __init__(self, parent):
  1518     def __init__(self, parent):
  1510         self._init_ctrls(parent)
  1519         self._init_ctrls(parent)
  1511         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
  1520         self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL|wxCENTRE)
  1512         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1521         self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
  1513         
  1522         
  1514         self.DefaultValue = {"Qualifier" : "N", "Duration" : "", "Type" : "Action", "Value" : "", "Indicator" : ""}
  1523         self.DefaultValue = {"Qualifier" : "N", "Duration" : "", "Type" : "Action", "Value" : "", "Indicator" : ""}
  1515         self.Table = ActionTable(self, [], ["Qualifier","Duration","Type","Value","Indicator"])
  1524         self.Table = ActionTable(self, [], ["Qualifier","Duration","Type","Value","Indicator"])
  1516         self.TypeList = "Action,Variable,Inline"
  1525         self.TypeList = "Action,Variable,Inline"
  1607                 action["duration"] = data["Duration"]
  1616                 action["duration"] = data["Duration"]
  1608             if data["Indicator"] != "":
  1617             if data["Indicator"] != "":
  1609                 action["indicator"] = data["Indicator"]
  1618                 action["indicator"] = data["Indicator"]
  1610             values.append(action)
  1619             values.append(action)
  1611         return values
  1620         return values
       
  1621 
       
  1622 
       
  1623 #-------------------------------------------------------------------------------
       
  1624 #                          Edit Step Name Dialog
       
  1625 #-------------------------------------------------------------------------------
       
  1626 
       
  1627 class StepNameDialog(wxTextEntryDialog):
       
  1628 
       
  1629     def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
       
  1630                        style = wxOK|wxCANCEL|wxCENTRE, pos = wxDefaultPosition):
       
  1631         wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
       
  1632         
       
  1633         self.PouNames = []
       
  1634         self.Variables = []
       
  1635         self.StepNames = []
       
  1636         
       
  1637         EVT_BUTTON(self, self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId(), self.OnOK)
       
  1638         
       
  1639     def OnOK(self, event):
       
  1640         step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
       
  1641         if step_name == "":
       
  1642             message = wxMessageDialog(self, "You must type a name!", "Error", wxOK|wxICON_ERROR)
       
  1643             message.ShowModal()
       
  1644             message.Destroy()
       
  1645         elif not TestIdentifier(step_name):
       
  1646             message = wxMessageDialog(self, "\"%s\" is not a valid identifier!"%step_name, "Error", wxOK|wxICON_ERROR)
       
  1647             message.ShowModal()
       
  1648             message.Destroy()
       
  1649         elif step_name.upper() in IEC_KEYWORDS:
       
  1650             message = wxMessageDialog(self, "\"%s\" is a keyword. It can't be used!"%step_name, "Error", wxOK|wxICON_ERROR)
       
  1651             message.ShowModal()
       
  1652             message.Destroy()
       
  1653         elif step_name.upper() in self.PouNames:
       
  1654             message = wxMessageDialog(self, "A pou with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
       
  1655             message.ShowModal()
       
  1656             message.Destroy()
       
  1657         elif step_name.upper() in self.Variables:
       
  1658             message = wxMessageDialog(self, "A variable with \"%s\" as name exists!"%step_name, "Error", wxOK|wxICON_ERROR)
       
  1659             message.ShowModal()
       
  1660             message.Destroy()
       
  1661         elif step_name.upper() in self.StepNames:
       
  1662             message = wxMessageDialog(self, "\"%s\" step already exists!"%step_name, "Error", wxOK|wxICON_ERROR)
       
  1663             message.ShowModal()
       
  1664             message.Destroy()
       
  1665         else:
       
  1666             self.EndModal(wxID_OK)
       
  1667 
       
  1668     def SetPouNames(self, pou_names):
       
  1669         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
  1670 
       
  1671     def SetVariables(self, variables):
       
  1672         self.Variables = [var["Name"].upper() for var in variables]
       
  1673 
       
  1674     def SetStepNames(self, step_names):
       
  1675         self.StepNames = [step_name.upper() for step_name in step_names]