dialogs/ForceVariableDialog.py
changeset 469 17411b970353
child 475 9ca03fdff80f
equal deleted inserted replaced
468:922da7834c81 469:17411b970353
       
     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 #                            Force Variable Dialog
       
    28 #-------------------------------------------------------------------------------
       
    29 
       
    30 LOCATIONDATATYPES = {"X" : ["BOOL"],
       
    31                      "B" : ["SINT", "USINT", "BYTE", "STRING"],
       
    32                      "W" : ["INT", "UINT", "WORD", "WSTRING"],
       
    33                      "D" : ["DINT", "UDINT", "REAL", "DWORD"],
       
    34                      "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
       
    35 
       
    36 def checkbool(v):
       
    37     return v in ["TRUE", "FALSE"]
       
    38 
       
    39 def gen_check_function(f):
       
    40     def check_function(v):
       
    41         try:
       
    42             f(v)
       
    43             return True
       
    44         except:
       
    45             return False
       
    46     return check_function
       
    47 
       
    48 checkinteger = gen_check_function(int)
       
    49 checkfloat = gen_check_function(float)
       
    50 checkstring = gen_check_function(str)
       
    51 
       
    52 CheckTypeValue = {"BOOL": lambda x: x in ["TRUE", "FALSE"],
       
    53                   "SINT": checkinteger,
       
    54                   "INT": checkinteger,
       
    55                   "DINT": checkinteger,
       
    56                   "LINT": checkinteger,
       
    57                   "USINT": checkinteger,
       
    58                   "UINT": checkinteger,
       
    59                   "UDINT": checkinteger,
       
    60                   "ULINT": checkinteger,
       
    61                   "BYTE": checkinteger,
       
    62                   "WORD": checkinteger,
       
    63                   "DWORD": checkinteger,
       
    64                   "LWORD": checkinteger,
       
    65                   "REAL": checkfloat,
       
    66                   "LREAL": checkfloat,
       
    67                   "STRING": checkstring,
       
    68                   "WSTRING": checkstring,}
       
    69 
       
    70 
       
    71 class ForceVariableDialog(wx.TextEntryDialog):
       
    72 
       
    73     if wx.VERSION < (2, 6, 0):
       
    74         def Bind(self, event, function, id = None):
       
    75             if id is not None:
       
    76                 event(self, id, function)
       
    77             else:
       
    78                 event(self, function)
       
    79 
       
    80     def __init__(self, parent, iec_type):
       
    81         wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), 
       
    82                 caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = "", 
       
    83                 style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition)
       
    84         
       
    85         self.IEC_Type = iec_type 
       
    86         if wx.VERSION >= (2, 8, 0):
       
    87             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId())
       
    88         elif wx.VERSION >= (2, 6, 0):
       
    89             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
       
    90         else:
       
    91             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
    92     
       
    93     def OnOK(self, event):
       
    94         value = self.GetSizer().GetItem(1).GetWindow().GetValue()
       
    95         if value == "":
       
    96             message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR)
       
    97             message.ShowModal()
       
    98             message.Destroy()
       
    99         elif not CheckTypeValue[self.IEC_Type](value):
       
   100             message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR)
       
   101             message.ShowModal()
       
   102             message.Destroy()
       
   103         else:
       
   104             self.EndModal(wx.ID_OK)
       
   105 
       
   106     def GetValue(self):
       
   107         return self.GetSizer().GetItem(1).GetWindow().GetValue()