dialogs/SFCStepNameDialog.py
changeset 409 34c9f624c2fe
child 446 0dd1a5f2a7a1
equal deleted inserted replaced
408:0e389fa5b160 409:34c9f624c2fe
       
     1 # -*- coding: utf-8 -*-
       
     2 
       
     3 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     4 #based on the plcopen standard. 
       
     5 #
       
     6 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     7 #
       
     8 #See COPYING file for copyrights details.
       
     9 #
       
    10 #This library is free software; you can redistribute it and/or
       
    11 #modify it under the terms of the GNU General Public
       
    12 #License as published by the Free Software Foundation; either
       
    13 #version 2.1 of the License, or (at your option) any later version.
       
    14 #
       
    15 #This library is distributed in the hope that it will be useful,
       
    16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    18 #General Public License for more details.
       
    19 #
       
    20 #You should have received a copy of the GNU General Public
       
    21 #License along with this library; if not, write to the Free Software
       
    22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    23 
       
    24 import wx
       
    25 
       
    26 #-------------------------------------------------------------------------------
       
    27 #                          Edit Step Name Dialog
       
    28 #-------------------------------------------------------------------------------
       
    29 
       
    30 class SFCStepNameDialog(wx.TextEntryDialog):
       
    31 
       
    32     if wx.VERSION < (2, 6, 0):
       
    33         def Bind(self, event, function, id = None):
       
    34             if id is not None:
       
    35                 event(self, id, function)
       
    36             else:
       
    37                 event(self, function)
       
    38     
       
    39 
       
    40     def __init__(self, parent, message, caption = "Please enter text", defaultValue = "", 
       
    41                        style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition):
       
    42         wx.TextEntryDialog.__init__(self, parent, message, caption, defaultValue, style, pos)
       
    43         
       
    44         self.PouNames = []
       
    45         self.Variables = []
       
    46         self.StepNames = []
       
    47         if wx.VERSION >= (2, 8, 0):
       
    48             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId())
       
    49         elif wx.VERSION >= (2, 6, 0):
       
    50             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
       
    51         else:
       
    52             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
    53     
       
    54     def OnOK(self, event):
       
    55         step_name = self.GetSizer().GetItem(1).GetWindow().GetValue()
       
    56         if step_name == "":
       
    57             message = wx.MessageDialog(self, _("You must type a name!"), _("Error"), wx.OK|wx.ICON_ERROR)
       
    58             message.ShowModal()
       
    59             message.Destroy()
       
    60         elif not TestIdentifier(step_name):
       
    61             message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR)
       
    62             message.ShowModal()
       
    63             message.Destroy()
       
    64         elif step_name.upper() in IEC_KEYWORDS:
       
    65             message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR)
       
    66             message.ShowModal()
       
    67             message.Destroy()
       
    68         elif step_name.upper() in self.PouNames:
       
    69             message = wx.MessageDialog(self, _("A pou with \"%s\" as name exists!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR)
       
    70             message.ShowModal()
       
    71             message.Destroy()
       
    72         elif step_name.upper() in self.Variables:
       
    73             message = wx.MessageDialog(self, _("A variable with \"%s\" as name already exists in this pou!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR)
       
    74             message.ShowModal()
       
    75             message.Destroy()
       
    76         elif step_name.upper() in self.StepNames:
       
    77             message = wx.MessageDialog(self, _("\"%s\" step already exists!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR)
       
    78             message.ShowModal()
       
    79             message.Destroy()
       
    80         else:
       
    81             self.EndModal(wx.ID_OK)
       
    82 
       
    83     def SetPouNames(self, pou_names):
       
    84         self.PouNames = [pou_name.upper() for pou_name in pou_names]
       
    85 
       
    86     def SetVariables(self, variables):
       
    87         self.Variables = [var["Name"].upper() for var in variables]
       
    88 
       
    89     def SetStepNames(self, step_names):
       
    90         self.StepNames = [step_name.upper() for step_name in step_names]
       
    91 
       
    92     def GetValue(self):
       
    93         return self.GetSizer().GetItem(1).GetWindow().GetValue()