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