author  Andrey Skvortsov <andrej.skvortzov@gmail.com> 
Mon, 21 Aug 2017 21:55:18 +0300  
changeset 1782  5b6ad7a7fd9d 
parent 1744  69dfdb26f600 
child 1847  6198190bc121 
permissions  rwrr 
814  1 
#!/usr/bin/env python 
2 
# * coding: utf8 * 

3 

1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

4 
# This file is part of Beremiz, a Integrated Development Environment for 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

5 
# programming IEC 611313 automates supporting plcopen standard and CanFestival. 
814  6 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

7 
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD 
814  8 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

9 
# See COPYING file for copyrights details. 
814  10 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

11 
# This program is free software; you can redistribute it and/or 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

12 
# modify it under the terms of the GNU General Public License 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

13 
# as published by the Free Software Foundation; either version 2 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

14 
# of the License, or (at your option) any later version. 
814  15 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

16 
# This program is distributed in the hope that it will be useful, 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

17 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

18 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

19 
# GNU General Public License for more details. 
814  20 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

21 
# You should have received a copy of the GNU General Public License 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

22 
# along with this program; if not, write to the Free Software 
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1356
diff
changeset

23 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 021101301, USA. 
814  24 

25 
import wx 

26 

1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset

27 
from graphics.GraphicCommons import * 
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset

28 
from graphics.DebugDataConsumer import DebugDataConsumer 
814  29 
from plcopen.structures import * 
30 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

31 

5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

32 
#  
814  33 
# Ladder Diagram PowerRail 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

34 
#  
814  35 

36 

37 
class LD_PowerRail(Graphic_Element): 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

38 
""" 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

39 
Class that implements the graphic representation of a power rail 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

40 
""" 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

41 

814  42 
# Create a new power rail 
43 
def __init__(self, parent, type, id=None, connectors=1): 

44 
Graphic_Element.__init__(self, parent) 

45 
self.Type = None 

46 
self.Connectors = [] 

47 
self.RealConnectors = None 

48 
self.Id = id 

49 
self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2] 

50 
self.SetType(type, connectors) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

51 

814  52 
def Flush(self): 
53 
for connector in self.Connectors: 

54 
connector.Flush() 

55 
self.Connectors = [] 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

56 

814  57 
# Make a clone of this LD_PowerRail 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

58 
def Clone(self, parent, id=None, pos=None): 
814  59 
powerrail = LD_PowerRail(parent, self.Type, id) 
60 
powerrail.SetSize(self.Size[0], self.Size[1]) 

61 
if pos is not None: 

62 
powerrail.SetPosition(pos.x, pos.y) 

63 
else: 

64 
powerrail.SetPosition(self.Pos.x, self.Pos.y) 

65 
powerrail.Connectors = [] 

66 
for connector in self.Connectors: 

67 
powerrail.Connectors.append(connector.Clone(powerrail)) 

68 
return powerrail 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

69 

814  70 
def GetConnectorTranslation(self, element): 
71 
return dict(zip([connector for connector in self.Connectors], 

72 
[connector for connector in element.Connectors])) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

73 

814  74 
# Returns the RedrawRect 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

75 
def GetRedrawRect(self, movex=0, movey=0): 
814  76 
rect = Graphic_Element.GetRedrawRect(self, movex, movey) 
77 
for connector in self.Connectors: 

78 
rect = rect.Union(connector.GetRedrawRect(movex, movey)) 

79 
if movex != 0 or movey != 0: 

80 
for connector in self.Connectors: 

81 
if connector.IsConnected(): 

82 
rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey)) 

83 
return rect 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

84 

814  85 
# Forbids to change the power rail size 
86 
def SetSize(self, width, height): 

87 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

88 
Graphic_Element.SetSize(self, width, height) 

89 
else: 

90 
Graphic_Element.SetSize(self, LD_POWERRAIL_WIDTH, height) 

91 
self.RefreshConnectors() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

92 

814  93 
# Forbids to select a power rail 
94 
def HitTest(self, pt, connectors=True): 

95 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

1743
c3c3d1318130
cleanup: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

96 
return Graphic_Element.HitTest(self, pt, connectors) or self.TestConnector(pt, exclude=False) is not None 
814  97 
return False 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

98 

814  99 
# Forbids to select a power rail 
100 
def IsInSelection(self, rect): 

101 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

102 
return Graphic_Element.IsInSelection(self, rect) 

103 
return False 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

104 

814  105 
# Deletes this power rail by calling the appropriate method 
106 
def Delete(self): 

107 
self.Parent.DeletePowerRail(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

108 

814  109 
# Unconnect all connectors 
110 
def Clean(self): 

111 
for connector in self.Connectors: 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

112 
connector.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

113 

814  114 
# Refresh the power rail bounding box 
115 
def RefreshBoundingBox(self): 

116 
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

117 

814  118 
# Refresh the power rail size 
119 
def RefreshSize(self): 

120 
self.Size = wx.Size(LD_POWERRAIL_WIDTH, max(LD_LINE_SIZE * len(self.Connectors), self.Size[1])) 

121 
self.RefreshBoundingBox() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

122 

814  123 
# Returns the block minimum size 
1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset

124 
def GetMinSize(self, default=False): 
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset

125 
height = (LD_LINE_SIZE * (len(self.Connectors)  1) 
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset

126 
if default else 0) 
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset

127 
return LD_POWERRAIL_WIDTH, height + self.Extensions[0] + self.Extensions[1] 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

128 

814  129 
# Add a connector or a blank to this power rail at the last place 
130 
def AddConnector(self): 

131 
self.InsertConnector(len(self.Connectors)) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

132 

814  133 
# Add a connector or a blank to this power rail at the place given 
134 
def InsertConnector(self, idx): 

135 
if self.Type == LEFTRAIL: 

136 
connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) 

137 
elif self.Type == RIGHTRAIL: 

138 
connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST) 

139 
self.Connectors.insert(idx, connector) 

140 
self.RefreshSize() 

141 
self.RefreshConnectors() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

142 

814  143 
# Moves the divergence connector given 
144 
def MoveConnector(self, connector, movey): 

145 
position = connector.GetRelPosition() 

146 
connector.SetPosition(wx.Point(position.x, position.y + movey)) 

147 
miny = self.Size[1] 

148 
maxy = 0 

149 
for connect in self.Connectors: 

150 
connect_pos = connect.GetRelPosition() 

151 
miny = min(miny, connect_pos.y  self.Extensions[0]) 

152 
maxy = max(maxy, connect_pos.y  self.Extensions[0]) 

153 
min_pos = self.Pos.y + miny 

154 
self.Pos.y = min(min_pos, self.Pos.y) 

155 
if min_pos == self.Pos.y: 

156 
for connect in self.Connectors: 

157 
connect_pos = connect.GetRelPosition() 

158 
connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y  miny)) 

159 
self.Connectors.sort(lambda x, y: cmp(x.Pos.y, y.Pos.y)) 

160 
maxy = 0 

161 
for connect in self.Connectors: 

162 
connect_pos = connect.GetRelPosition() 

163 
maxy = max(maxy, connect_pos.y) 

164 
self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) 

165 
connector.MoveConnected() 

166 
self.RefreshBoundingBox() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

167 

814  168 
# Returns the index in connectors list for the connector given 
169 
def GetConnectorIndex(self, connector): 

170 
if connector in self.Connectors: 

171 
return self.Connectors.index(connector) 

172 
return None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

173 

814  174 
# Delete the connector or blank from connectors list at the index given 
175 
def DeleteConnector(self, idx): 

176 
self.Connectors.pop(idx) 

177 
self.RefreshConnectors() 

178 
self.RefreshSize() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

179 

814  180 
# Refresh the positions of the power rail connectors 
181 
def RefreshConnectors(self): 

182 
scaling = self.Parent.GetScaling() 

183 
height = self.Size[1]  self.Extensions[0]  self.Extensions[1] 

184 
interval = float(height) / float(max(len(self.Connectors)  1, 1)) 

185 
for i, connector in enumerate(self.Connectors): 

186 
if self.RealConnectors: 

187 
position = self.Extensions[0] + int(round(self.RealConnectors[i] * height)) 

188 
else: 

189 
position = self.Extensions[0] + int(round(i * interval)) 

190 
if scaling is not None: 

191 
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1]  self.Pos.y 

192 
if self.Type == LEFTRAIL: 

193 
connector.SetPosition(wx.Point(self.Size[0], position)) 

194 
elif self.Type == RIGHTRAIL: 

195 
connector.SetPosition(wx.Point(0, position)) 

196 
self.RefreshConnected() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

197 

814  198 
# Refresh the position of wires connected to power rail 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

199 
def RefreshConnected(self, exclude=[]): 
814  200 
for connector in self.Connectors: 
201 
connector.MoveConnected(exclude) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

202 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

203 
# Returns the power rail connector that starts with the point given if it exists 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

204 
def GetConnector(self, position, name=None): 
814  205 
# if a name is given 
206 
if name is not None: 

207 
# Test each connector if it exists 

208 
for connector in self.Connectors: 

209 
if name == connector.GetName(): 

210 
return connector 

211 
return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None]) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

212 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

213 
# Returns all the power rail connectors 
814  214 
def GetConnectors(self): 
215 
connectors = [connector for connector in self.Connectors if connector] 

216 
if self.Type == LEFTRAIL: 

217 
return {"inputs": [], "outputs": connectors} 

218 
else: 

219 
return {"inputs": connectors, "outputs": []} 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

220 

814  221 
# Test if point given is on one of the power rail connectors 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

222 
def TestConnector(self, pt, direction=None, exclude=True): 
814  223 
for connector in self.Connectors: 
224 
if connector.TestPoint(pt, direction, exclude): 

225 
return connector 

226 
return None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

227 

814  228 
# Returns the power rail type 
229 
def SetType(self, type, connectors): 

230 
if type != self.Type or len(self.Connectors) != connectors: 

231 
# Create a connector or a blank according to 'connectors' and add it in 

232 
# the connectors list 

233 
self.Type = type 

234 
self.Clean() 

235 
self.Connectors = [] 

236 
for connector in xrange(connectors): 

237 
self.AddConnector() 

238 
self.RefreshSize() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

239 

814  240 
# Returns the power rail type 
241 
def GetType(self): 

242 
return self.Type 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

243 

814  244 
# Method called when a LeftDown event have been generated 
245 
def OnLeftDown(self, event, dc, scaling): 

246 
self.RealConnectors = [] 

247 
height = self.Size[1]  self.Extensions[0]  self.Extensions[1] 

248 
if height > 0: 

249 
for connector in self.Connectors: 

250 
position = connector.GetRelPosition() 

251 
self.RealConnectors.append(max(0., min(float(position.y  self.Extensions[0]) / float(height), 1.))) 

252 
elif len(self.Connectors) > 1: 

1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

253 
self.RealConnectors = map(lambda x: x * 1 / (len(self.Connectors)  1), xrange(len(self.Connectors))) 
814  254 
else: 
255 
self.RealConnectors = [0.5] 

256 
Graphic_Element.OnLeftDown(self, event, dc, scaling) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

257 

814  258 
# Method called when a LeftUp event have been generated 
259 
def OnLeftUp(self, event, dc, scaling): 

260 
Graphic_Element.OnLeftUp(self, event, dc, scaling) 

261 
self.RealConnectors = None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

262 

814  263 
# Method called when a LeftDown event have been generated 
264 
def OnRightDown(self, event, dc, scaling): 

265 
pos = GetScaledEventPosition(event, dc, scaling) 

266 
# Test if a connector have been handled 

267 
connector = self.TestConnector(pos, exclude=False) 

268 
if connector: 

269 
self.Handle = (HANDLE_CONNECTOR, connector) 

270 
wx.CallAfter(self.Parent.SetCurrentCursor, 1) 

271 
self.Selected = False 

272 
# Initializes the last position 

273 
self.oldPos = GetScaledEventPosition(event, dc, scaling) 

274 
else: 

275 
Graphic_Element.OnRightDown(self, event, dc, scaling) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

276 

814  277 
# Method called when a LeftDClick event have been generated 
278 
def OnLeftDClick(self, event, dc, scaling): 

279 
# Edit the powerrail properties 

280 
self.Parent.EditPowerRailContent(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

281 

814  282 
# Method called when a RightUp event have been generated 
283 
def OnRightUp(self, event, dc, scaling): 

284 
handle_type, handle = self.Handle 

1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset

285 
if handle_type == HANDLE_CONNECTOR and self.Dragging and self.oldPos: 
814  286 
wires = handle.GetWires() 
287 
if len(wires) == 1: 

288 
if handle == wires[0][0].StartConnected: 

289 
block = wires[0][0].EndConnected.GetParentBlock() 

290 
else: 

291 
block = wires[0][0].StartConnected.GetParentBlock() 

292 
block.RefreshModel(False) 

293 
Graphic_Element.OnRightUp(self, event, dc, scaling) 

294 
else: 

295 
self.Parent.PopupDefaultMenu() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

296 

814  297 
def Resize(self, x, y, width, height): 
298 
self.Move(x, y) 

299 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

300 
self.SetSize(width, height) 

301 
else: 

302 
self.SetSize(LD_POWERRAIL_WIDTH, height) 

303 

304 
# Refreshes the powerrail state according to move defined and handle selected 

305 
def ProcessDragging(self, movex, movey, event, scaling): 

306 
handle_type, handle = self.Handle 

307 
# A connector has been handled 

308 
if handle_type == HANDLE_CONNECTOR: 

309 
movey = max(self.BoundingBox.y, movey) 

310 
if scaling is not None: 

311 
position = handle.GetRelPosition() 

312 
movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1]  self.Pos.y  position.y 

313 
self.MoveConnector(handle, movey) 

314 
return 0, movey 

315 
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

316 
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) 

317 
return 0, 0 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

318 

814  319 
# Refreshes the power rail model 
320 
def RefreshModel(self, move=True): 

321 
self.Parent.RefreshPowerRailModel(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

322 
# If power rail has moved and power rail is of type LEFT, refresh the model 
814  323 
# of wires connected to connectors 
324 
if move and self.Type == LEFTRAIL: 

325 
for connector in self.Connectors: 

326 
connector.RefreshWires() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

327 

814  328 
# Draws power rail 
329 
def Draw(self, dc): 

330 
Graphic_Element.Draw(self, dc) 

331 
dc.SetPen(MiterPen(wx.BLACK)) 

332 
dc.SetBrush(wx.BLACK_BRUSH) 

333 
# Draw a rectangle with the power rail size 

334 
if self.Type == LEFTRAIL: 

335 
dc.DrawRectangle(self.Pos.x + self.Size[0]  LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) 

336 
else: 

337 
dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) 

338 
# Draw connectors 

339 
for connector in self.Connectors: 

340 
connector.Draw(dc) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

341 

814  342 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

343 
#  
814  344 
# Ladder Diagram Contact 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

345 
#  
814  346 

347 

348 
class LD_Contact(Graphic_Element, DebugDataConsumer): 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

349 
""" 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

350 
Class that implements the graphic representation of a contact 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

351 
""" 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

352 

814  353 
# Create a new contact 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

354 
def __init__(self, parent, type, name, id=None): 
814  355 
Graphic_Element.__init__(self, parent) 
356 
DebugDataConsumer.__init__(self) 

357 
self.Type = type 

358 
self.Name = name 

359 
self.Id = id 

360 
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) 

361 
self.Highlights = {} 

362 
# Create an input and output connector 

363 
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) 

364 
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) 

365 
self.PreviousValue = False 

366 
self.PreviousSpreading = False 

367 
self.RefreshNameSize() 

368 
self.RefreshTypeSize() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

369 

814  370 
def Flush(self): 
371 
if self.Input is not None: 

372 
self.Input.Flush() 

373 
self.Input = None 

374 
if self.Output is not None: 

375 
self.Output.Flush() 

376 
self.Output = None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

377 

814  378 
def SetForced(self, forced): 
379 
if self.Forced != forced: 

380 
self.Forced = forced 

381 
if self.Visible: 

382 
self.Parent.ElementNeedRefresh(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

383 

814  384 
def SetValue(self, value): 
385 
if self.Type == CONTACT_RISING: 

386 
refresh = self.Value and not self.PreviousValue 

387 
elif self.Type == CONTACT_FALLING: 

388 
refresh = not self.Value and self.PreviousValue 

389 
else: 

390 
refresh = False 

391 
self.PreviousValue = self.Value 

392 
self.Value = value 

393 
if self.Value != self.PreviousValue or refresh: 

394 
if self.Visible: 

395 
self.Parent.ElementNeedRefresh(self) 

396 
self.SpreadCurrent() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

397 

814  398 
def SpreadCurrent(self): 
399 
if self.Parent.Debug: 

400 
if self.Value is None: 

401 
self.Value = False 

402 
spreading = self.Input.ReceivingCurrent() 

403 
if self.Type == CONTACT_NORMAL: 

404 
spreading &= self.Value 

405 
elif self.Type == CONTACT_REVERSE: 

406 
spreading &= not self.Value 

407 
elif self.Type == CONTACT_RISING: 

408 
spreading &= self.Value and not self.PreviousValue 

409 
elif self.Type == CONTACT_FALLING: 

410 
spreading &= not self.Value and self.PreviousValue 

411 
else: 

412 
spreading = False 

413 
if spreading and not self.PreviousSpreading: 

414 
self.Output.SpreadCurrent(True) 

415 
elif not spreading and self.PreviousSpreading: 

416 
self.Output.SpreadCurrent(False) 

417 
self.PreviousSpreading = spreading 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

418 

814  419 
# Make a clone of this LD_Contact 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

420 
def Clone(self, parent, id=None, pos=None): 
814  421 
contact = LD_Contact(parent, self.Type, self.Name, id) 
422 
contact.SetSize(self.Size[0], self.Size[1]) 

423 
if pos is not None: 

424 
contact.SetPosition(pos.x, pos.y) 

425 
else: 

426 
contact.SetPosition(self.Pos.x, self.Pos.y) 

427 
contact.Input = self.Input.Clone(contact) 

428 
contact.Output = self.Output.Clone(contact) 

429 
return contact 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

430 

814  431 
def GetConnectorTranslation(self, element): 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

432 
return {self.Input: element.Input, self.Output: element.Output} 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

433 

814  434 
# Returns the RedrawRect 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

435 
def GetRedrawRect(self, movex=0, movey=0): 
814  436 
rect = Graphic_Element.GetRedrawRect(self, movex, movey) 
437 
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) 

438 
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) 

439 
if movex != 0 or movey != 0: 

440 
if self.Input.IsConnected(): 

441 
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) 

442 
if self.Output.IsConnected(): 

443 
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) 

444 
return rect 

445 

446 
def ProcessDragging(self, movex, movey, event, scaling): 

447 
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: 

448 
movex = movey = 0 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

449 
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac=2) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

450 

814  451 
# Forbids to change the contact size 
452 
def SetSize(self, width, height): 

453 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

454 
Graphic_Element.SetSize(self, width, height) 

455 
self.RefreshConnectors() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

456 

814  457 
# Delete this contact by calling the appropriate method 
458 
def Delete(self): 

459 
self.Parent.DeleteContact(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

460 

814  461 
# Unconnect input and output 
462 
def Clean(self): 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

463 
self.Input.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) 
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

464 
self.Output.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

465 

814  466 
# Refresh the size of text for name 
467 
def RefreshNameSize(self): 

468 
if self.Name != "": 

469 
self.NameSize = self.Parent.GetTextExtent(self.Name) 

470 
else: 

471 
self.NameSize = 0, 0 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

472 

814  473 
# Refresh the size of text for type 
474 
def RefreshTypeSize(self): 

475 
typetext = "" 

476 
if self.Type == CONTACT_REVERSE: 

477 
typetext = "/" 

478 
elif self.Type == CONTACT_RISING: 

479 
typetext = "P" 

480 
elif self.Type == CONTACT_FALLING: 

481 
typetext = "N" 

482 
if typetext != "": 

483 
self.TypeSize = self.Parent.GetTextExtent(typetext) 

484 
else: 

485 
self.TypeSize = 0, 0 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

486 

814  487 
# Refresh the contact bounding box 
488 
def RefreshBoundingBox(self): 

489 
# Calculate the size of the name outside the contact 

490 
text_width, text_height = self.Parent.GetTextExtent(self.Name) 

491 
# Calculate the bounding box size 

492 
if self.Name != "": 

493 
bbx_x = self.Pos.x  max(0, (text_width  self.Size[0]) / 2) 

494 
bbx_width = max(self.Size[0], text_width) 

495 
bbx_y = self.Pos.y  (text_height + 2) 

496 
bbx_height = self.Size[1] + (text_height + 2) 

497 
else: 

498 
bbx_x = self.Pos.x 

499 
bbx_width = self.Size[0] 

500 
bbx_y = self.Pos.y 

501 
bbx_height = self.Size[1] 

502 
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

503 

814  504 
# Returns the block minimum size 
505 
def GetMinSize(self): 

506 
return LD_ELEMENT_SIZE 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

507 

814  508 
# Refresh the position of wire connected to contact 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

509 
def RefreshConnected(self, exclude=[]): 
814  510 
self.Input.MoveConnected(exclude) 
511 
self.Output.MoveConnected(exclude) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

512 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

513 
# Returns the contact connector that starts with the point given if it exists 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

514 
def GetConnector(self, position, name=None): 
814  515 
# if a name is given 
516 
if name is not None: 

517 
# Test input and output connector 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

518 
# if name == self.Input.GetName(): 
814  519 
# return self.Input 
520 
if name == self.Output.GetName(): 

521 
return self.Output 

522 
return self.FindNearestConnector(position, [self.Input, self.Output]) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

523 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

524 
# Returns input and output contact connectors 
814  525 
def GetConnectors(self): 
526 
return {"inputs": [self.Input], "outputs": [self.Output]} 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

527 

814  528 
# Test if point given is on contact input or output connector 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

529 
def TestConnector(self, pt, direction=None, exclude=True): 
814  530 
# Test input connector 
531 
if self.Input.TestPoint(pt, direction, exclude): 

532 
return self.Input 

533 
# Test output connector 

534 
if self.Output.TestPoint(pt, direction, exclude): 

535 
return self.Output 

536 
return None 

537 

538 
# Refresh the positions of the block connectors 

539 
def RefreshConnectors(self): 

540 
scaling = self.Parent.GetScaling() 

541 
position = self.Size[1] / 2 + 1 

542 
if scaling is not None: 

543 
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1]  self.Pos.y 

544 
self.Input.SetPosition(wx.Point(0, position)) 

545 
self.Output.SetPosition(wx.Point(self.Size[0], position)) 

546 
self.RefreshConnected() 

547 

548 
# Changes the contact name 

549 
def SetName(self, name): 

550 
self.Name = name 

551 
self.RefreshNameSize() 

552 

553 
# Returns the contact name 

554 
def GetName(self): 

555 
return self.Name 

556 

557 
# Changes the contact type 

558 
def SetType(self, type): 

559 
self.Type = type 

560 
self.RefreshTypeSize() 

561 

562 
# Returns the contact type 

563 
def GetType(self): 

564 
return self.Type 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

565 

814  566 
# Method called when a LeftDClick event have been generated 
567 
def OnLeftDClick(self, event, dc, scaling): 

568 
# Edit the contact properties 

569 
self.Parent.EditContactContent(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

570 

814  571 
# Method called when a RightUp event have been generated 
572 
def OnRightUp(self, event, dc, scaling): 

573 
# Popup the default menu 

574 
self.Parent.PopupDefaultMenu() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

575 

814  576 
# Refreshes the contact model 
577 
def RefreshModel(self, move=True): 

578 
self.Parent.RefreshContactModel(self) 

579 
# If contact has moved, refresh the model of wires connected to output 

580 
if move: 

581 
self.Output.RefreshWires() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

582 

814  583 
# Draws the highlightment of this element if it is highlighted 
584 
def DrawHighlightment(self, dc): 

585 
scalex, scaley = dc.GetUserScale() 

586 
dc.SetUserScale(1, 1) 

587 
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) 

588 
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) 

589 
dc.SetLogicalFunction(wx.AND) 

590 
# Draw two rectangles for representing the contact 

591 
left_left = (self.Pos.x  1) * scalex  2 

592 
right_left = (self.Pos.x + self.Size[0]  2) * scalex  2 

593 
top = (self.Pos.y  1) * scaley  2 

594 
width = 4 * scalex + 5 

595 
height = (self.Size[1] + 3) * scaley + 5 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

596 

814  597 
dc.DrawRectangle(left_left, top, width, height) 
598 
dc.DrawRectangle(right_left, top, width, height) 

599 
dc.SetLogicalFunction(wx.COPY) 

600 
dc.SetUserScale(scalex, scaley) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

601 

814  602 
# Adds an highlight to the connection 
603 
def AddHighlight(self, infos, start, end, highlight_type): 

604 
highlights = self.Highlights.setdefault(infos[0], []) 

605 
if infos[0] == "reference": 

606 
if start[0] == 0 and end[0] == 0: 

607 
AddHighlight(highlights, (start, end, highlight_type)) 

608 
else: 

609 
AddHighlight(highlights, ((0, 0), (0, 1), highlight_type)) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

610 

814  611 
# Removes an highlight from the connection 
612 
def RemoveHighlight(self, infos, start, end, highlight_type): 

613 
highlights = self.Highlights.get(infos[0], []) 

614 
if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: 

615 
self.Highlights.pop(infos[0]) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

616 

814  617 
# Removes all the highlights of one particular type from the connection 
618 
def ClearHighlight(self, highlight_type=None): 

619 
if highlight_type is None: 

620 
self.Highlights = {} 

621 
else: 

622 
highlight_items = self.Highlights.items() 

623 
for name, highlights in highlight_items: 

624 
highlights = ClearHighlights(highlight, highlight_type) 

625 
if len(highlights) == 0: 

626 
self.Highlights.pop(name) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

627 

814  628 
# Draws contact 
629 
def Draw(self, dc): 

630 
Graphic_Element.Draw(self, dc) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

631 
if self.Value is not None: 
814  632 
if self.Type == CONTACT_NORMAL and self.Value or \ 
633 
self.Type == CONTACT_REVERSE and not self.Value or \ 

634 
self.Type == CONTACT_RISING and self.Value and not self.PreviousValue or \ 

635 
self.Type == CONTACT_RISING and not self.Value and self.PreviousValue: 

636 
if self.Forced: 

637 
dc.SetPen(MiterPen(wx.CYAN)) 

638 
else: 

639 
dc.SetPen(MiterPen(wx.GREEN)) 

640 
elif self.Forced: 

641 
dc.SetPen(MiterPen(wx.BLUE)) 

642 
else: 

643 
dc.SetPen(MiterPen(wx.BLACK)) 

644 
else: 

645 
dc.SetPen(MiterPen(wx.BLACK)) 

646 
dc.SetBrush(wx.BLACK_BRUSH) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

647 

814  648 
# Compiling contact type modifier symbol 
649 
typetext = "" 

650 
if self.Type == CONTACT_REVERSE: 

651 
typetext = "/" 

652 
elif self.Type == CONTACT_RISING: 

653 
typetext = "P" 

654 
elif self.Type == CONTACT_FALLING: 

655 
typetext = "N" 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

656 

814  657 
if getattr(dc, "printing", False): 
658 
name_size = dc.GetTextExtent(self.Name) 

659 
if typetext != "": 

660 
type_size = dc.GetTextExtent(typetext) 

661 
else: 

662 
name_size = self.NameSize 

663 
if typetext != "": 

664 
type_size = self.TypeSize 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

665 

814  666 
# Draw two rectangles for representing the contact 
667 
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) 

668 
dc.DrawRectangle(self.Pos.x + self.Size[0]  1, self.Pos.y, 2, self.Size[1] + 1) 

669 
# Draw contact name 

670 
name_pos = (self.Pos.x + (self.Size[0]  name_size[0]) / 2, 

671 
self.Pos.y  (name_size[1] + 2)) 

672 
dc.DrawText(self.Name, name_pos[0], name_pos[1]) 

673 
# Draw the modifier symbol in the middle of contact 

674 
if typetext != "": 

675 
type_pos = (self.Pos.x + (self.Size[0]  type_size[0]) / 2 + 1, 

676 
self.Pos.y + (self.Size[1]  type_size[1]) / 2) 

677 
dc.DrawText(typetext, type_pos[0], type_pos[1]) 

678 
# Draw input and output connectors 

679 
self.Input.Draw(dc) 

680 
self.Output.Draw(dc) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

681 

814  682 
if not getattr(dc, "printing", False): 
683 
for name, highlights in self.Highlights.iteritems(): 

684 
if name == "reference": 

685 
DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1]) 

686 
elif typetext != "": 

687 
DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1]) 

688 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

689 

5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

690 
#  
814  691 
# Ladder Diagram Coil 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

692 
#  
814  693 

694 

695 
class LD_Coil(Graphic_Element): 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

696 
""" 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

697 
Class that implements the graphic representation of a coil 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

698 
""" 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

699 

814  700 
# Create a new coil 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

701 
def __init__(self, parent, type, name, id=None): 
814  702 
Graphic_Element.__init__(self, parent) 
703 
self.Type = type 

704 
self.Name = name 

705 
self.Id = id 

706 
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) 

707 
self.Highlights = {} 

708 
# Create an input and output connector 

709 
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) 

710 
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) 

711 
self.Value = None 

712 
self.PreviousValue = False 

713 
self.RefreshNameSize() 

714 
self.RefreshTypeSize() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

715 

814  716 
def Flush(self): 
717 
if self.Input is not None: 

718 
self.Input.Flush() 

719 
self.Input = None 

720 
if self.Output is not None: 

721 
self.Output.Flush() 

722 
self.Output = None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

723 

814  724 
def SpreadCurrent(self): 
725 
if self.Parent.Debug: 

726 
self.PreviousValue = self.Value 

727 
self.Value = self.Input.ReceivingCurrent() 

728 
if self.Value and not self.PreviousValue: 

729 
self.Output.SpreadCurrent(True) 

730 
elif not self.Value and self.PreviousValue: 

731 
self.Output.SpreadCurrent(False) 

732 
if self.Value != self.PreviousValue and self.Visible: 

733 
self.Parent.ElementNeedRefresh(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

734 

814  735 
# Make a clone of this LD_Coil 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

736 
def Clone(self, parent, id=None, pos=None): 
814  737 
coil = LD_Coil(parent, self.Type, self.Name, id) 
738 
coil.SetSize(self.Size[0], self.Size[1]) 

739 
if pos is not None: 

740 
coil.SetPosition(pos.x, pos.y) 

741 
else: 

742 
coil.SetPosition(self.Pos.x, self.Pos.y) 

743 
coil.Input = self.Input.Clone(coil) 

744 
coil.Output = self.Output.Clone(coil) 

745 
return coil 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

746 

814  747 
def GetConnectorTranslation(self, element): 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

748 
return {self.Input: element.Input, self.Output: element.Output} 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

749 

814  750 
# Returns the RedrawRect 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

751 
def GetRedrawRect(self, movex=0, movey=0): 
814  752 
rect = Graphic_Element.GetRedrawRect(self, movex, movey) 
753 
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) 

754 
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) 

755 
if movex != 0 or movey != 0: 

756 
if self.Input.IsConnected(): 

757 
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) 

758 
if self.Output.IsConnected(): 

759 
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) 

760 
return rect 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

761 

814  762 
def ProcessDragging(self, movex, movey, event, scaling): 
763 
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: 

764 
movex = movey = 0 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

765 
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac=2) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

766 

814  767 
# Forbids to change the Coil size 
768 
def SetSize(self, width, height): 

769 
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: 

770 
Graphic_Element.SetSize(self, width, height) 

771 
self.RefreshConnectors() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

772 

814  773 
# Delete this coil by calling the appropriate method 
774 
def Delete(self): 

775 
self.Parent.DeleteCoil(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

776 

814  777 
# Unconnect input and output 
778 
def Clean(self): 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

779 
self.Input.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) 
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

780 
self.Output.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

781 

814  782 
# Refresh the size of text for name 
783 
def RefreshNameSize(self): 

784 
if self.Name != "": 

785 
self.NameSize = self.Parent.GetTextExtent(self.Name) 

786 
else: 

787 
self.NameSize = 0, 0 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

788 

814  789 
# Refresh the size of text for type 
790 
def RefreshTypeSize(self): 

791 
typetext = "" 

792 
if self.Type == COIL_REVERSE: 

793 
typetext = "/" 

794 
elif self.Type == COIL_SET: 

795 
typetext = "S" 

796 
elif self.Type == COIL_RESET: 

797 
typetext = "R" 

798 
elif self.Type == COIL_RISING: 

799 
typetext = "P" 

800 
elif self.Type == COIL_FALLING: 

801 
typetext = "N" 

802 
if typetext != "": 

803 
self.TypeSize = self.Parent.GetTextExtent(typetext) 

804 
else: 

805 
self.TypeSize = 0, 0 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

806 

814  807 
# Refresh the coil bounding box 
808 
def RefreshBoundingBox(self): 

809 
# Calculate the size of the name outside the coil 

810 
text_width, text_height = self.Parent.GetTextExtent(self.Name) 

811 
# Calculate the bounding box size 

812 
if self.Name != "": 

813 
bbx_x = self.Pos.x  max(0, (text_width  self.Size[0]) / 2) 

814 
bbx_width = max(self.Size[0], text_width) 

815 
bbx_y = self.Pos.y  (text_height + 2) 

816 
bbx_height = self.Size[1] + (text_height + 2) 

817 
else: 

818 
bbx_x = self.Pos.x 

819 
bbx_width = self.Size[0] 

820 
bbx_y = self.Pos.y 

821 
bbx_height = self.Size[1] 

822 
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

823 

814  824 
# Returns the block minimum size 
825 
def GetMinSize(self): 

826 
return LD_ELEMENT_SIZE 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

827 

814  828 
# Refresh the position of wire connected to coil 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

829 
def RefreshConnected(self, exclude=[]): 
814  830 
self.Input.MoveConnected(exclude) 
831 
self.Output.MoveConnected(exclude) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

832 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

833 
# Returns the coil connector that starts with the point given if it exists 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

834 
def GetConnector(self, position, name=None): 
814  835 
# if a name is given 
836 
if name is not None: 

837 
# Test input and output connector 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

838 
# if self.Input and name == self.Input.GetName(): 
814  839 
# return self.Input 
840 
if self.Output and name == self.Output.GetName(): 

841 
return self.Output 

842 
return self.FindNearestConnector(position, [self.Input, self.Output]) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

843 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

844 
# Returns input and output coil connectors 
814  845 
def GetConnectors(self): 
846 
return {"inputs": [self.Input], "outputs": [self.Output]} 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

847 

814  848 
# Test if point given is on coil input or output connector 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

849 
def TestConnector(self, pt, direction=None, exclude=True): 
814  850 
# Test input connector 
851 
if self.Input.TestPoint(pt, direction, exclude): 

852 
return self.Input 

853 
# Test output connector 

854 
if self.Output.TestPoint(pt, direction, exclude): 

855 
return self.Output 

856 
return None 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

857 

814  858 
# Refresh the positions of the block connectors 
859 
def RefreshConnectors(self): 

860 
scaling = self.Parent.GetScaling() 

861 
position = self.Size[1] / 2 + 1 

862 
if scaling is not None: 

863 
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1]  self.Pos.y 

864 
self.Input.SetPosition(wx.Point(0, position)) 

865 
self.Output.SetPosition(wx.Point(self.Size[0], position)) 

866 
self.RefreshConnected() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

867 

814  868 
# Changes the coil name 
869 
def SetName(self, name): 

870 
self.Name = name 

871 
self.RefreshNameSize() 

872 

873 
# Returns the coil name 

874 
def GetName(self): 

875 
return self.Name 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

876 

814  877 
# Changes the coil type 
878 
def SetType(self, type): 

879 
self.Type = type 

880 
self.RefreshTypeSize() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

881 

814  882 
# Returns the coil type 
883 
def GetType(self): 

884 
return self.Type 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

885 

814  886 
# Method called when a LeftDClick event have been generated 
887 
def OnLeftDClick(self, event, dc, scaling): 

888 
# Edit the coil properties 

889 
self.Parent.EditCoilContent(self) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

890 

814  891 
# Method called when a RightUp event have been generated 
892 
def OnRightUp(self, event, dc, scaling): 

893 
# Popup the default menu 

894 
self.Parent.PopupDefaultMenu() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

895 

814  896 
# Refreshes the coil model 
897 
def RefreshModel(self, move=True): 

898 
self.Parent.RefreshCoilModel(self) 

899 
# If coil has moved, refresh the model of wires connected to output 

900 
if move: 

901 
self.Output.RefreshWires() 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

902 

814  903 
# Draws the highlightment of this element if it is highlighted 
904 
def DrawHighlightment(self, dc): 

905 
scalex, scaley = dc.GetUserScale() 

906 
dc.SetUserScale(1, 1) 

907 
dc.SetPen(MiterPen(HIGHLIGHTCOLOR, (3 * scalex + 5), wx.SOLID)) 

908 
dc.SetBrush(wx.TRANSPARENT_BRUSH) 

909 
dc.SetLogicalFunction(wx.AND) 

910 
# Draw a two circle arcs for representing the coil 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

911 
dc.DrawEllipticArc(round(self.Pos.x * scalex), 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

912 
round((self.Pos.y  int(self.Size[1] * (sqrt(2)  1.) / 2.) + 1) * scaley), 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

913 
round(self.Size[0] * scalex), 
814  914 
round((int(self.Size[1] * sqrt(2))  1) * scaley), 
915 
135, 225) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

916 
dc.DrawEllipticArc(round(self.Pos.x * scalex), 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

917 
round((self.Pos.y  int(self.Size[1] * (sqrt(2)  1.) / 2.) + 1) * scaley), 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

918 
round(self.Size[0] * scalex), 
814  919 
round((int(self.Size[1] * sqrt(2))  1) * scaley), 
920 
45, 45) 

921 
dc.SetLogicalFunction(wx.COPY) 

922 
dc.SetUserScale(scalex, scaley) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

923 

814  924 
# Adds an highlight to the connection 
925 
def AddHighlight(self, infos, start, end, highlight_type): 

926 
highlights = self.Highlights.setdefault(infos[0], []) 

927 
if infos[0] == "reference": 

928 
if start[0] == 0 and end[0] == 0: 

929 
AddHighlight(highlights, (start, end, highlight_type)) 

930 
else: 

931 
AddHighlight(highlights, ((0, 0), (0, 1), highlight_type)) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

932 

814  933 
# Removes an highlight from the connection 
934 
def RemoveHighlight(self, infos, start, end, highlight_type): 

935 
highlights = self.Highlights.get(infos[0], []) 

936 
if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: 

937 
self.Highlights.pop(infos[0]) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

938 

814  939 
# Removes all the highlights of one particular type from the connection 
940 
def ClearHighlight(self, highlight_type=None): 

941 
if highlight_type is None: 

942 
self.Highlights = {} 

943 
else: 

944 
highlight_items = self.Highlights.items() 

945 
for name, highlights in highlight_items: 

946 
highlights = ClearHighlights(highlight, highlight_type) 

947 
if len(highlights) == 0: 

948 
self.Highlights.pop(name) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

949 

814  950 
# Draws coil 
951 
def Draw(self, dc): 

952 
Graphic_Element.Draw(self, dc) 

953 
if self.Value is not None and self.Value: 

954 
dc.SetPen(MiterPen(wx.GREEN, 2, wx.SOLID)) 

955 
else: 

956 
dc.SetPen(MiterPen(wx.BLACK, 2, wx.SOLID)) 

957 
dc.SetBrush(wx.TRANSPARENT_BRUSH) 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

958 

64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

959 
# Compiling coil type modifier symbol 
814  960 
typetext = "" 
961 
if self.Type == COIL_REVERSE: 

962 
typetext = "/" 

963 
elif self.Type == COIL_SET: 

964 
typetext = "S" 

965 
elif self.Type == COIL_RESET: 

966 
typetext = "R" 

967 
elif self.Type == COIL_RISING: 

968 
typetext = "P" 

969 
elif self.Type == COIL_FALLING: 

970 
typetext = "N" 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

971 

814  972 
if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC): 
973 
# Draw an clipped ellipse for representing the coil 

974 
clipping_box = dc.GetClippingBox() 

975 
dc.SetClippingRegion(self.Pos.x  1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1) 

976 
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) 

977 
dc.DestroyClippingRegion() 

978 
if clipping_box != (0, 0, 0, 0): 

979 
dc.SetClippingRegion(*clipping_box) 

980 
name_size = dc.GetTextExtent(self.Name) 

981 
if typetext != "": 

982 
type_size = dc.GetTextExtent(typetext) 

983 
else: 

984 
# Draw a two ellipse arcs for representing the coil 

985 
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) 

986 
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) 

987 
# Draw a point to avoid hole in left arc 

988 
if not getattr(dc, "printing", False): 

989 
if self.Value is not None and self.Value: 

990 
dc.SetPen(MiterPen(wx.GREEN)) 

991 
else: 

992 
dc.SetPen(MiterPen(wx.BLACK)) 

993 
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) 

994 
name_size = self.NameSize 

995 
if typetext != "": 

996 
type_size = self.TypeSize 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset

997 

814  998 
# Draw coil name 
999 
name_pos = (self.Pos.x + (self.Size[0]  name_size[0]) / 2, 

1000 
self.Pos.y  (name_size[1] + 2)) 

1001 
dc.DrawText(self.Name, name_pos[0], name_pos[1]) 

1002 
# Draw the modifier symbol in the middle of coil 

1003 
if typetext != "": 

1004 
type_pos = (self.Pos.x + (self.Size[0]  type_size[0]) / 2 + 1, 

1005 
self.Pos.y + (self.Size[1]  type_size[1]) / 2) 

1006 
dc.DrawText(typetext, type_pos[0], type_pos[1]) 

1007 
# Draw input and output connectors 

1008 
self.Input.Draw(dc) 

1009 
self.Output.Draw(dc) 

1010 

1011 
if not getattr(dc, "printing", False): 

1012 
for name, highlights in self.Highlights.iteritems(): 

1013 
if name == "reference": 

1014 
DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1]) 

1015 
elif typetext != "": 

1016 
DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1]) 