|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
4 #based on the plcopen standard. |
|
5 # |
|
6 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
7 # |
|
8 #See COPYING file for copyrights details. |
|
9 # |
|
10 #This library is free software; you can redistribute it and/or |
|
11 #modify it under the terms of the GNU General Public |
|
12 #License as published by the Free Software Foundation; either |
|
13 #version 2.1 of the License, or (at your option) any later version. |
|
14 # |
|
15 #This library is distributed in the hope that it will be useful, |
|
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 #General Public License for more details. |
|
19 # |
|
20 #You should have received a copy of the GNU General Public |
|
21 #License along with this library; if not, write to the Free Software |
|
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
23 |
|
24 import wx |
|
25 |
|
26 from graphics import * |
|
27 |
|
28 #------------------------------------------------------------------------------- |
|
29 # Edit Step Content Dialog |
|
30 #------------------------------------------------------------------------------- |
|
31 |
|
32 [ID_SFCSTEPDIALOG, ID_SFCSTEPDIALOGSPACER, |
|
33 ID_SFCSTEPDIALOGNAME, ID_SFCSTEPDIALOGPREVIEW, |
|
34 ID_SFCSTEPDIALOGCHECKBOX1, ID_SFCSTEPDIALOGCHECKBOX2, |
|
35 ID_SFCSTEPDIALOGCHECKBOX3, ID_SFCSTEPDIALOGSTATICTEXT1, |
|
36 ID_SFCSTEPDIALOGSTATICTEXT2, ID_SFCSTEPDIALOGSTATICTEXT3, |
|
37 ] = [wx.NewId() for _init_ctrls in range(10)] |
|
38 |
|
39 class SFCStepDialog(wx.Dialog): |
|
40 |
|
41 if wx.VERSION < (2, 6, 0): |
|
42 def Bind(self, event, function, id = None): |
|
43 if id is not None: |
|
44 event(self, id, function) |
|
45 else: |
|
46 event(self, function) |
|
47 |
|
48 def _init_coll_flexGridSizer1_Items(self, parent): |
|
49 parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
50 parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
51 |
|
52 def _init_coll_flexGridSizer1_Growables(self, parent): |
|
53 parent.AddGrowableCol(0) |
|
54 parent.AddGrowableRow(0) |
|
55 |
|
56 def _init_coll_MainSizer_Items(self, parent): |
|
57 parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT) |
|
58 parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT) |
|
59 |
|
60 def _init_coll_LeftGridSizer_Items(self, parent): |
|
61 parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW) |
|
62 parent.AddWindow(self.StepName, 0, border=0, flag=wx.GROW) |
|
63 parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW) |
|
64 parent.AddWindow(self.checkBox1, 0, border=0, flag=wx.GROW) |
|
65 parent.AddWindow(self.checkBox2, 0, border=0, flag=wx.GROW) |
|
66 parent.AddWindow(self.checkBox3, 0, border=0, flag=wx.GROW) |
|
67 parent.AddWindow(self.Spacer, 0, border=0, flag=wx.GROW) |
|
68 |
|
69 def _init_coll_LeftGridSizer_Growables(self, parent): |
|
70 parent.AddGrowableCol(0) |
|
71 parent.AddGrowableRow(6) |
|
72 |
|
73 def _init_coll_RightGridSizer_Items(self, parent): |
|
74 parent.AddWindow(self.staticText3, 0, border=0, flag=wx.GROW) |
|
75 parent.AddWindow(self.Preview, 0, border=0, flag=wx.GROW) |
|
76 |
|
77 def _init_coll_RightGridSizer_Growables(self, parent): |
|
78 parent.AddGrowableCol(0) |
|
79 parent.AddGrowableRow(1) |
|
80 |
|
81 def _init_sizers(self): |
|
82 self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10) |
|
83 self.MainSizer = wx.BoxSizer(wx.HORIZONTAL) |
|
84 self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=7, vgap=5) |
|
85 self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5) |
|
86 |
|
87 self._init_coll_flexGridSizer1_Items(self.flexGridSizer1) |
|
88 self._init_coll_flexGridSizer1_Growables(self.flexGridSizer1) |
|
89 self._init_coll_MainSizer_Items(self.MainSizer) |
|
90 self._init_coll_LeftGridSizer_Items(self.LeftGridSizer) |
|
91 self._init_coll_LeftGridSizer_Growables(self.LeftGridSizer) |
|
92 self._init_coll_RightGridSizer_Items(self.RightGridSizer) |
|
93 self._init_coll_RightGridSizer_Growables(self.RightGridSizer) |
|
94 |
|
95 self.SetSizer(self.flexGridSizer1) |
|
96 |
|
97 def _init_ctrls(self, prnt, ctrler): |
|
98 wx.Dialog.__init__(self, id=ID_SFCSTEPDIALOG, |
|
99 name='SFCStepDialog', parent=prnt, pos=wx.Point(376, 223), |
|
100 size=wx.Size(400, 250), style=wx.DEFAULT_DIALOG_STYLE, |
|
101 title=_('Edit Step')) |
|
102 self.SetClientSize(wx.Size(400, 250)) |
|
103 |
|
104 self.staticText1 = wx.StaticText(id=ID_SFCSTEPDIALOGSTATICTEXT1, |
|
105 label=_('Name:'), name='staticText1', parent=self, |
|
106 pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) |
|
107 |
|
108 self.staticText2 = wx.StaticText(id=ID_SFCSTEPDIALOGSTATICTEXT2, |
|
109 label=_('Connectors:'), name='staticText2', parent=self, |
|
110 pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) |
|
111 |
|
112 self.staticText3 = wx.StaticText(id=ID_SFCSTEPDIALOGSTATICTEXT3, |
|
113 label=_('Preview:'), name='staticText4', parent=self, |
|
114 pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) |
|
115 |
|
116 self.StepName = wx.TextCtrl(id=ID_SFCSTEPDIALOGNAME, |
|
117 name='Name', parent=self, pos=wx.Point(0, 0), |
|
118 size=wx.Size(0, 24), style=0) |
|
119 self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_SFCSTEPDIALOGNAME) |
|
120 |
|
121 self.checkBox1 = wx.CheckBox(id=ID_SFCSTEPDIALOGCHECKBOX1, |
|
122 label=_("Input"), name='checkBox1', parent=self, |
|
123 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) |
|
124 self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_SFCSTEPDIALOGCHECKBOX1) |
|
125 |
|
126 self.checkBox2 = wx.CheckBox(id=ID_SFCSTEPDIALOGCHECKBOX2, |
|
127 label=_("Output"), name='checkBox2', parent=self, |
|
128 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) |
|
129 self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_SFCSTEPDIALOGCHECKBOX2) |
|
130 |
|
131 self.checkBox3 = wx.CheckBox(id=ID_SFCSTEPDIALOGCHECKBOX3, |
|
132 label=_("Action"), name='checkBox3', parent=self, |
|
133 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) |
|
134 self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, id=ID_SFCSTEPDIALOGCHECKBOX3) |
|
135 |
|
136 self.Spacer = wx.Panel(id=ID_SFCSTEPDIALOGSPACER, |
|
137 name='Spacer', parent=self, pos=wx.Point(0, 0), |
|
138 size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) |
|
139 |
|
140 self.Preview = wx.Panel(id=ID_SFCSTEPDIALOGPREVIEW, |
|
141 name='Preview', parent=self, pos=wx.Point(0, 0), |
|
142 size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER) |
|
143 self.Preview.SetBackgroundColour(wx.Colour(255,255,255)) |
|
144 setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) |
|
145 setattr(self.Preview, "RefreshStepModel", lambda x:None) |
|
146 setattr(self.Preview, "GetScaling", lambda:None) |
|
147 setattr(self.Preview, "IsOfType", ctrler.IsOfType) |
|
148 |
|
149 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
150 if wx.VERSION >= (2, 5, 0): |
|
151 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) |
|
152 self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) |
|
153 else: |
|
154 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
155 wx.EVT_PAINT(self.Preview, self.OnPaint) |
|
156 |
|
157 self._init_sizers() |
|
158 |
|
159 def __init__(self, parent, controler, initial = False): |
|
160 self._init_ctrls(parent, controler) |
|
161 self.Step = None |
|
162 self.Initial = initial |
|
163 self.MinStepSize = None |
|
164 |
|
165 self.PouNames = [] |
|
166 self.Variables = [] |
|
167 self.StepNames = [] |
|
168 |
|
169 def SetPreviewFont(self, font): |
|
170 self.Preview.SetFont(font) |
|
171 |
|
172 def OnOK(self, event): |
|
173 step_name = self.StepName.GetValue() |
|
174 if step_name == "": |
|
175 message = wx.MessageDialog(self, _("You must type a name!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
176 message.ShowModal() |
|
177 message.Destroy() |
|
178 elif not TestIdentifier(step_name): |
|
179 message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
180 message.ShowModal() |
|
181 message.Destroy() |
|
182 elif step_name.upper() in IEC_KEYWORDS: |
|
183 message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
184 message.ShowModal() |
|
185 message.Destroy() |
|
186 elif step_name.upper() in self.PouNames: |
|
187 message = wx.MessageDialog(self, _("A pou with \"%s\" as name exists!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
188 message.ShowModal() |
|
189 message.Destroy() |
|
190 elif step_name.upper() in self.Variables: |
|
191 message = wx.MessageDialog(self, _("A variable with \"%s\" as name already exists in this pou!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
192 message.ShowModal() |
|
193 message.Destroy() |
|
194 elif step_name.upper() in self.StepNames: |
|
195 message = wx.MessageDialog(self, _("\"%s\" step already exists!")%step_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
196 message.ShowModal() |
|
197 message.Destroy() |
|
198 else: |
|
199 self.EndModal(wx.ID_OK) |
|
200 |
|
201 def SetMinStepSize(self, size): |
|
202 self.MinStepSize = size |
|
203 |
|
204 def SetPouNames(self, pou_names): |
|
205 self.PouNames = [pou_name.upper() for pou_name in pou_names] |
|
206 |
|
207 def SetVariables(self, variables): |
|
208 self.Variables = [var["Name"].upper() for var in variables] |
|
209 |
|
210 def SetStepNames(self, step_names): |
|
211 self.StepNames = [step_name.upper() for step_name in step_names] |
|
212 |
|
213 def SetValues(self, values): |
|
214 value_name = values.get("name", None) |
|
215 if value_name: |
|
216 self.StepName.SetValue(value_name) |
|
217 else: |
|
218 self.StepName.SetValue("") |
|
219 self.checkBox1.SetValue(values.get("input", False)) |
|
220 self.checkBox2.SetValue(values.get("output", False)) |
|
221 self.checkBox3.SetValue(values.get("action", False)) |
|
222 self.RefreshPreview() |
|
223 |
|
224 def GetValues(self): |
|
225 values = {} |
|
226 values["name"] = self.StepName.GetValue() |
|
227 values["input"] = self.checkBox1.IsChecked() |
|
228 values["output"] = self.checkBox2.IsChecked() |
|
229 values["action"] = self.checkBox3.IsChecked() |
|
230 values["width"], values["height"] = self.Step.GetSize() |
|
231 return values |
|
232 |
|
233 def OnConnectorsChanged(self, event): |
|
234 self.RefreshPreview() |
|
235 event.Skip() |
|
236 |
|
237 def OnNameChanged(self, event): |
|
238 self.RefreshPreview() |
|
239 event.Skip() |
|
240 |
|
241 def RefreshPreview(self): |
|
242 dc = wx.ClientDC(self.Preview) |
|
243 dc.SetFont(self.Preview.GetFont()) |
|
244 dc.Clear() |
|
245 self.Step = SFC_Step(self.Preview, self.StepName.GetValue(), self.Initial) |
|
246 if self.checkBox1.IsChecked(): |
|
247 self.Step.AddInput() |
|
248 else: |
|
249 self.Step.RemoveInput() |
|
250 if self.checkBox2.IsChecked(): |
|
251 self.Step.AddOutput() |
|
252 else: |
|
253 self.Step.RemoveOutput() |
|
254 if self.checkBox3.IsChecked(): |
|
255 self.Step.AddAction() |
|
256 else: |
|
257 self.Step.RemoveAction() |
|
258 width, height = self.MinStepSize |
|
259 min_width, min_height = self.Step.GetMinSize() |
|
260 width, height = max(min_width, width), max(min_height, height) |
|
261 self.Step.SetSize(width, height) |
|
262 clientsize = self.Preview.GetClientSize() |
|
263 x = (clientsize.width - width) / 2 |
|
264 y = (clientsize.height - height) / 2 |
|
265 self.Step.SetPosition(x, y) |
|
266 self.Step.Draw(dc) |
|
267 |
|
268 def OnPaint(self, event): |
|
269 self.RefreshPreview() |
|
270 event.Skip() |