dialogs/PouActionDialog.py
changeset 705 1a343b239e9c
child 714 131ea7f237b9
equal deleted inserted replaced
704:aca0c83ed82e 705:1a343b239e9c
       
     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, id=-1, parent=parent,
       
    38               name='PouActionDialog', title=_('Create a new action'), 
       
    39               size=wx.Size(320, 200), style=wx.DEFAULT_DIALOG_STYLE)
       
    40         self.SetClientSize(wx.Size(320, 160))
       
    41         
       
    42         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
       
    43         main_sizer.AddGrowableCol(0)
       
    44         main_sizer.AddGrowableRow(0)
       
    45         
       
    46         infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15)
       
    47         infos_sizer.AddGrowableCol(1)
       
    48         main_sizer.AddSizer(infos_sizer, 0, border=20, 
       
    49                             flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    50         
       
    51         actionname_label = wx.StaticText(id=-1, parent=self, 
       
    52               label=_('Action Name:'), name='actionname_label', 
       
    53               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
       
    54         infos_sizer.AddWindow(actionname_label, 0, border=4, 
       
    55                               flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
       
    56         
       
    57         self.ActionName = wx.TextCtrl(id=-1, parent=self, 
       
    58               name='ActionName', pos=wx.Point(0, 0),
       
    59               size=wx.Size(0, 24), style=0)
       
    60         infos_sizer.AddWindow(self.ActionName, 0, border=0, flag=wx.GROW)
       
    61         
       
    62         language_label = wx.StaticText(id=-1, parent=self, 
       
    63               label=_('Language:'), name='language_label', 
       
    64               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
       
    65         infos_sizer.AddWindow(language_label, 0, border=4, 
       
    66                               flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP)
       
    67         
       
    68         self.Language = wx.ComboBox(id=ID_POUACTIONDIALOGLANGUAGE,
       
    69               name='Language', parent=self, pos=wx.Point(0, 0),
       
    70               size=wx.Size(0, 28), style=wx.CB_READONLY)
       
    71         infos_sizer.AddWindow(self.Language, 0, border=0, flag=wx.GROW)
       
    72         
       
    73         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    74         self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
    75         main_sizer.AddSizer(self.ButtonSizer, 0, border=20, 
       
    76                             flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    77             
       
    78         self.SetSizer(main_sizer)
       
    79         
       
    80         for option in GetActionLanguages():
       
    81             self.Language.Append(_(option))
       
    82         
       
    83         self.PouNames = []
       
    84         self.PouElementNames = []
       
    85     
       
    86     def OnOK(self, event):
       
    87         error = []
       
    88         action_name = self.ActionName.GetValue()
       
    89         if action_name == "":
       
    90             error.append(_("Action Name"))
       
    91         if self.Language.GetSelection() == -1:
       
    92             error.append(_("Language"))
       
    93         message = None
       
    94         if len(error) > 0:
       
    95             text = ""
       
    96             for i, item in enumerate(error):
       
    97                 if i == 0:
       
    98                     text += item
       
    99                 elif i == len(error) - 1:
       
   100                     text += _(" and %s")%item
       
   101                 else:
       
   102                     text += _(", %s")%item 
       
   103             message = _("Form isn't complete. %s must be filled!") % text
       
   104         elif not TestIdentifier(action_name):
       
   105             message = _("\"%s\" is not a valid identifier!") % action_name
       
   106         elif action_name.upper() in IEC_KEYWORDS:
       
   107             message = _("\"%s\" is a keyword. It can't be used!") % action_name
       
   108         elif action_name.upper() in self.PouNames:
       
   109             message = _("A POU named \"%s\" already exists!") % action_name
       
   110         elif action_name.upper() in self.PouElementNames:
       
   111             message = _("\"%s\" element for this pou already exists!") % action_name
       
   112         if message is not None:
       
   113             dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
       
   114             dialog.ShowModal()
       
   115             dialog.Destroy()
       
   116         else:
       
   117             self.EndModal(wx.ID_OK)
       
   118     
       
   119     def SetPouNames(self, pou_names):
       
   120         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
   121         
       
   122     def SetPouElementNames(self, element_names):
       
   123         self.PouElementNames = [element_name.upper() for element_name in element_names]
       
   124         
       
   125     def SetValues(self, values):
       
   126         for item, value in values.items():
       
   127             if item == "actionName":
       
   128                 self.ActionName.SetValue(value)
       
   129             elif item == "language":
       
   130                 self.Language.SetStringSelection(_(value))
       
   131                 
       
   132     def GetValues(self):
       
   133         values = {}
       
   134         values["actionName"] = self.ActionName.GetValue()
       
   135         values["language"] = ACTION_LANGUAGES_DICT[self.Language.GetStringSelection()]
       
   136         return values
       
   137