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