Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Laurent@814: # andrej@1571: # Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD andrej@1696: # Copyright (C) 2017: Andrey Skvortsov Laurent@814: # andrej@1571: # See COPYING file for copyrights details. Laurent@814: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Laurent@814: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Laurent@814: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Laurent@814: andrej@1881: andrej@1881: from __future__ import absolute_import Laurent@814: import wx Laurent@814: Laurent@814: from plcopen.structures import TestIdentifier, IEC_KEYWORDS andrej@1762: from util.TranslationCatalogs import NoTranslate Laurent@814: andrej@1736: Laurent@814: class PouDialog(wx.Dialog): andrej@2299: """ andrej@2299: Dialog to create new POU. andrej@2299: It allows selection of POU type, name and andrej@2299: used IEC 61131-3 programming language andrej@2299: """ andrej@2299: andrej@2299: POU_TYPES = None andrej@2299: POU_TYPES_DICT = None andrej@2299: POU_LANGUAGES = None andrej@2299: POU_LANGUAGES_DICT = None Edouard@1421: surkovsv93@1708: def __init__(self, parent, pou_type=None, type_readonly=False): Laurent@814: wx.Dialog.__init__(self, id=-1, parent=parent, andrej@1768: name='PouDialog', title=_('Create a new POU'), andrej@1768: style=wx.DEFAULT_DIALOG_STYLE) Edouard@1421: andrej@2299: if PouDialog.POU_TYPES_DICT is None: andrej@2299: self.InitPouTypesDict() andrej@2299: self.InitPouLanguagesDict() andrej@2299: Laurent@814: main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) Laurent@814: main_sizer.AddGrowableCol(0) Laurent@814: main_sizer.AddGrowableRow(0) Edouard@1421: Laurent@814: infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) Laurent@814: infos_sizer.AddGrowableCol(1) Edouard@1421: main_sizer.AddSizer(infos_sizer, border=20, andrej@1768: flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT) Edouard@1421: Laurent@814: pouname_label = wx.StaticText(self, label=_('POU Name:')) Edouard@1421: infos_sizer.AddWindow(pouname_label, border=4, andrej@1768: flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP) Edouard@1421: Laurent@814: self.PouName = wx.TextCtrl(self) Laurent@814: infos_sizer.AddWindow(self.PouName, flag=wx.GROW) Edouard@1421: Laurent@814: poutype_label = wx.StaticText(self, label=_('POU Type:')) Edouard@1421: infos_sizer.AddWindow(poutype_label, border=4, andrej@1768: flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP) Edouard@1421: Laurent@814: self.PouType = wx.ComboBox(self, style=wx.CB_READONLY) Laurent@814: self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, self.PouType) Laurent@814: infos_sizer.AddWindow(self.PouType, flag=wx.GROW) Edouard@1421: Laurent@814: language_label = wx.StaticText(self, label=_('Language:')) Edouard@1421: infos_sizer.AddWindow(language_label, border=4, andrej@1768: flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP) Edouard@1421: Laurent@814: self.Language = wx.ComboBox(self, style=wx.CB_READONLY) Laurent@814: infos_sizer.AddWindow(self.Language, flag=wx.GROW) Edouard@1421: andrej@1745: button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) Edouard@1421: main_sizer.AddSizer(button_sizer, border=20, andrej@1768: flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT) Edouard@1421: Laurent@814: self.SetSizer(main_sizer) Edouard@1421: andrej@2299: for option in self.POU_TYPES: surkovsv93@1708: if not type_readonly or _(option) == _(pou_type): surkovsv93@1708: self.PouType.Append(_(option)) Laurent@814: if pou_type is not None: Laurent@814: self.PouType.SetStringSelection(_(pou_type)) Laurent@814: self.RefreshLanguage() andrej@1696: self.Fit() Laurent@814: self.PouNames = [] Laurent@814: self.PouElementNames = [] Laurent@814: andrej@2299: def InitPouTypes(self): andrej@2299: """ andrej@2299: Initialize POU_TYPES class list. andrej@2299: This list contains not translated POU types used in PLCopen XML. andrej@2299: _() are necessary so mk18n.py could find these string for localization. andrej@2299: """ andrej@2299: _ = NoTranslate andrej@2299: self.__class__.POU_TYPES = [_("function"), _("functionBlock"), _("program")] andrej@2299: andrej@2299: def InitPouTypesDict(self): andrej@2299: """ andrej@2299: Initialize POU_TYPES_DICT class dictionary and POU_TYPE list andrej@2299: Dictionary contains localized POU types and they are shown in UI. andrej@2299: """ andrej@2299: self.InitPouTypes() andrej@2299: self.__class__.POU_TYPES_DICT = dict([(_(pou_type), pou_type) andrej@2299: for pou_type in self.POU_TYPES]) andrej@2299: andrej@2299: def InitPouLanguages(self): andrej@2299: """ andrej@2299: Initialize POU_LANGUAGES class list. andrej@2299: This list contains not translated programming languages used in PLCopen XML. andrej@2299: _() are necessary so mk18n.py could find these string for localization. andrej@2299: """ andrej@2299: _ = NoTranslate andrej@2299: self.__class__.POU_LANGUAGES = [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")] andrej@2299: andrej@2299: def InitPouLanguagesDict(self): andrej@2299: """ andrej@2299: Initialize POU_LANGUAGES_DICT class dictionary and POU_LANGUAGES list andrej@2299: Dictionary contains localized names for programming languages andrej@2299: as they are shown in UI. andrej@2299: """ andrej@2299: self.InitPouLanguages() andrej@2299: self.__class__.POU_LANGUAGES_DICT = dict([(_(language), language) andrej@2299: for language in self.POU_LANGUAGES]) andrej@2299: Laurent@814: def OnOK(self, event): Laurent@814: error = [] Laurent@814: pou_name = self.PouName.GetValue() Laurent@814: if pou_name == "": Laurent@814: error.append(_("POU Name")) Laurent@814: if self.PouType.GetSelection() == -1: Laurent@814: error.append(_("POU Type")) Laurent@814: if self.Language.GetSelection() == -1: Laurent@814: error.append(_("Language")) Laurent@814: message = None Laurent@814: question = False Laurent@814: if len(error) > 0: Laurent@814: text = "" Laurent@814: for i, item in enumerate(error): Laurent@814: if i == 0: Laurent@814: text += item Laurent@814: elif i == len(error) - 1: andrej@1734: text += _(" and %s") % item Laurent@814: else: andrej@1734: text += _(", %s") % item Laurent@814: message = _("Form isn't complete. %s must be filled!") % text Laurent@814: elif not TestIdentifier(pou_name): Laurent@814: message = _("\"%s\" is not a valid identifier!") % pou_name Laurent@814: elif pou_name.upper() in IEC_KEYWORDS: Laurent@814: message = _("\"%s\" is a keyword. It can't be used!") % pou_name Laurent@814: elif pou_name.upper() in self.PouNames: Laurent@814: message = _("\"%s\" pou already exists!") % pou_name Laurent@814: elif pou_name.upper() in self.PouElementNames: Laurent@814: message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name Laurent@814: question = True Laurent@814: if message is not None: Laurent@814: if question: andrej@1745: dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO | wx.ICON_EXCLAMATION) Laurent@814: result = dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@814: if result == wx.ID_YES: Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: else: andrej@1745: dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) Laurent@814: dialog.ShowModal() Laurent@814: dialog.Destroy() Laurent@814: else: Laurent@814: self.EndModal(wx.ID_OK) Laurent@814: Laurent@814: def RefreshLanguage(self): Edouard@1421: selection = self.POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") Laurent@814: self.Language.Clear() Edouard@1421: for language in self.POU_LANGUAGES: andrej@2299: if language != "SFC" or self.POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": Laurent@814: self.Language.Append(_(language)) Laurent@814: if self.Language.FindString(_(selection)) != wx.NOT_FOUND: Laurent@814: self.Language.SetStringSelection(_(selection)) Laurent@814: Laurent@814: def OnTypeChanged(self, event): Laurent@814: self.RefreshLanguage() Laurent@814: event.Skip() Laurent@814: Laurent@814: def SetPouNames(self, pou_names): Laurent@814: self.PouNames = [pou_name.upper() for pou_name in pou_names] Laurent@814: Laurent@814: def SetPouElementNames(self, element_names): Laurent@814: self.PouElementNames = [element_name.upper() for element_name in element_names] Laurent@814: Laurent@814: def SetValues(self, values): Laurent@814: for item, value in values.items(): Laurent@814: if item == "pouName": Laurent@814: self.PouName.SetValue(value) Laurent@814: elif item == "pouType": Laurent@814: self.PouType.SetStringSelection(_(value)) Laurent@814: elif item == "language": Laurent@814: self.Language.SetStringSelection(_(value)) Edouard@1421: Laurent@814: def GetValues(self): Laurent@814: values = {} Laurent@814: values["pouName"] = self.PouName.GetValue() andrej@2299: values["pouType"] = self.POU_TYPES_DICT[self.PouType.GetStringSelection()] Edouard@1421: values["language"] = self.POU_LANGUAGES_DICT[self.Language.GetStringSelection()] Laurent@814: return values