author | Edouard Tisserant |
Tue, 14 Aug 2018 13:10:15 +0200 | |
changeset 2268 | e6c9a52d3877 |
parent 1881 | 091005ec69c4 |
child 2432 | dbc065a2f7a5 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
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 61131-3 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 02110-1301, USA. |
814 | 24 |
|
1881
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1872
diff
changeset
|
25 |
|
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1872
diff
changeset
|
26 |
from __future__ import absolute_import |
814 | 27 |
import wx |
28 |
||
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset
|
29 |
from graphics.GraphicCommons import * |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset
|
30 |
from graphics.DebugDataConsumer import DebugDataConsumer |
814 | 31 |
from plcopen.structures import * |
32 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
33 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
34 |
# ------------------------------------------------------------------------------- |
814 | 35 |
# Ladder Diagram PowerRail |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
36 |
# ------------------------------------------------------------------------------- |
814 | 37 |
|
38 |
||
39 |
class LD_PowerRail(Graphic_Element): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
40 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
41 |
Class that implements the graphic representation of a power rail |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
42 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
43 |
|
814 | 44 |
# Create a new power rail |
45 |
def __init__(self, parent, type, id=None, connectors=1): |
|
46 |
Graphic_Element.__init__(self, parent) |
|
47 |
self.Type = None |
|
48 |
self.Connectors = [] |
|
49 |
self.RealConnectors = None |
|
50 |
self.Id = id |
|
51 |
self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2] |
|
52 |
self.SetType(type, connectors) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
53 |
|
814 | 54 |
def Flush(self): |
55 |
for connector in self.Connectors: |
|
56 |
connector.Flush() |
|
57 |
self.Connectors = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
58 |
|
814 | 59 |
# Make a clone of this LD_PowerRail |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
60 |
def Clone(self, parent, id=None, pos=None): |
814 | 61 |
powerrail = LD_PowerRail(parent, self.Type, id) |
62 |
powerrail.SetSize(self.Size[0], self.Size[1]) |
|
63 |
if pos is not None: |
|
64 |
powerrail.SetPosition(pos.x, pos.y) |
|
65 |
else: |
|
66 |
powerrail.SetPosition(self.Pos.x, self.Pos.y) |
|
67 |
powerrail.Connectors = [] |
|
68 |
for connector in self.Connectors: |
|
69 |
powerrail.Connectors.append(connector.Clone(powerrail)) |
|
70 |
return powerrail |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
71 |
|
814 | 72 |
def GetConnectorTranslation(self, element): |
73 |
return dict(zip([connector for connector in self.Connectors], |
|
74 |
[connector for connector in element.Connectors])) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
75 |
|
814 | 76 |
# Returns the RedrawRect |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
77 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 78 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
79 |
for connector in self.Connectors: |
|
80 |
rect = rect.Union(connector.GetRedrawRect(movex, movey)) |
|
81 |
if movex != 0 or movey != 0: |
|
82 |
for connector in self.Connectors: |
|
83 |
if connector.IsConnected(): |
|
84 |
rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey)) |
|
85 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
86 |
|
814 | 87 |
# Forbids to change the power rail size |
88 |
def SetSize(self, width, height): |
|
89 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
90 |
Graphic_Element.SetSize(self, width, height) |
|
91 |
else: |
|
92 |
Graphic_Element.SetSize(self, LD_POWERRAIL_WIDTH, height) |
|
93 |
self.RefreshConnectors() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
94 |
|
814 | 95 |
# Forbids to select a power rail |
96 |
def HitTest(self, pt, connectors=True): |
|
97 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
98 |
return Graphic_Element.HitTest(self, pt, connectors) or self.TestConnector(pt, exclude=False) is not None |
814 | 99 |
return False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
100 |
|
814 | 101 |
# Forbids to select a power rail |
102 |
def IsInSelection(self, rect): |
|
103 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
104 |
return Graphic_Element.IsInSelection(self, rect) |
|
105 |
return False |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
106 |
|
814 | 107 |
# Deletes this power rail by calling the appropriate method |
108 |
def Delete(self): |
|
109 |
self.Parent.DeletePowerRail(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
110 |
|
814 | 111 |
# Unconnect all connectors |
112 |
def Clean(self): |
|
113 |
for connector in self.Connectors: |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
114 |
connector.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
115 |
|
814 | 116 |
# Refresh the power rail bounding box |
117 |
def RefreshBoundingBox(self): |
|
118 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
119 |
|
814 | 120 |
# Refresh the power rail size |
121 |
def RefreshSize(self): |
|
122 |
self.Size = wx.Size(LD_POWERRAIL_WIDTH, max(LD_LINE_SIZE * len(self.Connectors), self.Size[1])) |
|
123 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
124 |
|
814 | 125 |
# 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
|
126 |
def GetMinSize(self, default=False): |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
127 |
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
|
128 |
if default else 0) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
129 |
return LD_POWERRAIL_WIDTH, height + self.Extensions[0] + self.Extensions[1] |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
130 |
|
814 | 131 |
# Add a connector or a blank to this power rail at the last place |
132 |
def AddConnector(self): |
|
133 |
self.InsertConnector(len(self.Connectors)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
134 |
|
814 | 135 |
# Add a connector or a blank to this power rail at the place given |
136 |
def InsertConnector(self, idx): |
|
137 |
if self.Type == LEFTRAIL: |
|
138 |
connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) |
|
139 |
elif self.Type == RIGHTRAIL: |
|
140 |
connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST) |
|
141 |
self.Connectors.insert(idx, connector) |
|
142 |
self.RefreshSize() |
|
143 |
self.RefreshConnectors() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
144 |
|
814 | 145 |
# Moves the divergence connector given |
146 |
def MoveConnector(self, connector, movey): |
|
147 |
position = connector.GetRelPosition() |
|
148 |
connector.SetPosition(wx.Point(position.x, position.y + movey)) |
|
149 |
miny = self.Size[1] |
|
150 |
maxy = 0 |
|
151 |
for connect in self.Connectors: |
|
152 |
connect_pos = connect.GetRelPosition() |
|
153 |
miny = min(miny, connect_pos.y - self.Extensions[0]) |
|
154 |
maxy = max(maxy, connect_pos.y - self.Extensions[0]) |
|
155 |
min_pos = self.Pos.y + miny |
|
156 |
self.Pos.y = min(min_pos, self.Pos.y) |
|
157 |
if min_pos == self.Pos.y: |
|
158 |
for connect in self.Connectors: |
|
159 |
connect_pos = connect.GetRelPosition() |
|
160 |
connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny)) |
|
161 |
self.Connectors.sort(lambda x, y: cmp(x.Pos.y, y.Pos.y)) |
|
162 |
maxy = 0 |
|
163 |
for connect in self.Connectors: |
|
164 |
connect_pos = connect.GetRelPosition() |
|
165 |
maxy = max(maxy, connect_pos.y) |
|
166 |
self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) |
|
167 |
connector.MoveConnected() |
|
168 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
169 |
|
814 | 170 |
# Returns the index in connectors list for the connector given |
171 |
def GetConnectorIndex(self, connector): |
|
172 |
if connector in self.Connectors: |
|
173 |
return self.Connectors.index(connector) |
|
174 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
175 |
|
814 | 176 |
# Delete the connector or blank from connectors list at the index given |
177 |
def DeleteConnector(self, idx): |
|
178 |
self.Connectors.pop(idx) |
|
179 |
self.RefreshConnectors() |
|
180 |
self.RefreshSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
181 |
|
814 | 182 |
# Refresh the positions of the power rail connectors |
183 |
def RefreshConnectors(self): |
|
184 |
scaling = self.Parent.GetScaling() |
|
185 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
186 |
interval = float(height) / float(max(len(self.Connectors) - 1, 1)) |
|
187 |
for i, connector in enumerate(self.Connectors): |
|
188 |
if self.RealConnectors: |
|
189 |
position = self.Extensions[0] + int(round(self.RealConnectors[i] * height)) |
|
190 |
else: |
|
191 |
position = self.Extensions[0] + int(round(i * interval)) |
|
192 |
if scaling is not None: |
|
193 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
194 |
if self.Type == LEFTRAIL: |
|
195 |
connector.SetPosition(wx.Point(self.Size[0], position)) |
|
196 |
elif self.Type == RIGHTRAIL: |
|
197 |
connector.SetPosition(wx.Point(0, position)) |
|
198 |
self.RefreshConnected() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
199 |
|
814 | 200 |
# Refresh the position of wires connected to power rail |
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
201 |
def RefreshConnected(self, exclude=None): |
814 | 202 |
for connector in self.Connectors: |
203 |
connector.MoveConnected(exclude) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
204 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
205 |
# Returns the power rail connector that starts with the point given if it exists |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
206 |
def GetConnector(self, position, name=None): |
814 | 207 |
# if a name is given |
208 |
if name is not None: |
|
209 |
# Test each connector if it exists |
|
210 |
for connector in self.Connectors: |
|
211 |
if name == connector.GetName(): |
|
212 |
return connector |
|
213 |
return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
214 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
215 |
# Returns all the power rail connectors |
814 | 216 |
def GetConnectors(self): |
217 |
connectors = [connector for connector in self.Connectors if connector] |
|
218 |
if self.Type == LEFTRAIL: |
|
219 |
return {"inputs": [], "outputs": connectors} |
|
220 |
else: |
|
221 |
return {"inputs": connectors, "outputs": []} |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
222 |
|
814 | 223 |
# Test if point given is on one of the power rail connectors |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
224 |
def TestConnector(self, pt, direction=None, exclude=True): |
814 | 225 |
for connector in self.Connectors: |
226 |
if connector.TestPoint(pt, direction, exclude): |
|
227 |
return connector |
|
228 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
229 |
|
814 | 230 |
# Returns the power rail type |
231 |
def SetType(self, type, connectors): |
|
232 |
if type != self.Type or len(self.Connectors) != connectors: |
|
233 |
# Create a connector or a blank according to 'connectors' and add it in |
|
234 |
# the connectors list |
|
235 |
self.Type = type |
|
236 |
self.Clean() |
|
237 |
self.Connectors = [] |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
238 |
for dummy in xrange(connectors): |
814 | 239 |
self.AddConnector() |
240 |
self.RefreshSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
241 |
|
814 | 242 |
# Returns the power rail type |
243 |
def GetType(self): |
|
244 |
return self.Type |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
245 |
|
814 | 246 |
# Method called when a LeftDown event have been generated |
247 |
def OnLeftDown(self, event, dc, scaling): |
|
248 |
self.RealConnectors = [] |
|
249 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
250 |
if height > 0: |
|
251 |
for connector in self.Connectors: |
|
252 |
position = connector.GetRelPosition() |
|
253 |
self.RealConnectors.append(max(0., min(float(position.y - self.Extensions[0]) / float(height), 1.))) |
|
254 |
elif len(self.Connectors) > 1: |
|
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
255 |
self.RealConnectors = map(lambda x: x * 1 / (len(self.Connectors) - 1), xrange(len(self.Connectors))) |
814 | 256 |
else: |
257 |
self.RealConnectors = [0.5] |
|
258 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
259 |
|
814 | 260 |
# Method called when a LeftUp event have been generated |
261 |
def OnLeftUp(self, event, dc, scaling): |
|
262 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
|
263 |
self.RealConnectors = None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
264 |
|
814 | 265 |
# Method called when a LeftDown event have been generated |
266 |
def OnRightDown(self, event, dc, scaling): |
|
267 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
268 |
# Test if a connector have been handled |
|
269 |
connector = self.TestConnector(pos, exclude=False) |
|
270 |
if connector: |
|
271 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
272 |
wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
|
273 |
self.Selected = False |
|
274 |
# Initializes the last position |
|
275 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
276 |
else: |
|
277 |
Graphic_Element.OnRightDown(self, event, dc, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
278 |
|
814 | 279 |
# Method called when a LeftDClick event have been generated |
280 |
def OnLeftDClick(self, event, dc, scaling): |
|
281 |
# Edit the powerrail properties |
|
282 |
self.Parent.EditPowerRailContent(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
283 |
|
814 | 284 |
# Method called when a RightUp event have been generated |
285 |
def OnRightUp(self, event, dc, scaling): |
|
286 |
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
|
287 |
if handle_type == HANDLE_CONNECTOR and self.Dragging and self.oldPos: |
814 | 288 |
wires = handle.GetWires() |
289 |
if len(wires) == 1: |
|
290 |
if handle == wires[0][0].StartConnected: |
|
291 |
block = wires[0][0].EndConnected.GetParentBlock() |
|
292 |
else: |
|
293 |
block = wires[0][0].StartConnected.GetParentBlock() |
|
294 |
block.RefreshModel(False) |
|
295 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
|
296 |
else: |
|
297 |
self.Parent.PopupDefaultMenu() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
298 |
|
814 | 299 |
def Resize(self, x, y, width, height): |
300 |
self.Move(x, y) |
|
301 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
302 |
self.SetSize(width, height) |
|
303 |
else: |
|
304 |
self.SetSize(LD_POWERRAIL_WIDTH, height) |
|
305 |
||
306 |
# Refreshes the powerrail state according to move defined and handle selected |
|
307 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
308 |
handle_type, handle = self.Handle |
|
309 |
# A connector has been handled |
|
310 |
if handle_type == HANDLE_CONNECTOR: |
|
311 |
movey = max(-self.BoundingBox.y, movey) |
|
312 |
if scaling is not None: |
|
313 |
position = handle.GetRelPosition() |
|
314 |
movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y |
|
315 |
self.MoveConnector(handle, movey) |
|
316 |
return 0, movey |
|
317 |
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
318 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
319 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
320 |
|
814 | 321 |
# Refreshes the power rail model |
322 |
def RefreshModel(self, move=True): |
|
323 |
self.Parent.RefreshPowerRailModel(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
324 |
# If power rail has moved and power rail is of type LEFT, refresh the model |
814 | 325 |
# of wires connected to connectors |
326 |
if move and self.Type == LEFTRAIL: |
|
327 |
for connector in self.Connectors: |
|
328 |
connector.RefreshWires() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
329 |
|
814 | 330 |
# Draws power rail |
331 |
def Draw(self, dc): |
|
332 |
Graphic_Element.Draw(self, dc) |
|
333 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
334 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
335 |
# Draw a rectangle with the power rail size |
|
336 |
if self.Type == LEFTRAIL: |
|
337 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) |
|
338 |
else: |
|
339 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1) |
|
340 |
# Draw connectors |
|
341 |
for connector in self.Connectors: |
|
342 |
connector.Draw(dc) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
343 |
|
814 | 344 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
345 |
# ------------------------------------------------------------------------------- |
814 | 346 |
# Ladder Diagram Contact |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
347 |
# ------------------------------------------------------------------------------- |
814 | 348 |
|
349 |
||
350 |
class LD_Contact(Graphic_Element, DebugDataConsumer): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
351 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
352 |
Class that implements the graphic representation of a contact |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
353 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
354 |
|
814 | 355 |
# Create a new contact |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
356 |
def __init__(self, parent, type, name, id=None): |
814 | 357 |
Graphic_Element.__init__(self, parent) |
358 |
DebugDataConsumer.__init__(self) |
|
359 |
self.Type = type |
|
360 |
self.Name = name |
|
361 |
self.Id = id |
|
362 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
|
363 |
self.Highlights = {} |
|
364 |
# Create an input and output connector |
|
365 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
|
366 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
|
367 |
self.PreviousValue = False |
|
368 |
self.PreviousSpreading = False |
|
369 |
self.RefreshNameSize() |
|
370 |
self.RefreshTypeSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
371 |
|
814 | 372 |
def Flush(self): |
373 |
if self.Input is not None: |
|
374 |
self.Input.Flush() |
|
375 |
self.Input = None |
|
376 |
if self.Output is not None: |
|
377 |
self.Output.Flush() |
|
378 |
self.Output = None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
379 |
|
814 | 380 |
def SetForced(self, forced): |
381 |
if self.Forced != forced: |
|
382 |
self.Forced = forced |
|
383 |
if self.Visible: |
|
384 |
self.Parent.ElementNeedRefresh(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
385 |
|
814 | 386 |
def SetValue(self, value): |
387 |
if self.Type == CONTACT_RISING: |
|
388 |
refresh = self.Value and not self.PreviousValue |
|
389 |
elif self.Type == CONTACT_FALLING: |
|
390 |
refresh = not self.Value and self.PreviousValue |
|
391 |
else: |
|
392 |
refresh = False |
|
393 |
self.PreviousValue = self.Value |
|
394 |
self.Value = value |
|
395 |
if self.Value != self.PreviousValue or refresh: |
|
396 |
if self.Visible: |
|
397 |
self.Parent.ElementNeedRefresh(self) |
|
398 |
self.SpreadCurrent() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
399 |
|
814 | 400 |
def SpreadCurrent(self): |
401 |
if self.Parent.Debug: |
|
402 |
if self.Value is None: |
|
403 |
self.Value = False |
|
404 |
spreading = self.Input.ReceivingCurrent() |
|
405 |
if self.Type == CONTACT_NORMAL: |
|
406 |
spreading &= self.Value |
|
407 |
elif self.Type == CONTACT_REVERSE: |
|
408 |
spreading &= not self.Value |
|
409 |
elif self.Type == CONTACT_RISING: |
|
410 |
spreading &= self.Value and not self.PreviousValue |
|
411 |
elif self.Type == CONTACT_FALLING: |
|
412 |
spreading &= not self.Value and self.PreviousValue |
|
413 |
else: |
|
414 |
spreading = False |
|
415 |
if spreading and not self.PreviousSpreading: |
|
416 |
self.Output.SpreadCurrent(True) |
|
417 |
elif not spreading and self.PreviousSpreading: |
|
418 |
self.Output.SpreadCurrent(False) |
|
419 |
self.PreviousSpreading = spreading |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
420 |
|
814 | 421 |
# Make a clone of this LD_Contact |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
422 |
def Clone(self, parent, id=None, pos=None): |
814 | 423 |
contact = LD_Contact(parent, self.Type, self.Name, id) |
424 |
contact.SetSize(self.Size[0], self.Size[1]) |
|
425 |
if pos is not None: |
|
426 |
contact.SetPosition(pos.x, pos.y) |
|
427 |
else: |
|
428 |
contact.SetPosition(self.Pos.x, self.Pos.y) |
|
429 |
contact.Input = self.Input.Clone(contact) |
|
430 |
contact.Output = self.Output.Clone(contact) |
|
431 |
return contact |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
432 |
|
814 | 433 |
def GetConnectorTranslation(self, element): |
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
434 |
return {self.Input: element.Input, self.Output: element.Output} |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
435 |
|
814 | 436 |
# Returns the RedrawRect |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
437 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 438 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
439 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
440 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
441 |
if movex != 0 or movey != 0: |
|
442 |
if self.Input.IsConnected(): |
|
443 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
444 |
if self.Output.IsConnected(): |
|
445 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
446 |
return rect |
|
447 |
||
448 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
449 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
450 |
movex = movey = 0 |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
451 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac=2) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
452 |
|
814 | 453 |
# Forbids to change the contact size |
454 |
def SetSize(self, width, height): |
|
455 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
456 |
Graphic_Element.SetSize(self, width, height) |
|
457 |
self.RefreshConnectors() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
458 |
|
814 | 459 |
# Delete this contact by calling the appropriate method |
460 |
def Delete(self): |
|
461 |
self.Parent.DeleteContact(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
462 |
|
814 | 463 |
# Unconnect input and output |
464 |
def Clean(self): |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
465 |
self.Input.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
466 |
self.Output.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
467 |
|
814 | 468 |
# Refresh the size of text for name |
469 |
def RefreshNameSize(self): |
|
470 |
if self.Name != "": |
|
471 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
|
472 |
else: |
|
473 |
self.NameSize = 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
474 |
|
814 | 475 |
# Refresh the size of text for type |
476 |
def RefreshTypeSize(self): |
|
477 |
typetext = "" |
|
478 |
if self.Type == CONTACT_REVERSE: |
|
479 |
typetext = "/" |
|
480 |
elif self.Type == CONTACT_RISING: |
|
481 |
typetext = "P" |
|
482 |
elif self.Type == CONTACT_FALLING: |
|
483 |
typetext = "N" |
|
484 |
if typetext != "": |
|
485 |
self.TypeSize = self.Parent.GetTextExtent(typetext) |
|
486 |
else: |
|
487 |
self.TypeSize = 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
488 |
|
814 | 489 |
# Refresh the contact bounding box |
490 |
def RefreshBoundingBox(self): |
|
491 |
# Calculate the size of the name outside the contact |
|
492 |
text_width, text_height = self.Parent.GetTextExtent(self.Name) |
|
493 |
# Calculate the bounding box size |
|
494 |
if self.Name != "": |
|
495 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
496 |
bbx_width = max(self.Size[0], text_width) |
|
497 |
bbx_y = self.Pos.y - (text_height + 2) |
|
498 |
bbx_height = self.Size[1] + (text_height + 2) |
|
499 |
else: |
|
500 |
bbx_x = self.Pos.x |
|
501 |
bbx_width = self.Size[0] |
|
502 |
bbx_y = self.Pos.y |
|
503 |
bbx_height = self.Size[1] |
|
504 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
505 |
|
814 | 506 |
# Returns the block minimum size |
507 |
def GetMinSize(self): |
|
508 |
return LD_ELEMENT_SIZE |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
509 |
|
814 | 510 |
# Refresh the position of wire connected to contact |
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
511 |
def RefreshConnected(self, exclude=None): |
814 | 512 |
self.Input.MoveConnected(exclude) |
513 |
self.Output.MoveConnected(exclude) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
514 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
515 |
# Returns the contact connector that starts with the point given if it exists |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
516 |
def GetConnector(self, position, name=None): |
814 | 517 |
# if a name is given |
518 |
if name is not None: |
|
519 |
# Test input and output connector |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
520 |
# if name == self.Input.GetName(): |
814 | 521 |
# return self.Input |
522 |
if name == self.Output.GetName(): |
|
523 |
return self.Output |
|
524 |
return self.FindNearestConnector(position, [self.Input, self.Output]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
525 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
526 |
# Returns input and output contact connectors |
814 | 527 |
def GetConnectors(self): |
528 |
return {"inputs": [self.Input], "outputs": [self.Output]} |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
529 |
|
814 | 530 |
# Test if point given is on contact input or output connector |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
531 |
def TestConnector(self, pt, direction=None, exclude=True): |
814 | 532 |
# Test input connector |
533 |
if self.Input.TestPoint(pt, direction, exclude): |
|
534 |
return self.Input |
|
535 |
# Test output connector |
|
536 |
if self.Output.TestPoint(pt, direction, exclude): |
|
537 |
return self.Output |
|
538 |
return None |
|
539 |
||
540 |
# Refresh the positions of the block connectors |
|
541 |
def RefreshConnectors(self): |
|
542 |
scaling = self.Parent.GetScaling() |
|
543 |
position = self.Size[1] / 2 + 1 |
|
544 |
if scaling is not None: |
|
545 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
546 |
self.Input.SetPosition(wx.Point(0, position)) |
|
547 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
|
548 |
self.RefreshConnected() |
|
549 |
||
550 |
# Changes the contact name |
|
551 |
def SetName(self, name): |
|
552 |
self.Name = name |
|
553 |
self.RefreshNameSize() |
|
554 |
||
555 |
# Returns the contact name |
|
556 |
def GetName(self): |
|
557 |
return self.Name |
|
558 |
||
559 |
# Changes the contact type |
|
560 |
def SetType(self, type): |
|
561 |
self.Type = type |
|
562 |
self.RefreshTypeSize() |
|
563 |
||
564 |
# Returns the contact type |
|
565 |
def GetType(self): |
|
566 |
return self.Type |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
567 |
|
814 | 568 |
# Method called when a LeftDClick event have been generated |
569 |
def OnLeftDClick(self, event, dc, scaling): |
|
570 |
# Edit the contact properties |
|
571 |
self.Parent.EditContactContent(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
572 |
|
814 | 573 |
# Method called when a RightUp event have been generated |
574 |
def OnRightUp(self, event, dc, scaling): |
|
575 |
# Popup the default menu |
|
576 |
self.Parent.PopupDefaultMenu() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
577 |
|
814 | 578 |
# Refreshes the contact model |
579 |
def RefreshModel(self, move=True): |
|
580 |
self.Parent.RefreshContactModel(self) |
|
581 |
# If contact has moved, refresh the model of wires connected to output |
|
582 |
if move: |
|
583 |
self.Output.RefreshWires() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
584 |
|
814 | 585 |
# Draws the highlightment of this element if it is highlighted |
586 |
def DrawHighlightment(self, dc): |
|
587 |
scalex, scaley = dc.GetUserScale() |
|
588 |
dc.SetUserScale(1, 1) |
|
589 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
590 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
591 |
dc.SetLogicalFunction(wx.AND) |
|
592 |
# Draw two rectangles for representing the contact |
|
593 |
left_left = (self.Pos.x - 1) * scalex - 2 |
|
594 |
right_left = (self.Pos.x + self.Size[0] - 2) * scalex - 2 |
|
595 |
top = (self.Pos.y - 1) * scaley - 2 |
|
596 |
width = 4 * scalex + 5 |
|
597 |
height = (self.Size[1] + 3) * scaley + 5 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
598 |
|
814 | 599 |
dc.DrawRectangle(left_left, top, width, height) |
600 |
dc.DrawRectangle(right_left, top, width, height) |
|
601 |
dc.SetLogicalFunction(wx.COPY) |
|
602 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
603 |
|
814 | 604 |
# Adds an highlight to the connection |
605 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
606 |
highlights = self.Highlights.setdefault(infos[0], []) |
|
607 |
if infos[0] == "reference": |
|
608 |
if start[0] == 0 and end[0] == 0: |
|
609 |
AddHighlight(highlights, (start, end, highlight_type)) |
|
610 |
else: |
|
611 |
AddHighlight(highlights, ((0, 0), (0, 1), highlight_type)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
612 |
|
814 | 613 |
# Removes an highlight from the connection |
614 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
615 |
highlights = self.Highlights.get(infos[0], []) |
|
616 |
if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: |
|
617 |
self.Highlights.pop(infos[0]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
618 |
|
814 | 619 |
# Removes all the highlights of one particular type from the connection |
620 |
def ClearHighlight(self, highlight_type=None): |
|
621 |
if highlight_type is None: |
|
622 |
self.Highlights = {} |
|
623 |
else: |
|
624 |
highlight_items = self.Highlights.items() |
|
625 |
for name, highlights in highlight_items: |
|
1872
866fb3ab8778
fix pylint error "(undefined-variable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1852
diff
changeset
|
626 |
highlights = ClearHighlights(highlights, highlight_type) |
814 | 627 |
if len(highlights) == 0: |
628 |
self.Highlights.pop(name) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
629 |
|
814 | 630 |
# Draws contact |
631 |
def Draw(self, dc): |
|
632 |
Graphic_Element.Draw(self, dc) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
633 |
if self.Value is not None: |
814 | 634 |
if self.Type == CONTACT_NORMAL and self.Value or \ |
635 |
self.Type == CONTACT_REVERSE and not self.Value or \ |
|
636 |
self.Type == CONTACT_RISING and self.Value and not self.PreviousValue or \ |
|
637 |
self.Type == CONTACT_RISING and not self.Value and self.PreviousValue: |
|
638 |
if self.Forced: |
|
639 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
640 |
else: |
|
641 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
642 |
elif self.Forced: |
|
643 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
644 |
else: |
|
645 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
646 |
else: |
|
647 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
648 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
649 |
|
814 | 650 |
# Compiling contact type modifier symbol |
651 |
typetext = "" |
|
652 |
if self.Type == CONTACT_REVERSE: |
|
653 |
typetext = "/" |
|
654 |
elif self.Type == CONTACT_RISING: |
|
655 |
typetext = "P" |
|
656 |
elif self.Type == CONTACT_FALLING: |
|
657 |
typetext = "N" |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
658 |
|
814 | 659 |
if getattr(dc, "printing", False): |
660 |
name_size = dc.GetTextExtent(self.Name) |
|
661 |
if typetext != "": |
|
662 |
type_size = dc.GetTextExtent(typetext) |
|
663 |
else: |
|
664 |
name_size = self.NameSize |
|
665 |
if typetext != "": |
|
666 |
type_size = self.TypeSize |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
667 |
|
814 | 668 |
# Draw two rectangles for representing the contact |
669 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) |
|
670 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) |
|
671 |
# Draw contact name |
|
672 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
|
673 |
self.Pos.y - (name_size[1] + 2)) |
|
674 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
|
675 |
# Draw the modifier symbol in the middle of contact |
|
676 |
if typetext != "": |
|
677 |
type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, |
|
678 |
self.Pos.y + (self.Size[1] - type_size[1]) / 2) |
|
679 |
dc.DrawText(typetext, type_pos[0], type_pos[1]) |
|
680 |
# Draw input and output connectors |
|
681 |
self.Input.Draw(dc) |
|
682 |
self.Output.Draw(dc) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
683 |
|
814 | 684 |
if not getattr(dc, "printing", False): |
685 |
for name, highlights in self.Highlights.iteritems(): |
|
686 |
if name == "reference": |
|
687 |
DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1]) |
|
688 |
elif typetext != "": |
|
689 |
DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1]) |
|
690 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
691 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
692 |
# ------------------------------------------------------------------------------- |
814 | 693 |
# Ladder Diagram Coil |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
694 |
# ------------------------------------------------------------------------------- |
814 | 695 |
|
696 |
||
697 |
class LD_Coil(Graphic_Element): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
698 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
699 |
Class that implements the graphic representation of a coil |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
700 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
701 |
|
814 | 702 |
# Create a new coil |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
703 |
def __init__(self, parent, type, name, id=None): |
814 | 704 |
Graphic_Element.__init__(self, parent) |
705 |
self.Type = type |
|
706 |
self.Name = name |
|
707 |
self.Id = id |
|
708 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
|
709 |
self.Highlights = {} |
|
710 |
# Create an input and output connector |
|
711 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
|
712 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
|
713 |
self.Value = None |
|
714 |
self.PreviousValue = False |
|
715 |
self.RefreshNameSize() |
|
716 |
self.RefreshTypeSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
717 |
|
814 | 718 |
def Flush(self): |
719 |
if self.Input is not None: |
|
720 |
self.Input.Flush() |
|
721 |
self.Input = None |
|
722 |
if self.Output is not None: |
|
723 |
self.Output.Flush() |
|
724 |
self.Output = None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
725 |
|
814 | 726 |
def SpreadCurrent(self): |
727 |
if self.Parent.Debug: |
|
728 |
self.PreviousValue = self.Value |
|
729 |
self.Value = self.Input.ReceivingCurrent() |
|
730 |
if self.Value and not self.PreviousValue: |
|
731 |
self.Output.SpreadCurrent(True) |
|
732 |
elif not self.Value and self.PreviousValue: |
|
733 |
self.Output.SpreadCurrent(False) |
|
734 |
if self.Value != self.PreviousValue and self.Visible: |
|
735 |
self.Parent.ElementNeedRefresh(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
736 |
|
814 | 737 |
# Make a clone of this LD_Coil |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
738 |
def Clone(self, parent, id=None, pos=None): |
814 | 739 |
coil = LD_Coil(parent, self.Type, self.Name, id) |
740 |
coil.SetSize(self.Size[0], self.Size[1]) |
|
741 |
if pos is not None: |
|
742 |
coil.SetPosition(pos.x, pos.y) |
|
743 |
else: |
|
744 |
coil.SetPosition(self.Pos.x, self.Pos.y) |
|
745 |
coil.Input = self.Input.Clone(coil) |
|
746 |
coil.Output = self.Output.Clone(coil) |
|
747 |
return coil |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
748 |
|
814 | 749 |
def GetConnectorTranslation(self, element): |
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
750 |
return {self.Input: element.Input, self.Output: element.Output} |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
751 |
|
814 | 752 |
# Returns the RedrawRect |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
753 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 754 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
755 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
756 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
757 |
if movex != 0 or movey != 0: |
|
758 |
if self.Input.IsConnected(): |
|
759 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
760 |
if self.Output.IsConnected(): |
|
761 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
762 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
763 |
|
814 | 764 |
def ProcessDragging(self, movex, movey, event, scaling): |
765 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
766 |
movex = movey = 0 |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
767 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac=2) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
768 |
|
814 | 769 |
# Forbids to change the Coil size |
770 |
def SetSize(self, width, height): |
|
771 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
772 |
Graphic_Element.SetSize(self, width, height) |
|
773 |
self.RefreshConnectors() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
774 |
|
814 | 775 |
# Delete this coil by calling the appropriate method |
776 |
def Delete(self): |
|
777 |
self.Parent.DeleteCoil(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
778 |
|
814 | 779 |
# Unconnect input and output |
780 |
def Clean(self): |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
781 |
self.Input.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
782 |
self.Output.UnConnect(delete=self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
783 |
|
814 | 784 |
# Refresh the size of text for name |
785 |
def RefreshNameSize(self): |
|
786 |
if self.Name != "": |
|
787 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
|
788 |
else: |
|
789 |
self.NameSize = 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
790 |
|
814 | 791 |
# Refresh the size of text for type |
792 |
def RefreshTypeSize(self): |
|
793 |
typetext = "" |
|
794 |
if self.Type == COIL_REVERSE: |
|
795 |
typetext = "/" |
|
796 |
elif self.Type == COIL_SET: |
|
797 |
typetext = "S" |
|
798 |
elif self.Type == COIL_RESET: |
|
799 |
typetext = "R" |
|
800 |
elif self.Type == COIL_RISING: |
|
801 |
typetext = "P" |
|
802 |
elif self.Type == COIL_FALLING: |
|
803 |
typetext = "N" |
|
804 |
if typetext != "": |
|
805 |
self.TypeSize = self.Parent.GetTextExtent(typetext) |
|
806 |
else: |
|
807 |
self.TypeSize = 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
808 |
|
814 | 809 |
# Refresh the coil bounding box |
810 |
def RefreshBoundingBox(self): |
|
811 |
# Calculate the size of the name outside the coil |
|
812 |
text_width, text_height = self.Parent.GetTextExtent(self.Name) |
|
813 |
# Calculate the bounding box size |
|
814 |
if self.Name != "": |
|
815 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
816 |
bbx_width = max(self.Size[0], text_width) |
|
817 |
bbx_y = self.Pos.y - (text_height + 2) |
|
818 |
bbx_height = self.Size[1] + (text_height + 2) |
|
819 |
else: |
|
820 |
bbx_x = self.Pos.x |
|
821 |
bbx_width = self.Size[0] |
|
822 |
bbx_y = self.Pos.y |
|
823 |
bbx_height = self.Size[1] |
|
824 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
825 |
|
814 | 826 |
# Returns the block minimum size |
827 |
def GetMinSize(self): |
|
828 |
return LD_ELEMENT_SIZE |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
829 |
|
814 | 830 |
# Refresh the position of wire connected to coil |
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
831 |
def RefreshConnected(self, exclude=None): |
814 | 832 |
self.Input.MoveConnected(exclude) |
833 |
self.Output.MoveConnected(exclude) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
834 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
835 |
# Returns the coil connector that starts with the point given if it exists |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
836 |
def GetConnector(self, position, name=None): |
814 | 837 |
# if a name is given |
838 |
if name is not None: |
|
839 |
# Test input and output connector |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
840 |
# if self.Input and name == self.Input.GetName(): |
814 | 841 |
# return self.Input |
842 |
if self.Output and name == self.Output.GetName(): |
|
843 |
return self.Output |
|
844 |
return self.FindNearestConnector(position, [self.Input, self.Output]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
845 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
846 |
# Returns input and output coil connectors |
814 | 847 |
def GetConnectors(self): |
848 |
return {"inputs": [self.Input], "outputs": [self.Output]} |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
849 |
|
814 | 850 |
# Test if point given is on coil input or output connector |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
851 |
def TestConnector(self, pt, direction=None, exclude=True): |
814 | 852 |
# Test input connector |
853 |
if self.Input.TestPoint(pt, direction, exclude): |
|
854 |
return self.Input |
|
855 |
# Test output connector |
|
856 |
if self.Output.TestPoint(pt, direction, exclude): |
|
857 |
return self.Output |
|
858 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
859 |
|
814 | 860 |
# Refresh the positions of the block connectors |
861 |
def RefreshConnectors(self): |
|
862 |
scaling = self.Parent.GetScaling() |
|
863 |
position = self.Size[1] / 2 + 1 |
|
864 |
if scaling is not None: |
|
865 |
position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
866 |
self.Input.SetPosition(wx.Point(0, position)) |
|
867 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
|
868 |
self.RefreshConnected() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
869 |
|
814 | 870 |
# Changes the coil name |
871 |
def SetName(self, name): |
|
872 |
self.Name = name |
|
873 |
self.RefreshNameSize() |
|
874 |
||
875 |
# Returns the coil name |
|
876 |
def GetName(self): |
|
877 |
return self.Name |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
878 |
|
814 | 879 |
# Changes the coil type |
880 |
def SetType(self, type): |
|
881 |
self.Type = type |
|
882 |
self.RefreshTypeSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
883 |
|
814 | 884 |
# Returns the coil type |
885 |
def GetType(self): |
|
886 |
return self.Type |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
887 |
|
814 | 888 |
# Method called when a LeftDClick event have been generated |
889 |
def OnLeftDClick(self, event, dc, scaling): |
|
890 |
# Edit the coil properties |
|
891 |
self.Parent.EditCoilContent(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
892 |
|
814 | 893 |
# Method called when a RightUp event have been generated |
894 |
def OnRightUp(self, event, dc, scaling): |
|
895 |
# Popup the default menu |
|
896 |
self.Parent.PopupDefaultMenu() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
897 |
|
814 | 898 |
# Refreshes the coil model |
899 |
def RefreshModel(self, move=True): |
|
900 |
self.Parent.RefreshCoilModel(self) |
|
901 |
# If coil has moved, refresh the model of wires connected to output |
|
902 |
if move: |
|
903 |
self.Output.RefreshWires() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
904 |
|
814 | 905 |
# Draws the highlightment of this element if it is highlighted |
906 |
def DrawHighlightment(self, dc): |
|
907 |
scalex, scaley = dc.GetUserScale() |
|
908 |
dc.SetUserScale(1, 1) |
|
909 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR, (3 * scalex + 5), wx.SOLID)) |
|
910 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
911 |
dc.SetLogicalFunction(wx.AND) |
|
912 |
# Draw a two circle arcs for representing the coil |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
913 |
dc.DrawEllipticArc(round(self.Pos.x * scalex), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
914 |
round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
915 |
round(self.Size[0] * scalex), |
814 | 916 |
round((int(self.Size[1] * sqrt(2)) - 1) * scaley), |
917 |
135, 225) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
918 |
dc.DrawEllipticArc(round(self.Pos.x * scalex), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
919 |
round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
920 |
round(self.Size[0] * scalex), |
814 | 921 |
round((int(self.Size[1] * sqrt(2)) - 1) * scaley), |
922 |
-45, 45) |
|
923 |
dc.SetLogicalFunction(wx.COPY) |
|
924 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
925 |
|
814 | 926 |
# Adds an highlight to the connection |
927 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
928 |
highlights = self.Highlights.setdefault(infos[0], []) |
|
929 |
if infos[0] == "reference": |
|
930 |
if start[0] == 0 and end[0] == 0: |
|
931 |
AddHighlight(highlights, (start, end, highlight_type)) |
|
932 |
else: |
|
933 |
AddHighlight(highlights, ((0, 0), (0, 1), highlight_type)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
934 |
|
814 | 935 |
# Removes an highlight from the connection |
936 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
937 |
highlights = self.Highlights.get(infos[0], []) |
|
938 |
if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: |
|
939 |
self.Highlights.pop(infos[0]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
940 |
|
814 | 941 |
# Removes all the highlights of one particular type from the connection |
942 |
def ClearHighlight(self, highlight_type=None): |
|
943 |
if highlight_type is None: |
|
944 |
self.Highlights = {} |
|
945 |
else: |
|
946 |
highlight_items = self.Highlights.items() |
|
947 |
for name, highlights in highlight_items: |
|
1872
866fb3ab8778
fix pylint error "(undefined-variable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1852
diff
changeset
|
948 |
highlights = ClearHighlights(highlights, highlight_type) |
814 | 949 |
if len(highlights) == 0: |
950 |
self.Highlights.pop(name) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
951 |
|
814 | 952 |
# Draws coil |
953 |
def Draw(self, dc): |
|
954 |
Graphic_Element.Draw(self, dc) |
|
955 |
if self.Value is not None and self.Value: |
|
956 |
dc.SetPen(MiterPen(wx.GREEN, 2, wx.SOLID)) |
|
957 |
else: |
|
958 |
dc.SetPen(MiterPen(wx.BLACK, 2, wx.SOLID)) |
|
959 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
960 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
961 |
# Compiling coil type modifier symbol |
814 | 962 |
typetext = "" |
963 |
if self.Type == COIL_REVERSE: |
|
964 |
typetext = "/" |
|
965 |
elif self.Type == COIL_SET: |
|
966 |
typetext = "S" |
|
967 |
elif self.Type == COIL_RESET: |
|
968 |
typetext = "R" |
|
969 |
elif self.Type == COIL_RISING: |
|
970 |
typetext = "P" |
|
971 |
elif self.Type == COIL_FALLING: |
|
972 |
typetext = "N" |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
973 |
|
814 | 974 |
if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC): |
975 |
# Draw an clipped ellipse for representing the coil |
|
976 |
clipping_box = dc.GetClippingBox() |
|
977 |
dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1) |
|
978 |
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) |
|
979 |
dc.DestroyClippingRegion() |
|
980 |
if clipping_box != (0, 0, 0, 0): |
|
981 |
dc.SetClippingRegion(*clipping_box) |
|
982 |
name_size = dc.GetTextExtent(self.Name) |
|
983 |
if typetext != "": |
|
984 |
type_size = dc.GetTextExtent(typetext) |
|
985 |
else: |
|
986 |
# Draw a two ellipse arcs for representing the coil |
|
987 |
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) |
|
988 |
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) |
|
989 |
# Draw a point to avoid hole in left arc |
|
990 |
if not getattr(dc, "printing", False): |
|
991 |
if self.Value is not None and self.Value: |
|
992 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
993 |
else: |
|
994 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
995 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) |
|
996 |
name_size = self.NameSize |
|
997 |
if typetext != "": |
|
998 |
type_size = self.TypeSize |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
999 |
|
814 | 1000 |
# Draw coil name |
1001 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
|
1002 |
self.Pos.y - (name_size[1] + 2)) |
|
1003 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
|
1004 |
# Draw the modifier symbol in the middle of coil |
|
1005 |
if typetext != "": |
|
1006 |
type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1, |
|
1007 |
self.Pos.y + (self.Size[1] - type_size[1]) / 2) |
|
1008 |
dc.DrawText(typetext, type_pos[0], type_pos[1]) |
|
1009 |
# Draw input and output connectors |
|
1010 |
self.Input.Draw(dc) |
|
1011 |
self.Output.Draw(dc) |
|
1012 |
||
1013 |
if not getattr(dc, "printing", False): |
|
1014 |
for name, highlights in self.Highlights.iteritems(): |
|
1015 |
if name == "reference": |
|
1016 |
DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1]) |
|
1017 |
elif typetext != "": |
|
1018 |
DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1]) |