|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 #based on the plcopen standard. |
|
6 # |
|
7 #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD |
|
8 # |
|
9 #See COPYING file for copyrights details. |
|
10 # |
|
11 #This library is free software; you can redistribute it and/or |
|
12 #modify it under the terms of the GNU General Public |
|
13 #License as published by the Free Software Foundation; either |
|
14 #version 2.1 of the License, or (at your option) any later version. |
|
15 # |
|
16 #This library is distributed in the hope that it will be useful, |
|
17 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 #General Public License for more details. |
|
20 # |
|
21 #You should have received a copy of the GNU General Public |
|
22 #License along with this library; if not, write to the Free Software |
|
23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
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 POU_LANGUAGES_DICT = dict([(_(language), language) for language in GetPouLanguages()]) |
|
38 |
|
39 class PouDialog(wx.Dialog): |
|
40 |
|
41 def __init__(self, parent, pou_type = None): |
|
42 wx.Dialog.__init__(self, id=-1, parent=parent, |
|
43 name='PouDialog', title=_('Create a new POU'), |
|
44 size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE) |
|
45 self.SetClientSize(wx.Size(300, 200)) |
|
46 |
|
47 main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
|
48 main_sizer.AddGrowableCol(0) |
|
49 main_sizer.AddGrowableRow(0) |
|
50 |
|
51 infos_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=3, vgap=15) |
|
52 infos_sizer.AddGrowableCol(1) |
|
53 main_sizer.AddSizer(infos_sizer, 0, border=20, |
|
54 flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
55 |
|
56 pouname_label = wx.StaticText(id=-1, parent=self, |
|
57 label=_('POU Name:'), name='pouname_label', |
|
58 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
59 infos_sizer.AddWindow(pouname_label, 0, border=4, |
|
60 flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
|
61 |
|
62 self.PouName = wx.TextCtrl(id=-1, parent=self, |
|
63 name='POUName', pos=wx.Point(0, 0), |
|
64 size=wx.Size(0, 24), style=0) |
|
65 infos_sizer.AddWindow(self.PouName, 0, border=0, flag=wx.GROW) |
|
66 |
|
67 poutype_label = wx.StaticText(id=-1, parent=self, |
|
68 label=_('POU Type:'), name='poutype_label', |
|
69 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
70 infos_sizer.AddWindow(poutype_label, 0, border=4, |
|
71 flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
|
72 |
|
73 self.PouType = wx.ComboBox(id=-1, parent=self, |
|
74 name='POUType', pos=wx.Point(0, 0), |
|
75 size=wx.Size(0, 28), style=wx.CB_READONLY) |
|
76 self.Bind(wx.EVT_COMBOBOX, self.OnTypeChanged, id=self.PouType.GetId()) |
|
77 infos_sizer.AddWindow(self.PouType, 0, border=0, flag=wx.GROW) |
|
78 |
|
79 language_label = wx.StaticText(id=-1, parent=self, |
|
80 label=_('Language:'), name='language_label', |
|
81 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
82 infos_sizer.AddWindow(language_label, 0, border=4, |
|
83 flag=wx.ALIGN_CENTER_VERTICAL|wx.TOP) |
|
84 |
|
85 self.Language = wx.ComboBox(id=-1, parent=self, |
|
86 name='Language', pos=wx.Point(0, 0), |
|
87 size=wx.Size(0, 28), style=wx.CB_READONLY) |
|
88 infos_sizer.AddWindow(self.Language, 0, border=0, flag=wx.GROW) |
|
89 |
|
90 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
91 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) |
|
92 main_sizer.AddSizer(self.ButtonSizer, 0, border=20, |
|
93 flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
94 |
|
95 self.SetSizer(main_sizer) |
|
96 |
|
97 for option in GetPouTypes(): |
|
98 self.PouType.Append(_(option)) |
|
99 if pou_type is not None: |
|
100 self.PouType.SetStringSelection(_(pou_type)) |
|
101 self.RefreshLanguage() |
|
102 |
|
103 self.PouNames = [] |
|
104 self.PouElementNames = [] |
|
105 |
|
106 def OnOK(self, event): |
|
107 error = [] |
|
108 pou_name = self.PouName.GetValue() |
|
109 if pou_name == "": |
|
110 error.append(_("POU Name")) |
|
111 if self.PouType.GetSelection() == -1: |
|
112 error.append(_("POU Type")) |
|
113 if self.Language.GetSelection() == -1: |
|
114 error.append(_("Language")) |
|
115 message = None |
|
116 question = False |
|
117 if len(error) > 0: |
|
118 text = "" |
|
119 for i, item in enumerate(error): |
|
120 if i == 0: |
|
121 text += item |
|
122 elif i == len(error) - 1: |
|
123 text += _(" and %s")%item |
|
124 else: |
|
125 text += _(", %s")%item |
|
126 message = _("Form isn't complete. %s must be filled!") % text |
|
127 elif not TestIdentifier(pou_name): |
|
128 message = _("\"%s\" is not a valid identifier!") % pou_name |
|
129 elif pou_name.upper() in IEC_KEYWORDS: |
|
130 message = _("\"%s\" is a keyword. It can't be used!") % pou_name |
|
131 elif pou_name.upper() in self.PouNames: |
|
132 message = _("\"%s\" pou already exists!") % pou_name |
|
133 elif pou_name.upper() in self.PouElementNames: |
|
134 message = _("A POU has an element named \"%s\". This could cause a conflict. Do you wish to continue?") % pou_name |
|
135 question = True |
|
136 if message is not None: |
|
137 if question: |
|
138 dialog = wx.MessageDialog(self, message, _("Warning"), wx.YES_NO|wx.ICON_EXCLAMATION) |
|
139 result = dialog.ShowModal() |
|
140 dialog.Destroy() |
|
141 if result == wx.ID_YES: |
|
142 self.EndModal(wx.ID_OK) |
|
143 else: |
|
144 dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
145 dialog.ShowModal() |
|
146 dialog.Destroy() |
|
147 else: |
|
148 self.EndModal(wx.ID_OK) |
|
149 |
|
150 def RefreshLanguage(self): |
|
151 selection = POU_LANGUAGES_DICT.get(self.Language.GetStringSelection(), "") |
|
152 self.Language.Clear() |
|
153 for language in GetPouLanguages(): |
|
154 if language != "SFC" or POU_TYPES_DICT[self.PouType.GetStringSelection()] != "function": |
|
155 self.Language.Append(_(language)) |
|
156 if self.Language.FindString(_(selection)) != wx.NOT_FOUND: |
|
157 self.Language.SetStringSelection(_(selection)) |
|
158 |
|
159 def OnTypeChanged(self, event): |
|
160 self.RefreshLanguage() |
|
161 event.Skip() |
|
162 |
|
163 def SetPouNames(self, pou_names): |
|
164 self.PouNames = [pou_name.upper() for pou_name in pou_names] |
|
165 |
|
166 def SetPouElementNames(self, element_names): |
|
167 self.PouElementNames = [element_name.upper() for element_name in element_names] |
|
168 |
|
169 def SetValues(self, values): |
|
170 for item, value in values.items(): |
|
171 if item == "pouName": |
|
172 self.PouName.SetValue(value) |
|
173 elif item == "pouType": |
|
174 self.PouType.SetStringSelection(_(value)) |
|
175 elif item == "language": |
|
176 self.Language.SetStringSelection(_(value)) |
|
177 |
|
178 def GetValues(self): |
|
179 values = {} |
|
180 values["pouName"] = self.PouName.GetValue() |
|
181 values["pouType"] = POU_TYPES_DICT[self.PouType.GetStringSelection()] |
|
182 values["language"] = POU_LANGUAGES_DICT[self.Language.GetStringSelection()] |
|
183 return values |
|
184 |
|
185 |