Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
Edouard@1421: #based on the plcopen standard.
Laurent@814: #
Laurent@814: #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
Laurent@814: #See COPYING file for copyrights details.
Laurent@814: #
Laurent@814: #This library is free software; you can redistribute it and/or
Laurent@814: #modify it under the terms of the GNU General Public
Laurent@814: #License as published by the Free Software Foundation; either
Laurent@814: #version 2.1 of the License, or (at your option) any later version.
Laurent@814: #
Laurent@814: #This library is distributed in the hope that it will be useful,
Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of
Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Laurent@814: #General Public License for more details.
Laurent@814: #
Laurent@814: #You should have received a copy of the GNU General Public
Laurent@814: #License along with this library; if not, write to the Free Software
Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Laurent@814: 
Laurent@814: import wx
Laurent@814: 
Laurent@814: from plcopen.structures import TestIdentifier, IEC_KEYWORDS
Laurent@814: 
Laurent@814: def GetPouTypes():
Laurent@814:     _ = lambda x : x
Laurent@814:     return [_("function"), _("functionBlock"), _("program")]
Laurent@814: POU_TYPES_DICT = dict([(_(pou_type), pou_type) for pou_type in GetPouTypes()])
Laurent@814: 
Laurent@814: def GetPouLanguages():
Laurent@814:     _ = lambda x : x
Laurent@814:     return [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")]
Laurent@814: 
Laurent@814: class PouDialog(wx.Dialog):
Edouard@1421: 
Edouard@1421:     POU_LANGUAGES = GetPouLanguages()
Edouard@1421:     POU_LANGUAGES_DICT = dict([(_(language), language) for language in POU_LANGUAGES])
Edouard@1421: 
Laurent@814:     def __init__(self, parent, pou_type = None):
Laurent@814:         wx.Dialog.__init__(self, id=-1, parent=parent,
Edouard@1421:               name='PouDialog', title=_('Create a new POU'),
Laurent@814:               size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE)
Laurent@814:         self.SetClientSize(wx.Size(300, 200))
Edouard@1421: 
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,
Laurent@814:               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,
Laurent@814:               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,
Laurent@814:               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,
Laurent@814:               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: 
Laurent@814:         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,
Laurent@814:               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
Edouard@1421: 
Laurent@814:         self.SetSizer(main_sizer)
Edouard@1421: 
Laurent@814:         for option in GetPouTypes():
Laurent@814:             self.PouType.Append(_(option))
Laurent@814:         if pou_type is not None:
Laurent@814:             self.PouType.SetStringSelection(_(pou_type))
Laurent@814:         self.RefreshLanguage()
Laurent@814: 
Laurent@814:         self.PouNames = []
Laurent@814:         self.PouElementNames = []
Laurent@814: 
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:
Laurent@814:                     text += _(" and %s")%item
Laurent@814:                 else:
Edouard@1421:                     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:
Laurent@814:                 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:
Laurent@814:                 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:
Laurent@814:             if language != "SFC" or 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()
Laurent@814:         values["pouType"] = POU_TYPES_DICT[self.PouType.GetStringSelection()]
Edouard@1421:         values["language"] = self.POU_LANGUAGES_DICT[self.Language.GetStringSelection()]
Laurent@814:         return values