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