dialogs/PouActionDialog.py
changeset 814 5743cbdff669
child 1493 6dbebfcec074
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from plcopen.structures import TestIdentifier, IEC_KEYWORDS
       
    28 
       
    29 def GetActionLanguages():
       
    30     _ = lambda x : x
       
    31     return [_("IL"), _("ST"), _("LD"), _("FBD")]
       
    32 ACTION_LANGUAGES_DICT = dict([(_(language), language) for language in GetActionLanguages()])
       
    33 
       
    34 class PouActionDialog(wx.Dialog):
       
    35     
       
    36     def __init__(self, parent):
       
    37         wx.Dialog.__init__(self, parent, size=wx.Size(320, 160), 
       
    38               title=_('Create a new action'))
       
    39         
       
    40         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    41         main_sizer.AddGrowableCol(0)
       
    42         main_sizer.AddGrowableRow(0)
       
    43         
       
    44         infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15)
       
    45         infos_sizer.AddGrowableCol(1)
       
    46         main_sizer.AddSizer(infos_sizer, border=20, 
       
    47               flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    48         
       
    49         actionname_label = wx.StaticText(self, label=_('Action Name:'))
       
    50         infos_sizer.AddWindow(actionname_label, border=4, 
       
    51               flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
       
    52         
       
    53         self.ActionName = wx.TextCtrl(self)
       
    54         infos_sizer.AddWindow(self.ActionName, flag=wx.GROW)
       
    55         
       
    56         language_label = wx.StaticText(self, label=_('Language:'))
       
    57         infos_sizer.AddWindow(language_label, border=4, 
       
    58               flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
       
    59         
       
    60         self.Language = wx.ComboBox(self, style=wx.CB_READONLY)
       
    61         infos_sizer.AddWindow(self.Language, flag=wx.GROW)
       
    62         
       
    63         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    64         self.Bind(wx.EVT_BUTTON, self.OnOK, 
       
    65               button_sizer.GetAffirmativeButton())
       
    66         main_sizer.AddSizer(button_sizer, border=20, 
       
    67               flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    68             
       
    69         self.SetSizer(main_sizer)
       
    70         
       
    71         for option in GetActionLanguages():
       
    72             self.Language.Append(_(option))
       
    73         
       
    74         self.PouNames = []
       
    75         self.PouElementNames = []
       
    76     
       
    77     def OnOK(self, event):
       
    78         error = []
       
    79         action_name = self.ActionName.GetValue()
       
    80         if action_name == "":
       
    81             error.append(_("Action Name"))
       
    82         if self.Language.GetSelection() == -1:
       
    83             error.append(_("Language"))
       
    84         message = None
       
    85         if len(error) > 0:
       
    86             text = ""
       
    87             for i, item in enumerate(error):
       
    88                 if i == 0:
       
    89                     text += item
       
    90                 elif i == len(error) - 1:
       
    91                     text += _(" and %s")%item
       
    92                 else:
       
    93                     text += _(", %s")%item 
       
    94             message = _("Form isn't complete. %s must be filled!") % text
       
    95         elif not TestIdentifier(action_name):
       
    96             message = _("\"%s\" is not a valid identifier!") % action_name
       
    97         elif action_name.upper() in IEC_KEYWORDS:
       
    98             message = _("\"%s\" is a keyword. It can't be used!") % action_name
       
    99         elif action_name.upper() in self.PouNames:
       
   100             message = _("A POU named \"%s\" already exists!") % action_name
       
   101         elif action_name.upper() in self.PouElementNames:
       
   102             message = _("\"%s\" element for this pou already exists!") % action_name
       
   103         if message is not None:
       
   104             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
       
   105             dialog.ShowModal()
       
   106             dialog.Destroy()
       
   107         else:
       
   108             self.EndModal(wx.ID_OK)
       
   109     
       
   110     def SetPouNames(self, pou_names):
       
   111         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
   112         
       
   113     def SetPouElementNames(self, element_names):
       
   114         self.PouElementNames = [element_name.upper() for element_name in element_names]
       
   115         
       
   116     def SetValues(self, values):
       
   117         for item, value in values.items():
       
   118             if item == "actionName":
       
   119                 self.ActionName.SetValue(value)
       
   120             elif item == "language":
       
   121                 self.Language.SetStringSelection(_(value))
       
   122                 
       
   123     def GetValues(self):
       
   124         values = {}
       
   125         values["actionName"] = self.ActionName.GetValue()
       
   126         values["language"] = ACTION_LANGUAGES_DICT[self.Language.GetStringSelection()]
       
   127         return values
       
   128