|
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) 2007: 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 graphics import * |
|
28 |
|
29 #------------------------------------------------------------------------------- |
|
30 # Create New Connection Dialog |
|
31 #------------------------------------------------------------------------------- |
|
32 |
|
33 [ID_CONNECTIONDIALOG, ID_CONNECTIONDIALOGSPACER, |
|
34 ID_CONNECTIONDIALOGNAME, ID_CONNECTIONDIALOGRADIOBUTTON1, |
|
35 ID_CONNECTIONDIALOGRADIOBUTTON2, ID_CONNECTIONDIALOGPREVIEW, |
|
36 ID_CONNECTIONDIALOGSTATICTEXT1, ID_CONNECTIONDIALOGSTATICTEXT2, |
|
37 ID_CONNECTIONDIALOGSTATICTEXT3, |
|
38 ] = [wx.NewId() for _init_ctrls in range(9)] |
|
39 |
|
40 class ConnectionDialog(wx.Dialog): |
|
41 |
|
42 if wx.VERSION < (2, 6, 0): |
|
43 def Bind(self, event, function, id = None): |
|
44 if id is not None: |
|
45 event(self, id, function) |
|
46 else: |
|
47 event(self, function) |
|
48 |
|
49 def _init_coll_flexGridSizer1_Items(self, parent): |
|
50 parent.AddSizer(self.MainSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
51 parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
52 |
|
53 def _init_coll_flexGridSizer1_Growables(self, parent): |
|
54 parent.AddGrowableCol(0) |
|
55 parent.AddGrowableRow(0) |
|
56 |
|
57 def _init_coll_MainSizer_Items(self, parent): |
|
58 parent.AddSizer(self.LeftGridSizer, 1, border=5, flag=wx.GROW|wx.RIGHT) |
|
59 parent.AddSizer(self.RightGridSizer, 1, border=5, flag=wx.GROW|wx.LEFT) |
|
60 |
|
61 def _init_coll_LeftGridSizer_Items(self, parent): |
|
62 parent.AddWindow(self.staticText1, 0, border=0, flag=wx.GROW) |
|
63 parent.AddWindow(self.radioButton1, 0, border=0, flag=wx.GROW) |
|
64 parent.AddWindow(self.radioButton2, 0, border=0, flag=wx.GROW) |
|
65 parent.AddWindow(self.staticText2, 0, border=0, flag=wx.GROW) |
|
66 parent.AddWindow(self.ConnectionName, 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(5) |
|
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=6, 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_CONNECTIONDIALOG, |
|
99 name='ConnectionDialog', parent=prnt, pos=wx.Point(376, 223), |
|
100 size=wx.Size(350, 220), style=wx.DEFAULT_DIALOG_STYLE, |
|
101 title=_('Connection Properties')) |
|
102 self.SetClientSize(wx.Size(350, 220)) |
|
103 |
|
104 self.staticText1 = wx.StaticText(id=ID_CONNECTIONDIALOGSTATICTEXT1, |
|
105 label=_('Type:'), 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_CONNECTIONDIALOGSTATICTEXT2, |
|
109 label=_('Name:'), 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_CONNECTIONDIALOGSTATICTEXT3, |
|
113 label=_('Preview:'), name='staticText3', parent=self, |
|
114 pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) |
|
115 |
|
116 self.radioButton1 = wx.RadioButton(id=ID_CONNECTIONDIALOGRADIOBUTTON1, |
|
117 label=_('Connector'), name='radioButton1', parent=self, |
|
118 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP) |
|
119 self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_CONNECTIONDIALOGRADIOBUTTON1) |
|
120 self.radioButton1.SetValue(True) |
|
121 |
|
122 self.radioButton2 = wx.RadioButton(id=ID_CONNECTIONDIALOGRADIOBUTTON2, |
|
123 label=_('Continuation'), name='radioButton2', parent=self, |
|
124 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) |
|
125 self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, id=ID_CONNECTIONDIALOGRADIOBUTTON2) |
|
126 self.radioButton2.SetValue(False) |
|
127 |
|
128 self.ConnectionName = wx.TextCtrl(id=ID_CONNECTIONDIALOGNAME, |
|
129 name='Name', parent=self, pos=wx.Point(0, 0), |
|
130 size=wx.Size(0, 24), style=0) |
|
131 self.Bind(wx.EVT_TEXT, self.OnNameChanged, id=ID_CONNECTIONDIALOGNAME) |
|
132 |
|
133 self.Preview = wx.Panel(id=ID_CONNECTIONDIALOGPREVIEW, |
|
134 name='Preview', parent=self, pos=wx.Point(0, 0), |
|
135 size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER) |
|
136 self.Preview.SetBackgroundColour(wx.Colour(255,255,255)) |
|
137 setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE) |
|
138 setattr(self.Preview, "GetScaling", lambda:None) |
|
139 setattr(self.Preview, "IsOfType", ctrler.IsOfType) |
|
140 |
|
141 self.Spacer = wx.Panel(id=ID_CONNECTIONDIALOGSPACER, |
|
142 name='Spacer', parent=self, pos=wx.Point(0, 0), |
|
143 size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) |
|
144 |
|
145 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
146 if wx.VERSION >= (2, 5, 0): |
|
147 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) |
|
148 self.Preview.Bind(wx.EVT_PAINT, self.OnPaint) |
|
149 else: |
|
150 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
151 wx.EVT_PAINT(self.Preview, self.OnPaint) |
|
152 |
|
153 self._init_sizers() |
|
154 |
|
155 def __init__(self, parent, controler): |
|
156 self._init_ctrls(parent, controler) |
|
157 self.Connection = None |
|
158 self.MinConnectionSize = None |
|
159 |
|
160 self.PouNames = [] |
|
161 self.PouElementNames = [] |
|
162 |
|
163 def SetPreviewFont(self, font): |
|
164 self.Preview.SetFont(font) |
|
165 |
|
166 def SetMinConnectionSize(self, size): |
|
167 self.MinConnectionSize = size |
|
168 |
|
169 def SetValues(self, values): |
|
170 for name, value in values.items(): |
|
171 if name == "type": |
|
172 if value == CONNECTOR: |
|
173 self.radioButton1.SetValue(True) |
|
174 elif value == CONTINUATION: |
|
175 self.radioButton2.SetValue(True) |
|
176 elif name == "name": |
|
177 self.ConnectionName.SetValue(value) |
|
178 self.RefreshPreview() |
|
179 |
|
180 def GetValues(self): |
|
181 values = {} |
|
182 if self.radioButton1.GetValue(): |
|
183 values["type"] = CONNECTOR |
|
184 else: |
|
185 values["type"] = CONTINUATION |
|
186 values["name"] = self.ConnectionName.GetValue() |
|
187 values["width"], values["height"] = self.Connection.GetSize() |
|
188 return values |
|
189 |
|
190 def SetPouNames(self, pou_names): |
|
191 self.PouNames = [pou_name.upper() for pou_name in pou_names] |
|
192 |
|
193 def SetPouElementNames(self, element_names): |
|
194 self.PouElementNames = [element_name.upper() for element_name in element_names] |
|
195 |
|
196 def OnOK(self, event): |
|
197 connection_name = self.ConnectionName.GetValue() |
|
198 if connection_name == "": |
|
199 message = wx.MessageDialog(self, _("Form isn't complete. Name must be filled!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
200 message.ShowModal() |
|
201 message.Destroy() |
|
202 elif not TestIdentifier(connection_name): |
|
203 message = wx.MessageDialog(self, _("\"%s\" is not a valid identifier!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
204 message.ShowModal() |
|
205 message.Destroy() |
|
206 elif connection_name.upper() in IEC_KEYWORDS: |
|
207 message = wx.MessageDialog(self, _("\"%s\" is a keyword. It can't be used!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
208 message.ShowModal() |
|
209 message.Destroy() |
|
210 elif connection_name.upper() in self.PouNames: |
|
211 message = wx.MessageDialog(self, _("\"%s\" pou already exists!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
212 message.ShowModal() |
|
213 message.Destroy() |
|
214 elif connection_name.upper() in self.PouElementNames: |
|
215 message = wx.MessageDialog(self, _("\"%s\" element for this pou already exists!")%connection_name, _("Error"), wx.OK|wx.ICON_ERROR) |
|
216 message.ShowModal() |
|
217 message.Destroy() |
|
218 else: |
|
219 self.EndModal(wx.ID_OK) |
|
220 |
|
221 def OnTypeChanged(self, event): |
|
222 self.RefreshPreview() |
|
223 event.Skip() |
|
224 |
|
225 def OnNameChanged(self, event): |
|
226 self.RefreshPreview() |
|
227 event.Skip() |
|
228 |
|
229 def RefreshPreview(self): |
|
230 dc = wx.ClientDC(self.Preview) |
|
231 dc.SetFont(self.Preview.GetFont()) |
|
232 dc.Clear() |
|
233 if self.radioButton1.GetValue(): |
|
234 self.Connection = FBD_Connector(self.Preview, CONNECTOR, self.ConnectionName.GetValue()) |
|
235 else: |
|
236 self.Connection = FBD_Connector(self.Preview, CONTINUATION, self.ConnectionName.GetValue()) |
|
237 width, height = self.MinConnectionSize |
|
238 min_width, min_height = self.Connection.GetMinSize() |
|
239 width, height = max(min_width, width), max(min_height, height) |
|
240 self.Connection.SetSize(width, height) |
|
241 clientsize = self.Preview.GetClientSize() |
|
242 x = (clientsize.width - width) / 2 |
|
243 y = (clientsize.height - height) / 2 |
|
244 self.Connection.SetPosition(x, y) |
|
245 self.Connection.Draw(dc) |
|
246 |
|
247 def OnPaint(self, event): |
|
248 self.RefreshPreview() |
|
249 event.Skip() |