# HG changeset patch # User Laurent Bessard # Date 1339002150 -7200 # Node ID 1a343b239e9c3b5a9a6d66bcb3c6f2391d0854b3 # Parent aca0c83ed82ed938deece92480b337670c6d27a5 Replacing dialog asking for a name when adding datatype, configuration and resource by automatically generated name diff -r aca0c83ed82e -r 1a343b239e9c PLCControler.py --- a/PLCControler.py Wed Jun 06 16:57:58 2012 +0200 +++ b/PLCControler.py Wed Jun 06 19:02:30 2012 +0200 @@ -482,40 +482,43 @@ "debug": False} elif words[0] in ['C', 'R']: if words[0] == 'C': - pou_type = ITEM_CONFIGURATION + element_type = ITEM_CONFIGURATION element = project.getconfiguration(words[1]) - for resource in element.getresource(): - vars.append({"name": resource.getname(), - "type": None, - "class": ITEM_RESOURCE, - "edit": True, - "debug": False}) + if element is not None: + for resource in element.getresource(): + vars.append({"name": resource.getname(), + "type": None, + "class": ITEM_RESOURCE, + "edit": True, + "debug": False}) elif words[0] == 'R': - pou_type = ITEM_RESOURCE + element_type = ITEM_RESOURCE element = project.getconfigurationResource(words[1], words[2]) - for task in element.gettask(): - for pou in task.getpouInstance(): + if element is not None: + for task in element.gettask(): + for pou in task.getpouInstance(): + vars.append({"name": pou.getname(), + "type": pou.gettypeName(), + "class": ITEM_PROGRAM, + "edit": True, + "debug": True}) + for pou in element.getpouInstance(): vars.append({"name": pou.getname(), "type": pou.gettypeName(), "class": ITEM_PROGRAM, "edit": True, "debug": True}) - for pou in element.getpouInstance(): - vars.append({"name": pou.getname(), - "type": pou.gettypeName(), - "class": ITEM_PROGRAM, - "edit": True, - "debug": True}) - for varlist in element.getglobalVars(): - for variable in varlist.getvariable(): - var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug) - if var_infos is not None: - vars.append(var_infos) - return {"class": pou_type, - "type": None, - "variables": vars, - "edit": True, - "debug": False} + if element is not None: + for varlist in element.getglobalVars(): + for variable in varlist.getvariable(): + var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug) + if var_infos is not None: + vars.append(var_infos) + return {"class": element_type, + "type": None, + "variables": vars, + "edit": True, + "debug": False} return None def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False): @@ -709,8 +712,10 @@ #------------------------------------------------------------------------------- # Add a Data Type to Project - def ProjectAddDataType(self, datatype_name): - if self.Project is not None: + def ProjectAddDataType(self, datatype_name=None): + if self.Project is not None: + if datatype_name is None: + datatype_name = self.GenerateNewName(None, None, "datatype%d") # Add the datatype to project self.Project.appenddataType(datatype_name) self.BufferProject() @@ -807,8 +812,10 @@ return None # Add a configuration to Project - def ProjectAddConfiguration(self, config_name): - if self.Project is not None: + def ProjectAddConfiguration(self, config_name=None): + if self.Project is not None: + if config_name is None: + config_name = self.GenerateNewName(None, None, "configuration%d") self.Project.addconfiguration(config_name) self.BufferProject() return self.ComputeConfigurationName(config_name) @@ -821,8 +828,10 @@ self.BufferProject() # Add a resource to a configuration of the Project - def ProjectAddConfigurationResource(self, config_name, resource_name): - if self.Project is not None: + def ProjectAddConfigurationResource(self, config_name, resource_name=None): + if self.Project is not None: + if resource_name is None: + resource_name = self.GenerateNewName(None, None, "resource%d") self.Project.addconfigurationResource(config_name, resource_name) self.BufferProject() return self.ComputeConfigurationResourceName(config_name, resource_name) @@ -1958,12 +1967,31 @@ def GenerateNewName(self, tagname, name, format, exclude={}, debug=False): names = exclude.copy() - names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)])) - element = self.GetEditedElement(tagname, debug) - if element is not None: - for instance in element.getinstances(): - if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)): - names[instance.getname()] = True + if tagname is not None: + names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)])) + element = self.GetEditedElement(tagname, debug) + if element is not None: + for instance in element.getinstances(): + if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)): + names[instance.getname().upper()] = True + else: + project = self.GetProject(debug) + if project is not None: + for datatype in project.getdataTypes(): + names[datatype.getname().upper()] = True + for pou in project.getpous(): + names[pou.getname().upper()] = True + for var in self.GetPouInterfaceVars(pou, debug): + names[var["Name"].upper()] = True + for transition in pou.gettransitionList(): + names[transition.getname().upper()] = True + for action in pou.getactionList(): + names[action.getname().upper()] = True + for config in project.getconfigurations(): + names[config.getname().upper()] = True + for resource in config.getresource(): + names[resource.getname().upper()] = True + i = 0 while name is None or names.get(name.upper(), False): name = (format%i) diff -r aca0c83ed82e -r 1a343b239e9c PLCOpenEditor.py --- a/PLCOpenEditor.py Wed Jun 06 16:57:58 2012 +0200 +++ b/PLCOpenEditor.py Wed Jun 06 19:02:30 2012 +0200 @@ -109,7 +109,7 @@ from PLCControler import * from SearchResultPanel import SearchResultPanel from controls import CustomGrid, CustomTable, CustomTree, LibraryPanel, PouInstanceVariablesPanel -from dialogs import ProjectDialog +from dialogs import ProjectDialog, PouTransitionDialog, PouActionDialog # Define PLCOpenEditor controls id [ID_PLCOPENEDITOR, ID_PLCOPENEDITORLEFTNOTEBOOK, @@ -597,12 +597,8 @@ self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnProjectTreeItemSelected, id=ID_PLCOPENEDITORPROJECTTREE) else: - if wx.VERSION >= (2, 6, 0): - self.ProjectTree.Bind(wx.EVT_RIGHT_UP, self.OnProjectTreeRightUp) - self.ProjectTree.Bind(wx.EVT_LEFT_UP, self.OnProjectTreeLeftUp) - else: - wx.EVT_RIGHT_UP(self.ProjectTree, self.OnProjectTreeRightUp) - wx.EVT_LEFT_UP(self.ProjectTree, self.OnProjectTreeLeftUp) + self.ProjectTree.Bind(wx.EVT_RIGHT_UP, self.OnProjectTreeRightUp) + self.ProjectTree.Bind(wx.EVT_LEFT_UP, self.OnProjectTreeLeftUp) self.Bind(wx.EVT_TREE_SEL_CHANGING, self.OnProjectTreeItemChanging, id=ID_PLCOPENEDITORPROJECTTREE) self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnProjectTreeBeginDrag, @@ -2361,20 +2357,17 @@ #------------------------------------------------------------------------------- def OnAddDataTypeMenu(self, event): - dialog = DataTypeDialog(self, _("Add a new data type"), _("Please enter data type name"), "", wx.OK|wx.CANCEL) - dialog.SetDataTypeNames(self.Controler.GetProjectDataTypeNames()) - if dialog.ShowModal() == wx.ID_OK: - tagname = self.Controler.ProjectAddDataType(dialog.GetValue()) - if tagname is not None: - self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE) - self.EditProjectElement(ITEM_DATATYPE, tagname) - dialog.Destroy() - + tagname = self.Controler.ProjectAddDataType() + if tagname is not None: + self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE) + self.EditProjectElement(ITEM_DATATYPE, tagname) + def GenerateAddPouFunction(self, pou_type): def OnAddPouMenu(event): dialog = PouDialog(self, pou_type) dialog.SetPouNames(self.Controler.GetProjectPouNames()) dialog.SetPouElementNames(self.Controler.GetProjectPouVariables()) + dialog.SetValues({"pouName": self.Controler.GenerateNewName(None, None, "%s%%d" % pou_type)}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() tagname = self.Controler.ProjectAddPou(values["pouName"], values["pouType"], values["language"]) @@ -2389,6 +2382,7 @@ dialog = PouTransitionDialog(self) dialog.SetPouNames(self.Controler.GetProjectPouNames()) dialog.SetPouElementNames(self.Controler.GetProjectPouVariables(pou_name)) + dialog.SetValues({"transitionName": self.Controler.GenerateNewName(None, None, "transition%d")}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() tagname = self.Controler.ProjectAddPouTransition(pou_name, values["transitionName"], values["language"]) @@ -2403,6 +2397,7 @@ dialog = PouActionDialog(self) dialog.SetPouNames(self.Controler.GetProjectPouNames()) dialog.SetPouElementNames(self.Controler.GetProjectPouVariables(pou_name)) + dialog.SetValues({"actionName": self.Controler.GenerateNewName(None, None, "action%d")}) if dialog.ShowModal() == wx.ID_OK: values = dialog.GetValues() tagname = self.Controler.ProjectAddPouAction(pou_name, values["actionName"], values["language"]) @@ -2413,29 +2408,18 @@ return OnAddActionMenu def OnAddConfigurationMenu(self, event): - dialog = ConfigurationNameDialog(self, _("Please enter configuration name"), _("Add new configuration")) - dialog.SetPouNames(self.Controler.GetProjectPouNames()) - dialog.SetPouElementNames(self.Controler.GetProjectPouVariables()) - if dialog.ShowModal() == wx.ID_OK: - value = dialog.GetValue() - tagname = self.Controler.ProjectAddConfiguration(value) + tagname = self.Controler.ProjectAddConfiguration() + if tagname is not None: + self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL) + self.EditProjectElement(ITEM_CONFIGURATION, tagname) + dialog.Destroy() + + def GenerateAddResourceFunction(self, config_name): + def OnAddResourceMenu(event): + tagname = self.Controler.ProjectAddConfigurationResource(config_name) if tagname is not None: self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL) - self.EditProjectElement(ITEM_CONFIGURATION, tagname) - dialog.Destroy() - - def GenerateAddResourceFunction(self, config_name): - def OnAddResourceMenu(event): - dialog = ResourceNameDialog(self, _("Please enter resource name"), _("Add new resource")) - dialog.SetPouNames(self.Controler.GetProjectPouNames()) - dialog.SetPouElementNames(self.Controler.GetProjectPouVariables()) - if dialog.ShowModal() == wx.ID_OK: - value = dialog.GetValue() - tagname = self.Controler.ProjectAddConfigurationResource(config_name, value) - if tagname is not None: - self._Refresh(TITLE, FILEMENU, EDITMENU, PROJECTTREE, POUINSTANCEVARIABLESPANEL) - self.EditProjectElement(ITEM_RESOURCE, tagname) - dialog.Destroy() + self.EditProjectElement(ITEM_RESOURCE, tagname) return OnAddResourceMenu def GenerateChangePouTypeFunction(self, name, new_type): @@ -2893,656 +2877,6 @@ dialog.Destroy() #------------------------------------------------------------------------------- -# Edit Step Name Dialog -#------------------------------------------------------------------------------- - -class DataTypeDialog(wx.TextEntryDialog): - - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def __init__(self, parent, message, caption = _("Please enter text"), defaultValue = "", - style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition): - wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos) - - self.DataTypeNames = [] - if wx.VERSION >= (2, 8, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) - elif wx.VERSION >= (2, 6, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) - else: - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) - - def OnOK(self, event): - datatype_name = self.GetSizer().GetItem(1).GetWindow().GetValue() - if datatype_name == "": - message = wx.MessageDialog(self, _("You must type a name!"), _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(datatype_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%datatype_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif datatype_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%datatype_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif datatype_name.upper() in self.DataTypeNames: - message = wx.MessageDialog(self, _("\"%s\" data type already exists!")%datatype_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - else: - self.EndModal(wx.ID_OK) - - def SetDataTypeNames(self, datatype_names): - self.DataTypeNames = [datatype_name.upper() for datatype_name in datatype_names] - - def GetValue(self): - return self.GetSizer().GetItem(1).GetWindow().GetValue() - -#------------------------------------------------------------------------------- -# Create Pou Dialog -#------------------------------------------------------------------------------- - -[ID_POUDIALOG, ID_POUDIALOGPOUNAME, - ID_POUDIALOGPOUTYPE, ID_POUDIALOGLANGUAGE, ID_POUDIALOGSTATICTEXT1, - ID_POUDIALOGSTATICTEXT2, ID_POUDIALOGSTATICTEXT3, -] = [wx.NewId() for _init_ctrls in range(7)] - -def GetPouTypes(): - _ = lambda x : x - return [_("function"), _("functionBlock"), _("program")] -POU_TYPES_DICT = dict([(_(pou_type), pou_type) for pou_type in GetPouTypes()]) - -def GetPouLanguages(): - _ = lambda x : x - return [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")] -POU_LANGUAGES_DICT = dict([(_(language), language) for language in GetPouLanguages()]) - -class PouDialog(wx.Dialog): - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def _init_coll_flexGridSizer1_Items(self, parent): - parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) - parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) - - def _init_coll_flexGridSizer1_Growables(self, parent): - parent.AddGrowableCol(0) - parent.AddGrowableRow(0) - - def _init_coll_MainSizer_Items(self, parent): - parent.AddWindow(self.staticText1, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.PouName, 0, border=0, flag=wx.GROW) - parent.AddWindow(self.staticText2, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.PouType, 0, border=0, flag=wx.GROW) - parent.AddWindow(self.staticText3, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.Language, 0, border=0, flag=wx.GROW) - - def _init_coll_MainSizer_Growables(self, parent): - parent.AddGrowableCol(1) - - def _init_sizers(self): - self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) - self.MainSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) - - self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) - self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) - self._init_coll_MainSizer_Items(self.MainSizer) - self._init_coll_MainSizer_Growables(self.MainSizer) - - self.SetSizer(self.flexGridSizer1) - - def _init_ctrls(self, prnt): - wx.Dialog.__init__(self, id=ID_POUDIALOG, - name='PouDialog', parent=prnt, - size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE, - title=_('Create a new POU')) - self.SetClientSize(wx.Size(300, 200)) - - self.staticText1 = wx.StaticText(id=ID_POUDIALOGSTATICTEXT1, - label=_('POU Name:'), name='staticText1', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.PouName = wx.TextCtrl(id=ID_POUDIALOGPOUNAME, - name='POUName', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 24), style=0) - - self.staticText2 = wx.StaticText(id=ID_POUDIALOGSTATICTEXT2, - label=_('POU Type:'), name='staticText2', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.PouType = wx.ComboBox(id=ID_POUDIALOGPOUTYPE, - name='POUType', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 28), style=wx.CB_READONLY) - self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, id=ID_POUDIALOGPOUTYPE) - - self.staticText3 = wx.StaticText(id=ID_POUDIALOGSTATICTEXT3, - label=_('Language:'), name='staticText3', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.Language = wx.ComboBox(id=ID_POUDIALOGLANGUAGE, - name='Language', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 28), style=wx.CB_READONLY) - - self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) - - self._init_sizers() - - def __init__(self, parent, pou_type = None): - self._init_ctrls(parent) - - for option in GetPouTypes(): - self.PouType.Append(_(option)) - if pou_type is not None: - self.PouType.SetStringSelection(_(pou_type)) - self.RefreshLanguage() - - self.PouNames = [] - self.PouElementNames = [] - - def OnOK(self, event): - error = [] - pou_name = self.PouName.GetValue() - if pou_name == "": - error.append(_("POU Name")) - if self.PouType.GetSelection() == -1: - error.append(_("POU Type")) - if self.Language.GetSelection() == -1: - error.append(_("Language")) - if len(error) > 0: - text = "" - for i, item in enumerate(error): - if i == 0: - text += item - elif i == len(error) - 1: - text += _(" and %s")%item - else: - text += _(", %s")%item - message = wx.MessageDialog(self, _("Form isn't complete. %s must be filled!")%text, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(pou_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%pou_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif pou_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%pou_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif pou_name.upper() in self.PouNames: - message = wx.MessageDialog(self, _("\"%s\" pou already exists!")%pou_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif pou_name.upper() in self.PouElementNames: - message = wx.MessageDialog(self, _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?")%pou_name, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) - result = message.ShowModal() - message.Destroy() - if result == wx.ID_YES: - self.EndModal(wx.ID_OK) - else: - self.EndModal(wx.ID_OK) - - def RefreshLanguage(self): - selection = POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") - self.Language.Clear() - for language in GetPouLanguages(): - if language != "SFC" or POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": - self.Language.Append(_(language)) - if self.Language.FindString(_(selection)) != wx.NOT_FOUND: - self.Language.SetStringSelection(_(selection)) - - def OnTypeChanged(self, event): - self.RefreshLanguage() - event.Skip() - - def SetPouNames(self, pou_names): - self.PouNames = [pou_name.upper() for pou_name in pou_names] - - def SetPouElementNames(self, element_names): - self.PouElementNames = [element_name.upper() for element_name in element_names] - - def SetValues(self, values): - for item, value in values.items(): - if item == "pouName": - self.PouName.SetValue(value) - elif item == "pouType": - self.PouType.SetStringSelection(_(value)) - elif item == "language": - self.Language.SetStringSelection(_(value)) - - def GetValues(self): - values = {} - values["pouName"] = self.PouName.GetValue() - values["pouType"] = POU_TYPES_DICT[self.PouType.GetStringSelection()] - values["language"] = POU_LANGUAGES_DICT[self.Language.GetStringSelection()] - return values - - -#------------------------------------------------------------------------------- -# Create Pou Transition Dialog -#------------------------------------------------------------------------------- - -[ID_POUTRANSITIONDIALOG, ID_POUTRANSITIONDIALOGTRANSITIONNAME, - ID_POUTRANSITIONDIALOGLANGUAGE, ID_POUTRANSITIONDIALOGSTATICTEXT1, - ID_POUTRANSITIONDIALOGSTATICTEXT2, -] = [wx.NewId() for _init_ctrls in range(5)] - -def GetTransitionLanguages(): - _ = lambda x : x - return [_("IL"), _("ST"), _("LD"), _("FBD")] -TRANSITION_LANGUAGES_DICT = dict([(_(language), language) for language in GetTransitionLanguages()]) - -class PouTransitionDialog(wx.Dialog): - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def _init_coll_flexGridSizer1_Items(self, parent): - parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) - parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) - - def _init_coll_flexGridSizer1_Growables(self, parent): - parent.AddGrowableCol(0) - parent.AddGrowableRow(0) - - def _init_coll_MainSizer_Items(self, parent): - parent.AddWindow(self.staticText1, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.TransitionName, 0, border=0, flag=wx.GROW) - parent.AddWindow(self.staticText2, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.Language, 0, border=0, flag=wx.GROW) - - def _init_coll_MainSizer_Growables(self, parent): - parent.AddGrowableCol(1) - - def _init_sizers(self): - self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) - self.MainSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=15) - - self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) - self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) - self._init_coll_MainSizer_Items(self.MainSizer) - self._init_coll_MainSizer_Growables(self.MainSizer) - - self.SetSizer(self.flexGridSizer1) - - def _init_ctrls(self, prnt): - wx.Dialog.__init__(self, id=ID_POUTRANSITIONDIALOG, - name='PouTransitionDialog', parent=prnt, - size=wx.Size(350, 200), style=wx.DEFAULT_DIALOG_STYLE, - title=_('Create a new transition')) - self.SetClientSize(wx.Size(350, 160)) - - self.staticText1 = wx.StaticText(id=ID_POUTRANSITIONDIALOGSTATICTEXT1, - label=_('Transition Name:'), name='staticText1', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.TransitionName = wx.TextCtrl(id=ID_POUTRANSITIONDIALOGTRANSITIONNAME, - name='TransitionName', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 24), style=0) - - self.staticText2 = wx.StaticText(id=ID_POUTRANSITIONDIALOGSTATICTEXT2, - label=_('Language:'), name='staticText2', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.Language = wx.ComboBox(id=ID_POUTRANSITIONDIALOGLANGUAGE, - name='Language', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 28), style=wx.CB_READONLY) - - self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) - - self._init_sizers() - - def __init__(self, parent): - self._init_ctrls(parent) - - for language in GetTransitionLanguages(): - self.Language.Append(_(language)) - - self.PouNames = [] - self.PouElementNames = [] - - def OnOK(self, event): - error = [] - transition_name = self.TransitionName.GetValue() - if self.TransitionName.GetValue() == "": - error.append(_("Transition Name")) - if self.Language.GetSelection() == -1: - error.append(_("Language")) - if len(error) > 0: - text = "" - for i, item in enumerate(error): - if i == 0: - text += item - elif i == len(error) - 1: - text += _(" and %s")%item - else: - text += _(", %s")%item - message = wx.MessageDialog(self, _("Form isn't complete. %s must be filled!")%text, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(transition_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%transition_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif transition_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%transition_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif transition_name.upper() in self.PouNames: - message = wx.MessageDialog(self, _("A POU named \"%s\" already exists!")%transition_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif transition_name.upper() in self.PouElementNames: - message = wx.MessageDialog(self, _("\"%s\" element for this pou already exists!")%transition_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - else: - self.EndModal(wx.ID_OK) - - def SetPouNames(self, pou_names): - self.PouNames = [pou_name.upper() for pou_name in pou_names] - - def SetPouElementNames(self, pou_names): - self.PouElementNames = [pou_name.upper() for pou_name in pou_names] - - def SetValues(self, values): - for item, value in values.items(): - if item == "transitionName": - self.TransitionName.SetValue(value) - elif item == "language": - self.Language.SetSelection(_(value)) - - def GetValues(self): - values = {} - values["transitionName"] = self.TransitionName.GetValue() - values["language"] = TRANSITION_LANGUAGES_DICT[self.Language.GetStringSelection()] - return values - -#------------------------------------------------------------------------------- -# Create Pou Action Dialog -#------------------------------------------------------------------------------- - -[ID_POUACTIONDIALOG, ID_POUACTIONDIALOGACTIONNAME, - ID_POUACTIONDIALOGLANGUAGE, ID_POUACTIONDIALOGSTATICTEXT1, - ID_POUACTIONDIALOGSTATICTEXT2, -] = [wx.NewId() for _init_ctrls in range(5)] - -def GetActionLanguages(): - _ = lambda x : x - return [_("IL"), _("ST"), _("LD"), _("FBD")] -ACTION_LANGUAGES_DICT = dict([(_(language), language) for language in GetActionLanguages()]) - -class PouActionDialog(wx.Dialog): - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def _init_coll_flexGridSizer1_Items(self, parent): - parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) - parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) - - def _init_coll_flexGridSizer1_Growables(self, parent): - parent.AddGrowableCol(0) - parent.AddGrowableRow(0) - - def _init_coll_MainSizer_Items(self, parent): - parent.AddWindow(self.staticText1, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.ActionName, 0, border=0, flag=wx.GROW) - parent.AddWindow(self.staticText2, 0, border=4, flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) - parent.AddWindow(self.Language, 0, border=0, flag=wx.GROW) - - def _init_coll_MainSizer_Growables(self, parent): - parent.AddGrowableCol(1) - - def _init_sizers(self): - self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) - self.MainSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=15) - - self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) - self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) - self._init_coll_MainSizer_Items(self.MainSizer) - self._init_coll_MainSizer_Growables(self.MainSizer) - - self.SetSizer(self.flexGridSizer1) - - def _init_ctrls(self, prnt): - wx.Dialog.__init__(self, id=ID_POUACTIONDIALOG, - name='PouActionDialog', parent=prnt, - size=wx.Size(320, 200), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, - title=_('Create a new action')) - self.SetClientSize(wx.Size(320, 160)) - - self.staticText1 = wx.StaticText(id=ID_POUACTIONDIALOGSTATICTEXT1, - label=_('Action Name:'), name='staticText1', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.ActionName = wx.TextCtrl(id=ID_POUACTIONDIALOGACTIONNAME, - name='ActionName', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 24), style=0) - - self.staticText2 = wx.StaticText(id=ID_POUACTIONDIALOGSTATICTEXT2, - label=_('Language:'), name='staticText2', parent=self, - pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) - - self.Language = wx.ComboBox(id=ID_POUACTIONDIALOGLANGUAGE, - name='Language', parent=self, pos=wx.Point(0, 0), - size=wx.Size(0, 28), style=wx.CB_READONLY) - - self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) - - self._init_sizers() - - def __init__(self, parent): - self._init_ctrls(parent) - - for option in GetActionLanguages(): - self.Language.Append(_(option)) - - self.PouNames = [] - self.PouElementNames = [] - - def OnOK(self, event): - error = [] - action_name = self.ActionName.GetValue() - if action_name == "": - error.append(_("Action Name")) - if self.Language.GetSelection() == -1: - error.append(_("Language")) - if len(error) > 0: - text = "" - for i, item in enumerate(error): - if i == 0: - text += item - elif i == len(error) - 1: - text += _(" and %s")%item - else: - text += _(", %s")%item - message = wx.MessageDialog(self, _("Form isn't complete. %s must be filled!")%text, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(action_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%action_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif action_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%action_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif action_name.upper() in self.PouNames: - message = wx.MessageDialog(self, _("A POU named \"%s\" already exists!")%action_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif action_name.upper() in self.PouElementNames: - message = wx.MessageDialog(self, _("\"%s\" element for this pou already exists!")%action_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - else: - self.EndModal(wx.ID_OK) - - def SetPouNames(self, pou_names): - self.PouNames = [pou_name.upper() for pou_name in pou_names] - - def SetPouElementNames(self, element_names): - self.PouElementNames = [element_name.upper() for element_name in element_names] - - def SetValues(self, values): - for item, value in values.items(): - if item == "actionName": - self.ActionName.SetValue(value) - elif item == "language": - self.Language.SetStringSelection(_(value)) - - def GetValues(self): - values = {} - values["actionName"] = self.ActionName.GetValue() - values["language"] = ACTION_LANGUAGES_DICT[self.Language.GetStringSelection()] - return values - -#------------------------------------------------------------------------------- -# Configuration Name Dialog -#------------------------------------------------------------------------------- - -class ConfigurationNameDialog(wx.TextEntryDialog): - - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def __init__(self, parent, message, caption = _("Please enter configuration name"), defaultValue = "", - style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition): - wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos) - - self.PouNames = [] - self.PouElementNames = [] - - if wx.VERSION >= (2, 8, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) - elif wx.VERSION >= (2, 6, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) - else: - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) - - def OnOK(self, event): - config_name = self.GetSizer().GetItem(1).GetWindow().GetValue() - if config_name == "": - message = wx.MessageDialog(self, _("You must type a name!"), _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(config_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%config_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif config_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%config_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif config_name.upper() in self.PouNames: - message = wx.MessageDialog(self, _("A POU named \"%s\" already exists!")%config_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif config_name.upper() in self.PouElementNames: - message = wx.MessageDialog(self, _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?")%config_name, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) - result = message.ShowModal() - message.Destroy() - if result == wx.ID_YES: - self.EndModal(wx.ID_OK) - else: - self.EndModal(wx.ID_OK) - - def SetPouNames(self, pou_names): - self.PouNames = [pou_name.upper() for pou_name in pou_names] - - def SetPouElementNames(self, pou_names): - self.PouElementNames = [pou_name.upper() for pou_name in pou_names] - - def GetValue(self): - return self.GetSizer().GetItem(1).GetWindow().GetValue() - -#------------------------------------------------------------------------------- -# Resource Name Dialog -#------------------------------------------------------------------------------- - -class ResourceNameDialog(wx.TextEntryDialog): - - if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): - if id is not None: - event(self, id, function) - else: - event(self, function) - - def __init__(self, parent, message, caption = _("Please enter resource name"), defaultValue = "", - style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition): - wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos) - - self.PouNames = [] - self.PouElementNames = [] - - if wx.VERSION >= (2, 8, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) - elif wx.VERSION >= (2, 6, 0): - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) - else: - self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) - - def OnOK(self, event): - resource_name = self.GetSizer().GetItem(1).GetWindow().GetValue() - if resource_name == "": - message = wx.MessageDialog(self, _("You must type a name!"), _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif not TestIdentifier(resource_name): - message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%resource_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif resource_name.upper() in IEC_KEYWORDS: - message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%resource_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif resource_name.upper() in self.PouNames: - message = wx.MessageDialog(self, _("A POU named \"%s\" already exists!")%resource_name, _("Error"), wx.OK|wx.ICON_ERROR) - message.ShowModal() - message.Destroy() - elif resource_name.upper() in self.PouElementNames: - message = wx.MessageDialog(self, _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?")%resource_name, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) - result = message.ShowModal() - message.Destroy() - if result == wx.ID_YES: - self.EndModal(wx.ID_OK) - else: - self.EndModal(wx.ID_OK) - - def SetPouNames(self, pou_names): - self.PouNames = [pou_name.upper() for pou_name in pou_names] - - def SetPouElementNames(self, pou_names): - self.PouElementNames = [pou_name.upper() for pou_name in pou_names] - - def GetValue(self): - return self.GetSizer().GetItem(1).GetWindow().GetValue() - -#------------------------------------------------------------------------------- # Debug Variables Panel #------------------------------------------------------------------------------- diff -r aca0c83ed82e -r 1a343b239e9c dialogs/PouActionDialog.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dialogs/PouActionDialog.py Wed Jun 06 19:02:30 2012 +0200 @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor +#based on the plcopen standard. +# +#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD +# +#See COPYING file for copyrights details. +# +#This library is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public +#License as published by the Free Software Foundation; either +#version 2.1 of the License, or (at your option) any later version. +# +#This library is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +#General Public License for more details. +# +#You should have received a copy of the GNU General Public +#License along with this library; if not, write to the Free Software +#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import wx + +from plcopen.structures import TestIdentifier, IEC_KEYWORDS + +def GetActionLanguages(): + _ = lambda x : x + return [_("IL"), _("ST"), _("LD"), _("FBD")] +ACTION_LANGUAGES_DICT = dict([(_(language), language) for language in GetActionLanguages()]) + +class PouActionDialog(wx.Dialog): + + def __init__(self, parent): + wx.Dialog.__init__(self, id=-1, parent=parent, + name='PouActionDialog', title=_('Create a new action'), + size=wx.Size(320, 200), style=wx.DEFAULT_DIALOG_STYLE) + self.SetClientSize(wx.Size(320, 160)) + + main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) + main_sizer.AddGrowableCol(0) + main_sizer.AddGrowableRow(0) + + infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) + infos_sizer.AddGrowableCol(1) + main_sizer.AddSizer(infos_sizer, 0, border=20, + flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) + + actionname_label = wx.StaticText(id=-1, parent=self, + label=_('Action Name:'), name='actionname_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(actionname_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.ActionName = wx.TextCtrl(id=-1, parent=self, + name='ActionName', pos=wx.Point(0, 0), + size=wx.Size(0, 24), style=0) + infos_sizer.AddWindow(self.ActionName, 0, border=0, flag=wx.GROW) + + language_label = wx.StaticText(id=-1, parent=self, + label=_('Language:'), name='language_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(language_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.Language = wx.ComboBox(id=ID_POUACTIONDIALOGLANGUAGE, + name='Language', parent=self, pos=wx.Point(0, 0), + size=wx.Size(0, 28), style=wx.CB_READONLY) + infos_sizer.AddWindow(self.Language, 0, border=0, flag=wx.GROW) + + self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) + self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) + main_sizer.AddSizer(self.ButtonSizer, 0, border=20, + flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) + + self.SetSizer(main_sizer) + + for option in GetActionLanguages(): + self.Language.Append(_(option)) + + self.PouNames = [] + self.PouElementNames = [] + + def OnOK(self, event): + error = [] + action_name = self.ActionName.GetValue() + if action_name == "": + error.append(_("Action Name")) + if self.Language.GetSelection() == -1: + error.append(_("Language")) + message = None + if len(error) > 0: + text = "" + for i, item in enumerate(error): + if i == 0: + text += item + elif i == len(error) - 1: + text += _(" and %s")%item + else: + text += _(", %s")%item + message = _("Form isn't complete. %s must be filled!") % text + elif not TestIdentifier(action_name): + message = _("\"%s\" is not a valid identifier!") % action_name + elif action_name.upper() in IEC_KEYWORDS: + message = _("\"%s\" is a keyword. It can't be used!") % action_name + elif action_name.upper() in self.PouNames: + message = _("A POU named \"%s\" already exists!") % action_name + elif action_name.upper() in self.PouElementNames: + message = _("\"%s\" element for this pou already exists!") % action_name + if message is not None: + dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) + dialog.ShowModal() + dialog.Destroy() + else: + self.EndModal(wx.ID_OK) + + def SetPouNames(self, pou_names): + self.PouNames = [pou_name.upper() for pou_name in pou_names] + + def SetPouElementNames(self, element_names): + self.PouElementNames = [element_name.upper() for element_name in element_names] + + def SetValues(self, values): + for item, value in values.items(): + if item == "actionName": + self.ActionName.SetValue(value) + elif item == "language": + self.Language.SetStringSelection(_(value)) + + def GetValues(self): + values = {} + values["actionName"] = self.ActionName.GetValue() + values["language"] = ACTION_LANGUAGES_DICT[self.Language.GetStringSelection()] + return values + diff -r aca0c83ed82e -r 1a343b239e9c dialogs/PouDialog.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dialogs/PouDialog.py Wed Jun 06 19:02:30 2012 +0200 @@ -0,0 +1,185 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor +#based on the plcopen standard. +# +#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD +# +#See COPYING file for copyrights details. +# +#This library is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public +#License as published by the Free Software Foundation; either +#version 2.1 of the License, or (at your option) any later version. +# +#This library is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +#General Public License for more details. +# +#You should have received a copy of the GNU General Public +#License along with this library; if not, write to the Free Software +#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import wx + +from plcopen.structures import TestIdentifier, IEC_KEYWORDS + +def GetPouTypes(): + _ = lambda x : x + return [_("function"), _("functionBlock"), _("program")] +POU_TYPES_DICT = dict([(_(pou_type), pou_type) for pou_type in GetPouTypes()]) + +def GetPouLanguages(): + _ = lambda x : x + return [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")] +POU_LANGUAGES_DICT = dict([(_(language), language) for language in GetPouLanguages()]) + +class PouDialog(wx.Dialog): + + def __init__(self, parent, pou_type = None): + wx.Dialog.__init__(self, id=-1, parent=parent, + name='PouDialog', title=_('Create a new POU'), + size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE) + self.SetClientSize(wx.Size(300, 200)) + + main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) + main_sizer.AddGrowableCol(0) + main_sizer.AddGrowableRow(0) + + infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) + infos_sizer.AddGrowableCol(1) + main_sizer.AddSizer(infos_sizer, 0, border=20, + flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) + + pouname_label = wx.StaticText(id=-1, parent=self, + label=_('POU Name:'), name='pouname_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(pouname_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.PouName = wx.TextCtrl(id=-1, parent=self, + name='POUName', pos=wx.Point(0, 0), + size=wx.Size(0, 24), style=0) + infos_sizer.AddWindow(self.PouName, 0, border=0, flag=wx.GROW) + + poutype_label = wx.StaticText(id=-1, parent=self, + label=_('POU Type:'), name='poutype_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(poutype_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.PouType = wx.ComboBox(id=-1, parent=self, + name='POUType', pos=wx.Point(0, 0), + size=wx.Size(0, 28), style=wx.CB_READONLY) + self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, id=self.PouType.GetId()) + infos_sizer.AddWindow(self.PouType, 0, border=0, flag=wx.GROW) + + language_label = wx.StaticText(id=-1, parent=self, + label=_('Language:'), name='language_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(language_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.Language = wx.ComboBox(id=-1, parent=self, + name='Language', pos=wx.Point(0, 0), + size=wx.Size(0, 28), style=wx.CB_READONLY) + infos_sizer.AddWindow(self.Language, 0, border=0, flag=wx.GROW) + + self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) + self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) + main_sizer.AddSizer(self.ButtonSizer, 0, border=20, + flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) + + self.SetSizer(main_sizer) + + for option in GetPouTypes(): + self.PouType.Append(_(option)) + if pou_type is not None: + self.PouType.SetStringSelection(_(pou_type)) + self.RefreshLanguage() + + self.PouNames = [] + self.PouElementNames = [] + + def OnOK(self, event): + error = [] + pou_name = self.PouName.GetValue() + if pou_name == "": + error.append(_("POU Name")) + if self.PouType.GetSelection() == -1: + error.append(_("POU Type")) + if self.Language.GetSelection() == -1: + error.append(_("Language")) + message = None + question = False + if len(error) > 0: + text = "" + for i, item in enumerate(error): + if i == 0: + text += item + elif i == len(error) - 1: + text += _(" and %s")%item + else: + text += _(", %s")%item + message = _("Form isn't complete. %s must be filled!") % text + elif not TestIdentifier(pou_name): + message = _("\"%s\" is not a valid identifier!") % pou_name + elif pou_name.upper() in IEC_KEYWORDS: + message = _("\"%s\" is a keyword. It can't be used!") % pou_name + elif pou_name.upper() in self.PouNames: + message = _("\"%s\" pou already exists!") % pou_name + elif pou_name.upper() in self.PouElementNames: + message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name + question = True + if message is not None: + if question: + dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) + result = dialog.ShowModal() + dialog.Destroy() + if result == wx.ID_YES: + self.EndModal(wx.ID_OK) + else: + dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) + dialog.ShowModal() + dialog.Destroy() + else: + self.EndModal(wx.ID_OK) + + def RefreshLanguage(self): + selection = POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") + self.Language.Clear() + for language in GetPouLanguages(): + if language != "SFC" or POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": + self.Language.Append(_(language)) + if self.Language.FindString(_(selection)) != wx.NOT_FOUND: + self.Language.SetStringSelection(_(selection)) + + def OnTypeChanged(self, event): + self.RefreshLanguage() + event.Skip() + + def SetPouNames(self, pou_names): + self.PouNames = [pou_name.upper() for pou_name in pou_names] + + def SetPouElementNames(self, element_names): + self.PouElementNames = [element_name.upper() for element_name in element_names] + + def SetValues(self, values): + for item, value in values.items(): + if item == "pouName": + self.PouName.SetValue(value) + elif item == "pouType": + self.PouType.SetStringSelection(_(value)) + elif item == "language": + self.Language.SetStringSelection(_(value)) + + def GetValues(self): + values = {} + values["pouName"] = self.PouName.GetValue() + values["pouType"] = POU_TYPES_DICT[self.PouType.GetStringSelection()] + values["language"] = POU_LANGUAGES_DICT[self.Language.GetStringSelection()] + return values + + diff -r aca0c83ed82e -r 1a343b239e9c dialogs/PouTransitionDialog.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dialogs/PouTransitionDialog.py Wed Jun 06 19:02:30 2012 +0200 @@ -0,0 +1,136 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor +#based on the plcopen standard. +# +#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD +# +#See COPYING file for copyrights details. +# +#This library is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public +#License as published by the Free Software Foundation; either +#version 2.1 of the License, or (at your option) any later version. +# +#This library is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +#General Public License for more details. +# +#You should have received a copy of the GNU General Public +#License along with this library; if not, write to the Free Software +#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import wx + +from plcopen.structures import TestIdentifier, IEC_KEYWORDS + +def GetTransitionLanguages(): + _ = lambda x : x + return [_("IL"), _("ST"), _("LD"), _("FBD")] +TRANSITION_LANGUAGES_DICT = dict([(_(language), language) for language in GetTransitionLanguages()]) + +class PouTransitionDialog(wx.Dialog): + + def __init__(self, parent): + wx.Dialog.__init__(self, id=-1, parent=parent, + name='PouTransitionDialog', title=_('Create a new transition'), + size=wx.Size(350, 200), style=wx.DEFAULT_DIALOG_STYLE) + self.SetClientSize(wx.Size(350, 160)) + + main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) + main_sizer.AddGrowableCol(0) + main_sizer.AddGrowableRow(0) + + infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) + infos_sizer.AddGrowableCol(1) + main_sizer.AddSizer(infos_sizer, 0, border=20, + flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) + + transitionname_label = wx.StaticText(id=-1, parent=self, + label=_('Transition Name:'), name='transitionname_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(transitionname_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.TransitionName = wx.TextCtrl(id=-1, parent=self, + name='TransitionName', pos=wx.Point(0, 0), + size=wx.Size(0, 24), style=0) + infos_sizer.AddWindow(self.TransitionName, 0, border=0, flag=wx.GROW) + + language_label = wx.StaticText(id=-1, parent=self, + label=_('Language:'), name='language_label', + pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) + infos_sizer.AddWindow(language_label, 0, border=4, + flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) + + self.Language = wx.ComboBox(id=-1, parent=self, + name='Language', pos=wx.Point(0, 0), + size=wx.Size(0, 28), style=wx.CB_READONLY) + infos_sizer.AddWindow(self.Language, 0, border=0, flag=wx.GROW) + + self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) + self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) + main_sizer.AddSizer(self.ButtonSizer, 0, border=20, + flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) + + self.SetSizer(main_sizer) + + for language in GetTransitionLanguages(): + self.Language.Append(_(language)) + + self.PouNames = [] + self.PouElementNames = [] + + def OnOK(self, event): + error = [] + transition_name = self.TransitionName.GetValue() + if self.TransitionName.GetValue() == "": + error.append(_("Transition Name")) + if self.Language.GetSelection() == -1: + error.append(_("Language")) + message = None + if len(error) > 0: + text = "" + for i, item in enumerate(error): + if i == 0: + text += item + elif i == len(error) - 1: + text += _(" and %s")%item + else: + text += _(", %s")%item + message = _("Form isn't complete. %s must be filled!") % text + elif not TestIdentifier(transition_name): + message = _("\"%s\" is not a valid identifier!") % transition_name + elif transition_name.upper() in IEC_KEYWORDS: + message = _("\"%s\" is a keyword. It can't be used!") % transition_name + elif transition_name.upper() in self.PouNames: + message = _("A POU named \"%s\" already exists!") % transition_name + elif transition_name.upper() in self.PouElementNames: + message = _("\"%s\" element for this pou already exists!") % transition_name + if message is not None: + dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) + dialog.ShowModal() + dialog.Destroy() + else: + self.EndModal(wx.ID_OK) + + def SetPouNames(self, pou_names): + self.PouNames = [pou_name.upper() for pou_name in pou_names] + + def SetPouElementNames(self, pou_names): + self.PouElementNames = [pou_name.upper() for pou_name in pou_names] + + def SetValues(self, values): + for item, value in values.items(): + if item == "transitionName": + self.TransitionName.SetValue(value) + elif item == "language": + self.Language.SetSelection(_(value)) + + def GetValues(self): + values = {} + values["transitionName"] = self.TransitionName.GetValue() + values["language"] = TRANSITION_LANGUAGES_DICT[self.Language.GetStringSelection()] + return values diff -r aca0c83ed82e -r 1a343b239e9c dialogs/__init__.py --- a/dialogs/__init__.py Wed Jun 06 16:57:58 2012 +0200 +++ b/dialogs/__init__.py Wed Jun 06 19:02:30 2012 +0200 @@ -40,3 +40,6 @@ from SearchInProjectDialog import SearchInProjectDialog from BrowseLocationsDialog import BrowseLocationsDialog from ProjectDialog import ProjectDialog +from PouDialog import PouDialog +from PouTransitionDialog import PouTransitionDialog +from PouActionDialog import PouActionDialog