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