814
|
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 |
# Helpers
|
|
31 |
#-------------------------------------------------------------------------------
|
|
32 |
|
|
33 |
VARIABLE_CLASSES_DICT = {INPUT : _("Input"),
|
|
34 |
INOUT : _("InOut"),
|
|
35 |
OUTPUT : _("Output")}
|
|
36 |
VARIABLE_CLASSES_DICT_REVERSE = dict(
|
|
37 |
[(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()])
|
|
38 |
|
|
39 |
#-------------------------------------------------------------------------------
|
|
40 |
# Create New Variable Dialog
|
|
41 |
#-------------------------------------------------------------------------------
|
|
42 |
|
|
43 |
class FBDVariableDialog(wx.Dialog):
|
|
44 |
|
|
45 |
def __init__(self, parent, controller, transition = ""):
|
|
46 |
wx.Dialog.__init__(self, parent,
|
|
47 |
size=wx.Size(400, 380), title=_('Variable Properties'))
|
|
48 |
|
|
49 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10)
|
|
50 |
main_sizer.AddGrowableCol(0)
|
|
51 |
main_sizer.AddGrowableRow(2)
|
|
52 |
|
|
53 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
54 |
main_sizer.AddSizer(column_sizer, border=20,
|
|
55 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
56 |
|
1182
|
57 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=5)
|
814
|
58 |
left_gridsizer.AddGrowableCol(0)
|
|
59 |
column_sizer.AddSizer(left_gridsizer, 1, border=5,
|
|
60 |
flag=wx.GROW|wx.RIGHT)
|
|
61 |
|
|
62 |
class_label = wx.StaticText(self, label=_('Class:'))
|
|
63 |
left_gridsizer.AddWindow(class_label, flag=wx.GROW)
|
|
64 |
|
|
65 |
self.Class = wx.ComboBox(self, style=wx.CB_READONLY)
|
|
66 |
self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class)
|
|
67 |
left_gridsizer.AddWindow(self.Class, flag=wx.GROW)
|
|
68 |
|
|
69 |
execution_order_label = wx.StaticText(self, label=_('Execution Order:'))
|
|
70 |
left_gridsizer.AddWindow(execution_order_label, flag=wx.GROW)
|
|
71 |
|
|
72 |
self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
|
|
73 |
self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, self.ExecutionOrder)
|
|
74 |
left_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW)
|
|
75 |
|
1182
|
76 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
|
814
|
77 |
right_gridsizer.AddGrowableCol(0)
|
1182
|
78 |
right_gridsizer.AddGrowableRow(2)
|
814
|
79 |
column_sizer.AddSizer(right_gridsizer, 1, border=5,
|
|
80 |
flag=wx.GROW|wx.LEFT)
|
|
81 |
|
1182
|
82 |
name_label = wx.StaticText(self, label=_('Expression:'))
|
|
83 |
right_gridsizer.AddWindow(name_label, border=5, flag=wx.GROW|wx.BOTTOM)
|
|
84 |
|
|
85 |
self.Expression = wx.TextCtrl(self)
|
|
86 |
self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression)
|
|
87 |
right_gridsizer.AddWindow(self.Expression, flag=wx.GROW)
|
|
88 |
|
|
89 |
self.VariableName = wx.ListBox(self, size=wx.Size(0, 120),
|
814
|
90 |
style=wx.LB_SINGLE|wx.LB_SORT)
|
|
91 |
self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName)
|
|
92 |
right_gridsizer.AddWindow(self.VariableName, flag=wx.GROW)
|
|
93 |
|
|
94 |
preview_label = wx.StaticText(self, label=_('Preview:'))
|
|
95 |
main_sizer.AddWindow(preview_label, border=20,
|
|
96 |
flag=wx.GROW|wx.LEFT|wx.RIGHT)
|
|
97 |
|
|
98 |
self.Preview = wx.Panel(self,
|
|
99 |
style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
|
|
100 |
self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
|
|
101 |
setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
|
|
102 |
setattr(self.Preview, "GetScaling", lambda:None)
|
|
103 |
setattr(self.Preview, "IsOfType", controller.IsOfType)
|
|
104 |
self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
|
|
105 |
main_sizer.AddWindow(self.Preview, border=20,
|
|
106 |
flag=wx.GROW|wx.LEFT|wx.RIGHT)
|
|
107 |
|
|
108 |
button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
|
|
109 |
self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
|
|
110 |
main_sizer.AddSizer(button_sizer, border=20,
|
|
111 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
112 |
|
|
113 |
self.SetSizer(main_sizer)
|
|
114 |
|
|
115 |
self.Transition = transition
|
|
116 |
self.Variable = None
|
|
117 |
self.VarList = []
|
|
118 |
self.MinVariableSize = None
|
|
119 |
|
|
120 |
for choice in VARIABLE_CLASSES_DICT.itervalues():
|
|
121 |
self.Class.Append(choice)
|
|
122 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[INPUT])
|
|
123 |
|
|
124 |
self.RefreshNameList()
|
|
125 |
self.Class.SetFocus()
|
|
126 |
|
|
127 |
def SetPreviewFont(self, font):
|
|
128 |
self.Preview.SetFont(font)
|
|
129 |
|
|
130 |
def RefreshNameList(self):
|
1182
|
131 |
selected = self.Expression.GetValue()
|
814
|
132 |
var_class = VARIABLE_CLASSES_DICT_REVERSE[self.Class.GetStringSelection()]
|
|
133 |
self.VariableName.Clear()
|
|
134 |
for name, var_type, value_type in self.VarList:
|
|
135 |
if var_type != "Input" or var_class == INPUT:
|
|
136 |
self.VariableName.Append(name)
|
|
137 |
if selected != "" and self.VariableName.FindString(selected) != wx.NOT_FOUND:
|
|
138 |
self.VariableName.SetStringSelection(selected)
|
|
139 |
else:
|
1182
|
140 |
self.VariableName.SetSelection(wx.NOT_FOUND)
|
814
|
141 |
self.VariableName.Enable(self.VariableName.GetCount() > 0)
|
|
142 |
|
|
143 |
def SetMinVariableSize(self, size):
|
|
144 |
self.MinVariableSize = size
|
|
145 |
|
|
146 |
def SetVariables(self, vars):
|
|
147 |
self.VarList = vars
|
|
148 |
self.RefreshNameList()
|
|
149 |
|
|
150 |
def SetValues(self, values):
|
|
151 |
value_type = values.get("type", None)
|
|
152 |
value_name = values.get("name", None)
|
|
153 |
if value_type:
|
|
154 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[value_type])
|
|
155 |
self.RefreshNameList()
|
|
156 |
if value_name:
|
1182
|
157 |
self.Expression.ChangeValue(value_name)
|
814
|
158 |
if self.VariableName.FindString(value_name) != wx.NOT_FOUND:
|
|
159 |
self.VariableName.SetStringSelection(value_name)
|
|
160 |
else:
|
1182
|
161 |
self.VariableName.SetSelection(wx.NOT_FOUND)
|
814
|
162 |
if "executionOrder" in values:
|
|
163 |
self.ExecutionOrder.SetValue(values["executionOrder"])
|
|
164 |
self.RefreshPreview()
|
|
165 |
|
|
166 |
def GetValues(self):
|
|
167 |
values = {}
|
|
168 |
values["type"] = VARIABLE_CLASSES_DICT_REVERSE[self.Class.GetStringSelection()]
|
1182
|
169 |
values["name"] = self.Expression.GetValue()
|
814
|
170 |
values["value_type"] = None
|
|
171 |
for var_name, var_type, value_type in self.VarList:
|
|
172 |
if var_name == values["name"]:
|
|
173 |
values["value_type"] = value_type
|
|
174 |
values["width"], values["height"] = self.Variable.GetSize()
|
|
175 |
values["executionOrder"] = self.ExecutionOrder.GetValue()
|
|
176 |
return values
|
|
177 |
|
|
178 |
def OnOK(self, event):
|
|
179 |
message = None
|
1182
|
180 |
value = self.Expression.GetValue()
|
814
|
181 |
if value == "":
|
|
182 |
message = _("At least a variable or an expression must be selected!")
|
|
183 |
if message is not None:
|
|
184 |
message = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
|
|
185 |
message.ShowModal()
|
|
186 |
message.Destroy()
|
|
187 |
else:
|
|
188 |
self.EndModal(wx.ID_OK)
|
|
189 |
|
|
190 |
def OnClassChanged(self, event):
|
|
191 |
self.RefreshNameList()
|
|
192 |
self.RefreshPreview()
|
|
193 |
event.Skip()
|
|
194 |
|
|
195 |
def OnNameChanged(self, event):
|
1182
|
196 |
self.Expression.ChangeValue(
|
|
197 |
self.VariableName.GetStringSelection())
|
814
|
198 |
self.RefreshPreview()
|
|
199 |
event.Skip()
|
|
200 |
|
|
201 |
def OnExpressionChanged(self, event):
|
1182
|
202 |
expression = self.Expression.GetValue()
|
|
203 |
self.VariableName.SetSelection(
|
|
204 |
self.VariableName.FindString(expression))
|
814
|
205 |
self.RefreshPreview()
|
|
206 |
event.Skip()
|
|
207 |
|
|
208 |
def OnExecutionOrderChanged(self, event):
|
|
209 |
self.RefreshPreview()
|
|
210 |
event.Skip()
|
|
211 |
|
|
212 |
def RefreshPreview(self):
|
|
213 |
dc = wx.ClientDC(self.Preview)
|
|
214 |
dc.SetFont(self.Preview.GetFont())
|
|
215 |
dc.Clear()
|
1182
|
216 |
name = self.Expression.GetValue()
|
814
|
217 |
type = ""
|
|
218 |
for var_name, var_type, value_type in self.VarList:
|
|
219 |
if var_name == name:
|
|
220 |
type = value_type
|
|
221 |
classtype = VARIABLE_CLASSES_DICT_REVERSE[self.Class.GetStringSelection()]
|
|
222 |
self.Variable = FBD_Variable(self.Preview, classtype, name, type, executionOrder = self.ExecutionOrder.GetValue())
|
|
223 |
width, height = self.MinVariableSize
|
|
224 |
min_width, min_height = self.Variable.GetMinSize()
|
|
225 |
width, height = max(min_width, width), max(min_height, height)
|
|
226 |
self.Variable.SetSize(width, height)
|
|
227 |
clientsize = self.Preview.GetClientSize()
|
|
228 |
x = (clientsize.width - width) / 2
|
|
229 |
y = (clientsize.height - height) / 2
|
|
230 |
self.Variable.SetPosition(x, y)
|
|
231 |
self.Variable.Draw(dc)
|
|
232 |
|
|
233 |
def OnPaint(self, event):
|
|
234 |
self.RefreshPreview()
|
|
235 |
event.Skip()
|