814
|
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 |
class SFCStepDialog(wx.Dialog):
|
|
33 |
|
|
34 |
def __init__(self, parent, controller, initial = False):
|
|
35 |
wx.Dialog.__init__(self, parent, title=_('Edit Step'),
|
|
36 |
size=wx.Size(400, 250))
|
|
37 |
|
|
38 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
|
|
39 |
main_sizer.AddGrowableCol(0)
|
|
40 |
main_sizer.AddGrowableRow(0)
|
|
41 |
|
|
42 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
43 |
main_sizer.AddSizer(column_sizer, border=20,
|
|
44 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
45 |
|
|
46 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=6, vgap=5)
|
|
47 |
left_gridsizer.AddGrowableCol(0)
|
|
48 |
column_sizer.AddSizer(left_gridsizer, 1, border=5,
|
|
49 |
flag=wx.GROW|wx.RIGHT)
|
|
50 |
|
|
51 |
name_label = wx.StaticText(self, label=_('Name:'))
|
|
52 |
left_gridsizer.AddWindow(name_label, flag=wx.GROW)
|
|
53 |
|
|
54 |
self.StepName = wx.TextCtrl(self)
|
|
55 |
self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.StepName)
|
|
56 |
left_gridsizer.AddWindow(self.StepName, flag=wx.GROW)
|
|
57 |
|
|
58 |
connectors_label = wx.StaticText(self, label=_('Connectors:'))
|
|
59 |
left_gridsizer.AddWindow(connectors_label, flag=wx.GROW)
|
|
60 |
|
|
61 |
self.Input = wx.CheckBox(self, label=_("Input"))
|
|
62 |
self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Input)
|
|
63 |
left_gridsizer.AddWindow(self.Input, flag=wx.GROW)
|
|
64 |
|
|
65 |
self.Output = wx.CheckBox(self, label=_("Output"))
|
|
66 |
self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Output)
|
|
67 |
left_gridsizer.AddWindow(self.Output, flag=wx.GROW)
|
|
68 |
|
|
69 |
self.Action = wx.CheckBox(self, label=_("Action"))
|
|
70 |
self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, self.Action)
|
|
71 |
left_gridsizer.AddWindow(self.Action, flag=wx.GROW)
|
|
72 |
|
|
73 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
|
|
74 |
right_gridsizer.AddGrowableCol(0)
|
|
75 |
right_gridsizer.AddGrowableRow(1)
|
|
76 |
column_sizer.AddSizer(right_gridsizer, 1, border=5,
|
|
77 |
flag=wx.GROW|wx.LEFT)
|
|
78 |
|
|
79 |
preview_label = wx.StaticText(self, label=_('Preview:'))
|
|
80 |
right_gridsizer.AddWindow(preview_label, flag=wx.GROW)
|
|
81 |
|
|
82 |
self.Preview = wx.Panel(self,
|
|
83 |
style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
|
|
84 |
self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
|
|
85 |
setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
|
|
86 |
setattr(self.Preview, "RefreshStepModel", lambda x:None)
|
|
87 |
setattr(self.Preview, "GetScaling", lambda:None)
|
|
88 |
setattr(self.Preview, "IsOfType", controller.IsOfType)
|
|
89 |
self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
|
|
90 |
right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
|
|
91 |
|
|
92 |
button_sizer = self.CreateButtonSizer(
|
|
93 |
wx.OK|wx.CANCEL|wx.CENTRE)
|
|
94 |
self.Bind(wx.EVT_BUTTON, self.OnOK,
|
|
95 |
button_sizer.GetAffirmativeButton())
|
|
96 |
main_sizer.AddSizer(button_sizer, border=20,
|
|
97 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
98 |
|
|
99 |
self.SetSizer(main_sizer)
|
|
100 |
|
|
101 |
self.Step = None
|
|
102 |
self.Initial = initial
|
|
103 |
self.MinStepSize = None
|
|
104 |
|
|
105 |
self.PouNames = []
|
|
106 |
self.Variables = []
|
|
107 |
self.StepNames = []
|
|
108 |
|
|
109 |
self.StepName.SetFocus()
|
|
110 |
|
|
111 |
def SetPreviewFont(self, font):
|
|
112 |
self.Preview.SetFont(font)
|
|
113 |
|
|
114 |
def OnOK(self, event):
|
|
115 |
message = None
|
|
116 |
step_name = self.StepName.GetValue()
|
|
117 |
if step_name == "":
|
|
118 |
message = _("You must type a name!")
|
|
119 |
elif not TestIdentifier(step_name):
|
|
120 |
message = _("\"%s\" is not a valid identifier!") % step_name
|
|
121 |
elif step_name.upper() in IEC_KEYWORDS:
|
|
122 |
message = _("\"%s\" is a keyword. It can't be used!") % step_name
|
|
123 |
elif step_name.upper() in self.PouNames:
|
|
124 |
message = _("A POU named \"%s\" already exists!") % step_name
|
|
125 |
elif step_name.upper() in self.Variables:
|
|
126 |
message = _("A variable with \"%s\" as name already exists in this pou!") % step_name
|
|
127 |
elif step_name.upper() in self.StepNames:
|
|
128 |
message = _("\"%s\" step already exists!") % step_name
|
|
129 |
if message is not None:
|
|
130 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
|
|
131 |
dialog.ShowModal()
|
|
132 |
dialog.Destroy()
|
|
133 |
else:
|
|
134 |
self.EndModal(wx.ID_OK)
|
|
135 |
|
|
136 |
def SetMinStepSize(self, size):
|
|
137 |
self.MinStepSize = size
|
|
138 |
|
|
139 |
def SetPouNames(self, pou_names):
|
|
140 |
self.PouNames = [pou_name.upper() for pou_name in pou_names]
|
|
141 |
|
|
142 |
def SetVariables(self, variables):
|
|
143 |
self.Variables = [var["Name"].upper() for var in variables]
|
|
144 |
|
|
145 |
def SetStepNames(self, step_names):
|
|
146 |
self.StepNames = [step_name.upper() for step_name in step_names]
|
|
147 |
|
|
148 |
def SetValues(self, values):
|
|
149 |
value_name = values.get("name", None)
|
|
150 |
if value_name:
|
|
151 |
self.StepName.SetValue(value_name)
|
|
152 |
else:
|
|
153 |
self.StepName.SetValue("")
|
|
154 |
self.Input.SetValue(values.get("input", False))
|
|
155 |
self.Output.SetValue(values.get("output", False))
|
|
156 |
self.Action.SetValue(values.get("action", False))
|
|
157 |
self.RefreshPreview()
|
|
158 |
|
|
159 |
def GetValues(self):
|
|
160 |
values = {}
|
|
161 |
values["name"] = self.StepName.GetValue()
|
|
162 |
values["input"] = self.Input.IsChecked()
|
|
163 |
values["output"] = self.Output.IsChecked()
|
|
164 |
values["action"] = self.Action.IsChecked()
|
|
165 |
values["width"], values["height"] = self.Step.GetSize()
|
|
166 |
return values
|
|
167 |
|
|
168 |
def OnConnectorsChanged(self, event):
|
|
169 |
self.RefreshPreview()
|
|
170 |
event.Skip()
|
|
171 |
|
|
172 |
def OnNameChanged(self, event):
|
|
173 |
self.RefreshPreview()
|
|
174 |
event.Skip()
|
|
175 |
|
|
176 |
def RefreshPreview(self):
|
|
177 |
dc = wx.ClientDC(self.Preview)
|
|
178 |
dc.SetFont(self.Preview.GetFont())
|
|
179 |
dc.Clear()
|
|
180 |
self.Step = SFC_Step(self.Preview, self.StepName.GetValue(), self.Initial)
|
|
181 |
if self.Input.IsChecked():
|
|
182 |
self.Step.AddInput()
|
|
183 |
else:
|
|
184 |
self.Step.RemoveInput()
|
|
185 |
if self.Output.IsChecked():
|
|
186 |
self.Step.AddOutput()
|
|
187 |
else:
|
|
188 |
self.Step.RemoveOutput()
|
|
189 |
if self.Action.IsChecked():
|
|
190 |
self.Step.AddAction()
|
|
191 |
else:
|
|
192 |
self.Step.RemoveAction()
|
|
193 |
width, height = self.MinStepSize
|
|
194 |
min_width, min_height = self.Step.GetMinSize()
|
|
195 |
width, height = max(min_width, width), max(min_height, height)
|
|
196 |
self.Step.SetSize(width, height)
|
|
197 |
clientsize = self.Preview.GetClientSize()
|
|
198 |
x = (clientsize.width - width) / 2
|
|
199 |
y = (clientsize.height - height) / 2
|
|
200 |
self.Step.SetPosition(x, y)
|
|
201 |
self.Step.Draw(dc)
|
|
202 |
|
|
203 |
def OnPaint(self, event):
|
|
204 |
self.RefreshPreview()
|
|
205 |
event.Skip()
|