author | Laurent Bessard |
Wed, 25 Sep 2013 10:47:46 +0200 | |
changeset 1327 | 632780979432 |
parent 1259 | 8350222a81c3 |
child 1370 | ee795a8d4404 |
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 |
||
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, |
|
1259
8350222a81c3
Added support for adding graphic element when dropping wire in midair
Laurent Bessard
parents:
1252
diff
changeset
|
53 |
size=wx.Size(350, 280 if type == "contact" else 330), |
1246 | 54 |
title=(_("Edit Contact Values") |
55 |
if type == "contact" |
|
56 |
else _("Edit Coil Values"))) |
|
57 |
||
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
58 |
# Init common sizers |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
59 |
self._init_sizers(2, 0, |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
60 |
(7 if type == "contact" else 9), None, 2, 1) |
814 | 61 |
|
1246 | 62 |
# Create label for LD element modifier |
814 | 63 |
modifier_label = wx.StaticText(self, label=_('Modifier:')) |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
64 |
self.LeftGridSizer.AddWindow(modifier_label, border=5, |
1246 | 65 |
flag=wx.GROW|wx.BOTTOM) |
66 |
||
67 |
# Create radio buttons for selecting LD element modifier |
|
68 |
self.ModifierRadioButtons = {} |
|
69 |
first = True |
|
70 |
element_modifiers = ([CONTACT_NORMAL, CONTACT_REVERSE, |
|
71 |
CONTACT_RISING, CONTACT_FALLING] |
|
72 |
if type == "contact" |
|
73 |
else [COIL_NORMAL, COIL_REVERSE, COIL_SET, |
|
74 |
COIL_RESET, COIL_RISING, COIL_FALLING]) |
|
75 |
modifiers_label = [_("Normal"), _("Negated")] + \ |
|
76 |
([_("Set"), _("Reset")] if type == "coil" else []) + \ |
|
77 |
[_("Rising Edge"), _("Falling Edge")] |
|
78 |
||
79 |
for modifier, label in zip(element_modifiers, modifiers_label): |
|
80 |
radio_button = wx.RadioButton(self, label=label, |
|
1247 | 81 |
style=(wx.RB_GROUP if first else 0)) |
1246 | 82 |
radio_button.SetValue(first) |
83 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnModifierChanged, radio_button) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
84 |
self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW) |
1246 | 85 |
self.ModifierRadioButtons[modifier] = radio_button |
86 |
first = False |
|
87 |
||
88 |
# Create label for LD element variable |
|
89 |
element_variable_label = wx.StaticText(self, label=_('Variable:')) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
90 |
self.LeftGridSizer.AddWindow(element_variable_label, border=5, |
1246 | 91 |
flag=wx.GROW|wx.TOP) |
92 |
||
93 |
# Create a combo box for defining LD element variable |
|
1259
8350222a81c3
Added support for adding graphic element when dropping wire in midair
Laurent Bessard
parents:
1252
diff
changeset
|
94 |
self.ElementVariable = wx.ComboBox(self, style=wx.CB_READONLY|wx.CB_SORT) |
1246 | 95 |
self.Bind(wx.EVT_COMBOBOX, self.OnVariableChanged, |
96 |
self.ElementVariable) |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
97 |
self.LeftGridSizer.AddWindow(self.ElementVariable, border=5, |
1246 | 98 |
flag=wx.GROW|wx.TOP) |
99 |
||
100 |
# Add preview panel and associated label to sizers |
|
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
101 |
self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW) |
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
102 |
self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW) |
814 | 103 |
|
1246 | 104 |
# Add buttons sizer to sizers |
1250
7e6de17c687a
Rewrite SFCStepDialog and factorize code for creating common dialog sizers
Laurent Bessard
parents:
1247
diff
changeset
|
105 |
self.MainSizer.AddSizer(self.ButtonSizer, border=20, |
814 | 106 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
107 |
||
1246 | 108 |
# Save LD element class |
109 |
self.ElementClass = (LD_Contact if type == "contact" else LD_Coil) |
|
110 |
||
111 |
# Extract list of variables defined in POU |
|
112 |
self.RefreshVariableList() |
|
113 |
||
114 |
# Set values in ElementVariable |
|
115 |
for name, (var_type, value_type) in self.VariableList.iteritems(): |
|
116 |
# Only select BOOL variable and avoid input for coil |
|
117 |
if (type == "contact" or var_type != "Input") and \ |
|
118 |
value_type == "BOOL": |
|
119 |
self.ElementVariable.Append(name) |
|
120 |
self.ElementVariable.Enable(self.ElementVariable.GetCount() > 0) |
|
121 |
||
122 |
# Normal radio button is default control having keyboard focus |
|
123 |
self.ModifierRadioButtons[element_modifiers[0]].SetFocus() |
|
814 | 124 |
|
1246 | 125 |
def GetElementModifier(self): |
126 |
""" |
|
127 |
Return modifier selected for LD element |
|
128 |
@return: Modifier selected (None if not found) |
|
129 |
""" |
|
130 |
# Go through radio buttons and return modifier associated to the one |
|
131 |
# that is selected |
|
132 |
for modifier, control in self.ModifierRadioButtons.iteritems(): |
|
133 |
if control.GetValue(): |
|
134 |
return modifier |
|
135 |
return None |
|
814 | 136 |
|
137 |
def SetValues(self, values): |
|
1246 | 138 |
""" |
139 |
Set default LD element parameters |
|
1252 | 140 |
@param values: LD element parameters values |
1246 | 141 |
""" |
142 |
# For each parameters defined, set corresponding control value |
|
814 | 143 |
for name, value in values.items(): |
1246 | 144 |
|
145 |
# Parameter is LD element variable |
|
146 |
if name == "variable": |
|
147 |
self.ElementVariable.SetStringSelection(value) |
|
148 |
||
149 |
# Set value of other controls |
|
150 |
elif name == "modifier": |
|
151 |
self.ModifierRadioButtons[value].SetValue(True) |
|
152 |
||
153 |
# Refresh preview panel |
|
154 |
self.RefreshPreview() |
|
814 | 155 |
|
156 |
def GetValues(self): |
|
1246 | 157 |
""" |
158 |
Return LD element parameters defined in dialog |
|
159 |
@return: {parameter_name: parameter_value,...} |
|
160 |
""" |
|
161 |
values = { |
|
162 |
"variable": self.ElementVariable.GetValue(), |
|
163 |
"modifier": self.GetElementModifier()} |
|
814 | 164 |
values["width"], values["height"] = self.Element.GetSize() |
165 |
return values |
|
166 |
||
1246 | 167 |
def OnModifierChanged(self, event): |
168 |
""" |
|
169 |
Called when LD element modifier changed |
|
170 |
@param event: wx.RadioButtonEvent |
|
171 |
""" |
|
814 | 172 |
self.RefreshPreview() |
173 |
event.Skip() |
|
174 |
||
1246 | 175 |
def OnVariableChanged(self, event): |
176 |
""" |
|
177 |
Called when LD element associated variable changed |
|
178 |
@param event: wx.ComboBoxEvent |
|
179 |
""" |
|
814 | 180 |
self.RefreshPreview() |
181 |
event.Skip() |
|
182 |
||
183 |
def RefreshPreview(self): |
|
1246 | 184 |
""" |
185 |
Refresh preview panel of graphic element |
|
186 |
Override BlockPreviewDialog function |
|
187 |
""" |
|
188 |
# Set graphic element displayed, creating a LD element |
|
189 |
self.Element = self.ElementClass( |
|
190 |
self.Preview, |
|
191 |
self.GetElementModifier(), |
|
192 |
self.ElementVariable.GetStringSelection()) |
|
193 |
||
194 |
# Call BlockPreviewDialog function |
|
195 |
BlockPreviewDialog.RefreshPreview(self) |