author | Surkov Sergey <surkovsv93@gmail.com> |
Tue, 04 Jul 2017 14:00:42 +0300 | |
changeset 1713 | ae58d214997c |
parent 1696 | 8043f32de7b8 |
child 1730 | 64d8f52bc8c8 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
6 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
8 |
# Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com> |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
9 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
10 |
# See COPYING file for copyrights details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
11 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
12 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
13 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
14 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
15 |
# of the License, or (at your option) any later version. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
16 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
17 |
# This program is distributed in the hope that it will be useful, |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
20 |
# GNU General Public License for more details. |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
21 |
# |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
22 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
23 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
24 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 25 |
|
26 |
import wx |
|
27 |
||
1244 | 28 |
from graphics.GraphicCommons import INPUT, INOUT, OUTPUT |
29 |
from graphics.FBD_Objects import FBD_Variable |
|
30 |
from BlockPreviewDialog import BlockPreviewDialog |
|
814 | 31 |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
# Helpers |
|
34 |
#------------------------------------------------------------------------------- |
|
35 |
||
1244 | 36 |
# Dictionaries containing correspondence between variable block class and string |
37 |
# to be shown in Class combo box in both sense |
|
814 | 38 |
VARIABLE_CLASSES_DICT = {INPUT : _("Input"), |
39 |
INOUT : _("InOut"), |
|
40 |
OUTPUT : _("Output")} |
|
41 |
VARIABLE_CLASSES_DICT_REVERSE = dict( |
|
42 |
[(value, key) for key, value in VARIABLE_CLASSES_DICT.iteritems()]) |
|
43 |
||
44 |
#------------------------------------------------------------------------------- |
|
1244 | 45 |
# Set Variable Parameters Dialog |
814 | 46 |
#------------------------------------------------------------------------------- |
47 |
||
1244 | 48 |
""" |
49 |
Class that implements a dialog for defining parameters of a FBD variable graphic |
|
50 |
element |
|
51 |
""" |
|
52 |
||
53 |
class FBDVariableDialog(BlockPreviewDialog): |
|
54 |
||
1259
8350222a81c3
Added support for adding graphic element when dropping wire in midair
Laurent Bessard
parents:
1250
diff
changeset
|
55 |
def __init__(self, parent, controller, tagname, exclude_input=False): |
1244 | 56 |
""" |
57 |
Constructor |
|
58 |
@param parent: Parent wx.Window of dialog for modal |
|
59 |
@param controller: Reference to project controller |
|
60 |
@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
|
61 |
@param exclude_input: Exclude input from variable class selection |
1244 | 62 |
""" |
63 |
BlockPreviewDialog.__init__(self, parent, controller, tagname, |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
64 |
title=_('Variable Properties')) |
814 | 65 |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
66 |
# Init common sizers |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
67 |
self._init_sizers(4, 2, 4, None, 3, 2) |
814 | 68 |
|
1244 | 69 |
# Create label for variable class |
814 | 70 |
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
|
71 |
self.LeftGridSizer.AddWindow(class_label, flag=wx.GROW) |
814 | 72 |
|
1244 | 73 |
# Create a combo box for defining variable class |
814 | 74 |
self.Class = wx.ComboBox(self, style=wx.CB_READONLY) |
75 |
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
|
76 |
self.LeftGridSizer.AddWindow(self.Class, flag=wx.GROW) |
814 | 77 |
|
1244 | 78 |
# Create label for variable execution order |
79 |
execution_order_label = wx.StaticText(self, |
|
80 |
label=_('Execution Order:')) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
81 |
self.LeftGridSizer.AddWindow(execution_order_label, flag=wx.GROW) |
814 | 82 |
|
1244 | 83 |
# Create spin control for defining variable execution order |
814 | 84 |
self.ExecutionOrder = wx.SpinCtrl(self, min=0, style=wx.SP_ARROW_KEYS) |
1244 | 85 |
self.Bind(wx.EVT_SPINCTRL, self.OnExecutionOrderChanged, |
86 |
self.ExecutionOrder) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
87 |
self.LeftGridSizer.AddWindow(self.ExecutionOrder, flag=wx.GROW) |
814 | 88 |
|
1244 | 89 |
# Create label for variable expression |
1182 | 90 |
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
|
91 |
self.RightGridSizer.AddWindow(name_label, border=5, |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
92 |
flag=wx.GROW|wx.BOTTOM) |
1182 | 93 |
|
1244 | 94 |
# Create text control for defining variable expression |
1182 | 95 |
self.Expression = wx.TextCtrl(self) |
96 |
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
|
97 |
self.RightGridSizer.AddWindow(self.Expression, flag=wx.GROW) |
1182 | 98 |
|
1244 | 99 |
# Create a list box to selected variable expression in the list of |
100 |
# variables defined in POU |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
101 |
self.VariableName = wx.ListBox(self, size=wx.Size(-1,120), |
814 | 102 |
style=wx.LB_SINGLE|wx.LB_SORT) |
103 |
self.Bind(wx.EVT_LISTBOX, self.OnNameChanged, self.VariableName) |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
104 |
self.RightGridSizer.AddWindow(self.VariableName, border=4, flag=wx.GROW|wx.TOP) |
814 | 105 |
|
1244 | 106 |
# 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
|
107 |
self.MainSizer.AddWindow(self.PreviewLabel, border=20, |
814 | 108 |
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
|
109 |
self.MainSizer.AddWindow(self.Preview, border=20, |
814 | 110 |
flag=wx.GROW|wx.LEFT|wx.RIGHT) |
111 |
||
1244 | 112 |
# Add buttons sizer to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1246
diff
changeset
|
113 |
self.MainSizer.AddSizer(self.ButtonSizer, border=20, |
814 | 114 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
115 |
||
1244 | 116 |
# 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
|
117 |
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
|
118 |
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
|
119 |
self.Class.Append(choice) |
8350222a81c3
Added support for adding graphic element when dropping wire in midair
Laurent Bessard
parents:
1250
diff
changeset
|
120 |
self.Class.SetSelection(0) |
1244 | 121 |
|
1246 | 122 |
# Extract list of variables defined in POU |
123 |
self.RefreshVariableList() |
|
1244 | 124 |
|
125 |
# Refresh values in name list box |
|
814 | 126 |
self.RefreshNameList() |
1244 | 127 |
|
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
128 |
self.Preview.SetInitialSize(wx.Size(-1, 60)) |
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
129 |
self.Fit() |
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
130 |
|
1244 | 131 |
# Class combo box is default control having keyboard focus |
814 | 132 |
self.Class.SetFocus() |
133 |
||
134 |
def RefreshNameList(self): |
|
1244 | 135 |
""" |
136 |
Called to refresh names in name list box |
|
137 |
""" |
|
138 |
# Get variable class to select POU variable applicable |
|
139 |
var_class = VARIABLE_CLASSES_DICT_REVERSE[ |
|
140 |
self.Class.GetStringSelection()] |
|
141 |
||
142 |
# Refresh names in name list box by selecting variables in POU variables |
|
143 |
# list that can be applied to variable class |
|
814 | 144 |
self.VariableName.Clear() |
1246 | 145 |
for name, (var_type, value_type) in self.VariableList.iteritems(): |
814 | 146 |
if var_type != "Input" or var_class == INPUT: |
147 |
self.VariableName.Append(name) |
|
1244 | 148 |
|
149 |
# Get variable expression and select corresponding value in name list |
|
150 |
# box if it exists |
|
151 |
selected = self.Expression.GetValue() |
|
152 |
if (selected != "" and |
|
153 |
self.VariableName.FindString(selected) != wx.NOT_FOUND): |
|
814 | 154 |
self.VariableName.SetStringSelection(selected) |
155 |
else: |
|
1182 | 156 |
self.VariableName.SetSelection(wx.NOT_FOUND) |
1244 | 157 |
|
158 |
# Disable name list box if no name present inside |
|
814 | 159 |
self.VariableName.Enable(self.VariableName.GetCount() > 0) |
160 |
||
161 |
def SetValues(self, values): |
|
1244 | 162 |
""" |
163 |
Set default variable parameters |
|
164 |
@param values: Variable parameters values |
|
165 |
""" |
|
166 |
||
167 |
# Get class parameter value |
|
168 |
var_class = values.get("class", None) |
|
169 |
if var_class is not None: |
|
170 |
# Set class selected in class combo box |
|
171 |
self.Class.SetStringSelection(VARIABLE_CLASSES_DICT[var_class]) |
|
172 |
# Refresh names in name list box according to var class |
|
814 | 173 |
self.RefreshNameList() |
1244 | 174 |
|
175 |
# For each parameters defined, set corresponding control value |
|
176 |
for name, value in values.items(): |
|
177 |
||
178 |
# Parameter is variable expression |
|
179 |
if name == "expression": |
|
180 |
# Set expression text control value |
|
181 |
self.Expression.ChangeValue(value) |
|
182 |
# Select corresponding text in name list box if it exists |
|
183 |
if self.VariableName.FindString(value) != wx.NOT_FOUND: |
|
184 |
self.VariableName.SetStringSelection(value) |
|
185 |
else: |
|
186 |
self.VariableName.SetSelection(wx.NOT_FOUND) |
|
187 |
||
188 |
# Parameter is variable execution order |
|
189 |
elif name == "executionOrder": |
|
190 |
self.ExecutionOrder.SetValue(value) |
|
191 |
||
192 |
# Refresh preview panel |
|
814 | 193 |
self.RefreshPreview() |
1696
8043f32de7b8
make all dialog have non-fixed size
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
194 |
self.Fit() |
814 | 195 |
|
196 |
def GetValues(self): |
|
1244 | 197 |
""" |
198 |
Return block parameters defined in dialog |
|
199 |
@return: {parameter_name: parameter_value,...} |
|
200 |
""" |
|
201 |
expression = self.Expression.GetValue() |
|
202 |
values = { |
|
203 |
"class": VARIABLE_CLASSES_DICT_REVERSE[ |
|
204 |
self.Class.GetStringSelection()], |
|
205 |
"expression": expression, |
|
1246 | 206 |
"var_type": self.VariableList.get(expression, (None, None))[1], |
1244 | 207 |
"executionOrder": self.ExecutionOrder.GetValue()} |
208 |
values["width"], values["height"] = self.Element.GetSize() |
|
814 | 209 |
return values |
210 |
||
211 |
def OnOK(self, event): |
|
1244 | 212 |
""" |
213 |
Called when dialog OK button is pressed |
|
214 |
Test if parameters defined are valid |
|
215 |
@param event: wx.Event from OK button |
|
216 |
""" |
|
814 | 217 |
message = None |
1244 | 218 |
|
219 |
# Test that an expression have been selected or typed by user |
|
1182 | 220 |
value = self.Expression.GetValue() |
814 | 221 |
if value == "": |
222 |
message = _("At least a variable or an expression must be selected!") |
|
1244 | 223 |
|
224 |
# Show error message if an error is detected |
|
814 | 225 |
if message is not None: |
1244 | 226 |
self.ShowErrorMessage(message) |
227 |
||
814 | 228 |
else: |
1244 | 229 |
# Call BlockPreviewDialog function |
230 |
BlockPreviewDialog.OnOK(self, event) |
|
814 | 231 |
|
232 |
def OnClassChanged(self, event): |
|
1244 | 233 |
""" |
234 |
Called when variable class value changed |
|
235 |
@param event: wx.ComboBoxEvent |
|
236 |
""" |
|
237 |
# Refresh name list box values |
|
814 | 238 |
self.RefreshNameList() |
1244 | 239 |
|
814 | 240 |
self.RefreshPreview() |
241 |
event.Skip() |
|
242 |
||
243 |
def OnNameChanged(self, event): |
|
1244 | 244 |
""" |
245 |
Called when name selected in name list box changed |
|
246 |
@param event: wx.ListBoxEvent |
|
247 |
""" |
|
248 |
# Change expression test control value to the value selected in name |
|
249 |
# list box if value selected is valid |
|
250 |
if self.VariableName.GetSelection() != wx.NOT_FOUND: |
|
251 |
self.Expression.ChangeValue(self.VariableName.GetStringSelection()) |
|
252 |
||
814 | 253 |
self.RefreshPreview() |
254 |
event.Skip() |
|
255 |
||
256 |
def OnExpressionChanged(self, event): |
|
1244 | 257 |
""" |
258 |
Called when expression text control is changed by user |
|
259 |
@param event: wx.ListBoxEvent |
|
260 |
""" |
|
261 |
# Select the corresponding value in name list box if it exists |
|
1182 | 262 |
self.VariableName.SetSelection( |
1244 | 263 |
self.VariableName.FindString(self.Expression.GetValue())) |
264 |
||
814 | 265 |
self.RefreshPreview() |
266 |
event.Skip() |
|
267 |
||
268 |
def OnExecutionOrderChanged(self, event): |
|
1244 | 269 |
""" |
270 |
Called when block execution control value changed |
|
271 |
@param event: wx.SpinEvent |
|
272 |
""" |
|
814 | 273 |
self.RefreshPreview() |
274 |
event.Skip() |
|
275 |
||
276 |
def RefreshPreview(self): |
|
1244 | 277 |
""" |
278 |
Refresh preview panel of graphic element |
|
279 |
Override BlockPreviewDialog function |
|
280 |
""" |
|
281 |
# Get expression value to put in FBD variable element |
|
1182 | 282 |
name = self.Expression.GetValue() |
1244 | 283 |
|
284 |
# Set graphic element displayed, creating a FBD variable element |
|
285 |
self.Element = FBD_Variable(self.Preview, |
|
286 |
VARIABLE_CLASSES_DICT_REVERSE[ |
|
287 |
self.Class.GetStringSelection()], |
|
288 |
name, |
|
1246 | 289 |
self.VariableList.get(name, ("", ""))[1], |
1244 | 290 |
executionOrder = self.ExecutionOrder.GetValue()) |
291 |
||
292 |
# Call BlockPreviewDialog function |
|
293 |
BlockPreviewDialog.RefreshPreview(self) |
|
294 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1259
diff
changeset
|
295 |