dialogs/ForceVariableDialog.py
author laurent
Mon, 07 Dec 2009 13:38:16 +0100
changeset 472 fecd4f6c01ed
parent 469 17411b970353
child 475 9ca03fdff80f
permissions -rw-r--r--
Adding support for forcing PLC variable in DebugVariablePanel
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     2
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     3
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     4
#based on the plcopen standard. 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     5
#
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     6
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     7
#
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     8
#See COPYING file for copyrights details.
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
     9
#
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    10
#This library is free software; you can redistribute it and/or
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    11
#modify it under the terms of the GNU General Public
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    12
#License as published by the Free Software Foundation; either
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    13
#version 2.1 of the License, or (at your option) any later version.
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    14
#
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    15
#This library is distributed in the hope that it will be useful,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    18
#General Public License for more details.
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    19
#
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    20
#You should have received a copy of the GNU General Public
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    21
#License along with this library; if not, write to the Free Software
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    22
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    23
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    24
import wx
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    25
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    26
#-------------------------------------------------------------------------------
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    27
#                            Force Variable Dialog
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    28
#-------------------------------------------------------------------------------
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    29
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    30
LOCATIONDATATYPES = {"X" : ["BOOL"],
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    31
                     "B" : ["SINT", "USINT", "BYTE", "STRING"],
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    32
                     "W" : ["INT", "UINT", "WORD", "WSTRING"],
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    33
                     "D" : ["DINT", "UDINT", "REAL", "DWORD"],
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    34
                     "L" : ["LINT", "ULINT", "LREAL", "LWORD"]} 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    35
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    36
def checkbool(v):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    37
    return v in ["TRUE", "FALSE"]
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    38
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    39
def gen_check_function(f):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    40
    def check_function(v):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    41
        try:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    42
            f(v)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    43
            return True
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    44
        except:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    45
            return False
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    46
    return check_function
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    47
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    48
checkinteger = gen_check_function(int)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    49
checkfloat = gen_check_function(float)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    50
checkstring = gen_check_function(str)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    51
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    52
CheckTypeValue = {"BOOL": lambda x: x in ["TRUE", "FALSE"],
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    53
                  "SINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    54
                  "INT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    55
                  "DINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    56
                  "LINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    57
                  "USINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    58
                  "UINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    59
                  "UDINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    60
                  "ULINT": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    61
                  "BYTE": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    62
                  "WORD": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    63
                  "DWORD": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    64
                  "LWORD": checkinteger,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    65
                  "REAL": checkfloat,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    66
                  "LREAL": checkfloat,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    67
                  "STRING": checkstring,
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    68
                  "WSTRING": checkstring,}
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    69
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    70
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    71
class ForceVariableDialog(wx.TextEntryDialog):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    72
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    73
    if wx.VERSION < (2, 6, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    74
        def Bind(self, event, function, id = None):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    75
            if id is not None:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    76
                event(self, id, function)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    77
            else:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    78
                event(self, function)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    79
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    80
    def __init__(self, parent, iec_type):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    81
        wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    82
                caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = "", 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    83
                style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    84
        
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    85
        self.IEC_Type = iec_type 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    86
        if wx.VERSION >= (2, 8, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    87
            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(2).GetSizer().GetItem(1).GetSizer().GetAffirmativeButton().GetId())
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    88
        elif wx.VERSION >= (2, 6, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    89
            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetAffirmativeButton().GetId())
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    90
        else:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    91
            self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetSizer().GetItem(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    92
    
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    93
    def OnOK(self, event):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    94
        value = self.GetSizer().GetItem(1).GetWindow().GetValue()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    95
        if value == "":
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    96
            message = wx.MessageDialog(self, _("You must type a value!"), _("Error"), wx.OK|wx.ICON_ERROR)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    97
            message.ShowModal()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    98
            message.Destroy()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    99
        elif not CheckTypeValue[self.IEC_Type](value):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   100
            message = wx.MessageDialog(self, _("Invalid value \"%s\" for \"%s\" variable!")%(value, self.IEC_Type), _("Error"), wx.OK|wx.ICON_ERROR)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   101
            message.ShowModal()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   102
            message.Destroy()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   103
        else:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   104
            self.EndModal(wx.ID_OK)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   105
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   106
    def GetValue(self):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   107
        return self.GetSizer().GetItem(1).GetWindow().GetValue()