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