author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Fri, 22 Sep 2017 18:06:15 +0300 | |
changeset 1829 | a776ac02b079 |
parent 1768 | 691083b5682a |
child 1881 | 091005ec69c4 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
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 | 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 | 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 | 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 | 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 | 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 | 25 |
|
26 |
import wx |
|
27 |
||
28 |
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
|
29 |
from util.TranslationCatalogs import NoTranslate |
814 | 30 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
31 |
|
814 | 32 |
def GetPouTypes(): |
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
|
33 |
_ = NoTranslate |
814 | 34 |
return [_("function"), _("functionBlock"), _("program")] |
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
35 |
|
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1745
diff
changeset
|
36 |
|
814 | 37 |
POU_TYPES_DICT = dict([(_(pou_type), pou_type) for pou_type in GetPouTypes()]) |
38 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
39 |
|
814 | 40 |
def GetPouLanguages(): |
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
|
41 |
_ = NoTranslate |
814 | 42 |
return [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")] |
43 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset
|
44 |
|
814 | 45 |
class PouDialog(wx.Dialog): |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
46 |
|
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
47 |
POU_LANGUAGES = GetPouLanguages() |
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
48 |
POU_LANGUAGES_DICT = dict([(_(language), language) for language in POU_LANGUAGES]) |
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
49 |
|
1708
24416137cda7
add dialog "add program", that appears after creating new project
Surkov Sergey <surkovsv93@gmail.com>
parents:
1696
diff
changeset
|
50 |
def __init__(self, parent, pou_type=None, type_readonly=False): |
814 | 51 |
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
|
52 |
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
|
53 |
style=wx.DEFAULT_DIALOG_STYLE) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
54 |
|
814 | 55 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
56 |
main_sizer.AddGrowableCol(0) |
|
57 |
main_sizer.AddGrowableRow(0) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
58 |
|
814 | 59 |
infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) |
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 | 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 | 68 |
self.PouName = wx.TextCtrl(self) |
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 | 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 | 75 |
self.PouType = wx.ComboBox(self, style=wx.CB_READONLY) |
76 |
self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, self.PouType) |
|
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 | 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 | 83 |
self.Language = wx.ComboBox(self, style=wx.CB_READONLY) |
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 | 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 | 91 |
self.SetSizer(main_sizer) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
92 |
|
814 | 93 |
for option in GetPouTypes(): |
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 | 96 |
if pou_type is not None: |
97 |
self.PouType.SetStringSelection(_(pou_type)) |
|
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 | 100 |
self.PouNames = [] |
101 |
self.PouElementNames = [] |
|
102 |
||
103 |
def OnOK(self, event): |
|
104 |
error = [] |
|
105 |
pou_name = self.PouName.GetValue() |
|
106 |
if pou_name == "": |
|
107 |
error.append(_("POU Name")) |
|
108 |
if self.PouType.GetSelection() == -1: |
|
109 |
error.append(_("POU Type")) |
|
110 |
if self.Language.GetSelection() == -1: |
|
111 |
error.append(_("Language")) |
|
112 |
message = None |
|
113 |
question = False |
|
114 |
if len(error) > 0: |
|
115 |
text = "" |
|
116 |
for i, item in enumerate(error): |
|
117 |
if i == 0: |
|
118 |
text += item |
|
119 |
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
|
120 |
text += _(" and %s") % item |
814 | 121 |
else: |
1734
750eeb7230a1
clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1708
diff
changeset
|
122 |
text += _(", %s") % item |
814 | 123 |
message = _("Form isn't complete. %s must be filled!") % text |
124 |
elif not TestIdentifier(pou_name): |
|
125 |
message = _("\"%s\" is not a valid identifier!") % pou_name |
|
126 |
elif pou_name.upper() in IEC_KEYWORDS: |
|
127 |
message = _("\"%s\" is a keyword. It can't be used!") % pou_name |
|
128 |
elif pou_name.upper() in self.PouNames: |
|
129 |
message = _("\"%s\" pou already exists!") % pou_name |
|
130 |
elif pou_name.upper() in self.PouElementNames: |
|
131 |
message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name |
|
132 |
question = True |
|
133 |
if message is not None: |
|
134 |
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
|
135 |
dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO | wx.ICON_EXCLAMATION) |
814 | 136 |
result = dialog.ShowModal() |
137 |
dialog.Destroy() |
|
138 |
if result == wx.ID_YES: |
|
139 |
self.EndModal(wx.ID_OK) |
|
140 |
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
|
141 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK | wx.ICON_ERROR) |
814 | 142 |
dialog.ShowModal() |
143 |
dialog.Destroy() |
|
144 |
else: |
|
145 |
self.EndModal(wx.ID_OK) |
|
146 |
||
147 |
def RefreshLanguage(self): |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
148 |
selection = self.POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") |
814 | 149 |
self.Language.Clear() |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
150 |
for language in self.POU_LANGUAGES: |
814 | 151 |
if language != "SFC" or POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": |
152 |
self.Language.Append(_(language)) |
|
153 |
if self.Language.FindString(_(selection)) != wx.NOT_FOUND: |
|
154 |
self.Language.SetStringSelection(_(selection)) |
|
155 |
||
156 |
def OnTypeChanged(self, event): |
|
157 |
self.RefreshLanguage() |
|
158 |
event.Skip() |
|
159 |
||
160 |
def SetPouNames(self, pou_names): |
|
161 |
self.PouNames = [pou_name.upper() for pou_name in pou_names] |
|
162 |
||
163 |
def SetPouElementNames(self, element_names): |
|
164 |
self.PouElementNames = [element_name.upper() for element_name in element_names] |
|
165 |
||
166 |
def SetValues(self, values): |
|
167 |
for item, value in values.items(): |
|
168 |
if item == "pouName": |
|
169 |
self.PouName.SetValue(value) |
|
170 |
elif item == "pouType": |
|
171 |
self.PouType.SetStringSelection(_(value)) |
|
172 |
elif item == "language": |
|
173 |
self.Language.SetStringSelection(_(value)) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
174 |
|
814 | 175 |
def GetValues(self): |
176 |
values = {} |
|
177 |
values["pouName"] = self.PouName.GetValue() |
|
178 |
values["pouType"] = POU_TYPES_DICT[self.PouType.GetStringSelection()] |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
179 |
values["language"] = self.POU_LANGUAGES_DICT[self.Language.GetStringSelection()] |
814 | 180 |
return values |