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 |
|
1244
|
27 |
from graphics.GraphicCommons import INPUT, INOUT, OUTPUT
|
|
28 |
from graphics.FBD_Objects import FBD_Variable
|
|
29 |
from BlockPreviewDialog import BlockPreviewDialog
|
814
|
30 |
|
|
31 |
#-------------------------------------------------------------------------------
|
|
32 |
# Helpers
|
|
33 |
#-------------------------------------------------------------------------------
|
|
34 |
|
1244
|
35 |
# Dictionaries containing correspondence between variable block class and string
|
|
36 |
# to be shown in Class combo box in both sense
|
814
|
37 |
VARIABLE_CLASSES_DICT = {INPUT : _("Input"),
|
|
38 |
INOUT : _("InOut"),
|
|
39 |
OUTPUT : _("Output")}
|
|
40 |
VARIABLE_CLASSES_DICT_REVERSE = dict(
|
|
41 |
[(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()])
|
|
42 |
|
|
43 |
#-------------------------------------------------------------------------------
|
1244
|
44 |
# Set Variable Parameters Dialog
|
814
|
45 |
#-------------------------------------------------------------------------------
|
|
46 |
|
1244
|
47 |
"""
|
|
48 |
Class that implements a dialog for defining parameters of a FBD variable graphic
|
|
49 |
element
|
|
50 |
"""
|
|
51 |
|
|
52 |
class FBDVariableDialog(BlockPreviewDialog):
|
|
53 |
|
|
54 |
def __init__(self, parent, controller, tagname):
|
|
55 |
"""
|
|
56 |
Constructor
|
|
57 |
@param parent: Parent wx.Window of dialog for modal
|
|
58 |
@param controller: Reference to project controller
|
|
59 |
@param tagname: Tagname of project POU edited
|
|
60 |
"""
|
|
61 |
BlockPreviewDialog.__init__(self, parent, controller, tagname,
|
814
|
62 |
size=wx.Size(400, 380), title=_('Variable Properties'))
|
|
63 |
|
1244
|
64 |
# Create dialog main sizer
|
814
|
65 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=10)
|
|
66 |
main_sizer.AddGrowableCol(0)
|
|
67 |
main_sizer.AddGrowableRow(2)
|
|
68 |
|
1244
|
69 |
# Create a sizer for dividing FBD variable parameters in two columns
|
814
|
70 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
71 |
main_sizer.AddSizer(column_sizer, border=20,
|
|
72 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
73 |
|
1244
|
74 |
# Create a sizer for left column
|
1182
|
75 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=4, vgap=5)
|
814
|
76 |
left_gridsizer.AddGrowableCol(0)
|
|
77 |
column_sizer.AddSizer(left_gridsizer, 1, border=5,
|
|
78 |
flag=wx.GROW|wx.RIGHT)
|
|
79 |
|
1244
|
80 |
# Create label for variable class
|
814
|
81 |
class_label = wx.StaticText(self, label=_('Class:'))
|
|
82 |
left_gridsizer.AddWindow(class_label, flag=wx.GROW)
|
|
83 |
|
1244
|
84 |
# Create a combo box for defining variable class
|
814
|
85 |
self.Class = wx.ComboBox(self, style=wx.CB_READONLY)
|
|
86 |
self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class)
|
|
87 |
left_gridsizer.AddWindow(self.Class, flag=wx.GROW)
|
|
88 |
|
1244
|
89 |
# Create label for variable execution order
|
|
90 |
execution_order_label = wx.StaticText(self,
|
|
91 |
label=_('Execution Order:'))
|
814
|
92 |
left_gridsizer.AddWindow(execution_order_label, flag=wx.GROW)
|
|
93 |
|
1244
|
94 |
# Create spin control for defining variable execution order
|
814
|
95 |
self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS)
|
1244
|
96 |
self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged,
|
|
97 |
self.ExecutionOrder)
|
814
|
98 |
left_gridsizer.AddWindow(self.ExecutionOrder, flag=wx.GROW)
|
|
99 |
|
1244
|
100 |
# Create a sizer for right column
|
1182
|
101 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
|
814
|
102 |
right_gridsizer.AddGrowableCol(0)
|
1182
|
103 |
right_gridsizer.AddGrowableRow(2)
|
814
|
104 |
column_sizer.AddSizer(right_gridsizer, 1, border=5,
|
|
105 |
flag=wx.GROW|wx.LEFT)
|
|
106 |
|
1244
|
107 |
# Create label for variable expression
|
1182
|
108 |
name_label = wx.StaticText(self, label=_('Expression:'))
|
|
109 |
right_gridsizer.AddWindow(name_label, border=5, flag=wx.GROW|wx.BOTTOM)
|
|
110 |
|
1244
|
111 |
# Create text control for defining variable expression
|
1182
|
112 |
self.Expression = wx.TextCtrl(self)
|
|
113 |
self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression)
|
|
114 |
right_gridsizer.AddWindow(self.Expression, flag=wx.GROW)
|
|
115 |
|
1244
|
116 |
# Create a list box to selected variable expression in the list of
|
|
117 |
# variables defined in POU
|
1182
|
118 |
self.VariableName = wx.ListBox(self, size=wx.Size(0, 120),
|
814
|
119 |
style=wx.LB_SINGLE|wx.LB_SORT)
|
|
120 |
self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName)
|
|
121 |
right_gridsizer.AddWindow(self.VariableName, flag=wx.GROW)
|
|
122 |
|
1244
|
123 |
# Add preview panel and associated label to sizers
|
|
124 |
main_sizer.AddWindow(self.PreviewLabel, border=20,
|
814
|
125 |
flag=wx.GROW|wx.LEFT|wx.RIGHT)
|
|
126 |
main_sizer.AddWindow(self.Preview, border=20,
|
|
127 |
flag=wx.GROW|wx.LEFT|wx.RIGHT)
|
|
128 |
|
1244
|
129 |
# Add buttons sizer to sizers
|
|
130 |
main_sizer.AddSizer(self.ButtonSizer, border=20,
|
814
|
131 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
132 |
|
|
133 |
self.SetSizer(main_sizer)
|
|
134 |
|
1244
|
135 |
# Set options that can be selected in class combo box
|
814
|
136 |
for choice in VARIABLE_CLASSES_DICT.itervalues():
|
|
137 |
self.Class.Append(choice)
|
|
138 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[INPUT])
|
1244
|
139 |
|
1246
|
140 |
# Extract list of variables defined in POU
|
|
141 |
self.RefreshVariableList()
|
1244
|
142 |
|
|
143 |
# Refresh values in name list box
|
814
|
144 |
self.RefreshNameList()
|
1244
|
145 |
|
|
146 |
# Class combo box is default control having keyboard focus
|
814
|
147 |
self.Class.SetFocus()
|
|
148 |
|
|
149 |
def RefreshNameList(self):
|
1244
|
150 |
"""
|
|
151 |
Called to refresh names in name list box
|
|
152 |
"""
|
|
153 |
# Get variable class to select POU variable applicable
|
|
154 |
var_class = VARIABLE_CLASSES_DICT_REVERSE[
|
|
155 |
self.Class.GetStringSelection()]
|
|
156 |
|
|
157 |
# Refresh names in name list box by selecting variables in POU variables
|
|
158 |
# list that can be applied to variable class
|
814
|
159 |
self.VariableName.Clear()
|
1246
|
160 |
for name, (var_type, value_type) in self.VariableList.iteritems():
|
814
|
161 |
if var_type != "Input" or var_class == INPUT:
|
|
162 |
self.VariableName.Append(name)
|
1244
|
163 |
|
|
164 |
# Get variable expression and select corresponding value in name list
|
|
165 |
# box if it exists
|
|
166 |
selected = self.Expression.GetValue()
|
|
167 |
if (selected != "" and
|
|
168 |
self.VariableName.FindString(selected) != wx.NOT_FOUND):
|
814
|
169 |
self.VariableName.SetStringSelection(selected)
|
|
170 |
else:
|
1182
|
171 |
self.VariableName.SetSelection(wx.NOT_FOUND)
|
1244
|
172 |
|
|
173 |
# Disable name list box if no name present inside
|
814
|
174 |
self.VariableName.Enable(self.VariableName.GetCount() > 0)
|
|
175 |
|
|
176 |
def SetValues(self, values):
|
1244
|
177 |
"""
|
|
178 |
Set default variable parameters
|
|
179 |
@param values: Variable parameters values
|
|
180 |
"""
|
|
181 |
|
|
182 |
# Get class parameter value
|
|
183 |
var_class = values.get("class", None)
|
|
184 |
if var_class is not None:
|
|
185 |
# Set class selected in class combo box
|
|
186 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class])
|
|
187 |
# Refresh names in name list box according to var class
|
814
|
188 |
self.RefreshNameList()
|
1244
|
189 |
|
|
190 |
# For each parameters defined, set corresponding control value
|
|
191 |
for name, value in values.items():
|
|
192 |
|
|
193 |
# Parameter is variable expression
|
|
194 |
if name == "expression":
|
|
195 |
# Set expression text control value
|
|
196 |
self.Expression.ChangeValue(value)
|
|
197 |
# Select corresponding text in name list box if it exists
|
|
198 |
if self.VariableName.FindString(value) != wx.NOT_FOUND:
|
|
199 |
self.VariableName.SetStringSelection(value)
|
|
200 |
else:
|
|
201 |
self.VariableName.SetSelection(wx.NOT_FOUND)
|
|
202 |
|
|
203 |
# Parameter is variable execution order
|
|
204 |
elif name == "executionOrder":
|
|
205 |
self.ExecutionOrder.SetValue(value)
|
|
206 |
|
|
207 |
# Refresh preview panel
|
814
|
208 |
self.RefreshPreview()
|
|
209 |
|
|
210 |
def GetValues(self):
|
1244
|
211 |
"""
|
|
212 |
Return block parameters defined in dialog
|
|
213 |
@return: {parameter_name: parameter_value,...}
|
|
214 |
"""
|
|
215 |
expression = self.Expression.GetValue()
|
|
216 |
values = {
|
|
217 |
"class": VARIABLE_CLASSES_DICT_REVERSE[
|
|
218 |
self.Class.GetStringSelection()],
|
|
219 |
"expression": expression,
|
1246
|
220 |
"var_type": self.VariableList.get(expression, (None, None))[1],
|
1244
|
221 |
"executionOrder": self.ExecutionOrder.GetValue()}
|
|
222 |
values["width"], values["height"] = self.Element.GetSize()
|
814
|
223 |
return values
|
|
224 |
|
|
225 |
def OnOK(self, event):
|
1244
|
226 |
"""
|
|
227 |
Called when dialog OK button is pressed
|
|
228 |
Test if parameters defined are valid
|
|
229 |
@param event: wx.Event from OK button
|
|
230 |
"""
|
814
|
231 |
message = None
|
1244
|
232 |
|
|
233 |
# Test that an expression have been selected or typed by user
|
1182
|
234 |
value = self.Expression.GetValue()
|
814
|
235 |
if value == "":
|
|
236 |
message = _("At least a variable or an expression must be selected!")
|
1244
|
237 |
|
|
238 |
# Show error message if an error is detected
|
814
|
239 |
if message is not None:
|
1244
|
240 |
self.ShowErrorMessage(message)
|
|
241 |
|
814
|
242 |
else:
|
1244
|
243 |
# Call BlockPreviewDialog function
|
|
244 |
BlockPreviewDialog.OnOK(self, event)
|
814
|
245 |
|
|
246 |
def OnClassChanged(self, event):
|
1244
|
247 |
"""
|
|
248 |
Called when variable class value changed
|
|
249 |
@param event: wx.ComboBoxEvent
|
|
250 |
"""
|
|
251 |
# Refresh name list box values
|
814
|
252 |
self.RefreshNameList()
|
1244
|
253 |
|
814
|
254 |
self.RefreshPreview()
|
|
255 |
event.Skip()
|
|
256 |
|
|
257 |
def OnNameChanged(self, event):
|
1244
|
258 |
"""
|
|
259 |
Called when name selected in name list box changed
|
|
260 |
@param event: wx.ListBoxEvent
|
|
261 |
"""
|
|
262 |
# Change expression test control value to the value selected in name
|
|
263 |
# list box if value selected is valid
|
|
264 |
if self.VariableName.GetSelection() != wx.NOT_FOUND:
|
|
265 |
self.Expression.ChangeValue(self.VariableName.GetStringSelection())
|
|
266 |
|
814
|
267 |
self.RefreshPreview()
|
|
268 |
event.Skip()
|
|
269 |
|
|
270 |
def OnExpressionChanged(self, event):
|
1244
|
271 |
"""
|
|
272 |
Called when expression text control is changed by user
|
|
273 |
@param event: wx.ListBoxEvent
|
|
274 |
"""
|
|
275 |
# Select the corresponding value in name list box if it exists
|
1182
|
276 |
self.VariableName.SetSelection(
|
1244
|
277 |
self.VariableName.FindString(self.Expression.GetValue()))
|
|
278 |
|
814
|
279 |
self.RefreshPreview()
|
|
280 |
event.Skip()
|
|
281 |
|
|
282 |
def OnExecutionOrderChanged(self, event):
|
1244
|
283 |
"""
|
|
284 |
Called when block execution control value changed
|
|
285 |
@param event: wx.SpinEvent
|
|
286 |
"""
|
814
|
287 |
self.RefreshPreview()
|
|
288 |
event.Skip()
|
|
289 |
|
|
290 |
def RefreshPreview(self):
|
1244
|
291 |
"""
|
|
292 |
Refresh preview panel of graphic element
|
|
293 |
Override BlockPreviewDialog function
|
|
294 |
"""
|
|
295 |
# Get expression value to put in FBD variable element
|
1182
|
296 |
name = self.Expression.GetValue()
|
1244
|
297 |
|
|
298 |
# Set graphic element displayed, creating a FBD variable element
|
|
299 |
self.Element = FBD_Variable(self.Preview,
|
|
300 |
VARIABLE_CLASSES_DICT_REVERSE[
|
|
301 |
self.Class.GetStringSelection()],
|
|
302 |
name,
|
1246
|
303 |
self.VariableList.get(name, ("", ""))[1],
|
1244
|
304 |
executionOrder = self.ExecutionOrder.GetValue())
|
|
305 |
|
|
306 |
# Call BlockPreviewDialog function
|
|
307 |
BlockPreviewDialog.RefreshPreview(self)
|
|
308 |
|
|
309 |
|