dialogs/ForceVariableDialog.py
author Edouard Tisserant <edouard@beremiz.fr>
Fri, 08 Nov 2024 11:34:46 +0100
changeset 4035 c317fc64035e
parent 3752 9f6f46dbe3ae
permissions -rw-r--r--
eRPC: Version bump
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
     3
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
     4
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
     6
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
     8
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    10
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    11
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    12
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    13
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    15
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    18
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    19
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    20
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    21
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 941
diff changeset
    22
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    23
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
    24
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    25
import re
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    26
import datetime
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
import wx
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
    30
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
#                                Helpers
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
    32
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    33
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    34
LOCATIONDATATYPES = {"X": ["BOOL"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    35
                     "B": ["SINT", "USINT", "BYTE", "STRING"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    36
                     "W": ["INT", "UINT", "WORD", "WSTRING"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    37
                     "D": ["DINT", "UDINT", "REAL", "DWORD"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    38
                     "L": ["LINT", "ULINT", "LREAL", "LWORD"]}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    40
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
def gen_get_function(f):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
    def get_function(v):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
        try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    44
            return f(v)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    45
        except Exception:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
            return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
    return get_function
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    49
878
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    50
def gen_get_string(delimiter):
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    51
    STRING_MODEL = re.compile("%(delimiter)s([^%(delimiter)s]*)%(delimiter)s$" % {"delimiter": delimiter})
1750
acf02488f37f clean-up: fix PEP8 E306 expected 1 blank line before a nested definition, found X
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    52
878
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    53
    def get_string(v):
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    54
        result = STRING_MODEL.match(v)
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    55
        if result is not None:
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    56
            return result.group(1)
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    57
        return None
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    58
    return get_string
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    59
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
    60
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
getinteger = gen_get_function(int)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
getfloat = gen_get_function(float)
878
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    63
getstring = gen_get_string("'")
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
    64
getwstring = gen_get_string('"')
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    66
SECOND = 1000000
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    67
MINUTE = 60 * SECOND
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
HOUR = 60 * MINUTE
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
DAY = 24 * HOUR
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
2439
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    71
IEC_TIME_MODEL = re.compile(r"(?:(?:T|TIME)#)?(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M(?!S)_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?$" % {"float": r"[0-9]+(?:\.[0-9]+)?"})
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    72
IEC_DATE_MODEL = re.compile(r"(?:(?:D|DATE)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})$")
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    73
IEC_DATETIME_MODEL = re.compile(r"(?:(?:DT|DATE_AND_TIME)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)$")
f0a040f1de1b Fix pep8 warning: W605 invalid escape sequence ?x?
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2434
diff changeset
    74
IEC_TIMEOFDAY_MODEL = re.compile(r"(?:(?:TOD|TIME_OF_DAY)#)?([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)$")
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    76
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
    77
def gettime(v):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
    result = IEC_TIME_MODEL.match(v.upper())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
    if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
        negative, days, hours, minutes, seconds, milliseconds = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        microseconds = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
        not_null = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
        for value, factor in [(days, DAY),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
                              (hours, HOUR),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
                              (minutes, MINUTE),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
                              (seconds, SECOND),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
                              (milliseconds, 1000)]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
            if value is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
                microseconds += float(value) * factor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
                not_null = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
        if not not_null:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
            return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        if negative is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
            microseconds = -microseconds
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
        return datetime.timedelta(microseconds=microseconds)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
    96
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
    97
    else:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   100
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
def getdate(v):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
    result = IEC_DATE_MODEL.match(v.upper())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
    if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
        year, month, day = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
        try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
            date = datetime.datetime(int(year), int(month), int(day))
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   107
        except ValueError:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
            return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   109
        base_date = datetime.datetime(1970, 1, 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   110
        return date - base_date
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
   111
    else:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   112
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   114
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
def getdatetime(v):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   116
    result = IEC_DATETIME_MODEL.match(v.upper())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   117
    if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
        year, month, day, hours, minutes, seconds = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
        try:
941
b6e93549de2e Fixed bug ForceVariableDialog when forcing DATETIME variables
Laurent Bessard
parents: 878
diff changeset
   120
            date = datetime.datetime(int(year), int(month), int(day), int(hours), int(minutes), int(float(seconds)), int((float(seconds) * SECOND) % SECOND))
1846
14b40afccd69 remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   121
        except ValueError:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
            return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
        base_date = datetime.datetime(1970, 1, 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   124
        return date - base_date
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
   125
    else:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   128
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
def gettimeofday(v):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   130
    result = IEC_TIMEOFDAY_MODEL.match(v.upper())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
    if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
        hours, minutes, seconds = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        microseconds = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
        for value, factor in [(hours, HOUR),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
                              (minutes, MINUTE),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
                              (seconds, SECOND)]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
            microseconds += float(value) * factor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
        return datetime.timedelta(microseconds=microseconds)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
   142
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False, "0": False, "1": True}.get(x.upper(), None),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
                "SINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
                "INT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
                "DINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
                "LINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
                "USINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
                "UINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
                "UDINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
                "ULINT": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
                "BYTE": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
                "WORD": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
                "DWORD": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
                "LWORD": getinteger,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
                "REAL": getfloat,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   157
                "LREAL": getfloat,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
                "STRING": getstring,
878
37256069baed Fix display of string variables value in debug
Laurent Bessard
parents: 814
diff changeset
   159
                "WSTRING": getwstring,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
                "TIME": gettime,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
                "DATE": getdate,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
                "DT": getdatetime,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
                "TOD": gettimeofday}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
   165
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
#                            Force Variable Dialog
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
   167
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   169
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   170
class ForceVariableDialog(wx.Dialog):
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   171
    """Dialog to enforce new value for variables in debug panel"""
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   172
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   173
    def __init__(self, parent, iec_type, defaultValue=""):
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   174
        """
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   175
        Constructor
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   176
        @param parent: Parent wx.Window of dialog for modal
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   177
        @param iec_type: IEC type of variable (string). For example 'BOOL', 'LREAL'.
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   178
        @param defaultValue: current variable value as string. Default is empty string.
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   179
        """
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   180
        wx.Dialog.__init__(
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1750
diff changeset
   181
            self, parent,
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   182
            name='ForceVariableDialog',
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   183
            title=_("Please enter value for a \"%s\" variable:") % iec_type,
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   184
            style=wx.DEFAULT_DIALOG_STYLE, pos=wx.DefaultPosition)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
   185
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1701
diff changeset
   186
        self.IEC_Type = iec_type
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   187
        info_sizer = wx.BoxSizer(wx.VERTICAL)
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   188
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   189
        message_label = wx.StaticText(self, label=_("Forcing Variable Value"))
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   190
        info_sizer.Add(message_label, border=10,
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   191
                             flag=wx.ALIGN_LEFT | wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   192
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   193
        if GetTypeValue[self.IEC_Type] in [getinteger, getfloat]:
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   194
            self.InitCtrlNumber(info_sizer, defaultValue)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   195
        elif self.IEC_Type == "BOOL":
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   196
            self.InitCtrlBool(info_sizer, defaultValue)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   197
        else:
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   198
            self.InitCtrlDefault(info_sizer, defaultValue)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   199
        self.GetEnteredValue = self.GetValueDefault
1701
8337f6c169d0 add button to ForceVariableDialog to toggle forced boolean value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   200
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   201
        button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE)
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   202
        self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId())
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   203
        info_sizer.Add(button_sizer, border=10, flag=wx.ALIGN_RIGHT | wx.ALL)
2508
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   204
9076249f11eb Rewrite ForceVariableDialog. It's not based on wx.TextEntryDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2439
diff changeset
   205
        self.SetSizer(info_sizer)
1701
8337f6c169d0 add button to ForceVariableDialog to toggle forced boolean value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   206
        self.Fit()
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   207
        self.ValueCtrl.SetFocus()
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   208
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   209
    # ---------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   210
    # default type methods
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   211
    # ---------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   212
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   213
    def InitCtrlDefault(self, info_sizer, defaultValue):
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   214
        """Add simple text control to change variable of any type"""
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   215
        self.ValueCtrl = wx.TextCtrl(self)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   216
        self.ValueCtrl.SetValue(defaultValue)
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   217
        info_sizer.Add(self.ValueCtrl, border=10, proportion=1,
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   218
                             flag=wx.ALIGN_LEFT | wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   219
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   220
    def GetValueDefault(self):
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   221
        """
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   222
        Returns text representation for a variable value
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   223
        @return: variable value as a string
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   224
        """
3752
9f6f46dbe3ae removed Python2 modules
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   225
        return str(self.ValueCtrl.GetValue())
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   226
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   227
    # -----------------------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   228
    # integer and floating point number type methods
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   229
    # ----------------------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   230
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   231
    def InitCtrlNumber(self, info_sizer, defaultValue):
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   232
        """Add controls to change float and integer variables"""
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   233
        sizer = wx.BoxSizer(wx.HORIZONTAL)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   234
        self.InitCtrlDefault(sizer, defaultValue)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   235
        self.SpinButtonCtrl = wx.SpinButton(self, style=wx.HORIZONTAL | wx.SP_WRAP)
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   236
        sizer.Add(self.SpinButtonCtrl, border=10,
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   237
                        flag=wx.ALIGN_LEFT | wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT | wx.EXPAND)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   238
        self.Bind(wx.EVT_SPIN_UP, self.SpinButtonChanged)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   239
        self.Bind(wx.EVT_SPIN_DOWN, self.SpinButtonChanged)
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   240
        info_sizer.Add(sizer, proportion=1, flag=wx.EXPAND)
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   241
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   242
    def SpinButtonChanged(self, evt):
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   243
        """Increment/decrement variable value"""
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   244
        value = self.GetValue()
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   245
        if value is not None:
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   246
            up = evt.GetEventType() == wx.EVT_SPIN_UP._getEvtType()
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   247
            value = value + 1 if up else value - 1
3752
9f6f46dbe3ae removed Python2 modules
GP Orcullo <kinsamanka@gmail.com>
parents: 3750
diff changeset
   248
            self.ValueCtrl.SetValue(str(value))
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   249
        evt.Skip()
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   250
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   251
    # -----------------------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   252
    # bool type related methods
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   253
    # ----------------------------------------------
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   254
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   255
    def InitCtrlBool(self, info_sizer, defaultValue):
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   256
        """Add button to change value of boolean variable"""
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   257
        self.ValueCtrl = wx.ToggleButton(self, label=_("Toggle value"))
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   258
        value = GetTypeValue[self.IEC_Type](defaultValue)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   259
        if value is not None:
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   260
            self.ValueCtrl.SetValue(value)
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   261
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2513
diff changeset
   262
        info_sizer.Add(self.ValueCtrl, border=10,
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   263
                             flag=wx.ALIGN_LEFT | wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT | wx.GROW)
1701
8337f6c169d0 add button to ForceVariableDialog to toggle forced boolean value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   264
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   265
    def OnOK(self, event):
2511
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   266
        """
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   267
        Checks new entered value
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   268
        before closing dialog window
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   269
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   270
        message = None
2511
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   271
        ret = True
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   272
        value = self.GetEnteredValue()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   273
        if value == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   274
            message = _("You must type a value!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   275
        elif GetTypeValue[self.IEC_Type](value) is None:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
   276
            message = _("Invalid value \"{a1}\" for \"{a2}\" variable!").format(a1=value, a2=self.IEC_Type)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   277
        if message is not None:
1745
f9d32913bad4 clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   278
            dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   279
            dialog.ShowModal()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
            dialog.Destroy()
2511
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   281
            ret = False
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   282
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   283
            self.EndModal(wx.ID_OK)
2511
d6e0442a3986 Don't close ForceVariableDialog if user entered wrong new value
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2510
diff changeset
   284
        event.Skip(ret)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   285
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   286
    def GetValue(self):
2510
4f78f0e201d9 Additional fix for rewrite ForceVariableDialog commit (9076249f)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2508
diff changeset
   287
        """
4f78f0e201d9 Additional fix for rewrite ForceVariableDialog commit (9076249f)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2508
diff changeset
   288
        Return new enforce value of particular type
4f78f0e201d9 Additional fix for rewrite ForceVariableDialog commit (9076249f)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2508
diff changeset
   289
        """
2513
1ee326651a74 Add SpinButtons for integer and floating point variables in ForceVariableDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2511
diff changeset
   290
        return GetTypeValue[self.IEC_Type](self.GetEnteredValue())