author | laurent |
Tue, 28 Jun 2011 11:28:28 +0200 | |
changeset 547 | 2faf0964feb2 |
parent 537 | a31bf722aa82 |
child 550 | cfa295862d55 |
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 |
|
249 | 52 |
def Flush(self): |
53 |
for connector in self.Connectors: |
|
54 |
if connector is not None: |
|
55 |
connector.Flush() |
|
0 | 56 |
self.Connectors = [] |
57 |
||
112 | 58 |
# Make a clone of this LD_PowerRail |
162 | 59 |
def Clone(self, parent, id = None, pos = None): |
60 |
powerrail = LD_PowerRail(parent, self.Type, id) |
|
112 | 61 |
powerrail.SetSize(self.Size[0], self.Size[1]) |
62 |
if pos is not None: |
|
63 |
powerrail.SetPosition(pos.x, pos.y) |
|
283 | 64 |
else: |
65 |
powerrail.SetPosition(self.Pos.x, self.Pos.y) |
|
112 | 66 |
powerrail.Connectors = [] |
67 |
for connector in self.Connectors: |
|
68 |
if connector is not None: |
|
69 |
powerrail.Connectors.append(connector.Clone(powerrail)) |
|
70 |
else: |
|
71 |
powerrail.Connectors.append(None) |
|
72 |
return powerrail |
|
73 |
||
283 | 74 |
def GetConnectorTranslation(self, element): |
75 |
return dict(zip([connector for connector in self.Connectors if connector is not None], |
|
76 |
[connector for connector in element.Connectors if connector is not None])) |
|
77 |
||
144 | 78 |
# Returns the RedrawRect |
79 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
80 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
81 |
for connector in self.Connectors: |
|
82 |
if connector is not None: |
|
83 |
rect = rect.Union(connector.GetRedrawRect(movex, movey)) |
|
84 |
if movex != 0 or movey != 0: |
|
85 |
for connector in self.Connectors: |
|
86 |
if connector is not None and connector.IsConnected(): |
|
87 |
rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey)) |
|
88 |
return rect |
|
89 |
||
0 | 90 |
# Forbids to change the power rail size |
91 |
def SetSize(self, width, height): |
|
42 | 92 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 93 |
Graphic_Element.SetSize(self, width, height) |
94 |
self.RefreshConnectors() |
|
0 | 95 |
|
96 |
# Forbids to select a power rail |
|
97 |
def HitTest(self, pt): |
|
42 | 98 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
99 |
return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, exclude=False) != None |
0 | 100 |
return False |
101 |
||
42 | 102 |
# Forbids to select a power rail |
103 |
def IsInSelection(self, rect): |
|
104 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
105 |
return Graphic_Element.IsInSelection(self, rect) |
42 | 106 |
return False |
107 |
||
0 | 108 |
# Deletes this power rail by calling the appropriate method |
109 |
def Delete(self): |
|
110 |
self.Parent.DeletePowerRail(self) |
|
111 |
||
112 |
# Unconnect all connectors |
|
113 |
def Clean(self): |
|
114 |
for connector in self.Connectors: |
|
115 |
if connector: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
116 |
connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 117 |
|
118 |
# Refresh the power rail bounding box |
|
119 |
def RefreshBoundingBox(self): |
|
144 | 120 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
0 | 121 |
|
122 |
# Refresh the power rail size |
|
123 |
def RefreshSize(self): |
|
144 | 124 |
self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)) |
0 | 125 |
self.RefreshBoundingBox() |
126 |
||
27 | 127 |
# Returns the block minimum size |
128 |
def GetMinSize(self): |
|
96 | 129 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
144 | 130 |
return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1] |
131 |
else: |
|
132 |
return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors) |
|
27 | 133 |
|
0 | 134 |
# Add a connector or a blank to this power rail at the last place |
135 |
def AddConnector(self, connector = True): |
|
136 |
self.InsertConnector(len(self.Connectors), connector) |
|
137 |
||
138 |
# Add a connector or a blank to this power rail at the place given |
|
139 |
def InsertConnector(self, idx, connector = True): |
|
140 |
if connector: |
|
141 |
if self.Type == LEFTRAIL: |
|
80 | 142 |
connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) |
0 | 143 |
elif self.Type == RIGHTRAIL: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
144 |
connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST) |
0 | 145 |
self.Connectors.insert(idx, connector) |
146 |
else: |
|
147 |
self.Connectors.insert(idx, None) |
|
148 |
self.RefreshSize() |
|
149 |
self.RefreshConnectors() |
|
150 |
||
27 | 151 |
# Moves the divergence connector given |
152 |
def MoveConnector(self, connector, movey): |
|
153 |
position = connector.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
154 |
connector.SetPosition(wx.Point(position.x, position.y + movey)) |
27 | 155 |
miny = self.Size[1] |
156 |
maxy = 0 |
|
157 |
for connect in self.Connectors: |
|
158 |
connect_pos = connect.GetRelPosition() |
|
80 | 159 |
miny = min(miny, connect_pos.y - self.Extensions[0]) |
160 |
maxy = max(maxy, connect_pos.y - self.Extensions[0]) |
|
161 |
min_pos = self.Pos.y + miny |
|
27 | 162 |
self.Pos.y = min(min_pos, self.Pos.y) |
163 |
if min_pos == self.Pos.y: |
|
164 |
for connect in self.Connectors: |
|
165 |
connect_pos = connect.GetRelPosition() |
|
80 | 166 |
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
|
167 |
self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y)) |
80 | 168 |
maxy = 0 |
169 |
for connect in self.Connectors: |
|
170 |
connect_pos = connect.GetRelPosition() |
|
171 |
maxy = max(maxy, connect_pos.y) |
|
172 |
self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) |
|
27 | 173 |
connector.MoveConnected() |
174 |
self.RefreshBoundingBox() |
|
175 |
||
0 | 176 |
# Returns the index in connectors list for the connector given |
177 |
def GetConnectorIndex(self, connector): |
|
178 |
if connector in self.Connectors: |
|
179 |
return self.Connectors.index(connector) |
|
180 |
return None |
|
181 |
||
182 |
# Returns if there is a connector in connectors list at the index given |
|
183 |
def IsNullConnector(self, idx): |
|
184 |
if idx < len(self.Connectors): |
|
185 |
return self.Connectors[idx] == None |
|
186 |
return False |
|
187 |
||
188 |
# Delete the connector or blank from connectors list at the index given |
|
189 |
def DeleteConnector(self, idx): |
|
190 |
self.Connectors.pop(idx) |
|
191 |
self.RefreshConnectors() |
|
192 |
self.RefreshSize() |
|
193 |
||
194 |
# Refresh the positions of the power rail connectors |
|
195 |
def RefreshConnectors(self): |
|
145 | 196 |
scaling = self.Parent.GetScaling() |
71 | 197 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
198 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
80 | 199 |
interval = float(height) / float(max(len(self.Connectors) - 1, 1)) |
71 | 200 |
for i, connector in enumerate(self.Connectors): |
145 | 201 |
if self.RealConnectors: |
202 |
position = self.Extensions[0] + int(round(self.RealConnectors[i] * height)) |
|
203 |
else: |
|
204 |
position = self.Extensions[0] + int(round(i * interval)) |
|
205 |
if scaling is not None: |
|
206 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
80 | 207 |
if self.Type == LEFTRAIL: |
145 | 208 |
connector.SetPosition(wx.Point(self.Size[0], position)) |
80 | 209 |
elif self.Type == RIGHTRAIL: |
145 | 210 |
connector.SetPosition(wx.Point(0, position)) |
71 | 211 |
else: |
212 |
position = self.Extensions[0] |
|
213 |
for connector in self.Connectors: |
|
214 |
if connector: |
|
145 | 215 |
if scaling is not None: |
216 |
ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
217 |
else: |
|
218 |
ypos = position |
|
71 | 219 |
if self.Type == LEFTRAIL: |
145 | 220 |
connector.SetPosition(wx.Point(self.Size[0], ypos)) |
71 | 221 |
elif self.Type == RIGHTRAIL: |
145 | 222 |
connector.SetPosition(wx.Point(0, ypos)) |
71 | 223 |
position += LD_LINE_SIZE |
0 | 224 |
self.RefreshConnected() |
225 |
||
7 | 226 |
# Refresh the position of wires connected to power rail |
0 | 227 |
def RefreshConnected(self, exclude = []): |
228 |
for connector in self.Connectors: |
|
8 | 229 |
if connector: |
230 |
connector.MoveConnected(exclude) |
|
0 | 231 |
|
232 |
# Returns the power rail connector that starts with the point given if it exists |
|
27 | 233 |
def GetConnector(self, position, name = None): |
234 |
# if a name is given |
|
235 |
if name: |
|
236 |
# Test each connector if it exists |
|
237 |
for connector in self.Connectors: |
|
238 |
if connector and name == connector.GetName(): |
|
239 |
return connector |
|
537
a31bf722aa82
Fix wire disappearing when wire tip and connector are not exactly at the same position fixed.
laurent
parents:
508
diff
changeset
|
240 |
return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None]) |
0 | 241 |
|
242 |
# Returns all the power rail connectors |
|
243 |
def GetConnectors(self): |
|
383
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
244 |
connectors = [connector for connector in self.Connectors if connector] |
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
245 |
if self.Type == LEFTRAIL: |
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
246 |
return {"inputs": [], "outputs": connectors} |
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
247 |
else: |
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
248 |
return {"inputs": connectors, "outputs": []} |
0 | 249 |
|
250 |
# Test if point given is on one of the power rail connectors |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
251 |
def TestConnector(self, pt, direction = None, exclude = True): |
0 | 252 |
for connector in self.Connectors: |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
253 |
if connector and connector.TestPoint(pt, direction, exclude): |
0 | 254 |
return connector |
255 |
return None |
|
256 |
||
257 |
# Returns the power rail type |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
258 |
def SetType(self, type, connectors): |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
259 |
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
|
260 |
# 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
|
261 |
# the connectors list |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
262 |
self.Type = type |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
263 |
self.Clean() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
264 |
self.Connectors = [] |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
265 |
for connector in connectors: |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
266 |
self.AddConnector(connector) |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
267 |
self.RefreshSize() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
268 |
|
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
269 |
# Returns the power rail type |
0 | 270 |
def GetType(self): |
271 |
return self.Type |
|
272 |
||
27 | 273 |
# Method called when a LeftDown event have been generated |
274 |
def OnLeftDown(self, event, dc, scaling): |
|
145 | 275 |
self.RealConnectors = [] |
276 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
254 | 277 |
if height > 0: |
278 |
for connector in self.Connectors: |
|
279 |
position = connector.GetRelPosition() |
|
280 |
self.RealConnectors.append(max(0., min(float(position.y - self.Extensions[0]) / float(height), 1.))) |
|
281 |
elif len(self.Connectors) > 1: |
|
282 |
self.RealConnectors = map(lambda x : x * 1 / (len(self.Connectors) - 1), xrange(len(self.Connectors))) |
|
283 |
else: |
|
284 |
self.RealConnectors = [0.5] |
|
145 | 285 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
286 |
||
287 |
# Method called when a LeftUp event have been generated |
|
288 |
def OnLeftUp(self, event, dc, scaling): |
|
289 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
|
290 |
self.RealConnectors = None |
|
291 |
||
292 |
# Method called when a LeftDown event have been generated |
|
293 |
def OnRightDown(self, event, dc, scaling): |
|
27 | 294 |
pos = GetScaledEventPosition(event, dc, scaling) |
295 |
# Test if a connector have been handled |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
296 |
connector = self.TestConnector(pos, exclude=False) |
27 | 297 |
if connector: |
298 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
381
98890d848701
Redefine cursor switching procedure in graphic viewers
laurent
parents:
379
diff
changeset
|
299 |
wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
27 | 300 |
self.Selected = False |
301 |
# Initializes the last position |
|
302 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
303 |
else: |
|
145 | 304 |
Graphic_Element.OnRightDown(self, event, dc, scaling) |
305 |
||
306 |
# Method called when a LeftDClick event have been generated |
|
307 |
def OnLeftDClick(self, event, dc, scaling): |
|
308 |
# Edit the powerrail properties |
|
309 |
self.Parent.EditPowerRailContent(self) |
|
310 |
||
311 |
# Method called when a RightUp event have been generated |
|
312 |
def OnRightUp(self, event, dc, scaling): |
|
27 | 313 |
handle_type, handle = self.Handle |
314 |
if handle_type == HANDLE_CONNECTOR: |
|
315 |
wires = handle.GetWires() |
|
80 | 316 |
if len(wires) == 1: |
317 |
if handle == wires[0][0].StartConnected: |
|
318 |
block = wires[0][0].EndConnected.GetParentBlock() |
|
319 |
else: |
|
320 |
block = wires[0][0].StartConnected.GetParentBlock() |
|
321 |
block.RefreshModel(False) |
|
145 | 322 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
323 |
else: |
|
324 |
self.Parent.PopupDefaultMenu() |
|
27 | 325 |
|
175 | 326 |
def Resize(self, x, y, width, height): |
327 |
self.Move(x, y) |
|
254 | 328 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
329 |
self.SetSize(width, height) |
|
330 |
else: |
|
331 |
self.SetSize(LD_POWERRAIL_WIDTH, height) |
|
175 | 332 |
|
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
333 |
# Refreshes the powerrail state according to move defined and handle selected |
327
7fd5233ce5ce
Adding support for contraining move to only one direction when control down
lbessard
parents:
283
diff
changeset
|
334 |
def ProcessDragging(self, movex, movey, event, scaling): |
27 | 335 |
handle_type, handle = self.Handle |
336 |
# A connector has been handled |
|
337 |
if handle_type == HANDLE_CONNECTOR: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
127
diff
changeset
|
338 |
movey = max(-self.BoundingBox.y, movey) |
145 | 339 |
if scaling is not None: |
340 |
position = handle.GetRelPosition() |
|
341 |
movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y |
|
27 | 342 |
self.MoveConnector(handle, movey) |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
127
diff
changeset
|
343 |
return 0, movey |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
344 |
else: |
327
7fd5233ce5ce
Adding support for contraining move to only one direction when control down
lbessard
parents:
283
diff
changeset
|
345 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
27 | 346 |
|
0 | 347 |
# Refreshes the power rail model |
348 |
def RefreshModel(self, move=True): |
|
349 |
self.Parent.RefreshPowerRailModel(self) |
|
350 |
# If power rail has moved and power rail is of type LEFT, refresh the model |
|
351 |
# of wires connected to connectors |
|
352 |
if move and self.Type == LEFTRAIL: |
|
353 |
for connector in self.Connectors: |
|
354 |
if connector: |
|
355 |
connector.RefreshWires() |
|
356 |
||
357 |
# Draws power rail |
|
358 |
def Draw(self, dc): |
|
144 | 359 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
360 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
361 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 362 |
# Draw a rectangle with the power rail size |
175 | 363 |
if self.Type == LEFTRAIL: |
364 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) |
|
365 |
else: |
|
366 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) |
|
0 | 367 |
# Draw connectors |
368 |
for connector in self.Connectors: |
|
369 |
if connector: |
|
370 |
connector.Draw(dc) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
371 |
|
0 | 372 |
|
373 |
#------------------------------------------------------------------------------- |
|
374 |
# Ladder Diagram Contact |
|
375 |
#------------------------------------------------------------------------------- |
|
376 |
||
377 |
""" |
|
378 |
Class that implements the graphic representation of a contact |
|
379 |
""" |
|
380 |
||
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
360
diff
changeset
|
381 |
class LD_Contact(Graphic_Element, DebugDataConsumer): |
0 | 382 |
|
383 |
# Create a new contact |
|
384 |
def __init__(self, parent, type, name, id = None): |
|
385 |
Graphic_Element.__init__(self, parent) |
|
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
360
diff
changeset
|
386 |
DebugDataConsumer.__init__(self) |
0 | 387 |
self.Type = type |
388 |
self.Name = name |
|
389 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
390 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
391 |
self.Errors = {} |
0 | 392 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
393 |
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
|
394 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
249 | 395 |
self.PreviousValue = False |
396 |
self.PreviousSpreading = False |
|
42 | 397 |
self.RefreshNameSize() |
398 |
self.RefreshTypeSize() |
|
0 | 399 |
|
249 | 400 |
def Flush(self): |
401 |
if self.Input is not None: |
|
402 |
self.Input.Flush() |
|
403 |
self.Input = None |
|
404 |
if self.Output is not None: |
|
405 |
self.Output.Flush() |
|
406 |
self.Output = None |
|
407 |
||
478
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
408 |
def SetForced(self, forced): |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
409 |
if self.Forced != forced: |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
410 |
self.Forced = forced |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
411 |
if self.Visible: |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
412 |
self.Parent.UpdateRefreshRect(self.GetRedrawRect()) |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
413 |
|
249 | 414 |
def SetValue(self, value): |
508
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
415 |
if self.Type == CONTACT_RISING: |
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
416 |
refresh = self.Value and not self.PreviousValue |
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
417 |
elif self.Type == CONTACT_FALLING: |
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
418 |
refresh = not self.Value and self.PreviousValue |
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
419 |
else: |
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
420 |
refresh = False |
249 | 421 |
self.PreviousValue = self.Value |
422 |
self.Value = value |
|
508
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
423 |
if self.Value != self.PreviousValue or refresh: |
368 | 424 |
if self.Visible: |
425 |
self.Parent.UpdateRefreshRect(self.GetRedrawRect()) |
|
249 | 426 |
self.SpreadCurrent() |
427 |
||
428 |
def SpreadCurrent(self): |
|
253 | 429 |
if self.Parent.Debug: |
430 |
if self.Value is None: |
|
431 |
self.Value = False |
|
432 |
spreading = self.Input.ReceivingCurrent() |
|
249 | 433 |
if self.Type == CONTACT_NORMAL: |
434 |
spreading &= self.Value |
|
435 |
elif self.Type == CONTACT_REVERSE: |
|
436 |
spreading &= not self.Value |
|
437 |
elif self.Type == CONTACT_RISING: |
|
438 |
spreading &= self.Value and not self.PreviousValue |
|
439 |
elif self.Type == CONTACT_FALLING: |
|
508
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
440 |
spreading &= not self.Value and self.PreviousValue |
249 | 441 |
else: |
442 |
spreading = False |
|
253 | 443 |
if spreading and not self.PreviousSpreading: |
444 |
self.Output.SpreadCurrent(True) |
|
445 |
elif not spreading and self.PreviousSpreading: |
|
446 |
self.Output.SpreadCurrent(False) |
|
447 |
self.PreviousSpreading = spreading |
|
0 | 448 |
|
112 | 449 |
# Make a clone of this LD_Contact |
162 | 450 |
def Clone(self, parent, id = None, pos = None): |
451 |
contact = LD_Contact(parent, self.Type, self.Name, id) |
|
112 | 452 |
contact.SetSize(self.Size[0], self.Size[1]) |
453 |
if pos is not None: |
|
454 |
contact.SetPosition(pos.x, pos.y) |
|
283 | 455 |
else: |
456 |
contact.SetPosition(self.Pos.x, self.Pos.y) |
|
112 | 457 |
contact.Input = self.Input.Clone(contact) |
458 |
contact.Output = self.Output.Clone(contact) |
|
459 |
return contact |
|
460 |
||
283 | 461 |
def GetConnectorTranslation(self, element): |
462 |
return {self.Input : element.Input, self.Output : element.Output} |
|
463 |
||
144 | 464 |
# Returns the RedrawRect |
465 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
466 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
467 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
468 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
469 |
if movex != 0 or movey != 0: |
|
470 |
if self.Input.IsConnected(): |
|
471 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
472 |
if self.Output.IsConnected(): |
|
473 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
474 |
return rect |
|
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
475 |
|
327
7fd5233ce5ce
Adding support for contraining move to only one direction when control down
lbessard
parents:
283
diff
changeset
|
476 |
def ProcessDragging(self, movex, movey, event, scaling): |
360 | 477 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2) |
144 | 478 |
|
0 | 479 |
# Forbids to change the contact size |
480 |
def SetSize(self, width, height): |
|
42 | 481 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 482 |
Graphic_Element.SetSize(self, width, height) |
483 |
self.RefreshConnectors() |
|
0 | 484 |
|
485 |
# Delete this contact by calling the appropriate method |
|
486 |
def Delete(self): |
|
487 |
self.Parent.DeleteContact(self) |
|
488 |
||
489 |
# Unconnect input and output |
|
490 |
def Clean(self): |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
491 |
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
|
492 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 493 |
|
42 | 494 |
# Refresh the size of text for name |
495 |
def RefreshNameSize(self): |
|
496 |
if self.Name != "": |
|
165 | 497 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
42 | 498 |
else: |
499 |
self.NameSize = 0, 0 |
|
500 |
||
501 |
# Refresh the size of text for type |
|
502 |
def RefreshTypeSize(self): |
|
503 |
typetext = "" |
|
504 |
if self.Type == CONTACT_REVERSE: |
|
505 |
typetext = "/" |
|
506 |
elif self.Type == CONTACT_RISING: |
|
507 |
typetext = "P" |
|
508 |
elif self.Type == CONTACT_FALLING: |
|
509 |
typetext = "N" |
|
510 |
if typetext != "": |
|
165 | 511 |
self.TypeSize = self.Parent.GetTextExtent(typetext) |
42 | 512 |
else: |
513 |
self.TypeSize = 0, 0 |
|
514 |
||
0 | 515 |
# Refresh the contact bounding box |
516 |
def RefreshBoundingBox(self): |
|
517 |
# Calculate the size of the name outside the contact |
|
165 | 518 |
text_width, text_height = self.Parent.GetTextExtent(self.Name) |
0 | 519 |
# Calculate the bounding box size |
520 |
if self.Name != "": |
|
521 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
522 |
bbx_width = max(self.Size[0], text_width) |
|
523 |
bbx_y = self.Pos.y - (text_height + 2) |
|
524 |
bbx_height = self.Size[1] + (text_height + 2) |
|
525 |
else: |
|
526 |
bbx_x = self.Pos.x |
|
527 |
bbx_width = self.Size[0] |
|
528 |
bbx_y = self.Pos.y |
|
529 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
530 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 531 |
|
27 | 532 |
# Returns the block minimum size |
533 |
def GetMinSize(self): |
|
534 |
return LD_ELEMENT_SIZE |
|
535 |
||
0 | 536 |
# Refresh the position of wire connected to contact |
537 |
def RefreshConnected(self, exclude = []): |
|
538 |
self.Input.MoveConnected(exclude) |
|
539 |
self.Output.MoveConnected(exclude) |
|
540 |
||
541 |
# Returns the contact connector that starts with the point given if it exists |
|
27 | 542 |
def GetConnector(self, position, name = None): |
543 |
# if a name is given |
|
544 |
if name: |
|
545 |
# Test input and output connector |
|
546 |
if name == self.Input.GetName(): |
|
547 |
return self.Input |
|
548 |
if name == self.Output.GetName(): |
|
549 |
return self.Output |
|
537
a31bf722aa82
Fix wire disappearing when wire tip and connector are not exactly at the same position fixed.
laurent
parents:
508
diff
changeset
|
550 |
return self.FindNearestConnector(position, [self.Input, self.Output]) |
0 | 551 |
|
552 |
# Returns input and output contact connectors |
|
553 |
def GetConnectors(self): |
|
383
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
554 |
return {"inputs": [self.Input], "outputs": [self.Output]} |
0 | 555 |
|
556 |
# Test if point given is on contact input or output connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
557 |
def TestConnector(self, pt, direction = None, exclude=True): |
0 | 558 |
# Test input connector |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
559 |
if self.Input.TestPoint(pt, direction, exclude): |
0 | 560 |
return self.Input |
561 |
# Test output connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
562 |
if self.Output.TestPoint(pt, direction, exclude): |
0 | 563 |
return self.Output |
564 |
return None |
|
565 |
||
27 | 566 |
# Refresh the positions of the block connectors |
567 |
def RefreshConnectors(self): |
|
145 | 568 |
scaling = self.Parent.GetScaling() |
569 |
position = self.Size[1] / 2 + 1 |
|
570 |
if scaling is not None: |
|
571 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
572 |
self.Input.SetPosition(wx.Point(0, position)) |
|
573 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
|
27 | 574 |
self.RefreshConnected() |
575 |
||
0 | 576 |
# Changes the contact name |
577 |
def SetName(self, name): |
|
578 |
self.Name = name |
|
42 | 579 |
self.RefreshNameSize() |
0 | 580 |
|
581 |
# Returns the contact name |
|
582 |
def GetName(self): |
|
583 |
return self.Name |
|
584 |
||
585 |
# Changes the contact type |
|
586 |
def SetType(self, type): |
|
587 |
self.Type = type |
|
50 | 588 |
self.RefreshTypeSize() |
0 | 589 |
|
590 |
# Returns the contact type |
|
591 |
def GetType(self): |
|
592 |
return self.Type |
|
593 |
||
594 |
# Method called when a LeftDClick event have been generated |
|
27 | 595 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 596 |
# Edit the contact properties |
597 |
self.Parent.EditContactContent(self) |
|
598 |
||
127
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
599 |
# Method called when a RightUp event have been generated |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
600 |
def OnRightUp(self, event, dc, scaling): |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
601 |
# Popup the default menu |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
602 |
self.Parent.PopupDefaultMenu() |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
603 |
|
0 | 604 |
# Refreshes the contact model |
605 |
def RefreshModel(self, move=True): |
|
606 |
self.Parent.RefreshContactModel(self) |
|
607 |
# If contact has moved, refresh the model of wires connected to output |
|
608 |
if move: |
|
609 |
self.Output.RefreshWires() |
|
610 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
611 |
# Draws the highlightment of this element if it is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
612 |
def DrawHighlightment(self, dc): |
144 | 613 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) |
614 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
615 |
dc.SetLogicalFunction(wx.AND) |
|
616 |
# Draw two rectangles for representing the contact |
|
617 |
dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, 6, self.Size[1] + 5) |
|
618 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 3, self.Pos.y - 2, 6, self.Size[1] + 5) |
|
619 |
dc.SetLogicalFunction(wx.COPY) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
620 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
621 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
622 |
self.Errors[infos[0]] = (start[1], end[1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
623 |
|
0 | 624 |
# Draws contact |
625 |
def Draw(self, dc): |
|
144 | 626 |
Graphic_Element.Draw(self, dc) |
249 | 627 |
if self.Value is not None: |
478
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
628 |
if self.Type == CONTACT_NORMAL and self.Value or \ |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
629 |
self.Type == CONTACT_REVERSE and not self.Value or \ |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
630 |
self.Type == CONTACT_RISING and self.Value and not self.PreviousValue or \ |
508
01f49e491be5
Bug on Contact Rising and Falling edge not displayed correctly while debugging fixed
laurent
parents:
495
diff
changeset
|
631 |
self.Type == CONTACT_RISING and not self.Value and self.PreviousValue: |
478
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
632 |
if self.Forced: |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
633 |
dc.SetPen(wx.CYAN_PEN) |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
634 |
else: |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
635 |
dc.SetPen(wx.GREEN_PEN) |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
636 |
elif self.Forced: |
dc403c47af54
Adding colour to graphic element that showing forced values
laurent
parents:
383
diff
changeset
|
637 |
dc.SetPen(wx.Pen(wx.BLUE)) |
249 | 638 |
else: |
639 |
dc.SetPen(wx.BLACK_PEN) |
|
640 |
else: |
|
641 |
dc.SetPen(wx.BLACK_PEN) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
642 |
dc.SetBrush(wx.BLACK_BRUSH) |
213 | 643 |
|
644 |
# Compiling contact type modifier symbol |
|
0 | 645 |
typetext = "" |
646 |
if self.Type == CONTACT_REVERSE: |
|
647 |
typetext = "/" |
|
648 |
elif self.Type == CONTACT_RISING: |
|
649 |
typetext = "P" |
|
650 |
elif self.Type == CONTACT_FALLING: |
|
651 |
typetext = "N" |
|
213 | 652 |
|
653 |
if getattr(dc, "printing", False): |
|
654 |
name_size = dc.GetTextExtent(self.Name) |
|
655 |
if typetext != "": |
|
656 |
type_size = dc.GetTextExtent(typetext) |
|
657 |
else: |
|
658 |
name_size = self.NameSize |
|
659 |
if typetext != "": |
|
660 |
type_size = self.TypeSize |
|
661 |
||
662 |
# Draw two rectangles for representing the contact |
|
663 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) |
|
664 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) |
|
665 |
# Draw contact name |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
666 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
667 |
self.Pos.y - (name_size[1] + 2)) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
668 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
213 | 669 |
# Draw the modifier symbol in the middle of contact |
0 | 670 |
if typetext != "": |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
671 |
type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
672 |
self.Pos.y + (self.Size[1] - type_size[1]) / 2) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
673 |
dc.DrawText(typetext, type_pos[0], type_pos[1]) |
0 | 674 |
# Draw input and output connectors |
675 |
self.Input.Draw(dc) |
|
676 |
self.Output.Draw(dc) |
|
379
e4c26ee9c998
Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents:
368
diff
changeset
|
677 |
if self.Errors.has_key("reference"): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
678 |
HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) |
379
e4c26ee9c998
Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents:
368
diff
changeset
|
679 |
if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
680 |
HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
681 |
|
0 | 682 |
|
683 |
#------------------------------------------------------------------------------- |
|
684 |
# Ladder Diagram Coil |
|
685 |
#------------------------------------------------------------------------------- |
|
686 |
||
687 |
""" |
|
688 |
Class that implements the graphic representation of a coil |
|
689 |
""" |
|
690 |
||
691 |
class LD_Coil(Graphic_Element): |
|
692 |
||
693 |
# Create a new coil |
|
694 |
def __init__(self, parent, type, name, id = None): |
|
695 |
Graphic_Element.__init__(self, parent) |
|
696 |
self.Type = type |
|
697 |
self.Name = name |
|
698 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
699 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
700 |
self.Errors = {} |
0 | 701 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
702 |
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
|
703 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
249 | 704 |
self.Value = None |
705 |
self.PreviousValue = False |
|
42 | 706 |
self.RefreshNameSize() |
707 |
self.RefreshTypeSize() |
|
0 | 708 |
|
249 | 709 |
def Flush(self): |
710 |
if self.Input is not None: |
|
711 |
self.Input.Flush() |
|
712 |
self.Input = None |
|
713 |
if self.Output is not None: |
|
714 |
self.Output.Flush() |
|
715 |
self.Output = None |
|
716 |
||
717 |
def SpreadCurrent(self): |
|
253 | 718 |
if self.Parent.Debug: |
719 |
self.PreviousValue = self.Value |
|
720 |
self.Value = self.Input.ReceivingCurrent() |
|
721 |
if self.Value and not self.PreviousValue: |
|
722 |
self.Output.SpreadCurrent(True) |
|
723 |
elif not self.Value and self.PreviousValue: |
|
724 |
self.Output.SpreadCurrent(False) |
|
368 | 725 |
if self.Value != self.PreviousValue and self.Visible: |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
360
diff
changeset
|
726 |
self.Parent.UpdateRefreshRect(self.GetRedrawRect()) |
0 | 727 |
|
112 | 728 |
# Make a clone of this LD_Coil |
162 | 729 |
def Clone(self, parent, id = None, pos = None): |
730 |
coil = LD_Coil(parent, self.Type, self.Name, id) |
|
112 | 731 |
coil.SetSize(self.Size[0], self.Size[1]) |
732 |
if pos is not None: |
|
733 |
coil.SetPosition(pos.x, pos.y) |
|
283 | 734 |
else: |
735 |
coil.SetPosition(self.Pos.x, self.Pos.y) |
|
112 | 736 |
coil.Input = self.Input.Clone(coil) |
737 |
coil.Output = self.Output.Clone(coil) |
|
738 |
return coil |
|
739 |
||
283 | 740 |
def GetConnectorTranslation(self, element): |
741 |
return {self.Input : element.Input, self.Output : element.Output} |
|
742 |
||
144 | 743 |
# Returns the RedrawRect |
744 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
745 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
746 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
747 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
748 |
if movex != 0 or movey != 0: |
|
749 |
if self.Input.IsConnected(): |
|
750 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
751 |
if self.Output.IsConnected(): |
|
752 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
753 |
return rect |
|
754 |
||
327
7fd5233ce5ce
Adding support for contraining move to only one direction when control down
lbessard
parents:
283
diff
changeset
|
755 |
def ProcessDragging(self, movex, movey, event, scaling): |
7fd5233ce5ce
Adding support for contraining move to only one direction when control down
lbessard
parents:
283
diff
changeset
|
756 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2) |
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
757 |
|
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
758 |
# Forbids to change the Coil size |
0 | 759 |
def SetSize(self, width, height): |
42 | 760 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 761 |
Graphic_Element.SetSize(self, width, height) |
762 |
self.RefreshConnectors() |
|
0 | 763 |
|
764 |
# Delete this coil by calling the appropriate method |
|
765 |
def Delete(self): |
|
766 |
self.Parent.DeleteCoil(self) |
|
767 |
||
768 |
# Unconnect input and output |
|
769 |
def Clean(self): |
|
770 |
self.Input.UnConnect() |
|
771 |
self.Output.UnConnect() |
|
772 |
||
42 | 773 |
# Refresh the size of text for name |
774 |
def RefreshNameSize(self): |
|
775 |
if self.Name != "": |
|
165 | 776 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
42 | 777 |
else: |
778 |
self.NameSize = 0, 0 |
|
779 |
||
780 |
# Refresh the size of text for type |
|
781 |
def RefreshTypeSize(self): |
|
782 |
typetext = "" |
|
783 |
if self.Type == COIL_REVERSE: |
|
784 |
typetext = "/" |
|
785 |
elif self.Type == COIL_SET: |
|
786 |
typetext = "S" |
|
787 |
elif self.Type == COIL_RESET: |
|
788 |
typetext = "R" |
|
269
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
789 |
elif self.Type == COIL_RISING: |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
790 |
typetext = "P" |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
791 |
elif self.Type == COIL_FALLING: |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
792 |
typetext = "N" |
42 | 793 |
if typetext != "": |
165 | 794 |
self.TypeSize = self.Parent.GetTextExtent(typetext) |
42 | 795 |
else: |
796 |
self.TypeSize = 0, 0 |
|
797 |
||
0 | 798 |
# Refresh the coil bounding box |
799 |
def RefreshBoundingBox(self): |
|
800 |
# Calculate the size of the name outside the coil |
|
165 | 801 |
text_width, text_height = self.Parent.GetTextExtent(self.Name) |
0 | 802 |
# Calculate the bounding box size |
803 |
if self.Name != "": |
|
804 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
805 |
bbx_width = max(self.Size[0], text_width) |
|
806 |
bbx_y = self.Pos.y - (text_height + 2) |
|
807 |
bbx_height = self.Size[1] + (text_height + 2) |
|
808 |
else: |
|
809 |
bbx_x = self.Pos.x |
|
810 |
bbx_width = self.Size[0] |
|
811 |
bbx_y = self.Pos.y |
|
812 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
813 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
27 | 814 |
|
815 |
# Returns the block minimum size |
|
816 |
def GetMinSize(self): |
|
817 |
return LD_ELEMENT_SIZE |
|
0 | 818 |
|
819 |
# Refresh the position of wire connected to coil |
|
820 |
def RefreshConnected(self, exclude = []): |
|
821 |
self.Input.MoveConnected(exclude) |
|
822 |
self.Output.MoveConnected(exclude) |
|
823 |
||
824 |
# Returns the coil connector that starts with the point given if it exists |
|
27 | 825 |
def GetConnector(self, position, name = None): |
826 |
# if a name is given |
|
827 |
if name: |
|
828 |
# Test input and output connector |
|
829 |
if self.Input and name == self.Input.GetName(): |
|
830 |
return self.Input |
|
831 |
if self.Output and name == self.Output.GetName(): |
|
832 |
return self.Output |
|
537
a31bf722aa82
Fix wire disappearing when wire tip and connector are not exactly at the same position fixed.
laurent
parents:
508
diff
changeset
|
833 |
return self.FindNearestConnector(position, [self.Input, self.Output]) |
0 | 834 |
|
835 |
# Returns input and output coil connectors |
|
836 |
def GetConnectors(self): |
|
383
25ffba02b6a8
Improving viewer loading instances procedure to faster
laurent
parents:
381
diff
changeset
|
837 |
return {"inputs": [self.Input], "outputs": [self.Output]} |
0 | 838 |
|
839 |
# Test if point given is on coil input or output connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
840 |
def TestConnector(self, pt, direction = None, exclude=True): |
0 | 841 |
# Test input connector |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
842 |
if self.Input.TestPoint(pt, direction, exclude): |
0 | 843 |
return self.Input |
844 |
# Test output connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
845 |
if self.Output.TestPoint(pt, direction, exclude): |
0 | 846 |
return self.Output |
847 |
return None |
|
848 |
||
27 | 849 |
# Refresh the positions of the block connectors |
850 |
def RefreshConnectors(self): |
|
145 | 851 |
scaling = self.Parent.GetScaling() |
852 |
position = self.Size[1] / 2 + 1 |
|
853 |
if scaling is not None: |
|
854 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
855 |
self.Input.SetPosition(wx.Point(0, position)) |
|
856 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
|
27 | 857 |
self.RefreshConnected() |
858 |
||
0 | 859 |
# Changes the coil name |
860 |
def SetName(self, name): |
|
861 |
self.Name = name |
|
42 | 862 |
self.RefreshNameSize() |
0 | 863 |
|
864 |
# Returns the coil name |
|
865 |
def GetName(self): |
|
866 |
return self.Name |
|
867 |
||
868 |
# Changes the coil type |
|
869 |
def SetType(self, type): |
|
870 |
self.Type = type |
|
42 | 871 |
self.RefreshTypeSize() |
0 | 872 |
|
873 |
# Returns the coil type |
|
874 |
def GetType(self): |
|
875 |
return self.Type |
|
876 |
||
877 |
# Method called when a LeftDClick event have been generated |
|
27 | 878 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 879 |
# Edit the coil properties |
880 |
self.Parent.EditCoilContent(self) |
|
881 |
||
127
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
882 |
# Method called when a RightUp event have been generated |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
883 |
def OnRightUp(self, event, dc, scaling): |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
884 |
# Popup the default menu |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
885 |
self.Parent.PopupDefaultMenu() |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
886 |
|
0 | 887 |
# Refreshes the coil model |
888 |
def RefreshModel(self, move=True): |
|
889 |
self.Parent.RefreshCoilModel(self) |
|
890 |
# If coil has moved, refresh the model of wires connected to output |
|
891 |
if move: |
|
892 |
self.Output.RefreshWires() |
|
893 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
894 |
# Draws the highlightment of this element if it is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
895 |
def DrawHighlightment(self, dc): |
144 | 896 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR, 6, wx.SOLID)) |
897 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
898 |
dc.SetLogicalFunction(wx.AND) |
|
899 |
# Draw a two circle arcs for representing the coil |
|
900 |
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) |
|
901 |
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) |
|
902 |
dc.SetLogicalFunction(wx.COPY) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
903 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
904 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
905 |
self.Errors[infos[0]] = (start[1], end[1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
906 |
|
0 | 907 |
# Draws coil |
908 |
def Draw(self, dc): |
|
144 | 909 |
Graphic_Element.Draw(self, dc) |
249 | 910 |
if self.Value is not None and self.Value: |
495 | 911 |
dc.SetPen(wx.Pen(wx.GREEN, 2, wx.SOLID)) |
249 | 912 |
else: |
913 |
dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID)) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
914 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
213 | 915 |
|
916 |
# Compiling coil type modifier symbol |
|
0 | 917 |
typetext = "" |
918 |
if self.Type == COIL_REVERSE: |
|
919 |
typetext = "/" |
|
920 |
elif self.Type == COIL_SET: |
|
921 |
typetext = "S" |
|
922 |
elif self.Type == COIL_RESET: |
|
923 |
typetext = "R" |
|
269
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
924 |
elif self.Type == COIL_RISING: |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
925 |
typetext = "P" |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
926 |
elif self.Type == COIL_FALLING: |
34eff05909b0
Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents:
254
diff
changeset
|
927 |
typetext = "N" |
213 | 928 |
|
929 |
if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC): |
|
930 |
# Draw an clipped ellipse for representing the coil |
|
931 |
clipping_box = dc.GetClippingBox() |
|
932 |
dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1) |
|
933 |
dc.DrawEllipse(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) |
|
934 |
dc.DestroyClippingRegion() |
|
935 |
if clipping_box != (0, 0, 0, 0): |
|
936 |
dc.SetClippingRegion(*clipping_box) |
|
937 |
name_size = dc.GetTextExtent(self.Name) |
|
938 |
if typetext != "": |
|
939 |
type_size = dc.GetTextExtent(typetext) |
|
940 |
else: |
|
941 |
# Draw a two ellipse arcs for representing the coil |
|
942 |
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) |
|
943 |
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) |
|
944 |
# Draw a point to avoid hole in left arc |
|
945 |
if not getattr(dc, "printing", False): |
|
249 | 946 |
if self.Value is not None and self.Value: |
947 |
dc.SetPen(wx.GREEN_PEN) |
|
948 |
else: |
|
949 |
dc.SetPen(wx.BLACK_PEN) |
|
213 | 950 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) |
951 |
name_size = self.NameSize |
|
952 |
if typetext != "": |
|
953 |
type_size = self.TypeSize |
|
954 |
||
955 |
# Draw coil name |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
956 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
957 |
self.Pos.y - (name_size[1] + 2)) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
958 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
213 | 959 |
# Draw the modifier symbol in the middle of coil |
0 | 960 |
if typetext != "": |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
961 |
type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
962 |
self.Pos.y + (self.Size[1] - type_size[1]) / 2) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
963 |
dc.DrawText(typetext, type_pos[0], type_pos[1]) |
0 | 964 |
# Draw input and output connectors |
965 |
self.Input.Draw(dc) |
|
966 |
self.Output.Draw(dc) |
|
379
e4c26ee9c998
Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents:
368
diff
changeset
|
967 |
if self.Errors.has_key("reference"): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
968 |
HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) |
379
e4c26ee9c998
Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents:
368
diff
changeset
|
969 |
if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
970 |
HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
971 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
972 |