dialogs/PouDialog.py
author Edouard Tisserant
Mon, 29 Mar 2021 15:46:02 +0200
changeset 2734 3768af52835a
parent 2299 8df93571af65
child 3303 0ffb41625592
permissions -rw-r--r--
Fixed misuse of GridCellEditor in DurationCellEditor, potentially leading to crash on windows.
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: 1421
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: 1421
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: 1421
diff changeset
     7
# Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
1696
8043f32de7b8 make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
     8
# Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
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: 1421
diff changeset
    10
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1421
diff changeset
    12
# 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: 1421
diff changeset
    13
# 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: 1421
diff changeset
    14
# 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: 1421
diff changeset
    15
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1421
diff changeset
    17
# 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: 1421
diff changeset
    18
# 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: 1421
diff changeset
    19
# 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: 1421
diff changeset
    20
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    21
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1421
diff changeset
    22
# 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: 1421
diff changeset
    23
# 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: 1421
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    25
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    26
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
    27
from __future__ import absolute_import
814
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
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
from plcopen.structures import TestIdentifier, IEC_KEYWORDS
1762
fcc406143e5b clean-up: fix PEP8 E731 do not assign a lambda expression, use a def
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    31
from util.TranslationCatalogs import NoTranslate
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    33
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
class PouDialog(wx.Dialog):
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    35
    """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    36
    Dialog to create new POU.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    37
    It allows selection of POU type, name and
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    38
    used IEC 61131-3 programming language
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    39
    """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    40
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    41
    POU_TYPES = None
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    42
    POU_TYPES_DICT = None
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    43
    POU_LANGUAGES = None
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    44
    POU_LANGUAGES_DICT = None
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    45
1708
24416137cda7 add dialog "add program", that appears after creating new project
Surkov Sergey <surkovsv93@gmail.com>
parents: 1696
diff changeset
    46
    def __init__(self, parent, pou_type=None, type_readonly=False):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
        wx.Dialog.__init__(self, id=-1, parent=parent,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    48
                           name='PouDialog', title=_('Create a new POU'),
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    49
                           style=wx.DEFAULT_DIALOG_STYLE)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    50
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    51
        if PouDialog.POU_TYPES_DICT is None:
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    52
            self.InitPouTypesDict()
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    53
            self.InitPouLanguagesDict()
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    54
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    55
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
        main_sizer.AddGrowableCol(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    57
        main_sizer.AddGrowableRow(0)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    58
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
        infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    60
        infos_sizer.AddGrowableCol(1)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    61
        main_sizer.AddSizer(infos_sizer, border=20,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    62
                            flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    63
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
        pouname_label = wx.StaticText(self, label=_('POU Name:'))
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    65
        infos_sizer.AddWindow(pouname_label, border=4,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    66
                              flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    67
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
        self.PouName = wx.TextCtrl(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
        infos_sizer.AddWindow(self.PouName, flag=wx.GROW)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    70
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
        poutype_label = wx.StaticText(self, label=_('POU Type:'))
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    72
        infos_sizer.AddWindow(poutype_label, border=4,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    73
                              flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    74
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
        self.PouType = wx.ComboBox(self, style=wx.CB_READONLY)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, self.PouType)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
        infos_sizer.AddWindow(self.PouType, flag=wx.GROW)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    78
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
        language_label = wx.StaticText(self, label=_('Language:'))
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    80
        infos_sizer.AddWindow(language_label, border=4,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    81
                              flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    82
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
        self.Language = wx.ComboBox(self, style=wx.CB_READONLY)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
        infos_sizer.AddWindow(self.Language, flag=wx.GROW)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    85
1745
f9d32913bad4 clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    86
        button_sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
        self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    88
        main_sizer.AddSizer(button_sizer, border=20,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
    89
                            flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    90
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
        self.SetSizer(main_sizer)
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
    92
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
    93
        for option in self.POU_TYPES:
1708
24416137cda7 add dialog "add program", that appears after creating new project
Surkov Sergey <surkovsv93@gmail.com>
parents: 1696
diff changeset
    94
            if not type_readonly or _(option) == _(pou_type):
24416137cda7 add dialog "add program", that appears after creating new project
Surkov Sergey <surkovsv93@gmail.com>
parents: 1696
diff changeset
    95
                self.PouType.Append(_(option))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
        if pou_type is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
            self.PouType.SetStringSelection(_(pou_type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
        self.RefreshLanguage()
1696
8043f32de7b8 make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    99
        self.Fit()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
        self.PouNames = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
        self.PouElementNames = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   103
    def InitPouTypes(self):
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   104
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   105
        Initialize POU_TYPES class list.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   106
        This list contains not translated POU types used in PLCopen XML.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   107
        _() are necessary so mk18n.py could find these string for localization.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   108
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   109
        _ = NoTranslate
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   110
        self.__class__.POU_TYPES = [_("function"), _("functionBlock"), _("program")]
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   111
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   112
    def InitPouTypesDict(self):
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   113
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   114
        Initialize POU_TYPES_DICT class dictionary and POU_TYPE list
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   115
        Dictionary contains localized POU types and they are shown in UI.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   116
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   117
        self.InitPouTypes()
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   118
        self.__class__.POU_TYPES_DICT = dict([(_(pou_type), pou_type)
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   119
                                              for pou_type in self.POU_TYPES])
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   120
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   121
    def InitPouLanguages(self):
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   122
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   123
        Initialize POU_LANGUAGES class list.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   124
        This list contains not translated programming languages used in PLCopen XML.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   125
        _() are necessary so mk18n.py could find these string for localization.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   126
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   127
        _ = NoTranslate
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   128
        self.__class__.POU_LANGUAGES = [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")]
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   129
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   130
    def InitPouLanguagesDict(self):
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   131
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   132
        Initialize POU_LANGUAGES_DICT class dictionary and POU_LANGUAGES list
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   133
        Dictionary contains localized names for programming languages
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   134
        as they are shown in UI.
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   135
        """
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   136
        self.InitPouLanguages()
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   137
        self.__class__.POU_LANGUAGES_DICT = dict([(_(language), language)
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   138
                                                  for language in self.POU_LANGUAGES])
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   139
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
    def OnOK(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
        error = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
        pou_name = self.PouName.GetValue()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
        if pou_name == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
            error.append(_("POU Name"))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
        if self.PouType.GetSelection() == -1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
            error.append(_("POU Type"))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
        if self.Language.GetSelection() == -1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
            error.append(_("Language"))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
        message = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
        question = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
        if len(error) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
            text = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
            for i, item in enumerate(error):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
                if i == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
                    text += item
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
                elif i == len(error) - 1:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1708
diff changeset
   157
                    text += _(" and %s") % item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
                else:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1708
diff changeset
   159
                    text += _(", %s") % item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
            message = _("Form isn't complete. %s must be filled!") % text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
        elif not TestIdentifier(pou_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
            message = _("\"%s\" is not a valid identifier!") % pou_name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
        elif pou_name.upper() in IEC_KEYWORDS:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
            message = _("\"%s\" is a keyword. It can't be used!") % pou_name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   165
        elif pou_name.upper() in self.PouNames:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
            message = _("\"%s\" pou already exists!") % pou_name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   167
        elif pou_name.upper() in self.PouElementNames:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
            message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
            question = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
        if message is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
            if question:
1745
f9d32913bad4 clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   172
                dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO | wx.ICON_EXCLAMATION)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   173
                result = dialog.ShowModal()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
                dialog.Destroy()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
                if result == wx.ID_YES:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
                    self.EndModal(wx.ID_OK)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
            else:
1745
f9d32913bad4 clean-up: fix PEP8 E227 missing whitespace around bitwise or shift operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   178
                dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   179
                dialog.ShowModal()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   180
                dialog.Destroy()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   181
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   182
            self.EndModal(wx.ID_OK)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
    def RefreshLanguage(self):
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
   185
        selection = self.POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "")
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   186
        self.Language.Clear()
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
   187
        for language in self.POU_LANGUAGES:
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   188
            if language != "SFC" or self.POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function":
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   189
                self.Language.Append(_(language))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   190
        if self.Language.FindString(_(selection)) != wx.NOT_FOUND:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   191
            self.Language.SetStringSelection(_(selection))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   192
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   193
    def OnTypeChanged(self, event):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
        self.RefreshLanguage()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   195
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   196
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   197
    def SetPouNames(self, pou_names):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   198
        self.PouNames = [pou_name.upper() for pou_name in pou_names]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   199
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   200
    def SetPouElementNames(self, element_names):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   201
        self.PouElementNames = [element_name.upper() for element_name in element_names]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   203
    def SetValues(self, values):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   204
        for item, value in values.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   205
            if item == "pouName":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
                self.PouName.SetValue(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   207
            elif item == "pouType":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   208
                self.PouType.SetStringSelection(_(value))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
            elif item == "language":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   210
                self.Language.SetStringSelection(_(value))
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
   211
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   212
    def GetValues(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   213
        values = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   214
        values["pouName"] = self.PouName.GetValue()
2299
8df93571af65 Avoid dictionary initialization during import in PouDialog
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   215
        values["pouType"] = self.POU_TYPES_DICT[self.PouType.GetStringSelection()]
1421
0e1a6fcf4670 Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents: 814
diff changeset
   216
        values["language"] = self.POU_LANGUAGES_DICT[self.Language.GetStringSelection()]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   217
        return values