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 |
|
|
27 |
from graphics import *
|
|
28 |
|
|
29 |
#-------------------------------------------------------------------------------
|
|
30 |
# Edit Ladder Element Properties Dialog
|
|
31 |
#-------------------------------------------------------------------------------
|
|
32 |
|
|
33 |
class LDElementDialog(wx.Dialog):
|
|
34 |
|
|
35 |
def __init__(self, parent, controller, type):
|
|
36 |
if type == "contact":
|
|
37 |
wx.Dialog.__init__(self, parent, size=wx.Size(350, 260),
|
|
38 |
title=_("Edit Contact Values"))
|
|
39 |
else:
|
|
40 |
wx.Dialog.__init__(self, parent, size=wx.Size(350, 310),
|
|
41 |
title=_("Edit Coil Values"))
|
|
42 |
|
|
43 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=10)
|
|
44 |
main_sizer.AddGrowableCol(0)
|
|
45 |
main_sizer.AddGrowableRow(0)
|
|
46 |
|
|
47 |
column_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
48 |
main_sizer.AddSizer(column_sizer, border=20,
|
|
49 |
flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
|
|
50 |
|
|
51 |
if type == "contact":
|
|
52 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=7, vgap=0)
|
|
53 |
else:
|
|
54 |
left_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=9, vgap=0)
|
|
55 |
left_gridsizer.AddGrowableCol(0)
|
|
56 |
column_sizer.AddSizer(left_gridsizer, 1, border=5,
|
|
57 |
flag=wx.GROW|wx.RIGHT)
|
|
58 |
|
|
59 |
modifier_label = wx.StaticText(self, label=_('Modifier:'))
|
|
60 |
left_gridsizer.AddWindow(modifier_label, border=5, flag=wx.GROW|wx.BOTTOM)
|
|
61 |
|
|
62 |
self.Normal = wx.RadioButton(self, label=_("Normal"), style=wx.RB_GROUP)
|
|
63 |
self.Normal.SetValue(True)
|
|
64 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.Normal)
|
|
65 |
left_gridsizer.AddWindow(self.Normal, flag=wx.GROW)
|
|
66 |
|
|
67 |
self.Negated = wx.RadioButton(self, label=_("Negated"))
|
|
68 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.Negated)
|
|
69 |
left_gridsizer.AddWindow(self.Negated, flag=wx.GROW)
|
|
70 |
|
|
71 |
if type != "contact":
|
|
72 |
self.Set = wx.RadioButton(self, label=_("Set"))
|
|
73 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.Set)
|
|
74 |
left_gridsizer.AddWindow(self.Set, flag=wx.GROW)
|
|
75 |
|
|
76 |
self.Reset = wx.RadioButton(self, label=_("Reset"))
|
|
77 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.Reset)
|
|
78 |
left_gridsizer.AddWindow(self.Reset, flag=wx.GROW)
|
|
79 |
|
|
80 |
self.RisingEdge = wx.RadioButton(self, label=_("Rising Edge"))
|
|
81 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.RisingEdge)
|
|
82 |
left_gridsizer.AddWindow(self.RisingEdge, flag=wx.GROW)
|
|
83 |
|
|
84 |
self.FallingEdge = wx.RadioButton(self, label=_("Falling Edge"))
|
|
85 |
self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, self.FallingEdge)
|
|
86 |
left_gridsizer.AddWindow(self.FallingEdge, flag=wx.GROW)
|
|
87 |
|
|
88 |
element_name_label = wx.StaticText(self, label=_('Name:'))
|
|
89 |
left_gridsizer.AddWindow(element_name_label, border=5, flag=wx.GROW|wx.TOP)
|
|
90 |
|
|
91 |
self.ElementName = wx.ComboBox(self, style=wx.CB_READONLY)
|
|
92 |
self.Bind(wx.EVT_COMBOBOX, self.OnNameChanged, self.ElementName)
|
|
93 |
left_gridsizer.AddWindow(self.ElementName, border=5, flag=wx.GROW|wx.TOP)
|
|
94 |
|
|
95 |
right_gridsizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=5)
|
|
96 |
right_gridsizer.AddGrowableCol(0)
|
|
97 |
right_gridsizer.AddGrowableRow(1)
|
|
98 |
column_sizer.AddSizer(right_gridsizer, 1, border=5,
|
|
99 |
flag=wx.GROW|wx.LEFT)
|
|
100 |
|
|
101 |
preview_label = wx.StaticText(self, label=_('Preview:'))
|
|
102 |
right_gridsizer.AddWindow(preview_label, flag=wx.GROW)
|
|
103 |
|
|
104 |
self.Preview = wx.Panel(self,
|
|
105 |
style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
|
|
106 |
self.Preview.SetBackgroundColour(wx.Colour(255,255,255))
|
|
107 |
setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
|
|
108 |
setattr(self.Preview, "GetScaling", lambda:None)
|
|
109 |
setattr(self.Preview, "IsOfType", controller.IsOfType)
|
|
110 |
self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
|
|
111 |
right_gridsizer.AddWindow(self.Preview, flag=wx.GROW)
|
|
112 |
|
|
113 |
button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
|
|
114 |
main_sizer.AddSizer(button_sizer, border=20,
|
|
115 |
flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
|
|
116 |
|
|
117 |
self.SetSizer(main_sizer)
|
|
118 |
|
|
119 |
if type == "contact":
|
|
120 |
self.Element = LD_Contact(self.Preview, CONTACT_NORMAL, "")
|
|
121 |
else:
|
|
122 |
self.Element = LD_Coil(self.Preview, COIL_NORMAL, "")
|
|
123 |
|
|
124 |
self.Type = type
|
|
125 |
|
|
126 |
self.Normal.SetFocus()
|
|
127 |
|
|
128 |
def SetPreviewFont(self, font):
|
|
129 |
self.Preview.SetFont(font)
|
|
130 |
|
|
131 |
def SetElementSize(self, size):
|
|
132 |
min_width, min_height = self.Element.GetMinSize()
|
|
133 |
width, height = max(min_width, size[0]), max(min_height, size[1])
|
|
134 |
self.Element.SetSize(width, height)
|
|
135 |
|
|
136 |
def SetVariables(self, vars):
|
|
137 |
self.ElementName.Clear()
|
|
138 |
for name in vars:
|
|
139 |
self.ElementName.Append(name)
|
|
140 |
self.ElementName.Enable(self.ElementName.GetCount() > 0)
|
|
141 |
|
|
142 |
def SetValues(self, values):
|
|
143 |
for name, value in values.items():
|
|
144 |
if name == "name":
|
|
145 |
self.Element.SetName(value)
|
|
146 |
self.ElementName.SetStringSelection(value)
|
|
147 |
elif name == "type":
|
|
148 |
self.Element.SetType(value)
|
|
149 |
if self.Type == "contact":
|
|
150 |
if value == CONTACT_NORMAL:
|
|
151 |
self.Normal.SetValue(True)
|
|
152 |
elif value == CONTACT_REVERSE:
|
|
153 |
self.Negated.SetValue(True)
|
|
154 |
elif value == CONTACT_RISING:
|
|
155 |
self.RisingEdge.SetValue(True)
|
|
156 |
elif value == CONTACT_FALLING:
|
|
157 |
self.FallingEdge.SetValue(True)
|
|
158 |
elif self.Type == "coil":
|
|
159 |
if value == COIL_NORMAL:
|
|
160 |
self.Normal.SetValue(True)
|
|
161 |
elif value == COIL_REVERSE:
|
|
162 |
self.Negated.SetValue(True)
|
|
163 |
elif value == COIL_SET:
|
|
164 |
self.Set.SetValue(True)
|
|
165 |
elif value == COIL_RESET:
|
|
166 |
self.Reset.SetValue(True)
|
|
167 |
elif value == COIL_RISING:
|
|
168 |
self.RisingEdge.SetValue(True)
|
|
169 |
elif value == COIL_FALLING:
|
|
170 |
self.FallingEdge.SetValue(True)
|
|
171 |
|
|
172 |
def GetValues(self):
|
|
173 |
values = {}
|
|
174 |
values["name"] = self.Element.GetName()
|
|
175 |
values["type"] = self.Element.GetType()
|
|
176 |
values["width"], values["height"] = self.Element.GetSize()
|
|
177 |
return values
|
|
178 |
|
|
179 |
def OnTypeChanged(self, event):
|
|
180 |
if self.Type == "contact":
|
|
181 |
if self.Normal.GetValue():
|
|
182 |
self.Element.SetType(CONTACT_NORMAL)
|
|
183 |
elif self.Negated.GetValue():
|
|
184 |
self.Element.SetType(CONTACT_REVERSE)
|
|
185 |
elif self.RisingEdge.GetValue():
|
|
186 |
self.Element.SetType(CONTACT_RISING)
|
|
187 |
elif self.FallingEdge.GetValue():
|
|
188 |
self.Element.SetType(CONTACT_FALLING)
|
|
189 |
elif self.Type == "coil":
|
|
190 |
if self.Normal.GetValue():
|
|
191 |
self.Element.SetType(COIL_NORMAL)
|
|
192 |
elif self.Negated.GetValue():
|
|
193 |
self.Element.SetType(COIL_REVERSE)
|
|
194 |
elif self.Set.GetValue():
|
|
195 |
self.Element.SetType(COIL_SET)
|
|
196 |
elif self.Reset.GetValue():
|
|
197 |
self.Element.SetType(COIL_RESET)
|
|
198 |
elif self.RisingEdge.GetValue():
|
|
199 |
self.Element.SetType(COIL_RISING)
|
|
200 |
elif self.FallingEdge.GetValue():
|
|
201 |
self.Element.SetType(COIL_FALLING)
|
|
202 |
self.RefreshPreview()
|
|
203 |
event.Skip()
|
|
204 |
|
|
205 |
def OnNameChanged(self, event):
|
|
206 |
self.Element.SetName(self.ElementName.GetStringSelection())
|
|
207 |
self.RefreshPreview()
|
|
208 |
event.Skip()
|
|
209 |
|
|
210 |
def RefreshPreview(self):
|
|
211 |
dc = wx.ClientDC(self.Preview)
|
|
212 |
dc.SetFont(self.Preview.GetFont())
|
|
213 |
dc.Clear()
|
|
214 |
clientsize = self.Preview.GetClientSize()
|
|
215 |
width, height = self.Element.GetSize()
|
|
216 |
self.Element.SetPosition((clientsize.width - width) / 2, (clientsize.height - height) / 2)
|
|
217 |
self.Element.Draw(dc)
|
|
218 |
|
|
219 |
def OnPaint(self, event):
|
|
220 |
self.RefreshPreview()
|
|
221 |
event.Skip()
|