author | Laurent Bessard |
Wed, 12 Jun 2013 22:23:57 +0200 | |
changeset 1252 | 18cd1357e8d3 |
parent 1250 | 7e6de17c687a |
child 1259 | 8350222a81c3 |
permissions | -rw-r--r-- |
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 |
||
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
64 |
# Init common sizers |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
65 |
self._init_sizers(4, 2, 4, None, 3, 2) |
814 | 66 |
|
1244 | 67 |
# Create label for variable class |
814 | 68 |
class_label = wx.StaticText(self, label=_('Class:')) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
69 |
self.LeftGridSizer.AddWindow(class_label, flag=wx.GROW) |
814 | 70 |
|
1244 | 71 |
# Create a combo box for defining variable class |
814 | 72 |
self.Class = wx.ComboBox(self, style=wx.CB_READONLY) |
73 |
self.Bind(wx.EVT_COMBOBOX, self.OnClassChanged, self.Class) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
74 |
self.LeftGridSizer.AddWindow(self.Class, flag=wx.GROW) |
814 | 75 |
|
1244 | 76 |
# Create label for variable execution order |
77 |
execution_order_label = wx.StaticText(self, |
|
78 |
label=_('Execution Order:')) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
79 |
self.LeftGridSizer.AddWindow(execution_order_label, flag=wx.GROW) |
814 | 80 |
|
1244 | 81 |
# Create spin control for defining variable execution order |
814 | 82 |
self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) |
1244 | 83 |
self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, |
84 |
self.ExecutionOrder) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
85 |
self.LeftGridSizer.AddWindow(self.ExecutionOrder, flag=wx.GROW) |
814 | 86 |
|
1244 | 87 |
# Create label for variable expression |
1182 | 88 |
name_label = wx.StaticText(self, label=_('Expression:')) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
89 |
self.RightGridSizer.AddWindow(name_label, border=5, |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
90 |
flag=wx.GROW|wx.BOTTOM) |
1182 | 91 |
|
1244 | 92 |
# Create text control for defining variable expression |
1182 | 93 |
self.Expression = wx.TextCtrl(self) |
94 |
self.Bind(wx.EVT_TEXT, self.OnExpressionChanged, self.Expression) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
95 |
self.RightGridSizer.AddWindow(self.Expression, flag=wx.GROW) |
1182 | 96 |
|
1244 | 97 |
# Create a list box to selected variable expression in the list of |
98 |
# variables defined in POU |
|
1182 | 99 |
self.VariableName = wx.ListBox(self, size=wx.Size(0, 120), |
814 | 100 |
style=wx.LB_SINGLE|wx.LB_SORT) |
101 |
self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
102 |
self.RightGridSizer.AddWindow(self.VariableName, flag=wx.GROW) |
814 | 103 |
|
1244 | 104 |
# Add preview panel and associated label to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
105 |
self.MainSizer.AddWindow(self.PreviewLabel, border=20, |
814 | 106 |
flag=wx.GROW|wx.LEFT|wx.RIGHT) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
107 |
self.MainSizer.AddWindow(self.Preview, border=20, |
814 | 108 |
flag=wx.GROW|wx.LEFT|wx.RIGHT) |
109 |
||
1244 | 110 |
# Add buttons sizer to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
111 |
self.MainSizer.AddSizer(self.ButtonSizer, border=20, |
814 | 112 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
113 |
||
1244 | 114 |
# Set options that can be selected in class combo box |
814 | 115 |
for choice in VARIABLE_CLASSES_DICT.itervalues(): |
116 |
self.Class.Append(choice) |
|
117 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[INPUT]) |
|
1244 | 118 |
|
1246 | 119 |
# Extract list of variables defined in POU |
120 |
self.RefreshVariableList() |
|
1244 | 121 |
|
122 |
# Refresh values in name list box |
|
814 | 123 |
self.RefreshNameList() |
1244 | 124 |
|
125 |
# Class combo box is default control having keyboard focus |
|
814 | 126 |
self.Class.SetFocus() |
127 |
||
128 |
def RefreshNameList(self): |
|
1244 | 129 |
""" |
130 |
Called to refresh names in name list box |
|
131 |
""" |
|
132 |
# Get variable class to select POU variable applicable |
|
133 |
var_class = VARIABLE_CLASSES_DICT_REVERSE[ |
|
134 |
self.Class.GetStringSelection()] |
|
135 |
||
136 |
# Refresh names in name list box by selecting variables in POU variables |
|
137 |
# list that can be applied to variable class |
|
814 | 138 |
self.VariableName.Clear() |
1246 | 139 |
for name, (var_type, value_type) in self.VariableList.iteritems(): |
814 | 140 |
if var_type != "Input" or var_class == INPUT: |
141 |
self.VariableName.Append(name) |
|
1244 | 142 |
|
143 |
# Get variable expression and select corresponding value in name list |
|
144 |
# box if it exists |
|
145 |
selected = self.Expression.GetValue() |
|
146 |
if (selected != "" and |
|
147 |
self.VariableName.FindString(selected) != wx.NOT_FOUND): |
|
814 | 148 |
self.VariableName.SetStringSelection(selected) |
149 |
else: |
|
1182 | 150 |
self.VariableName.SetSelection(wx.NOT_FOUND) |
1244 | 151 |
|
152 |
# Disable name list box if no name present inside |
|
814 | 153 |
self.VariableName.Enable(self.VariableName.GetCount() > 0) |
154 |
||
155 |
def SetValues(self, values): |
|
1244 | 156 |
""" |
157 |
Set default variable parameters |
|
158 |
@param values: Variable parameters values |
|
159 |
""" |
|
160 |
||
161 |
# Get class parameter value |
|
162 |
var_class = values.get("class", None) |
|
163 |
if var_class is not None: |
|
164 |
# Set class selected in class combo box |
|
165 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class]) |
|
166 |
# Refresh names in name list box according to var class |
|
814 | 167 |
self.RefreshNameList() |
1244 | 168 |
|
169 |
# For each parameters defined, set corresponding control value |
|
170 |
for name, value in values.items(): |
|
171 |
||
172 |
# Parameter is variable expression |
|
173 |
if name == "expression": |
|
174 |
# Set expression text control value |
|
175 |
self.Expression.ChangeValue(value) |
|
176 |
# Select corresponding text in name list box if it exists |
|
177 |
if self.VariableName.FindString(value) != wx.NOT_FOUND: |
|
178 |
self.VariableName.SetStringSelection(value) |
|
179 |
else: |
|
180 |
self.VariableName.SetSelection(wx.NOT_FOUND) |
|
181 |
||
182 |
# Parameter is variable execution order |
|
183 |
elif name == "executionOrder": |
|
184 |
self.ExecutionOrder.SetValue(value) |
|
185 |
||
186 |
# Refresh preview panel |
|
814 | 187 |
self.RefreshPreview() |
188 |
||
189 |
def GetValues(self): |
|
1244 | 190 |
""" |
191 |
Return block parameters defined in dialog |
|
192 |
@return: {parameter_name: parameter_value,...} |
|
193 |
""" |
|
194 |
expression = self.Expression.GetValue() |
|
195 |
values = { |
|
196 |
"class": VARIABLE_CLASSES_DICT_REVERSE[ |
|
197 |
self.Class.GetStringSelection()], |
|
198 |
"expression": expression, |
|
1246 | 199 |
"var_type": self.VariableList.get(expression, (None, None))[1], |
1244 | 200 |
"executionOrder": self.ExecutionOrder.GetValue()} |
201 |
values["width"], values["height"] = self.Element.GetSize() |
|
814 | 202 |
return values |
203 |
||
204 |
def OnOK(self, event): |
|
1244 | 205 |
""" |
206 |
Called when dialog OK button is pressed |
|
207 |
Test if parameters defined are valid |
|
208 |
@param event: wx.Event from OK button |
|
209 |
""" |
|
814 | 210 |
message = None |
1244 | 211 |
|
212 |
# Test that an expression have been selected or typed by user |
|
1182 | 213 |
value = self.Expression.GetValue() |
814 | 214 |
if value == "": |
215 |
message = _("At least a variable or an expression must be selected!") |
|
1244 | 216 |
|
217 |
# Show error message if an error is detected |
|
814 | 218 |
if message is not None: |
1244 | 219 |
self.ShowErrorMessage(message) |
220 |
||
814 | 221 |
else: |
1244 | 222 |
# Call BlockPreviewDialog function |
223 |
BlockPreviewDialog.OnOK(self, event) |
|
814 | 224 |
|
225 |
def OnClassChanged(self, event): |
|
1244 | 226 |
""" |
227 |
Called when variable class value changed |
|
228 |
@param event: wx.ComboBoxEvent |
|
229 |
""" |
|
230 |
# Refresh name list box values |
|
814 | 231 |
self.RefreshNameList() |
1244 | 232 |
|
814 | 233 |
self.RefreshPreview() |
234 |
event.Skip() |
|
235 |
||
236 |
def OnNameChanged(self, event): |
|
1244 | 237 |
""" |
238 |
Called when name selected in name list box changed |
|
239 |
@param event: wx.ListBoxEvent |
|
240 |
""" |
|
241 |
# Change expression test control value to the value selected in name |
|
242 |
# list box if value selected is valid |
|
243 |
if self.VariableName.GetSelection() != wx.NOT_FOUND: |
|
244 |
self.Expression.ChangeValue(self.VariableName.GetStringSelection()) |
|
245 |
||
814 | 246 |
self.RefreshPreview() |
247 |
event.Skip() |
|
248 |
||
249 |
def OnExpressionChanged(self, event): |
|
1244 | 250 |
""" |
251 |
Called when expression text control is changed by user |
|
252 |
@param event: wx.ListBoxEvent |
|
253 |
""" |
|
254 |
# Select the corresponding value in name list box if it exists |
|
1182 | 255 |
self.VariableName.SetSelection( |
1244 | 256 |
self.VariableName.FindString(self.Expression.GetValue())) |
257 |
||
814 | 258 |
self.RefreshPreview() |
259 |
event.Skip() |
|
260 |
||
261 |
def OnExecutionOrderChanged(self, event): |
|
1244 | 262 |
""" |
263 |
Called when block execution control value changed |
|
264 |
@param event: wx.SpinEvent |
|
265 |
""" |
|
814 | 266 |
self.RefreshPreview() |
267 |
event.Skip() |
|
268 |
||
269 |
def RefreshPreview(self): |
|
1244 | 270 |
""" |
271 |
Refresh preview panel of graphic element |
|
272 |
Override BlockPreviewDialog function |
|
273 |
""" |
|
274 |
# Get expression value to put in FBD variable element |
|
1182 | 275 |
name = self.Expression.GetValue() |
1244 | 276 |
|
277 |
# Set graphic element displayed, creating a FBD variable element |
|
278 |
self.Element = FBD_Variable(self.Preview, |
|
279 |
VARIABLE_CLASSES_DICT_REVERSE[ |
|
280 |
self.Class.GetStringSelection()], |
|
281 |
name, |
|
1246 | 282 |
self.VariableList.get(name, ("", ""))[1], |
1244 | 283 |
executionOrder = self.ExecutionOrder.GetValue()) |
284 |
||
285 |
# Call BlockPreviewDialog function |
|
286 |
BlockPreviewDialog.RefreshPreview(self) |
|
287 |
||
288 |