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: 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@1782: # ------------------------------------------------------------------------------- Laurent@814: # POU Transition Dialog andrej@1782: # ------------------------------------------------------------------------------- Laurent@814: andrej@1736: Laurent@814: def GetTransitionLanguages(): andrej@1762: _ = NoTranslate andrej@1670: return [_("IL"), _("ST"), _("LD"), _("FBD")] andrej@1749: andrej@1749: Laurent@814: TRANSITION_LANGUAGES_DICT = dict([(_(language), language) for language in GetTransitionLanguages()]) Laurent@814: andrej@1736: Laurent@814: class PouTransitionDialog(wx.Dialog): andrej@1730: Laurent@814: def __init__(self, parent): andrej@1696: wx.Dialog.__init__(self, parent, title=_('Create a new transition')) andrej@1730: 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) andrej@1730: andrej@1696: infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=10) Laurent@814: infos_sizer.AddGrowableCol(1) andrej@1730: main_sizer.AddSizer(infos_sizer, border=20, andrej@1768: flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT) andrej@1730: Laurent@814: transitionname_label = wx.StaticText(self, label=_('Transition Name:')) andrej@1730: infos_sizer.AddWindow(transitionname_label, border=4, andrej@1768: flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP) Laurent@814: andrej@1740: self.TransitionName = wx.TextCtrl(self, size=wx.Size(180, -1)) Laurent@814: infos_sizer.AddWindow(self.TransitionName, flag=wx.GROW) Laurent@814: Laurent@814: language_label = wx.StaticText(self, label=_('Language:')) andrej@1730: infos_sizer.AddWindow(language_label, border=4, andrej@1768: flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP) andrej@1730: Laurent@814: self.Language = wx.ComboBox(self, style=wx.CB_READONLY) Laurent@814: infos_sizer.AddWindow(self.Language, flag=wx.GROW) andrej@1696: andrej@1745: button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE) Laurent@814: self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) andrej@1745: main_sizer.AddSizer(button_sizer, border=20, flag=wx.ALIGN_RIGHT | wx.BOTTOM) andrej@1730: Laurent@814: self.SetSizer(main_sizer) andrej@1730: Laurent@814: for language in GetTransitionLanguages(): Laurent@814: self.Language.Append(_(language)) andrej@1730: andrej@1696: self.Fit() Laurent@814: self.PouNames = [] Laurent@814: self.PouElementNames = [] andrej@1730: Laurent@814: def OnOK(self, event): Laurent@814: error = [] Laurent@814: transition_name = self.TransitionName.GetValue() Laurent@814: if self.TransitionName.GetValue() == "": Laurent@814: error.append(_("Transition Name")) Laurent@814: if self.Language.GetSelection() == -1: Laurent@814: error.append(_("Language")) Laurent@814: message = None 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(transition_name): Laurent@814: message = _("\"%s\" is not a valid identifier!") % transition_name Laurent@814: elif transition_name.upper() in IEC_KEYWORDS: Laurent@814: message = _("\"%s\" is a keyword. It can't be used!") % transition_name Laurent@814: elif transition_name.upper() in self.PouNames: Laurent@814: message = _("A POU named \"%s\" already exists!") % transition_name Laurent@814: elif transition_name.upper() in self.PouElementNames: Laurent@814: message = _("\"%s\" element for this pou already exists!") % transition_name Laurent@814: if message is not None: 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) andrej@1730: Laurent@814: def SetPouNames(self, pou_names): Laurent@814: self.PouNames = [pou_name.upper() for pou_name in pou_names] andrej@1730: Laurent@814: def SetPouElementNames(self, pou_names): Laurent@814: self.PouElementNames = [pou_name.upper() for pou_name in pou_names] andrej@1730: Laurent@814: def SetValues(self, values): Laurent@814: for item, value in values.items(): Laurent@814: if item == "transitionName": Laurent@814: self.TransitionName.SetValue(value) Laurent@814: elif item == "language": Laurent@814: self.Language.SetSelection(_(value)) andrej@1730: Laurent@814: def GetValues(self): Laurent@814: values = {} Laurent@814: values["transitionName"] = self.TransitionName.GetValue() Laurent@814: values["language"] = TRANSITION_LANGUAGES_DICT[self.Language.GetStringSelection()] Laurent@814: return values