controls/LocationCellEditor.py
author Edouard Tisserant
Mon, 01 Mar 2021 15:45:13 +0100
changeset 2727 6330e6bb345d
parent 2579 8fb5c6eddc72
child 3303 0ffb41625592
permissions -rw-r--r--
IDE: Make ST code generation more verbose, since it can be really long in case of big programs, and it is better to let the user know build is still in progress.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
     4
# 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: 1550
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
     9
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
    11
# 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: 1550
diff changeset
    12
# 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: 1550
diff changeset
    13
# 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: 1550
diff changeset
    14
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
    16
# 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: 1550
diff changeset
    17
# 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: 1550
diff changeset
    18
# 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: 1550
diff changeset
    19
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1550
diff changeset
    21
# 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: 1550
diff changeset
    22
# 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: 1550
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1836
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1836
diff changeset
    26
from __future__ import absolute_import
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
import wx
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
from dialogs.BrowseLocationsDialog import BrowseLocationsDialog
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    31
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
class LocationCellControl(wx.PyControl):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    33
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
    '''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
    Custom cell editor control with a text box and a button that launches
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
    the BrowseLocationsDialog.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    37
    '''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    38
    def __init__(self, parent):
1836
d42b6cf00fa6 fix error __init__ method from base class is not called
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    39
        wx.PyControl.__init__(self, parent)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    40
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
        main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
        main_sizer.AddGrowableCol(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
        main_sizer.AddGrowableRow(0)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    44
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
        # create location text control
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    46
        self.Location = wx.TextCtrl(self, size=wx.Size(0, -1),
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
    47
                                    style=wx.TE_PROCESS_ENTER)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
        self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    49
        main_sizer.AddWindow(self.Location, flag=wx.GROW)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    50
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    51
        # create browse button
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
        self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    53
        self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
        main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    55
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
        self.Bind(wx.EVT_SIZE, self.OnSize)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    57
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
        self.SetSizer(main_sizer)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    60
        self.Controller = None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
        self.VarType = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
        self.Default = False
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
    63
        self.VariableName = None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    65
    def __del__(self):
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    66
        self.Controller = None
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    67
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    68
    def SetController(self, controller):
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    69
        self.Controller = controller
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
    def SetVarType(self, vartype):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
        self.VarType = vartype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    74
    def GetVarType(self):
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    75
        return self.VarType
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    76
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
    def SetValue(self, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
        self.Default = value
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
    79
        self.VariableName = None
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
    80
        self.VarType = None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        self.Location.SetValue(value)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
    82
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
    def GetValue(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
        return self.Location.GetValue()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
2573
071d10c7ab8f LocationCellEditor now systematically change variable name according to name given by CTN, calls VariablePanel' OnVariableNameChange to update POU accordingly.
Edouard Tisserant
parents: 1881
diff changeset
    86
    def GetName(self):
071d10c7ab8f LocationCellEditor now systematically change variable name according to name given by CTN, calls VariablePanel' OnVariableNameChange to update POU accordingly.
Edouard Tisserant
parents: 1881
diff changeset
    87
        return self.VariableName
2579
8fb5c6eddc72 Conform to pep8 and pylint :
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2575
diff changeset
    88
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
    def OnSize(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
        self.Layout()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
    def OnBrowseButtonClick(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        # pop up the location browser dialog
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
    94
        dialog = BrowseLocationsDialog(self, self.VarType, self.Controller)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
        if dialog.ShowModal() == wx.ID_OK:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
            infos = dialog.GetValues()
1004
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
    97
        else:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
    98
            infos = None
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
    99
        dialog.Destroy()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   100
1004
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   101
        if infos is not None:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   102
            location = infos["location"]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
            # set the location
1004
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   104
            if not infos["location"].startswith("%"):
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   105
                dialog = wx.SingleChoiceDialog(
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   106
                    self,
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   107
                    _("Select a variable class:"),
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   108
                    _("Variable class"),
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   109
                    [_("Input"), _("Output"), _("Memory")],
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   110
                    wx.DEFAULT_DIALOG_STYLE | wx.OK | wx.CANCEL)
1004
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   111
                if dialog.ShowModal() == wx.ID_OK:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   112
                    selected = dialog.GetSelection()
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   113
                else:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   114
                    selected = None
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   115
                dialog.Destroy()
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   116
                if selected is None:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   117
                    self.Location.SetFocus()
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   118
                    return
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   119
                if selected == 0:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   120
                    location = "%I" + location
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   121
                elif selected == 1:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   122
                    location = "%Q" + location
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   123
                else:
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   124
                    location = "%M" + location
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   125
1004
bf3b6998f7b6 Added support in LocationCellEditor for locations with undefined direction
Laurent Bessard
parents: 855
diff changeset
   126
            self.Location.SetValue(location)
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   127
            self.VariableName = infos["var_name"]
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
   128
            self.VarType = infos["IEC_type"]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
2575
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   130
            # when user selected something, end editing immediately
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   131
            # so that changes over multiple colums appear
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   132
            wx.CallAfter(self.Parent.Parent.CloseEditControl)
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   133
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
        self.Location.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
    def OnLocationChar(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
        keycode = event.GetKeyCode()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
        if keycode in [wx.WXK_RETURN, wx.WXK_TAB]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
            self.Parent.Parent.ProcessEvent(event)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
        elif keycode == wx.WXK_ESCAPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
            self.Location.SetValue(self.Default)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
            self.Parent.Parent.CloseEditControl()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
            event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
    def SetInsertionPoint(self, i):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
        self.Location.SetInsertionPoint(i)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   148
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
    def SetFocus(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
        self.Location.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   152
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
class LocationCellEditor(wx.grid.PyGridCellEditor):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
    '''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
    Grid cell editor that uses LocationCellControl to display a browse button.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
    '''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   157
    def __init__(self, table, controller):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
        wx.grid.PyGridCellEditor.__init__(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   159
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
        self.Table = table
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
        self.Controller = controller
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
    def __del__(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
        self.CellControl = None
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
   165
        self.Controller = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   166
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   167
    def Create(self, parent, id, evt_handler):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
        self.CellControl = LocationCellControl(parent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
        self.SetControl(self.CellControl)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
        if evt_handler:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
            self.CellControl.PushEventHandler(evt_handler)
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 BeginEdit(self, row, col, grid):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
        self.CellControl.Enable()
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
   175
        self.CellControl.SetController(self.Controller)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
        self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
        if isinstance(self.CellControl, LocationCellControl):
855
b30421d07e8c Adding support for selecting variable type filtering in BrowseLocationsDialog and modify variable type when validated
Laurent Bessard
parents: 814
diff changeset
   178
            self.CellControl.SetVarType(self.Table.GetValueByName(row, 'Type'))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   179
        self.CellControl.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   180
1536
3518d10487db fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1004
diff changeset
   181
    def EndEditInternal(self, row, col, grid, old_loc):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   182
        loc = self.CellControl.GetValue()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
        changed = loc != old_loc
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
        if changed:
2573
071d10c7ab8f LocationCellEditor now systematically change variable name according to name given by CTN, calls VariablePanel' OnVariableNameChange to update POU accordingly.
Edouard Tisserant
parents: 1881
diff changeset
   185
            name = self.CellControl.GetName()
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   186
            if name is not None:
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   187
                message = self.Table.Parent.CheckVariableName(name, row)
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   188
                if message is not None:
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   189
                    wx.CallAfter(self.Table.Parent.ShowErrorMessage, message)
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   190
                    return None
2579
8fb5c6eddc72 Conform to pep8 and pylint :
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2575
diff changeset
   191
                old_name = self.Table.GetValueByName(row, 'Name')
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   192
                self.Table.SetValueByName(row, 'Name', name)
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   193
                self.Table.Parent.OnVariableNameChange(old_name, name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
            self.Table.SetValueByName(row, 'Location', loc)
2574
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   195
            var_type = self.CellControl.GetVarType()
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   196
            if var_type is not None:
48a486000e84 LocationCellEditor use better source for variable name replacement, and validate variable name before applying it or complains otherwise.
Edouard Tisserant
parents: 2573
diff changeset
   197
                self.Table.SetValueByName(row, 'Type', var_type)
2575
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   198
        else:
aee08bd135d8 Rename checkbox and immediately apply.
Edouard Tisserant
parents: 2574
diff changeset
   199
            wx.CallAfter(self.Table.Parent.ShowErrorMessage,
2579
8fb5c6eddc72 Conform to pep8 and pylint :
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2575
diff changeset
   200
                         _("Selected location is identical to previous one"))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   201
        self.CellControl.Disable()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
        return changed
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   203
1536
3518d10487db fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1004
diff changeset
   204
    if wx.VERSION >= (3, 0, 0):
3518d10487db fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1004
diff changeset
   205
        def EndEdit(self, row, col, grid, oldval):
1550
deadfe7377e9 fix not saving location address from variable panel
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1540
diff changeset
   206
            return self.EndEditInternal(row, col, grid, oldval)
1536
3518d10487db fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1004
diff changeset
   207
    else:
3518d10487db fix issue, that was impossible to change variable location IEC-address, when wx 3.0 is used
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1004
diff changeset
   208
        def EndEdit(self, row, col, grid):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   209
            old_loc = self.Table.GetValueByName(row, 'Location')
1550
deadfe7377e9 fix not saving location address from variable panel
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1540
diff changeset
   210
            return self.EndEditInternal(row, col, grid, old_loc)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1578
diff changeset
   211
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   212
    def SetSize(self, rect):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   213
        self.CellControl.SetDimensions(rect.x + 1, rect.y,
1767
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
   214
                                       rect.width, rect.height,
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1745
diff changeset
   215
                                       wx.SIZE_ALLOW_MINUS_ONE)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   216
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   217
    def Clone(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   218
        return LocationCellEditor(self.Table, self.Controller)