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 |
|
1246
|
27 |
from graphics.GraphicCommons import CONTACT_NORMAL, CONTACT_REVERSE, \
|
|
28 |
CONTACT_RISING, CONTACT_FALLING, COIL_NORMAL, COIL_REVERSE, COIL_SET, \
|
|
29 |
COIL_RESET, COIL_RISING, COIL_FALLING
|
|
30 |
from graphics.LD_Objects import LD_Contact, LD_Coil
|
|
31 |
from BlockPreviewDialog import BlockPreviewDialog
|
814
|
32 |
|
|
33 |
#-------------------------------------------------------------------------------
|
1246
|
34 |
# Set Ladder Element Parmeters Dialog
|
814
|
35 |
#-------------------------------------------------------------------------------
|
|
36 |
|
1246
|
37 |
"""
|
|
38 |
Class that implements a dialog for defining parameters of a LD contact or coil
|
|
39 |
graphic element
|
|
40 |
"""
|
|
41 |
|
|
42 |
class LDElementDialog(BlockPreviewDialog):
|
814
|
43 |
|
1246
|
44 |
def __init__(self, parent, controller, tagname, type):
|
|
45 |
"""
|
|
46 |
Constructor
|
|
47 |
@param parent: Parent wx.Window of dialog for modal
|
|
48 |
@param controller: Reference to project controller
|
|
49 |
@param tagname: Tagname of project POU edited
|
|
50 |
@param type: Type of LD element ('contact or 'coil')
|
|
51 |
"""
|
|
52 |
BlockPreviewDialog.__init__(self, parent, controller, tagname,
|
|
53 |
size=wx.Size(350, 260 if type == "contact" else 310),
|
|
54 |
title=(_("Edit Contact Values")
|
|
55 |
if type == "contact"
|
|
56 |
else _("Edit Coil Values")))
|
|
57 |
|
|
58 |
# Create dialog main sizer
|
814
|
59 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
|
|
60 |
main_sizer.AddGrowableCol(0)
|
|
61 |
main_sizer.AddGrowableRow(0)
|
|
62 |
|
1246
|
63 |
# Create a sizer for dividing LD element parameters in two columns
|
814
|
64 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
65 |
main_sizer.AddSizer(column_sizer, border=20,
|
|
66 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
67 |
|
1246
|
68 |
# Create a sizer for left column
|
|
69 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0,
|
|
70 |
rows=(7 if type == "contact" else 9), vgap=0)
|
814
|
71 |
left_gridsizer.AddGrowableCol(0)
|
|
72 |
column_sizer.AddSizer(left_gridsizer, 1, border=5,
|
|
73 |
flag=wx.GROW|wx.RIGHT)
|
|
74 |
|
1246
|
75 |
# Create label for LD element modifier
|
814
|
76 |
modifier_label = wx.StaticText(self, label=_('Modifier:'))
|
1246
|
77 |
left_gridsizer.AddWindow(modifier_label, border=5,
|
|
78 |
flag=wx.GROW|wx.BOTTOM)
|
|
79 |
|
|
80 |
# Create radio buttons for selecting LD element modifier
|
|
81 |
self.ModifierRadioButtons = {}
|
|
82 |
first = True
|
|
83 |
element_modifiers = ([CONTACT_NORMAL, CONTACT_REVERSE,
|
|
84 |
CONTACT_RISING, CONTACT_FALLING]
|
|
85 |
if type == "contact"
|
|
86 |
else [COIL_NORMAL, COIL_REVERSE, COIL_SET,
|
|
87 |
COIL_RESET, COIL_RISING, COIL_FALLING])
|
|
88 |
modifiers_label = [_("Normal"), _("Negated")] + \
|
|
89 |
([_("Set"), _("Reset")] if type == "coil" else []) + \
|
|
90 |
[_("Rising Edge"), _("Falling Edge")]
|
|
91 |
|
|
92 |
for modifier, label in zip(element_modifiers, modifiers_label):
|
|
93 |
radio_button = wx.RadioButton(self, label=label,
|
|
94 |
style=(wx.RB_GROUP if first else wx.RB_SINGLE))
|
|
95 |
radio_button.SetValue(first)
|
|
96 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnModifierChanged, radio_button)
|
|
97 |
left_gridsizer.AddWindow(radio_button, flag=wx.GROW)
|
|
98 |
self.ModifierRadioButtons[modifier] = radio_button
|
|
99 |
first = False
|
|
100 |
|
|
101 |
# Create label for LD element variable
|
|
102 |
element_variable_label = wx.StaticText(self, label=_('Variable:'))
|
|
103 |
left_gridsizer.AddWindow(element_variable_label, border=5,
|
|
104 |
flag=wx.GROW|wx.TOP)
|
|
105 |
|
|
106 |
# Create a combo box for defining LD element variable
|
|
107 |
self.ElementVariable = wx.ComboBox(self, style=wx.CB_READONLY)
|
|
108 |
self.Bind(wx.EVT_COMBOBOX, self.OnVariableChanged,
|
|
109 |
self.ElementVariable)
|
|
110 |
left_gridsizer.AddWindow(self.ElementVariable, border=5,
|
|
111 |
flag=wx.GROW|wx.TOP)
|
|
112 |
|
|
113 |
# Create a sizer for right column
|
814
|
114 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
|
|
115 |
right_gridsizer.AddGrowableCol(0)
|
|
116 |
right_gridsizer.AddGrowableRow(1)
|
|
117 |
column_sizer.AddSizer(right_gridsizer, 1, border=5,
|
|
118 |
flag=wx.GROW|wx.LEFT)
|
|
119 |
|
1246
|
120 |
# Add preview panel and associated label to sizers
|
|
121 |
right_gridsizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
|
814
|
122 |
right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
|
|
123 |
|
1246
|
124 |
# Add buttons sizer to sizers
|
|
125 |
main_sizer.AddSizer(self.ButtonSizer, border=20,
|
814
|
126 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
127 |
|
|
128 |
self.SetSizer(main_sizer)
|
|
129 |
|
1246
|
130 |
# Save LD element class
|
|
131 |
self.ElementClass = (LD_Contact if type == "contact" else LD_Coil)
|
|
132 |
|
|
133 |
# Extract list of variables defined in POU
|
|
134 |
self.RefreshVariableList()
|
|
135 |
|
|
136 |
# Set values in ElementVariable
|
|
137 |
for name, (var_type, value_type) in self.VariableList.iteritems():
|
|
138 |
# Only select BOOL variable and avoid input for coil
|
|
139 |
if (type == "contact" or var_type != "Input") and \
|
|
140 |
value_type == "BOOL":
|
|
141 |
self.ElementVariable.Append(name)
|
|
142 |
self.ElementVariable.Enable(self.ElementVariable.GetCount() > 0)
|
|
143 |
|
|
144 |
# Normal radio button is default control having keyboard focus
|
|
145 |
self.ModifierRadioButtons[element_modifiers[0]].SetFocus()
|
814
|
146 |
|
1246
|
147 |
def GetElementModifier(self):
|
|
148 |
"""
|
|
149 |
Return modifier selected for LD element
|
|
150 |
@return: Modifier selected (None if not found)
|
|
151 |
"""
|
|
152 |
# Go through radio buttons and return modifier associated to the one
|
|
153 |
# that is selected
|
|
154 |
for modifier, control in self.ModifierRadioButtons.iteritems():
|
|
155 |
if control.GetValue():
|
|
156 |
return modifier
|
|
157 |
return None
|
814
|
158 |
|
|
159 |
def SetValues(self, values):
|
1246
|
160 |
"""
|
|
161 |
Set default LD element parameters
|
|
162 |
@param values: Block parameters values
|
|
163 |
"""
|
|
164 |
# For each parameters defined, set corresponding control value
|
814
|
165 |
for name, value in values.items():
|
1246
|
166 |
|
|
167 |
# Parameter is LD element variable
|
|
168 |
if name == "variable":
|
|
169 |
self.ElementVariable.SetStringSelection(value)
|
|
170 |
|
|
171 |
# Set value of other controls
|
|
172 |
elif name == "modifier":
|
|
173 |
self.ModifierRadioButtons[value].SetValue(True)
|
|
174 |
|
|
175 |
# Refresh preview panel
|
|
176 |
self.RefreshPreview()
|
814
|
177 |
|
|
178 |
def GetValues(self):
|
1246
|
179 |
"""
|
|
180 |
Return LD element parameters defined in dialog
|
|
181 |
@return: {parameter_name: parameter_value,...}
|
|
182 |
"""
|
|
183 |
values = {
|
|
184 |
"variable": self.ElementVariable.GetValue(),
|
|
185 |
"modifier": self.GetElementModifier()}
|
814
|
186 |
values["width"], values["height"] = self.Element.GetSize()
|
|
187 |
return values
|
|
188 |
|
1246
|
189 |
def OnModifierChanged(self, event):
|
|
190 |
"""
|
|
191 |
Called when LD element modifier changed
|
|
192 |
@param event: wx.RadioButtonEvent
|
|
193 |
"""
|
814
|
194 |
self.RefreshPreview()
|
|
195 |
event.Skip()
|
|
196 |
|
1246
|
197 |
def OnVariableChanged(self, event):
|
|
198 |
"""
|
|
199 |
Called when LD element associated variable changed
|
|
200 |
@param event: wx.ComboBoxEvent
|
|
201 |
"""
|
814
|
202 |
self.RefreshPreview()
|
|
203 |
event.Skip()
|
|
204 |
|
|
205 |
def RefreshPreview(self):
|
1246
|
206 |
"""
|
|
207 |
Refresh preview panel of graphic element
|
|
208 |
Override BlockPreviewDialog function
|
|
209 |
"""
|
|
210 |
# Set graphic element displayed, creating a LD element
|
|
211 |
self.Element = self.ElementClass(
|
|
212 |
self.Preview,
|
|
213 |
self.GetElementModifier(),
|
|
214 |
self.ElementVariable.GetStringSelection())
|
|
215 |
|
|
216 |
# Call BlockPreviewDialog function
|
|
217 |
BlockPreviewDialog.RefreshPreview(self)
|