controls/CustomIntCtrl.py
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 05 Dec 2024 16:45:37 +0100
changeset 4061 e0dfe8b8db4a
parent 3750 f62625418bff
permissions -rw-r--r--
py_ext: rework logic CSV reader FB output refresh logic.

Saving with CSV write FB now triggers CSV read FBs to refresh outputs immediately.
1811
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     3
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     6
#
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     7
# Copyright (C) 2017: Andrey Skvortsov
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     8
#
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     9
# See COPYING file for copyrights details.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    10
#
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    11
# This program is free software; you can redistribute it and/or
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    12
# modify it under the terms of the GNU General Public License
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    13
# as published by the Free Software Foundation; either version 2
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    14
# of the License, or (at your option) any later version.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    15
#
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    16
# This program is distributed in the hope that it will be useful,
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    19
# GNU General Public License for more details.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    20
#
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    21
# You should have received a copy of the GNU General Public License
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    22
# along with this program; if not, write to the Free Software
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    24
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    25
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 1881
diff changeset
    26
1811
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    27
import wx
1850
614396cbffbf fix pylint warning '(unused-import), Unused import connectors'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1811
diff changeset
    28
import wx.lib.intctrl
1811
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    29
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    30
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    31
class CustomIntUpdatedEvent(wx.PyCommandEvent):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    32
    def __init__(self, id, value=0, object=None):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    33
        wx.PyCommandEvent.__init__(self, CustomIntCtrl.wxEVT_COMMAND_CUSTOM_INT_UPDATED, id)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    34
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    35
        self.__value = value
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    36
        self.SetEventObject(object)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    37
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    38
    def GetValue(self):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    39
        """Retrieve the value of the control at the time
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    40
        this event was generated."""
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    41
        return self.__value
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    42
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    43
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    44
class CustomIntCtrl(wx.lib.intctrl.IntCtrl):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    45
    """
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    46
    This class provides a control that takes and returns long as
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    47
    value, and provides bounds support and optional value limiting.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    48
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    49
    It handles entering negative numbers more user-friendly than
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    50
    original wx.lib.intctrl.IntCtrl.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    51
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    52
    It applies limits as focus is changed to other control and
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    53
    sends event afterwards to signal that editing is done.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    54
    """
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    55
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    56
    # Used to trap events indicating that the current
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    57
    # integer value of the control has been changed.
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    58
    wxEVT_COMMAND_CUSTOM_INT_UPDATED = wx.NewEventType()
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    59
    EVT_CUSTOM_INT = wx.PyEventBinder(wxEVT_COMMAND_CUSTOM_INT_UPDATED, 1)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    60
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    61
    def __init__(self, *args, **kwargs):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    62
        wx.lib.intctrl.IntCtrl.__init__(self, *args, **kwargs)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    63
        self.Bind(wx.EVT_KILL_FOCUS, self.UpdateValue)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    64
        self.SetLongAllowed(True)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    65
        self.SetLimited(False)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    66
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    67
    def GetValue(self):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    68
        """
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    69
        Returns integer (long) value of the control,
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    70
        but handles entering negative numbers
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    71
        """
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    72
        s = wx.TextCtrl.GetValue(self)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    73
        if s == '-':
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    74
            s = ''
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    75
        return self._fromGUI(s)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    76
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    77
    def GetValueStr(self):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    78
        """Returns string value of TextCtrl"""
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    79
        return wx.TextCtrl.GetValue(self)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    80
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    81
    def UpdateValue(self, event):
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    82
        self.SetLimited(True)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    83
        self.SetLimited(False)
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    84
        try:
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    85
            self.GetEventHandler().ProcessEvent(
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    86
                CustomIntUpdatedEvent(self.GetId(), self.GetValue(), self))
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    87
        except ValueError:
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    88
            return
4e3c78a84c64 add custom TextCtrl allowed to enter long integer with bounds checking
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    89
        event.Skip()