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