dialogs/ForceVariableDialog.py
author laurent
Thu, 31 Mar 2011 17:37:47 +0200
changeset 515 55213a347f16
parent 479 2fab0eefa66e
child 516 40290ddff19c
permissions -rw-r--r--
Merged with Edouard's modifications
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
475
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    36
def gen_get_function(f):
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    37
    def get_function(v):
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    38
        try:
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    39
            return f(v)
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    40
        except:
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    41
            return None
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    42
    return get_function
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    43
476
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 475
diff changeset
    44
getinteger = gen_get_function(int)
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 475
diff changeset
    45
getfloat = gen_get_function(float)
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 475
diff changeset
    46
getstring = gen_get_function(str)
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    47
479
2fab0eefa66e Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents: 476
diff changeset
    48
GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x.upper(), None),
475
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    49
                "SINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    50
                "INT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    51
                "DINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    52
                "LINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    53
                "USINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    54
                "UINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    55
                "UDINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    56
                "ULINT": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    57
                "BYTE": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    58
                "WORD": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    59
                "DWORD": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    60
                "LWORD": getinteger,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    61
                "REAL": getfloat,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    62
                "LREAL": getfloat,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    63
                "STRING": getstring,
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    64
                "WSTRING": getstring}
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    65
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    66
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    67
class ForceVariableDialog(wx.TextEntryDialog):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    68
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    69
    if wx.VERSION < (2, 6, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    70
        def Bind(self, event, function, id = None):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    71
            if id is not None:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    72
                event(self, id, function)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    73
            else:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    74
                event(self, function)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    75
479
2fab0eefa66e Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents: 476
diff changeset
    76
    def __init__(self, parent, iec_type, defaultValue=""):
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    77
        wx.TextEntryDialog.__init__(self, parent, message = _("Forcing Variable Value"), 
479
2fab0eefa66e Adding support for getting current value in ForceVariableDialog default value and adding variable in DebugVariablePanel when forced
laurent
parents: 476
diff changeset
    78
                caption = _("Please enter value for a \"%s\" variable:"%iec_type), defaultValue = defaultValue, 
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    79
                style = wx.OK|wx.CANCEL|wx.CENTRE, pos = wx.DefaultPosition)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    80
        
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    81
        self.IEC_Type = iec_type 
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    82
        if wx.VERSION >= (2, 8, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    83
            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
    84
        elif wx.VERSION >= (2, 6, 0):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    85
            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
    86
        else:
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(3).GetSizer().GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    88
    
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    89
    def OnOK(self, event):
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    90
        value = self.GetSizer().GetItem(1).GetWindow().GetValue()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    91
        if value == "":
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    92
            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
    93
            message.ShowModal()
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    94
            message.Destroy()
475
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
    95
        elif GetTypeValue[self.IEC_Type](value) is None:
469
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
    96
            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
    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
        else:
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   100
            self.EndModal(wx.ID_OK)
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   101
17411b970353 Adding support for forcing PLC variable in Viewer
laurent
parents:
diff changeset
   102
    def GetValue(self):
475
9ca03fdff80f ForceVariableDialog return a python value instead of string
laurent
parents: 469
diff changeset
   103
        return GetTypeValue[self.IEC_Type](self.GetSizer().GetItem(1).GetWindow().GetValue())