laurent@469: # -*- coding: utf-8 -*- laurent@469: laurent@469: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor laurent@469: #based on the plcopen standard. laurent@469: # laurent@469: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD laurent@469: # laurent@469: #See COPYING file for copyrights details. laurent@469: # laurent@469: #This library is free software; you can redistribute it and/or laurent@469: #modify it under the terms of the GNU General Public laurent@469: #License as published by the Free Software Foundation; either laurent@469: #version 2.1 of the License, or (at your option) any later version. laurent@469: # laurent@469: #This library is distributed in the hope that it will be useful, laurent@469: #but WITHOUT ANY WARRANTY; without even the implied warranty of laurent@469: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU laurent@469: #General Public License for more details. laurent@469: # laurent@469: #You should have received a copy of the GNU General Public laurent@469: #License along with this library; if not, write to the Free Software laurent@469: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA laurent@469: laurent@469: import wx laurent@469: laurent@469: #------------------------------------------------------------------------------- laurent@469: # Force Variable Dialog laurent@469: #------------------------------------------------------------------------------- laurent@469: laurent@469: LOCATIONDATATYPES = {"X" : ["BOOL"], laurent@469: "B" : ["SINT", "USINT", "BYTE", "STRING"], laurent@469: "W" : ["INT", "UINT", "WORD", "WSTRING"], laurent@469: "D" : ["DINT", "UDINT", "REAL", "DWORD"], laurent@469: "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} laurent@469: laurent@475: def gen_get_function(f): laurent@475: def get_function(v): laurent@475: try: laurent@475: return f(v) laurent@475: except: laurent@475: return None laurent@475: return get_function laurent@469: laurent@475: getinteger = gen_check_function(int) laurent@475: getfloat = gen_check_function(float) laurent@475: getstring = gen_check_function(str) laurent@469: laurent@475: GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x, None), laurent@475: "SINT": getinteger, laurent@475: "INT": getinteger, laurent@475: "DINT": getinteger, laurent@475: "LINT": getinteger, laurent@475: "USINT": getinteger, laurent@475: "UINT": getinteger, laurent@475: "UDINT": getinteger, laurent@475: "ULINT": getinteger, laurent@475: "BYTE": getinteger, laurent@475: "WORD": getinteger, laurent@475: "DWORD": getinteger, laurent@475: "LWORD": getinteger, laurent@475: "REAL": getfloat, laurent@475: "LREAL": getfloat, laurent@475: "STRING": getstring, laurent@475: "WSTRING": getstring} laurent@469: laurent@469: laurent@469: class ForceVariableDialog(wx.TextEntryDialog): laurent@469: laurent@469: if wx.VERSION < (2, 6, 0): laurent@469: def Bind(self, event, function, id = None): laurent@469: if id is not None: laurent@469: event(self, id, function) laurent@469: else: laurent@469: event(self, function) laurent@469: laurent@469: def __init__(self, parent, iec_type): laurent@469: wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), laurent@469: caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = "", laurent@469: style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition) laurent@469: laurent@469: self.IEC_Type = iec_type laurent@469: if wx.VERSION >= (2, 8, 0): laurent@469: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId()) laurent@469: elif wx.VERSION >= (2, 6, 0): laurent@469: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId()) laurent@469: else: laurent@469: self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) laurent@469: laurent@469: def OnOK(self, event): laurent@469: value = self.GetSizer().GetItem(1).GetWindow().GetValue() laurent@469: if value == "": laurent@469: message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR) laurent@469: message.ShowModal() laurent@469: message.Destroy() laurent@475: elif GetTypeValue[self.IEC_Type](value) is None: laurent@469: message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR) laurent@469: message.ShowModal() laurent@469: message.Destroy() laurent@469: else: laurent@469: self.EndModal(wx.ID_OK) laurent@469: laurent@469: def GetValue(self): laurent@475: return GetTypeValue[self.IEC_Type](self.GetSizer().GetItem(1).GetWindow().GetValue())