author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 19 Jan 2017 13:56:09 +0300 | |
changeset 1639 | 1953c268a194 |
parent 1571 | 486f94a8032c |
child 1696 | 8043f32de7b8 |
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 |
814 | 8 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1421
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
814 | 10 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1421
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:
1421
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:
1421
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:
1421
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
814 | 15 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1421
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:
1421
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:
1421
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:
1421
diff
changeset
|
19 |
# GNU General Public License for more details. |
814 | 20 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1421
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:
1421
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:
1421
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
25 |
import wx |
|
26 |
||
27 |
from plcopen.structures import TestIdentifier, IEC_KEYWORDS |
|
28 |
||
29 |
def GetPouTypes(): |
|
30 |
_ = lambda x : x |
|
31 |
return [_("function"), _("functionBlock"), _("program")] |
|
32 |
POU_TYPES_DICT = dict([(_(pou_type), pou_type) for pou_type in GetPouTypes()]) |
|
33 |
||
34 |
def GetPouLanguages(): |
|
35 |
_ = lambda x : x |
|
36 |
return [_("IL"), _("ST"), _("LD"), _("FBD"), _("SFC")] |
|
37 |
||
38 |
class PouDialog(wx.Dialog): |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
39 |
|
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
40 |
POU_LANGUAGES = GetPouLanguages() |
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
41 |
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
|
42 |
|
814 | 43 |
def __init__(self, parent, pou_type = None): |
44 |
wx.Dialog.__init__(self, id=-1, parent=parent, |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
45 |
name='PouDialog', title=_('Create a new POU'), |
814 | 46 |
size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE) |
47 |
self.SetClientSize(wx.Size(300, 200)) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
48 |
|
814 | 49 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
50 |
main_sizer.AddGrowableCol(0) |
|
51 |
main_sizer.AddGrowableRow(0) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
52 |
|
814 | 53 |
infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) |
54 |
infos_sizer.AddGrowableCol(1) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
55 |
main_sizer.AddSizer(infos_sizer, border=20, |
814 | 56 |
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
|
57 |
|
814 | 58 |
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
|
59 |
infos_sizer.AddWindow(pouname_label, border=4, |
814 | 60 |
flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
61 |
|
814 | 62 |
self.PouName = wx.TextCtrl(self) |
63 |
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
|
64 |
|
814 | 65 |
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
|
66 |
infos_sizer.AddWindow(poutype_label, border=4, |
814 | 67 |
flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
68 |
|
814 | 69 |
self.PouType = wx.ComboBox(self, style=wx.CB_READONLY) |
70 |
self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, self.PouType) |
|
71 |
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
|
72 |
|
814 | 73 |
language_label = wx.StaticText(self, label=_('Language:')) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
74 |
infos_sizer.AddWindow(language_label, border=4, |
814 | 75 |
flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
76 |
|
814 | 77 |
self.Language = wx.ComboBox(self, style=wx.CB_READONLY) |
78 |
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
|
79 |
|
814 | 80 |
button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
81 |
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
|
82 |
main_sizer.AddSizer(button_sizer, border=20, |
814 | 83 |
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
|
84 |
|
814 | 85 |
self.SetSizer(main_sizer) |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
86 |
|
814 | 87 |
for option in GetPouTypes(): |
88 |
self.PouType.Append(_(option)) |
|
89 |
if pou_type is not None: |
|
90 |
self.PouType.SetStringSelection(_(pou_type)) |
|
91 |
self.RefreshLanguage() |
|
92 |
||
93 |
self.PouNames = [] |
|
94 |
self.PouElementNames = [] |
|
95 |
||
96 |
def OnOK(self, event): |
|
97 |
error = [] |
|
98 |
pou_name = self.PouName.GetValue() |
|
99 |
if pou_name == "": |
|
100 |
error.append(_("POU Name")) |
|
101 |
if self.PouType.GetSelection() == -1: |
|
102 |
error.append(_("POU Type")) |
|
103 |
if self.Language.GetSelection() == -1: |
|
104 |
error.append(_("Language")) |
|
105 |
message = None |
|
106 |
question = False |
|
107 |
if len(error) > 0: |
|
108 |
text = "" |
|
109 |
for i, item in enumerate(error): |
|
110 |
if i == 0: |
|
111 |
text += item |
|
112 |
elif i == len(error) - 1: |
|
113 |
text += _(" and %s")%item |
|
114 |
else: |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
115 |
text += _(", %s")%item |
814 | 116 |
message = _("Form isn't complete. %s must be filled!") % text |
117 |
elif not TestIdentifier(pou_name): |
|
118 |
message = _("\"%s\" is not a valid identifier!") % pou_name |
|
119 |
elif pou_name.upper() in IEC_KEYWORDS: |
|
120 |
message = _("\"%s\" is a keyword. It can't be used!") % pou_name |
|
121 |
elif pou_name.upper() in self.PouNames: |
|
122 |
message = _("\"%s\" pou already exists!") % pou_name |
|
123 |
elif pou_name.upper() in self.PouElementNames: |
|
124 |
message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name |
|
125 |
question = True |
|
126 |
if message is not None: |
|
127 |
if question: |
|
128 |
dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) |
|
129 |
result = dialog.ShowModal() |
|
130 |
dialog.Destroy() |
|
131 |
if result == wx.ID_YES: |
|
132 |
self.EndModal(wx.ID_OK) |
|
133 |
else: |
|
134 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
135 |
dialog.ShowModal() |
|
136 |
dialog.Destroy() |
|
137 |
else: |
|
138 |
self.EndModal(wx.ID_OK) |
|
139 |
||
140 |
def RefreshLanguage(self): |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
141 |
selection = self.POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") |
814 | 142 |
self.Language.Clear() |
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
143 |
for language in self.POU_LANGUAGES: |
814 | 144 |
if language != "SFC" or POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": |
145 |
self.Language.Append(_(language)) |
|
146 |
if self.Language.FindString(_(selection)) != wx.NOT_FOUND: |
|
147 |
self.Language.SetStringSelection(_(selection)) |
|
148 |
||
149 |
def OnTypeChanged(self, event): |
|
150 |
self.RefreshLanguage() |
|
151 |
event.Skip() |
|
152 |
||
153 |
def SetPouNames(self, pou_names): |
|
154 |
self.PouNames = [pou_name.upper() for pou_name in pou_names] |
|
155 |
||
156 |
def SetPouElementNames(self, element_names): |
|
157 |
self.PouElementNames = [element_name.upper() for element_name in element_names] |
|
158 |
||
159 |
def SetValues(self, values): |
|
160 |
for item, value in values.items(): |
|
161 |
if item == "pouName": |
|
162 |
self.PouName.SetValue(value) |
|
163 |
elif item == "pouType": |
|
164 |
self.PouType.SetStringSelection(_(value)) |
|
165 |
elif item == "language": |
|
166 |
self.Language.SetStringSelection(_(value)) |
|
1421
0e1a6fcf4670
Enable overloading of availables languages in POU creation dialog
Edouard Tisserant
parents:
814
diff
changeset
|
167 |
|
814 | 168 |
def GetValues(self): |
169 |
values = {} |
|
170 |
values["pouName"] = self.PouName.GetValue() |
|
171 |
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
|
172 |
values["language"] = self.POU_LANGUAGES_DICT[self.Language.GetStringSelection()] |
814 | 173 |
return values |