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