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