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