author | lbessard |
Tue, 30 Oct 2007 16:53:08 +0100 | |
changeset 118 | 0c53d6a36013 |
parent 112 | 317148fc1225 |
child 127 | 436268f31dae |
permissions | -rw-r--r-- |
0 | 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 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 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 |
|
58 | 19 |
#General Public License for more details. |
0 | 20 |
# |
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 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 GraphicCommons import * |
|
28 |
from plcopen.structures import * |
|
29 |
||
30 |
#------------------------------------------------------------------------------- |
|
31 |
# Ladder Diagram PowerRail |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
||
34 |
""" |
|
35 |
Class that implements the graphic representation of a power rail |
|
36 |
""" |
|
37 |
||
38 |
class LD_PowerRail(Graphic_Element): |
|
39 |
||
40 |
# Create a new power rail |
|
41 |
def __init__(self, parent, type, id = None, connectors = [True]): |
|
42 |
Graphic_Element.__init__(self, parent) |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
43 |
self.Type = None |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
44 |
self.Connectors = [] |
71 | 45 |
self.RealConnectors = None |
0 | 46 |
self.Id = id |
27 | 47 |
self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2] |
48 |
if len(connectors) < 1: |
|
49 |
connectors = [True] |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
50 |
self.SetType(type, connectors) |
0 | 51 |
|
52 |
# Destructor |
|
53 |
def __del__(self): |
|
54 |
self.Connectors = [] |
|
55 |
||
112 | 56 |
# Make a clone of this LD_PowerRail |
57 |
def Clone(self, id = None, pos = None): |
|
58 |
powerrail = LD_PowerRail(self.Parent, self.Type, id) |
|
59 |
powerrail.SetSize(self.Size[0], self.Size[1]) |
|
60 |
if pos is not None: |
|
61 |
powerrail.SetPosition(pos.x, pos.y) |
|
62 |
powerrail.Connectors = [] |
|
63 |
for connector in self.Connectors: |
|
64 |
if connector is not None: |
|
65 |
powerrail.Connectors.append(connector.Clone(powerrail)) |
|
66 |
else: |
|
67 |
powerrail.Connectors.append(None) |
|
68 |
return powerrail |
|
69 |
||
0 | 70 |
# Forbids to change the power rail size |
71 |
def SetSize(self, width, height): |
|
42 | 72 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 73 |
Graphic_Element.SetSize(self, width, height) |
74 |
self.RefreshConnectors() |
|
0 | 75 |
|
76 |
# Forbids to select a power rail |
|
77 |
def HitTest(self, pt): |
|
42 | 78 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 79 |
return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None |
0 | 80 |
return False |
81 |
||
42 | 82 |
# Forbids to select a power rail |
83 |
def IsInSelection(self, rect): |
|
84 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
85 |
return Graphic_Element.IsInSelection(self, rect) |
42 | 86 |
return False |
87 |
||
0 | 88 |
# Deletes this power rail by calling the appropriate method |
89 |
def Delete(self): |
|
90 |
self.Parent.DeletePowerRail(self) |
|
91 |
||
92 |
# Unconnect all connectors |
|
93 |
def Clean(self): |
|
94 |
for connector in self.Connectors: |
|
95 |
if connector: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
96 |
connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 97 |
|
98 |
# Refresh the power rail bounding box |
|
99 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
100 |
dc = wx.ClientDC(self.Parent) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
101 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1) |
0 | 102 |
|
103 |
# Refresh the power rail size |
|
104 |
def RefreshSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
105 |
self.Size = wx.Size(2, LD_LINE_SIZE * len(self.Connectors)) |
0 | 106 |
self.RefreshBoundingBox() |
107 |
||
27 | 108 |
# Returns the block minimum size |
109 |
def GetMinSize(self): |
|
96 | 110 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
111 |
return 2, self.Extensions[0] + self.Extensions[1] |
|
112 |
else: |
|
113 |
return 2, LD_LINE_SIZE * len(self.Connectors) |
|
27 | 114 |
|
0 | 115 |
# Add a connector or a blank to this power rail at the last place |
116 |
def AddConnector(self, connector = True): |
|
117 |
self.InsertConnector(len(self.Connectors), connector) |
|
118 |
||
119 |
# Add a connector or a blank to this power rail at the place given |
|
120 |
def InsertConnector(self, idx, connector = True): |
|
121 |
if connector: |
|
122 |
if self.Type == LEFTRAIL: |
|
80 | 123 |
connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) |
0 | 124 |
elif self.Type == RIGHTRAIL: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
125 |
connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST) |
0 | 126 |
self.Connectors.insert(idx, connector) |
127 |
else: |
|
128 |
self.Connectors.insert(idx, None) |
|
129 |
self.RefreshSize() |
|
130 |
self.RefreshConnectors() |
|
131 |
||
27 | 132 |
# Moves the divergence connector given |
133 |
def MoveConnector(self, connector, movey): |
|
134 |
position = connector.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
135 |
connector.SetPosition(wx.Point(position.x, position.y + movey)) |
27 | 136 |
miny = self.Size[1] |
137 |
maxy = 0 |
|
138 |
for connect in self.Connectors: |
|
139 |
connect_pos = connect.GetRelPosition() |
|
80 | 140 |
miny = min(miny, connect_pos.y - self.Extensions[0]) |
141 |
maxy = max(maxy, connect_pos.y - self.Extensions[0]) |
|
142 |
min_pos = self.Pos.y + miny |
|
27 | 143 |
self.Pos.y = min(min_pos, self.Pos.y) |
144 |
if min_pos == self.Pos.y: |
|
145 |
for connect in self.Connectors: |
|
146 |
connect_pos = connect.GetRelPosition() |
|
80 | 147 |
connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny)) |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
148 |
self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y)) |
80 | 149 |
maxy = 0 |
150 |
for connect in self.Connectors: |
|
151 |
connect_pos = connect.GetRelPosition() |
|
152 |
maxy = max(maxy, connect_pos.y) |
|
153 |
self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) |
|
27 | 154 |
connector.MoveConnected() |
155 |
self.RefreshBoundingBox() |
|
156 |
||
0 | 157 |
# Returns the index in connectors list for the connector given |
158 |
def GetConnectorIndex(self, connector): |
|
159 |
if connector in self.Connectors: |
|
160 |
return self.Connectors.index(connector) |
|
161 |
return None |
|
162 |
||
163 |
# Returns if there is a connector in connectors list at the index given |
|
164 |
def IsNullConnector(self, idx): |
|
165 |
if idx < len(self.Connectors): |
|
166 |
return self.Connectors[idx] == None |
|
167 |
return False |
|
168 |
||
169 |
# Delete the connector or blank from connectors list at the index given |
|
170 |
def DeleteConnector(self, idx): |
|
171 |
self.Connectors.pop(idx) |
|
172 |
self.RefreshConnectors() |
|
173 |
self.RefreshSize() |
|
174 |
||
175 |
# Refresh the positions of the power rail connectors |
|
176 |
def RefreshConnectors(self): |
|
71 | 177 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
178 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
80 | 179 |
interval = float(height) / float(max(len(self.Connectors) - 1, 1)) |
71 | 180 |
for i, connector in enumerate(self.Connectors): |
181 |
position = connector.GetRelPosition() |
|
80 | 182 |
if self.Type == LEFTRAIL: |
183 |
if self.RealConnectors: |
|
71 | 184 |
connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) |
80 | 185 |
else: |
96 | 186 |
connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(i * interval)))) |
80 | 187 |
elif self.Type == RIGHTRAIL: |
188 |
if self.RealConnectors: |
|
71 | 189 |
connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) |
80 | 190 |
else: |
96 | 191 |
connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(i * interval)))) |
71 | 192 |
else: |
193 |
position = self.Extensions[0] |
|
194 |
for connector in self.Connectors: |
|
195 |
if connector: |
|
196 |
if self.Type == LEFTRAIL: |
|
197 |
connector.SetPosition(wx.Point(self.Size[0], position)) |
|
198 |
elif self.Type == RIGHTRAIL: |
|
199 |
connector.SetPosition(wx.Point(0, position)) |
|
200 |
position += LD_LINE_SIZE |
|
0 | 201 |
self.RefreshConnected() |
202 |
||
7 | 203 |
# Refresh the position of wires connected to power rail |
0 | 204 |
def RefreshConnected(self, exclude = []): |
205 |
for connector in self.Connectors: |
|
8 | 206 |
if connector: |
207 |
connector.MoveConnected(exclude) |
|
0 | 208 |
|
209 |
# Returns the power rail connector that starts with the point given if it exists |
|
27 | 210 |
def GetConnector(self, position, name = None): |
211 |
# if a name is given |
|
212 |
if name: |
|
213 |
# Test each connector if it exists |
|
214 |
for connector in self.Connectors: |
|
215 |
if connector and name == connector.GetName(): |
|
216 |
return connector |
|
0 | 217 |
for connector in self.Connectors: |
218 |
if connector: |
|
219 |
connector_pos = connector.GetRelPosition() |
|
220 |
if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y: |
|
221 |
return connector |
|
222 |
return None |
|
223 |
||
224 |
# Returns all the power rail connectors |
|
225 |
def GetConnectors(self): |
|
226 |
return [connector for connector in self.Connectors if connector] |
|
227 |
||
228 |
# Test if point given is on one of the power rail connectors |
|
229 |
def TestConnector(self, pt, exclude=True): |
|
230 |
for connector in self.Connectors: |
|
231 |
if connector and connector.TestPoint(pt, exclude): |
|
232 |
return connector |
|
233 |
return None |
|
234 |
||
235 |
# Returns the power rail type |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
236 |
def SetType(self, type, connectors): |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
237 |
if type != self.Type or len(self.Connectors) != len(connectors): |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
238 |
# Create a connector or a blank according to 'connectors' and add it in |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
239 |
# the connectors list |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
240 |
self.Type = type |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
241 |
self.Clean() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
242 |
self.Connectors = [] |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
243 |
for connector in connectors: |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
244 |
self.AddConnector(connector) |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
245 |
self.RefreshSize() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
246 |
|
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
247 |
# Returns the power rail type |
0 | 248 |
def GetType(self): |
249 |
return self.Type |
|
250 |
||
27 | 251 |
# Method called when a LeftDown event have been generated |
252 |
def OnLeftDown(self, event, dc, scaling): |
|
253 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
254 |
# Test if a connector have been handled |
|
255 |
connector = self.TestConnector(pos, False) |
|
256 |
if connector: |
|
257 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
258 |
self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) |
27 | 259 |
self.Selected = False |
260 |
# Initializes the last position |
|
261 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
262 |
else: |
|
71 | 263 |
self.RealConnectors = [] |
264 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
265 |
for connector in self.Connectors: |
|
266 |
position = connector.GetRelPosition() |
|
267 |
self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height))) |
|
27 | 268 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
269 |
||
270 |
# Method called when a LeftUp event have been generated |
|
271 |
def OnLeftUp(self, event, dc, scaling): |
|
272 |
handle_type, handle = self.Handle |
|
273 |
if handle_type == HANDLE_CONNECTOR: |
|
274 |
wires = handle.GetWires() |
|
80 | 275 |
if len(wires) == 1: |
276 |
if handle == wires[0][0].StartConnected: |
|
277 |
block = wires[0][0].EndConnected.GetParentBlock() |
|
278 |
else: |
|
279 |
block = wires[0][0].StartConnected.GetParentBlock() |
|
280 |
block.RefreshModel(False) |
|
27 | 281 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
96 | 282 |
self.RealConnectors = None |
27 | 283 |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
284 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
285 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
286 |
# Edit the powerrail properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
287 |
self.Parent.EditPowerRailContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
288 |
|
27 | 289 |
# Method called when a RightUp event have been generated |
290 |
def OnRightUp(self, event, dc, scaling): |
|
291 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
292 |
# Popup the menu with special items for a block and a connector if one is handled |
|
293 |
connector = self.TestConnector(pos, False) |
|
294 |
if connector: |
|
295 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
296 |
# self.Parent.PopupDivergenceMenu(True) |
|
297 |
#else: |
|
298 |
# Popup the divergence menu without delete branch |
|
299 |
# self.Parent.PopupDivergenceMenu(False) |
|
300 |
||
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
301 |
# Refreshes the powerrail state according to move defined and handle selected |
27 | 302 |
def ProcessDragging(self, movex, movey): |
303 |
handle_type, handle = self.Handle |
|
304 |
# A connector has been handled |
|
305 |
if handle_type == HANDLE_CONNECTOR: |
|
306 |
self.MoveConnector(handle, movey) |
|
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
307 |
return False, True |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
308 |
else: |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
309 |
return Graphic_Element.ProcessDragging(self, movex, movey) |
27 | 310 |
|
0 | 311 |
# Refreshes the power rail model |
312 |
def RefreshModel(self, move=True): |
|
313 |
self.Parent.RefreshPowerRailModel(self) |
|
314 |
# If power rail has moved and power rail is of type LEFT, refresh the model |
|
315 |
# of wires connected to connectors |
|
316 |
if move and self.Type == LEFTRAIL: |
|
317 |
for connector in self.Connectors: |
|
318 |
if connector: |
|
319 |
connector.RefreshWires() |
|
320 |
||
321 |
# Draws power rail |
|
322 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
323 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
324 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 325 |
# Draw a rectangle with the power rail size |
326 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
327 |
# Draw connectors |
|
328 |
for connector in self.Connectors: |
|
329 |
if connector: |
|
330 |
connector.Draw(dc) |
|
331 |
Graphic_Element.Draw(self, dc) |
|
332 |
||
333 |
||
334 |
#------------------------------------------------------------------------------- |
|
335 |
# Ladder Diagram Contact |
|
336 |
#------------------------------------------------------------------------------- |
|
337 |
||
338 |
""" |
|
339 |
Class that implements the graphic representation of a contact |
|
340 |
""" |
|
341 |
||
342 |
class LD_Contact(Graphic_Element): |
|
343 |
||
344 |
# Create a new contact |
|
345 |
def __init__(self, parent, type, name, id = None): |
|
346 |
Graphic_Element.__init__(self, parent) |
|
347 |
self.Type = type |
|
348 |
self.Name = name |
|
349 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
350 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
0 | 351 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
352 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
353 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
42 | 354 |
self.RefreshNameSize() |
355 |
self.RefreshTypeSize() |
|
0 | 356 |
|
357 |
# Destructor |
|
358 |
def __del__(self): |
|
359 |
self.Input = None |
|
360 |
self.Output = None |
|
361 |
||
112 | 362 |
# Make a clone of this LD_Contact |
363 |
def Clone(self, id = None, pos = None): |
|
364 |
contact = LD_Contact(self.Parent, self.Type, self.Name, id) |
|
365 |
contact.SetSize(self.Size[0], self.Size[1]) |
|
366 |
if pos is not None: |
|
367 |
contact.SetPosition(pos.x, pos.y) |
|
368 |
contact.Input = self.Input.Clone(contact) |
|
369 |
contact.Output = self.Output.Clone(contact) |
|
370 |
return contact |
|
371 |
||
0 | 372 |
# Forbids to change the contact size |
373 |
def SetSize(self, width, height): |
|
42 | 374 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 375 |
Graphic_Element.SetSize(self, width, height) |
376 |
self.RefreshConnectors() |
|
0 | 377 |
|
378 |
# Delete this contact by calling the appropriate method |
|
379 |
def Delete(self): |
|
380 |
self.Parent.DeleteContact(self) |
|
381 |
||
382 |
# Unconnect input and output |
|
383 |
def Clean(self): |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
384 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
385 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 386 |
|
42 | 387 |
# Refresh the size of text for name |
388 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
389 |
dc = wx.ClientDC(self.Parent) |
42 | 390 |
if self.Name != "": |
391 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
392 |
else: |
|
393 |
self.NameSize = 0, 0 |
|
394 |
||
395 |
# Refresh the size of text for type |
|
396 |
def RefreshTypeSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
397 |
dc = wx.ClientDC(self.Parent) |
42 | 398 |
typetext = "" |
399 |
if self.Type == CONTACT_REVERSE: |
|
400 |
typetext = "/" |
|
401 |
elif self.Type == CONTACT_RISING: |
|
402 |
typetext = "P" |
|
403 |
elif self.Type == CONTACT_FALLING: |
|
404 |
typetext = "N" |
|
405 |
if typetext != "": |
|
406 |
self.TypeSize = dc.GetTextExtent(typetext) |
|
407 |
else: |
|
408 |
self.TypeSize = 0, 0 |
|
409 |
||
0 | 410 |
# Refresh the contact bounding box |
411 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
412 |
dc = wx.ClientDC(self.Parent) |
0 | 413 |
# Calculate the size of the name outside the contact |
414 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
415 |
# Calculate the bounding box size |
|
416 |
if self.Name != "": |
|
417 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
418 |
bbx_width = max(self.Size[0], text_width) |
|
419 |
bbx_y = self.Pos.y - (text_height + 2) |
|
420 |
bbx_height = self.Size[1] + (text_height + 2) |
|
421 |
else: |
|
422 |
bbx_x = self.Pos.x |
|
423 |
bbx_width = self.Size[0] |
|
424 |
bbx_y = self.Pos.y |
|
425 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
426 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 427 |
|
27 | 428 |
# Returns the block minimum size |
429 |
def GetMinSize(self): |
|
430 |
return LD_ELEMENT_SIZE |
|
431 |
||
0 | 432 |
# Refresh the position of wire connected to contact |
433 |
def RefreshConnected(self, exclude = []): |
|
434 |
self.Input.MoveConnected(exclude) |
|
435 |
self.Output.MoveConnected(exclude) |
|
436 |
||
437 |
# Returns the contact connector that starts with the point given if it exists |
|
27 | 438 |
def GetConnector(self, position, name = None): |
439 |
# if a name is given |
|
440 |
if name: |
|
441 |
# Test input and output connector |
|
442 |
if name == self.Input.GetName(): |
|
443 |
return self.Input |
|
444 |
if name == self.Output.GetName(): |
|
445 |
return self.Output |
|
0 | 446 |
# Test input connector |
447 |
input_pos = self.Input.GetRelPosition() |
|
448 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
449 |
return self.Input |
|
450 |
# Test output connector |
|
451 |
output_pos = self.Output.GetRelPosition() |
|
452 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
453 |
return self.Output |
|
454 |
return None |
|
455 |
||
456 |
# Returns input and output contact connectors |
|
457 |
def GetConnectors(self): |
|
458 |
return {"input":self.Input,"output":self.Output} |
|
459 |
||
460 |
# Test if point given is on contact input or output connector |
|
461 |
def TestConnector(self, pt, exclude=True): |
|
462 |
# Test input connector |
|
463 |
if self.Input.TestPoint(pt, exclude): |
|
464 |
return self.Input |
|
465 |
# Test output connector |
|
466 |
if self.Output.TestPoint(pt, exclude): |
|
467 |
return self.Output |
|
468 |
return None |
|
469 |
||
27 | 470 |
# Refresh the positions of the block connectors |
471 |
def RefreshConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
472 |
self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
473 |
self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) |
27 | 474 |
self.RefreshConnected() |
475 |
||
0 | 476 |
# Changes the contact name |
477 |
def SetName(self, name): |
|
478 |
self.Name = name |
|
42 | 479 |
self.RefreshNameSize() |
0 | 480 |
|
481 |
# Returns the contact name |
|
482 |
def GetName(self): |
|
483 |
return self.Name |
|
484 |
||
485 |
# Changes the contact type |
|
486 |
def SetType(self, type): |
|
487 |
self.Type = type |
|
50 | 488 |
self.RefreshTypeSize() |
0 | 489 |
|
490 |
# Returns the contact type |
|
491 |
def GetType(self): |
|
492 |
return self.Type |
|
493 |
||
494 |
# Method called when a LeftDClick event have been generated |
|
27 | 495 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 496 |
# Edit the contact properties |
497 |
self.Parent.EditContactContent(self) |
|
498 |
||
499 |
# Refreshes the contact model |
|
500 |
def RefreshModel(self, move=True): |
|
501 |
self.Parent.RefreshContactModel(self) |
|
502 |
# If contact has moved, refresh the model of wires connected to output |
|
503 |
if move: |
|
504 |
self.Output.RefreshWires() |
|
505 |
||
506 |
# Draws contact |
|
507 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
508 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
509 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 510 |
# Draw two rectangles for representing the contact |
511 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) |
|
512 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) |
|
513 |
# Draw contact name |
|
42 | 514 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
515 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
0 | 516 |
# Draw the modifier symbol in the middle of contact |
517 |
typetext = "" |
|
518 |
if self.Type == CONTACT_REVERSE: |
|
519 |
typetext = "/" |
|
520 |
elif self.Type == CONTACT_RISING: |
|
521 |
typetext = "P" |
|
522 |
elif self.Type == CONTACT_FALLING: |
|
523 |
typetext = "N" |
|
524 |
if typetext != "": |
|
42 | 525 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, |
526 |
self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2) |
|
0 | 527 |
# Draw input and output connectors |
528 |
self.Input.Draw(dc) |
|
529 |
self.Output.Draw(dc) |
|
530 |
Graphic_Element.Draw(self, dc) |
|
531 |
||
532 |
||
533 |
#------------------------------------------------------------------------------- |
|
534 |
# Ladder Diagram Coil |
|
535 |
#------------------------------------------------------------------------------- |
|
536 |
||
537 |
""" |
|
538 |
Class that implements the graphic representation of a coil |
|
539 |
""" |
|
540 |
||
541 |
class LD_Coil(Graphic_Element): |
|
542 |
||
543 |
# Create a new coil |
|
544 |
def __init__(self, parent, type, name, id = None): |
|
545 |
Graphic_Element.__init__(self, parent) |
|
546 |
self.Type = type |
|
547 |
self.Name = name |
|
548 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
549 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
0 | 550 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
551 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
552 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
42 | 553 |
self.RefreshNameSize() |
554 |
self.RefreshTypeSize() |
|
0 | 555 |
|
556 |
# Destructor |
|
557 |
def __del__(self): |
|
558 |
self.Input = None |
|
559 |
self.Output = None |
|
560 |
||
112 | 561 |
# Make a clone of this LD_Coil |
562 |
def Clone(self, id = None, pos = None): |
|
563 |
coil = LD_Coil(self.Parent, self.Type, self.Name, id) |
|
564 |
coil.SetSize(self.Size[0], self.Size[1]) |
|
565 |
if pos is not None: |
|
566 |
coil.SetPosition(pos.x, pos.y) |
|
567 |
coil.Input = self.Input.Clone(coil) |
|
568 |
coil.Output = self.Output.Clone(coil) |
|
569 |
return coil |
|
570 |
||
0 | 571 |
# Forbids to change the contact size |
572 |
def SetSize(self, width, height): |
|
42 | 573 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 574 |
Graphic_Element.SetSize(self, width, height) |
575 |
self.RefreshConnectors() |
|
0 | 576 |
|
577 |
# Delete this coil by calling the appropriate method |
|
578 |
def Delete(self): |
|
579 |
self.Parent.DeleteCoil(self) |
|
580 |
||
581 |
# Unconnect input and output |
|
582 |
def Clean(self): |
|
583 |
self.Input.UnConnect() |
|
584 |
self.Output.UnConnect() |
|
585 |
||
42 | 586 |
# Refresh the size of text for name |
587 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
588 |
dc = wx.ClientDC(self.Parent) |
42 | 589 |
if self.Name != "": |
590 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
591 |
else: |
|
592 |
self.NameSize = 0, 0 |
|
593 |
||
594 |
# Refresh the size of text for type |
|
595 |
def RefreshTypeSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
596 |
dc = wx.ClientDC(self.Parent) |
42 | 597 |
typetext = "" |
598 |
if self.Type == COIL_REVERSE: |
|
599 |
typetext = "/" |
|
600 |
elif self.Type == COIL_SET: |
|
601 |
typetext = "S" |
|
602 |
elif self.Type == COIL_RESET: |
|
603 |
typetext = "R" |
|
604 |
if typetext != "": |
|
605 |
self.TypeSize = dc.GetTextExtent(typetext) |
|
606 |
else: |
|
607 |
self.TypeSize = 0, 0 |
|
608 |
||
0 | 609 |
# Refresh the coil bounding box |
610 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
611 |
dc = wx.ClientDC(self.Parent) |
0 | 612 |
# Calculate the size of the name outside the coil |
613 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
614 |
# Calculate the bounding box size |
|
615 |
if self.Name != "": |
|
616 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
617 |
bbx_width = max(self.Size[0], text_width) |
|
618 |
bbx_y = self.Pos.y - (text_height + 2) |
|
619 |
bbx_height = self.Size[1] + (text_height + 2) |
|
620 |
else: |
|
621 |
bbx_x = self.Pos.x |
|
622 |
bbx_width = self.Size[0] |
|
623 |
bbx_y = self.Pos.y |
|
624 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
625 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
27 | 626 |
|
627 |
# Returns the block minimum size |
|
628 |
def GetMinSize(self): |
|
629 |
return LD_ELEMENT_SIZE |
|
0 | 630 |
|
631 |
# Refresh the position of wire connected to coil |
|
632 |
def RefreshConnected(self, exclude = []): |
|
633 |
self.Input.MoveConnected(exclude) |
|
634 |
self.Output.MoveConnected(exclude) |
|
635 |
||
636 |
# Returns the coil connector that starts with the point given if it exists |
|
27 | 637 |
def GetConnector(self, position, name = None): |
638 |
# if a name is given |
|
639 |
if name: |
|
640 |
# Test input and output connector |
|
641 |
if self.Input and name == self.Input.GetName(): |
|
642 |
return self.Input |
|
643 |
if self.Output and name == self.Output.GetName(): |
|
644 |
return self.Output |
|
0 | 645 |
# Test input connector |
646 |
input_pos = self.Input.GetRelPosition() |
|
647 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
648 |
return self.Input |
|
649 |
# Test output connector |
|
650 |
output_pos = self.Output.GetRelPosition() |
|
651 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
652 |
return self.Output |
|
653 |
return None |
|
654 |
||
655 |
# Returns input and output coil connectors |
|
656 |
def GetConnectors(self): |
|
657 |
return {"input":self.Input,"output":self.Output} |
|
658 |
||
659 |
# Test if point given is on coil input or output connector |
|
660 |
def TestConnector(self, pt, exclude=True): |
|
661 |
# Test input connector |
|
662 |
if self.Input.TestPoint(pt, exclude): |
|
663 |
return self.Input |
|
664 |
# Test output connector |
|
665 |
if self.Output.TestPoint(pt, exclude): |
|
666 |
return self.Output |
|
667 |
return None |
|
668 |
||
27 | 669 |
# Refresh the positions of the block connectors |
670 |
def RefreshConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
671 |
self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
672 |
self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) |
27 | 673 |
self.RefreshConnected() |
674 |
||
0 | 675 |
# Changes the coil name |
676 |
def SetName(self, name): |
|
677 |
self.Name = name |
|
42 | 678 |
self.RefreshNameSize() |
0 | 679 |
|
680 |
# Returns the coil name |
|
681 |
def GetName(self): |
|
682 |
return self.Name |
|
683 |
||
684 |
# Changes the coil type |
|
685 |
def SetType(self, type): |
|
686 |
self.Type = type |
|
42 | 687 |
self.RefreshTypeSize() |
0 | 688 |
|
689 |
# Returns the coil type |
|
690 |
def GetType(self): |
|
691 |
return self.Type |
|
692 |
||
693 |
# Method called when a LeftDClick event have been generated |
|
27 | 694 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 695 |
# Edit the coil properties |
696 |
self.Parent.EditCoilContent(self) |
|
697 |
||
698 |
# Refreshes the coil model |
|
699 |
def RefreshModel(self, move=True): |
|
700 |
self.Parent.RefreshCoilModel(self) |
|
701 |
# If coil has moved, refresh the model of wires connected to output |
|
702 |
if move: |
|
703 |
self.Output.RefreshWires() |
|
704 |
||
705 |
# Draws coil |
|
706 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
707 |
dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
708 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
0 | 709 |
# Draw a two circle arcs for representing the coil |
710 |
dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225) |
|
711 |
dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
712 |
dc.SetPen(wx.BLACK_PEN) |
0 | 713 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) |
714 |
# Draw coil name |
|
42 | 715 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
716 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
0 | 717 |
# Draw the modifier symbol in the middle of coil |
718 |
typetext = "" |
|
719 |
if self.Type == COIL_REVERSE: |
|
720 |
typetext = "/" |
|
721 |
elif self.Type == COIL_SET: |
|
722 |
typetext = "S" |
|
723 |
elif self.Type == COIL_RESET: |
|
724 |
typetext = "R" |
|
725 |
if typetext != "": |
|
42 | 726 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, |
727 |
self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2) |
|
0 | 728 |
# Draw input and output connectors |
729 |
self.Input.Draw(dc) |
|
730 |
self.Output.Draw(dc) |
|
731 |
Graphic_Element.Draw(self, dc) |