author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Sat, 29 Sep 2018 15:38:49 +0300 | |
changeset 2387 | 5cf1d70b5395 |
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
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:
1544
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1828
diff
changeset
|
25 |
|
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
|
26 |
from __future__ import absolute_import |
814 | 27 |
from math import * |
28 |
from types import * |
|
29 |
||
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1828
diff
changeset
|
30 |
import wx |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
31 |
from graphics.ToolTipProducer import ToolTipProducer |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
32 |
from graphics.DebugDataConsumer import DebugDataConsumer |
1169
53e4a2b775a7
Move CustomToolTip from GraphicCommons to controls
Laurent Bessard
parents:
1166
diff
changeset
|
33 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
34 |
# ------------------------------------------------------------------------------- |
814 | 35 |
# Common constants |
1837
c507c363625e
fix pylint warning '(pointless-string-statement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
36 |
# |
c507c363625e
fix pylint warning '(pointless-string-statement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
37 |
# Definition of constants for dimensions of graphic elements |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
38 |
# ------------------------------------------------------------------------------- |
814 | 39 |
|
40 |
# FBD and SFC constants |
|
41 |
MIN_MOVE = 5 # Minimum move before starting a element dragging |
|
42 |
CONNECTOR_SIZE = 8 # Size of connectors |
|
43 |
BLOCK_LINE_SIZE = 20 # Minimum size of each line in a block |
|
44 |
HANDLE_SIZE = 6 # Size of the squares for handles |
|
45 |
ANCHOR_DISTANCE = 5 # Distance where wire is automativally attached to a connector |
|
46 |
POINT_RADIUS = 2 # Radius of the point of wire ends |
|
47 |
MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire |
|
48 |
||
49 |
# LD constants |
|
50 |
LD_LINE_SIZE = 40 # Distance between two lines in a ladder rung |
|
51 |
LD_ELEMENT_SIZE = (21, 15) # Size (width, height) of a ladder element (contact or coil) |
|
52 |
LD_WIRE_SIZE = 30 # Size of a wire between two contact |
|
53 |
LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact |
|
54 |
LD_POWERRAIL_WIDTH = 3 # Width of a Powerrail |
|
55 |
LD_OFFSET = (10, 10) # Distance (x, y) between each comment and rung of the ladder |
|
56 |
LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box |
|
57 |
||
58 |
# SFC constants |
|
59 |
SFC_STEP_DEFAULT_SIZE = (40, 30) # Default size of a SFC step |
|
60 |
SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition |
|
61 |
SFC_DEFAULT_SEQUENCE_INTERVAL = 40 # Default size of the interval between two divergence branches |
|
62 |
SFC_SIMULTANEOUS_SEQUENCE_EXTRA = 20 # Size of extra lines for simultaneous divergence and convergence |
|
63 |
SFC_JUMP_SIZE = (12, 13) # Size of a SFC jump to step |
|
64 |
SFC_WIRE_MIN_SIZE = 25 # Size of a wire between two elements |
|
65 |
SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line |
|
66 |
||
67 |
# Type definition constants for graphic elements |
|
68 |
[INPUT, OUTPUT, INOUT] = range(3) |
|
69 |
[CONNECTOR, CONTINUATION] = range(2) |
|
70 |
[LEFTRAIL, RIGHTRAIL] = range(2) |
|
71 |
[CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) |
|
72 |
[COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET, COIL_RISING, COIL_FALLING] = range(6) |
|
73 |
[SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) |
|
74 |
||
75 |
# Constants for defining the type of dragging that has been selected |
|
76 |
[HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) |
|
77 |
||
78 |
# List of value for resize handle that are valid |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
79 |
VALID_HANDLES = [(1, 1), (1, 2), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1), (2, 1)] |
814 | 80 |
|
81 |
# Contants for defining the direction of a connector |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
82 |
[EAST, NORTH, WEST, SOUTH] = [(1, 0), (0, -1), (-1, 0), (0, 1)] |
814 | 83 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
84 |
# Contants for defining which mode is selected for each view |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
85 |
[MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
86 |
MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, |
814 | 87 |
MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION, MODE_MOTION] = range(15) |
88 |
||
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
89 |
# Contants for defining alignment types for graphic group |
814 | 90 |
[ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6) |
91 |
||
92 |
# Contants for defining which drawing mode is selected for app |
|
93 |
[FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] |
|
94 |
||
95 |
# Color for Highlighting |
|
96 |
HIGHLIGHTCOLOR = wx.CYAN |
|
97 |
||
98 |
# Define highlight types |
|
99 |
ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED) |
|
100 |
SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE) |
|
101 |
||
102 |
# Define highlight refresh inhibition period in second |
|
103 |
REFRESH_HIGHLIGHT_PERIOD = 0.1 |
|
104 |
||
105 |
HANDLE_CURSORS = { |
|
1739
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
106 |
(1, 1): 2, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
107 |
(3, 3): 2, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
108 |
(1, 3): 3, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
109 |
(3, 1): 3, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
110 |
(1, 2): 4, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
111 |
(3, 2): 4, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
112 |
(2, 1): 5, |
ec153828ded2
clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
113 |
(2, 3): 5 |
814 | 114 |
} |
115 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
116 |
|
814 | 117 |
def round_scaling(x, n, constraint=0): |
118 |
fraction = float(x) / float(n) |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
119 |
if constraint == -1: |
814 | 120 |
xround = int(fraction) |
121 |
else: |
|
122 |
xround = round(fraction) |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
123 |
if constraint == 1 and xround < fraction: |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
124 |
xround += 1 |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
125 |
return int(xround * n) |
814 | 126 |
|
1749
d73b64672238
clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
127 |
|
1837
c507c363625e
fix pylint warning '(pointless-string-statement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
128 |
# ------------------------------------------------------------------------------- |
c507c363625e
fix pylint warning '(pointless-string-statement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
129 |
# Basic vector operations for calculate wire points |
c507c363625e
fix pylint warning '(pointless-string-statement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
130 |
# ------------------------------------------------------------------------------- |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
131 |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
132 |
def vector(p1, p2, normal=True): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
133 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
134 |
Create a vector from two points and define if vector must be normal |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
135 |
""" |
814 | 136 |
vector = (p2.x - p1.x, p2.y - p1.y) |
137 |
if normal: |
|
138 |
return normalize(vector) |
|
139 |
return vector |
|
140 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
141 |
|
814 | 142 |
def norm(v): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
143 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
144 |
Calculate the norm of a given vector |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
145 |
""" |
814 | 146 |
return sqrt(v[0] * v[0] + v[1] * v[1]) |
147 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
148 |
|
814 | 149 |
def normalize(v): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
150 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
151 |
Normalize a given vector |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
152 |
""" |
814 | 153 |
v_norm = norm(v) |
154 |
# Verifie if it is not a null vector |
|
155 |
if v_norm > 0: |
|
156 |
return (v[0] / v_norm, v[1] / v_norm) |
|
157 |
else: |
|
158 |
return v |
|
159 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
160 |
|
814 | 161 |
def is_null_vector(v): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
162 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
163 |
Calculate the scalar product of two vectors |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
164 |
""" |
814 | 165 |
return v == (0, 0) |
166 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
167 |
|
814 | 168 |
def add_vectors(v1, v2): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
169 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
170 |
Calculate the scalar product of two vectors |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
171 |
""" |
814 | 172 |
return (v1[0] + v2[0], v1[1] + v2[1]) |
173 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
174 |
|
814 | 175 |
def product(v1, v2): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
176 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
177 |
Calculate the scalar product of two vectors |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
178 |
""" |
814 | 179 |
return v1[0] * v2[0] + v1[1] * v2[1] |
180 |
||
181 |
||
182 |
def GetScaledEventPosition(event, dc, scaling): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
183 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
184 |
Function that calculates the nearest point of the grid defined by scaling for the given point |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
185 |
""" |
814 | 186 |
pos = event.GetLogicalPosition(dc) |
187 |
if scaling: |
|
188 |
pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0] |
|
189 |
pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1] |
|
190 |
return pos |
|
191 |
||
192 |
||
193 |
def DirectionChoice(v_base, v_target, dir_target): |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
194 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
195 |
Function that choose a direction during the wire points generation |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
196 |
""" |
814 | 197 |
dir_product = product(v_base, v_target) |
198 |
if dir_product < 0: |
|
199 |
return (-v_base[0], -v_base[1]) |
|
200 |
elif dir_product == 0 and product(v_base, dir_target) != 0: |
|
201 |
return dir_target |
|
202 |
return v_base |
|
203 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
204 |
|
814 | 205 |
def MiterPen(colour, width=1, style=wx.SOLID): |
206 |
pen = wx.Pen(colour, width, style) |
|
207 |
pen.SetJoin(wx.JOIN_MITER) |
|
208 |
pen.SetCap(wx.CAP_PROJECTING) |
|
209 |
return pen |
|
210 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
211 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
212 |
# ------------------------------------------------------------------------------- |
814 | 213 |
# Helpers for highlighting text |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
214 |
# ------------------------------------------------------------------------------- |
814 | 215 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
216 |
|
814 | 217 |
def AddHighlight(highlights, infos): |
218 |
RemoveHighlight(highlights, infos) |
|
219 |
highlights.append(infos) |
|
220 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
221 |
|
814 | 222 |
def RemoveHighlight(highlights, infos): |
223 |
if infos in highlights: |
|
224 |
highlights.remove(infos) |
|
225 |
return True |
|
226 |
return False |
|
227 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
228 |
|
1872
866fb3ab8778
fix pylint error "(undefined-variable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1870
diff
changeset
|
229 |
def ClearHighlights(highlights, highlight_type=None): |
814 | 230 |
if highlight_type is not None: |
231 |
return [highlight for highlight in highlights if highlight[2] != highlight_type] |
|
232 |
return [] |
|
233 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
234 |
|
814 | 235 |
def DrawHighlightedText(dc, text, highlights, x, y): |
236 |
current_pen = dc.GetPen() |
|
237 |
dc.SetPen(wx.TRANSPARENT_PEN) |
|
238 |
for start, end, highlight_type in highlights: |
|
239 |
dc.SetBrush(wx.Brush(highlight_type[0])) |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
240 |
offset_width, _offset_height = dc.GetTextExtent(text[:start[1]]) |
814 | 241 |
part = text[start[1]:end[1] + 1] |
242 |
part_width, part_height = dc.GetTextExtent(part) |
|
243 |
dc.DrawRectangle(x + offset_width, y, part_width, part_height) |
|
244 |
dc.SetTextForeground(highlight_type[1]) |
|
245 |
dc.DrawText(part, x + offset_width, y) |
|
246 |
dc.SetPen(current_pen) |
|
247 |
dc.SetTextForeground(wx.BLACK) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
248 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
249 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
250 |
# ------------------------------------------------------------------------------- |
814 | 251 |
# Graphic element base class |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
252 |
# ------------------------------------------------------------------------------- |
814 | 253 |
|
254 |
||
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
255 |
class Graphic_Element(ToolTipProducer): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
256 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
257 |
Class that implements a generic graphic element |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
258 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
259 |
|
814 | 260 |
# Create a new graphic element |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
261 |
def __init__(self, parent, id=None): |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
262 |
ToolTipProducer.__init__(self, parent) |
814 | 263 |
self.Parent = parent |
264 |
self.Id = id |
|
265 |
self.oldPos = None |
|
266 |
self.StartPos = None |
|
267 |
self.CurrentDrag = None |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset
|
268 |
self.Handle = (None, None) |
814 | 269 |
self.Dragging = False |
270 |
self.Selected = False |
|
271 |
self.Highlighted = False |
|
272 |
self.Pos = wx.Point(0, 0) |
|
273 |
self.Size = wx.Size(0, 0) |
|
274 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
|
275 |
self.Visible = False |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
276 |
|
814 | 277 |
def GetDefinition(self): |
278 |
return [self.Id], [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
279 |
|
814 | 280 |
def TestVisible(self, screen): |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
281 |
self.Visible = self.Selected or self.GetRedrawRect().Intersects(screen) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
282 |
|
814 | 283 |
def IsVisible(self): |
284 |
return self.Visible |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
285 |
|
814 | 286 |
def SpreadCurrent(self): |
287 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
288 |
|
814 | 289 |
def GetConnectorTranslation(self, element): |
290 |
return {} |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
291 |
|
814 | 292 |
def FindNearestConnector(self, position, connectors): |
293 |
distances = [] |
|
294 |
for connector in connectors: |
|
295 |
connector_pos = connector.GetRelPosition() |
|
296 |
distances.append((sqrt((self.Pos.x + connector_pos.x - position.x) ** 2 + |
|
297 |
(self.Pos.y + connector_pos.y - position.y) ** 2), |
|
298 |
connector)) |
|
299 |
distances.sort() |
|
300 |
if len(distances) > 0: |
|
301 |
return distances[0][1] |
|
302 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
303 |
|
814 | 304 |
def IsOfType(self, type, reference): |
305 |
return self.Parent.IsOfType(type, reference) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
306 |
|
814 | 307 |
def IsEndType(self, type): |
308 |
return self.Parent.IsEndType(type) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
309 |
|
814 | 310 |
def GetDragging(self): |
311 |
return self.Dragging |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
312 |
|
814 | 313 |
# Make a clone of this element |
314 |
def Clone(self, parent): |
|
315 |
return Graphic_Element(parent, self.Id) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
316 |
|
814 | 317 |
# Changes the block position |
318 |
def SetPosition(self, x, y): |
|
319 |
self.Pos.x = x |
|
320 |
self.Pos.y = y |
|
321 |
self.RefreshConnected() |
|
322 |
self.RefreshBoundingBox() |
|
323 |
||
324 |
# Returns the block position |
|
325 |
def GetPosition(self): |
|
326 |
return self.Pos.x, self.Pos.y |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
327 |
|
814 | 328 |
# Changes the element size |
329 |
def SetSize(self, width, height): |
|
330 |
self.Size.SetWidth(width) |
|
331 |
self.Size.SetHeight(height) |
|
332 |
self.RefreshConnectors() |
|
333 |
self.RefreshBoundingBox() |
|
334 |
||
335 |
# Returns the element size |
|
336 |
def GetSize(self): |
|
337 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
338 |
|
814 | 339 |
# Returns the minimum element size |
340 |
def GetMinSize(self): |
|
341 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
342 |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
343 |
# Set size of the element to the minimum size |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
344 |
def SetBestSize(self, scaling, x_factor=0.5, y_factor=0.5): |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
345 |
width, height = self.GetSize() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
346 |
posx, posy = self.GetPosition() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
347 |
min_width, min_height = self.GetMinSize() |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
348 |
if width < min_width: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
349 |
self.Pos.x = max(0, self.Pos.x - (width - min_width) * x_factor) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
350 |
width = min_width |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
351 |
if height < min_height: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
352 |
self.Pos.y = max(0, self.Pos.y - (height - min_height) * y_factor) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
353 |
height = min_height |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
354 |
if scaling is not None: |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
355 |
self.Pos.x = round_scaling(self.Pos.x, scaling[0]) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
356 |
self.Pos.y = round_scaling(self.Pos.y, scaling[1]) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
357 |
width = round_scaling(width, scaling[0], 1) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
358 |
height = round_scaling(height, scaling[1], 1) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
359 |
self.SetSize(width, height) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
360 |
return self.Pos.x - posx, self.Pos.y - posy |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
361 |
|
814 | 362 |
# Refresh the element connectors position |
363 |
def RefreshConnectors(self): |
|
364 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
365 |
|
814 | 366 |
# Refresh the position of wires connected to element inputs and outputs |
1870
4d070115b552
fix pylint error '(too-many-function-args) Too many positional arguments for function call'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1868
diff
changeset
|
367 |
def RefreshConnected(self, exclude=None): |
814 | 368 |
pass |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
369 |
|
814 | 370 |
# Change the parent |
371 |
def SetParent(self, parent): |
|
372 |
self.Parent = parent |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
373 |
|
814 | 374 |
# Override this method for defining the method to call for deleting this element |
375 |
def Delete(self): |
|
376 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
377 |
|
814 | 378 |
# Returns the Id |
379 |
def GetId(self): |
|
380 |
return self.Id |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
381 |
|
814 | 382 |
# Returns if the point given is in the bounding box |
383 |
def HitTest(self, pt, connectors=True): |
|
384 |
if connectors: |
|
385 |
rect = self.BoundingBox |
|
386 |
else: |
|
387 |
rect = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
|
388 |
return rect.InsideXY(pt.x, pt.y) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
389 |
|
814 | 390 |
# Returns if the point given is in the bounding box |
391 |
def IsInSelection(self, rect): |
|
392 |
return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
393 |
|
814 | 394 |
# Override this method for refreshing the bounding box |
395 |
def RefreshBoundingBox(self): |
|
396 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
397 |
|
814 | 398 |
# Returns the bounding box |
399 |
def GetBoundingBox(self): |
|
400 |
return self.BoundingBox |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
401 |
|
814 | 402 |
# 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
|
403 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 404 |
scalex, scaley = self.Parent.GetViewScale() |
405 |
rect = wx.Rect() |
|
406 |
rect.x = self.BoundingBox.x - int(HANDLE_SIZE / scalex) - 3 - abs(movex) |
|
407 |
rect.y = self.BoundingBox.y - int(HANDLE_SIZE / scaley) - 3 - abs(movey) |
|
408 |
rect.width = self.BoundingBox.width + 2 * (int(HANDLE_SIZE / scalex) + abs(movex) + 1) + 4 |
|
409 |
rect.height = self.BoundingBox.height + 2 * (int(HANDLE_SIZE / scaley) + abs(movey) + 1) + 4 |
|
410 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
411 |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
412 |
def Refresh(self, rect=None): |
814 | 413 |
if self.Visible: |
414 |
if rect is not None: |
|
415 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
|
416 |
else: |
|
417 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
418 |
|
814 | 419 |
# Change the variable that indicates if this element is selected |
420 |
def SetSelected(self, selected): |
|
421 |
self.Selected = selected |
|
422 |
self.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
423 |
|
814 | 424 |
# Change the variable that indicates if this element is highlighted |
425 |
def SetHighlighted(self, highlighted): |
|
426 |
self.Highlighted = highlighted |
|
427 |
self.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
428 |
|
814 | 429 |
# Test if the point is on a handle of this element |
430 |
def TestHandle(self, event): |
|
431 |
dc = self.Parent.GetLogicalDC() |
|
432 |
scalex, scaley = dc.GetUserScale() |
|
433 |
pos = event.GetPosition() |
|
434 |
pt = wx.Point(*self.Parent.CalcUnscrolledPosition(pos.x, pos.y)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
435 |
|
814 | 436 |
left = (self.BoundingBox.x - 2) * scalex - HANDLE_SIZE |
437 |
center = (self.BoundingBox.x + self.BoundingBox.width / 2) * scalex - HANDLE_SIZE / 2 |
|
438 |
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
439 |
|
814 | 440 |
top = (self.BoundingBox.y - 2) * scaley - HANDLE_SIZE |
441 |
middle = (self.BoundingBox.y + self.BoundingBox.height / 2) * scaley - HANDLE_SIZE / 2 |
|
442 |
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
443 |
|
814 | 444 |
extern_rect = wx.Rect(left, top, right + HANDLE_SIZE - left, bottom + HANDLE_SIZE - top) |
445 |
intern_rect = wx.Rect(left + HANDLE_SIZE, top + HANDLE_SIZE, right - left - HANDLE_SIZE, bottom - top - HANDLE_SIZE) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
446 |
|
814 | 447 |
# Verify that this element is selected |
448 |
if self.Selected and extern_rect.InsideXY(pt.x, pt.y) and not intern_rect.InsideXY(pt.x, pt.y): |
|
449 |
# Find if point is on a handle horizontally |
|
450 |
if left <= pt.x < left + HANDLE_SIZE: |
|
451 |
handle_x = 1 |
|
452 |
elif center <= pt.x < center + HANDLE_SIZE: |
|
453 |
handle_x = 2 |
|
454 |
elif right <= pt.x < right + HANDLE_SIZE: |
|
455 |
handle_x = 3 |
|
456 |
else: |
|
457 |
handle_x = 0 |
|
458 |
# Find if point is on a handle vertically |
|
459 |
if top <= pt.y < top + HANDLE_SIZE: |
|
460 |
handle_y = 1 |
|
461 |
elif middle <= pt.y < middle + HANDLE_SIZE: |
|
462 |
handle_y = 2 |
|
463 |
elif bottom <= pt.y < bottom + HANDLE_SIZE: |
|
464 |
handle_y = 3 |
|
465 |
else: |
|
466 |
handle_y = 0 |
|
467 |
# Verify that the result is valid |
|
468 |
if (handle_x, handle_y) in VALID_HANDLES: |
|
469 |
return handle_x, handle_y |
|
470 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
471 |
|
814 | 472 |
# Method called when a LeftDown event have been generated |
473 |
def OnLeftDown(self, event, dc, scaling): |
|
474 |
# Test if an handle have been clicked |
|
475 |
handle = self.TestHandle(event) |
|
476 |
# Find which type of handle have been clicked, |
|
477 |
# Save a resize event and change the cursor |
|
478 |
cursor = HANDLE_CURSORS.get(handle, 1) |
|
479 |
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) |
|
480 |
if cursor > 1: |
|
481 |
self.Handle = (HANDLE_RESIZE, handle) |
|
482 |
else: |
|
483 |
self.Handle = (HANDLE_MOVE, None) |
|
484 |
self.SetSelected(False) |
|
485 |
# Initializes the last position |
|
486 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
487 |
self.StartPos = wx.Point(self.Pos.x, self.Pos.y) |
|
488 |
self.CurrentDrag = wx.Point(0, 0) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
489 |
|
814 | 490 |
# Method called when a LeftUp event have been generated |
491 |
def OnLeftUp(self, event, dc, scaling): |
|
492 |
# If a dragging have been initiated |
|
493 |
if self.Dragging and self.oldPos: |
|
494 |
self.RefreshModel() |
|
495 |
self.Parent.RefreshBuffer() |
|
496 |
wx.CallAfter(self.Parent.SetCurrentCursor, 0) |
|
497 |
self.SetSelected(True) |
|
498 |
self.oldPos = None |
|
499 |
||
500 |
# Method called when a RightDown event have been generated |
|
501 |
def OnRightDown(self, event, dc, scaling): |
|
502 |
pass |
|
503 |
||
504 |
# Method called when a RightUp event have been generated |
|
505 |
def OnRightUp(self, event, dc, scaling): |
|
506 |
if self.Dragging and self.oldPos: |
|
507 |
self.RefreshModel() |
|
508 |
self.Parent.RefreshBuffer() |
|
509 |
wx.CallAfter(self.Parent.SetCurrentCursor, 0) |
|
510 |
self.SetSelected(True) |
|
511 |
self.oldPos = None |
|
512 |
if self.Parent.Debug: |
|
513 |
self.Parent.PopupForceMenu() |
|
514 |
||
515 |
# Method called when a LeftDClick event have been generated |
|
516 |
def OnLeftDClick(self, event, dc, scaling): |
|
517 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
518 |
|
814 | 519 |
# Method called when a Motion event have been generated |
520 |
def OnMotion(self, event, dc, scaling): |
|
521 |
# If the cursor is dragging and the element have been clicked |
|
522 |
if event.Dragging() and self.oldPos: |
|
523 |
# Calculate the movement of cursor |
|
1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1226
diff
changeset
|
524 |
pos = GetScaledEventPosition(event, dc, scaling) |
814 | 525 |
movex = pos.x - self.oldPos.x |
526 |
movey = pos.y - self.oldPos.y |
|
527 |
# If movement is greater than MIN_MOVE then a dragging is initiated |
|
528 |
if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): |
|
529 |
self.Dragging = True |
|
530 |
# If a dragging have been initiated, refreshes the element state |
|
531 |
if self.Dragging: |
|
532 |
dragx, dragy = self.ProcessDragging(movex, movey, event, scaling) |
|
533 |
if event.ControlDown() and self.Handle[0] == HANDLE_MOVE: |
|
534 |
self.oldPos.x = self.StartPos.x + self.CurrentDrag.x |
|
535 |
self.oldPos.y = self.StartPos.y + self.CurrentDrag.y |
|
536 |
else: |
|
537 |
self.oldPos.x += dragx |
|
538 |
self.oldPos.y += dragy |
|
539 |
return dragx, dragy |
|
540 |
return movex, movey |
|
541 |
# If cursor just pass over the element, changes the cursor if it is on a handle |
|
542 |
else: |
|
543 |
pos = event.GetLogicalPosition(dc) |
|
544 |
handle = self.TestHandle(event) |
|
545 |
# Find which type of handle have been clicked, |
|
546 |
# Save a resize event and change the cursor |
|
547 |
cursor = HANDLE_CURSORS.get(handle, 0) |
|
548 |
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) |
|
549 |
return 0, 0 |
|
550 |
||
551 |
# Moves the element |
|
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
552 |
def Move(self, dx, dy, exclude=None): |
814 | 553 |
self.Pos.x += max(-self.BoundingBox.x, dx) |
554 |
self.Pos.y += max(-self.BoundingBox.y, dy) |
|
555 |
self.RefreshConnected(exclude) |
|
556 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
557 |
|
814 | 558 |
# Resizes the element from position and size given |
559 |
def Resize(self, x, y, width, height): |
|
560 |
self.Move(x, y) |
|
561 |
self.SetSize(width, height) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
562 |
|
814 | 563 |
# Refreshes the element state according to move defined and handle selected |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
564 |
def ProcessDragging(self, movex, movey, event, scaling, width_fac=1, height_fac=1): |
814 | 565 |
handle_type, handle = self.Handle |
566 |
# If it is a resize handle, calculate the values from resizing |
|
567 |
if handle_type == HANDLE_RESIZE: |
|
568 |
if scaling is not None: |
|
569 |
scaling = (scaling[0] * width_fac, scaling[1] * height_fac) |
|
570 |
x = y = start_x = start_y = 0 |
|
571 |
width, height = start_width, start_height = self.GetSize() |
|
572 |
if handle[0] == 1: |
|
573 |
movex = max(-self.BoundingBox.x, movex) |
|
574 |
if scaling is not None: |
|
575 |
movex = -(round_scaling(width - movex, scaling[0]) - width) |
|
576 |
x = movex |
|
577 |
if event.ShiftDown(): |
|
578 |
width -= 2 * movex |
|
579 |
else: |
|
580 |
width -= movex |
|
581 |
elif handle[0] == 3: |
|
582 |
if scaling is not None: |
|
583 |
movex = round_scaling(width + movex, scaling[0]) - width |
|
584 |
if event.ShiftDown(): |
|
585 |
movex = min(self.BoundingBox.x, movex) |
|
586 |
x = -movex |
|
587 |
width += 2 * movex |
|
588 |
else: |
|
589 |
width += movex |
|
590 |
if handle[1] == 1: |
|
591 |
movey = max(-self.BoundingBox.y, movey) |
|
592 |
if scaling is not None: |
|
593 |
movey = -(round_scaling(height - movey, scaling[1]) - height) |
|
594 |
y = movey |
|
595 |
if event.ShiftDown(): |
|
596 |
height -= 2 * movey |
|
597 |
else: |
|
598 |
height -= movey |
|
599 |
elif handle[1] == 3: |
|
600 |
if scaling is not None: |
|
601 |
movey = round_scaling(height + movey, scaling[1]) - height |
|
602 |
if event.ShiftDown(): |
|
603 |
movey = min(self.BoundingBox.y, movey) |
|
604 |
y = -movey |
|
605 |
height += 2 * movey |
|
606 |
else: |
|
607 |
height += movey |
|
608 |
# Verify that new size is not lesser than minimum |
|
609 |
min_width, min_height = self.GetMinSize() |
|
610 |
if handle[0] != 2 and (width >= min_width or width > self.Size[0]): |
|
611 |
start_x = x |
|
612 |
start_width = width |
|
613 |
else: |
|
614 |
movex = 0 |
|
615 |
if handle[1] != 2 and (height >= min_height or height > self.Size[1]): |
|
616 |
start_y = y |
|
617 |
start_height = height |
|
618 |
else: |
|
619 |
movey = 0 |
|
620 |
if movex != 0 or movey != 0: |
|
621 |
self.Resize(start_x, start_y, start_width, start_height) |
|
622 |
return movex, movey |
|
623 |
# If it is a move handle, Move this element |
|
624 |
elif handle_type == HANDLE_MOVE: |
|
625 |
movex = max(-self.BoundingBox.x, movex) |
|
626 |
movey = max(-self.BoundingBox.y, movey) |
|
627 |
if scaling is not None: |
|
628 |
movex = round_scaling(self.Pos.x + movex, scaling[0]) - self.Pos.x |
|
629 |
movey = round_scaling(self.Pos.y + movey, scaling[1]) - self.Pos.y |
|
630 |
if event.ControlDown(): |
|
631 |
self.CurrentDrag.x = self.CurrentDrag.x + movex |
|
632 |
self.CurrentDrag.y = self.CurrentDrag.y + movey |
|
633 |
if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y): |
|
634 |
movex = self.StartPos.x + self.CurrentDrag.x - self.Pos.x |
|
635 |
movey = self.StartPos.y - self.Pos.y |
|
636 |
else: |
|
637 |
movex = self.StartPos.x - self.Pos.x |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
638 |
movey = self.StartPos.y + self.CurrentDrag.y - self.Pos.y |
814 | 639 |
self.Move(movex, movey) |
640 |
return movex, movey |
|
641 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
642 |
|
814 | 643 |
# Override this method for defining the method to call for adding an highlight to this element |
644 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
645 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
646 |
|
814 | 647 |
# Override this method for defining the method to call for removing an highlight from this element |
648 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
649 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
650 |
|
814 | 651 |
# Override this method for defining the method to call for removing all the highlights of one particular type from this element |
652 |
def ClearHighlight(self, highlight_type=None): |
|
653 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
654 |
|
814 | 655 |
# Override this method for defining the method to call for refreshing the model of this element |
656 |
def RefreshModel(self, move=True): |
|
657 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
658 |
|
814 | 659 |
# Draws the highlightment of this element if it is highlighted (can be overwritten) |
660 |
def DrawHighlightment(self, dc): |
|
661 |
scalex, scaley = dc.GetUserScale() |
|
662 |
dc.SetUserScale(1, 1) |
|
663 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
664 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
665 |
dc.SetLogicalFunction(wx.AND) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
666 |
dc.DrawRectangle(int(round((self.Pos.x - 1) * scalex)) - 2, |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
667 |
int(round((self.Pos.y - 1) * scaley)) - 2, |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
668 |
int(round((self.Size.width + 3) * scalex)) + 5, |
814 | 669 |
int(round((self.Size.height + 3) * scaley)) + 5) |
670 |
dc.SetLogicalFunction(wx.COPY) |
|
671 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
672 |
|
814 | 673 |
# Draws the handles of this element if it is selected |
674 |
def Draw(self, dc): |
|
675 |
if not getattr(dc, "printing", False): |
|
676 |
if self.Highlighted: |
|
677 |
self.DrawHighlightment(dc) |
|
678 |
if self.Selected: |
|
679 |
scalex, scaley = dc.GetUserScale() |
|
680 |
dc.SetUserScale(1, 1) |
|
681 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
682 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
683 |
|
814 | 684 |
left = (self.BoundingBox.x - 2) * scalex - HANDLE_SIZE |
685 |
center = (self.BoundingBox.x + self.BoundingBox.width / 2) * scalex - HANDLE_SIZE / 2 |
|
686 |
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
687 |
|
814 | 688 |
top = (self.BoundingBox.y - 2) * scaley - HANDLE_SIZE |
689 |
middle = (self.BoundingBox.y + self.BoundingBox.height / 2) * scaley - HANDLE_SIZE / 2 |
|
690 |
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
691 |
|
814 | 692 |
for x, y in [(left, top), (center, top), (right, top), |
693 |
(left, middle), (right, middle), |
|
694 |
(left, bottom), (center, bottom), (right, bottom)]: |
|
695 |
dc.DrawRectangle(x, y, HANDLE_SIZE, HANDLE_SIZE) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
696 |
|
814 | 697 |
dc.SetUserScale(scalex, scaley) |
698 |
||
699 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
700 |
# ------------------------------------------------------------------------------- |
814 | 701 |
# Group of graphic elements |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
702 |
# ------------------------------------------------------------------------------- |
814 | 703 |
|
704 |
||
705 |
class Graphic_Group(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
|
706 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
707 |
Class that implements a group of graphic elements |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
708 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
709 |
|
814 | 710 |
# Create a new group of graphic elements |
711 |
def __init__(self, parent): |
|
712 |
Graphic_Element.__init__(self, parent) |
|
713 |
self.Elements = [] |
|
714 |
self.RefreshWireExclusion() |
|
715 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
716 |
|
814 | 717 |
# Destructor |
718 |
def __del__(self): |
|
719 |
self.Elements = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
720 |
|
814 | 721 |
def GetDefinition(self): |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
722 |
blocks = [] |
814 | 723 |
wires = [] |
724 |
for element in self.Elements: |
|
725 |
block, wire = element.GetDefinition() |
|
726 |
blocks.extend(block) |
|
727 |
wires.extend(wire) |
|
728 |
return blocks, wires |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
729 |
|
814 | 730 |
# Make a clone of this element |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
731 |
def Clone(self, parent, pos=None): |
814 | 732 |
group = Graphic_Group(parent) |
733 |
connectors = {} |
|
734 |
exclude_names = {} |
|
735 |
wires = [] |
|
736 |
if pos is not None: |
|
737 |
dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y |
|
738 |
for element in self.Elements: |
|
739 |
if isinstance(element, Wire): |
|
740 |
wires.append(element) |
|
741 |
else: |
|
742 |
if pos is not None: |
|
743 |
x, y = element.GetPosition() |
|
744 |
new_pos = wx.Point(x + dx, y + dy) |
|
745 |
newid = parent.GetNewId() |
|
746 |
if parent.IsNamedElement(element): |
|
747 |
name = parent.GenerateNewName(element, exclude_names) |
|
748 |
exclude_names[name.upper()] = True |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
749 |
new_element = element.Clone(parent, newid, name, pos=new_pos) |
814 | 750 |
else: |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
751 |
new_element = element.Clone(parent, newid, pos=new_pos) |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
752 |
new_element.SetBestSize(parent.Scaling) |
814 | 753 |
else: |
754 |
new_element = element.Clone(parent) |
|
755 |
connectors.update(element.GetConnectorTranslation(new_element)) |
|
756 |
group.SelectElement(new_element) |
|
757 |
for element in wires: |
|
758 |
if pos is not None: |
|
759 |
new_wire = element.Clone(parent, connectors, dx, dy) |
|
760 |
else: |
|
761 |
new_wire = element.Clone(parent, connectors) |
|
762 |
if new_wire is not None: |
|
763 |
if pos is not None: |
|
764 |
parent.AddWire(new_wire) |
|
765 |
group.SelectElement(new_wire) |
|
766 |
if pos is not None: |
|
767 |
for element in group.Elements: |
|
768 |
if not isinstance(element, Wire): |
|
769 |
parent.AddBlockInModel(element) |
|
770 |
return group |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
771 |
|
814 | 772 |
def CanAddBlocks(self, parent): |
773 |
valid = True |
|
774 |
for element in self.Elements: |
|
775 |
if not isinstance(element, Wire): |
|
776 |
valid &= parent.CanAddElement(element) |
|
777 |
return valid |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
778 |
|
814 | 779 |
def IsVisible(self): |
780 |
for element in self.Elements: |
|
781 |
if element.IsVisible(): |
|
782 |
return True |
|
783 |
return False |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
784 |
|
814 | 785 |
# Refresh the list of wire excluded |
786 |
def RefreshWireExclusion(self): |
|
787 |
self.WireExcluded = [] |
|
788 |
for element in self.Elements: |
|
789 |
if isinstance(element, Wire): |
|
790 |
startblock = element.StartConnected.GetParentBlock() |
|
791 |
endblock = element.EndConnected.GetParentBlock() |
|
792 |
if startblock in self.Elements and endblock in self.Elements: |
|
793 |
self.WireExcluded.append(element) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
794 |
|
814 | 795 |
# 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
|
796 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 797 |
rect = None |
798 |
for element in self.Elements: |
|
799 |
if rect is None: |
|
800 |
rect = element.GetRedrawRect(movex, movey) |
|
801 |
else: |
|
802 |
rect = rect.Union(element.GetRedrawRect(movex, movey)) |
|
803 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
804 |
|
814 | 805 |
# Clean this group of elements |
806 |
def Clean(self): |
|
807 |
# Clean all the elements of the group |
|
808 |
for element in self.Elements: |
|
809 |
element.Clean() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
810 |
|
814 | 811 |
# Delete this group of elements |
812 |
def Delete(self): |
|
813 |
# Delete all the elements of the group |
|
814 |
for element in self.Elements: |
|
815 |
element.Delete() |
|
816 |
self.WireExcluded = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
817 |
|
814 | 818 |
# Returns if the point given is in the bounding box of one of the elements of this group |
819 |
def HitTest(self, pt, connectors=True): |
|
820 |
result = False |
|
821 |
for element in self.Elements: |
|
822 |
result |= element.HitTest(pt, connectors) |
|
823 |
return result |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
824 |
|
814 | 825 |
# Returns if the element given is in this group |
826 |
def IsElementIn(self, element): |
|
827 |
return element in self.Elements |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
828 |
|
814 | 829 |
# Change the elements of the group |
830 |
def SetElements(self, elements): |
|
831 |
self.Elements = elements |
|
832 |
self.RefreshWireExclusion() |
|
833 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
834 |
|
814 | 835 |
# Returns the elements of the group |
836 |
def GetElements(self): |
|
837 |
return self.Elements |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
838 |
|
814 | 839 |
# Align the group elements |
840 |
def AlignElements(self, horizontally, vertically): |
|
841 |
minx = self.BoundingBox.x + self.BoundingBox.width |
|
842 |
miny = self.BoundingBox.y + self.BoundingBox.height |
|
843 |
maxx = self.BoundingBox.x |
|
844 |
maxy = self.BoundingBox.y |
|
845 |
for element in self.Elements: |
|
846 |
if not isinstance(element, Wire): |
|
847 |
posx, posy = element.GetPosition() |
|
848 |
width, height = element.GetSize() |
|
849 |
minx = min(minx, posx) |
|
850 |
miny = min(miny, posy) |
|
851 |
maxx = max(maxx, posx + width) |
|
852 |
maxy = max(maxy, posy + height) |
|
853 |
for element in self.Elements: |
|
854 |
if not isinstance(element, Wire): |
|
855 |
posx, posy = element.GetPosition() |
|
856 |
width, height = element.GetSize() |
|
857 |
movex = movey = 0 |
|
858 |
if horizontally == ALIGN_LEFT: |
|
859 |
movex = minx - posx |
|
860 |
elif horizontally == ALIGN_CENTER: |
|
861 |
movex = (maxx + minx - width) / 2 - posx |
|
862 |
elif horizontally == ALIGN_RIGHT: |
|
863 |
movex = maxx - width - posx |
|
864 |
if vertically == ALIGN_TOP: |
|
865 |
movey = miny - posy |
|
866 |
elif vertically == ALIGN_MIDDLE: |
|
867 |
movey = (maxy + miny - height) / 2 - posy |
|
868 |
elif vertically == ALIGN_BOTTOM: |
|
869 |
movey = maxy - height - posy |
|
870 |
if movex != 0 or movey != 0: |
|
871 |
element.Move(movex, movey) |
|
872 |
element.RefreshModel() |
|
873 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
874 |
|
1226
93e7a8abce5e
Fixed lag when selecting all elements using CTRL+A or after paste a lot of blocks
Laurent Bessard
parents:
1176
diff
changeset
|
875 |
# Add the given element to the group of elements |
93e7a8abce5e
Fixed lag when selecting all elements using CTRL+A or after paste a lot of blocks
Laurent Bessard
parents:
1176
diff
changeset
|
876 |
def AddElement(self, element): |
93e7a8abce5e
Fixed lag when selecting all elements using CTRL+A or after paste a lot of blocks
Laurent Bessard
parents:
1176
diff
changeset
|
877 |
self.Elements.append(element) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
878 |
|
814 | 879 |
# Remove or select the given element if it is or not in the group |
880 |
def SelectElement(self, element): |
|
881 |
if element in self.Elements: |
|
882 |
self.Elements.remove(element) |
|
883 |
else: |
|
884 |
self.Elements.append(element) |
|
885 |
self.RefreshWireExclusion() |
|
886 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
887 |
|
814 | 888 |
# Move this group of elements |
889 |
def Move(self, movex, movey): |
|
890 |
movex = max(-self.BoundingBox.x, movex) |
|
891 |
movey = max(-self.BoundingBox.y, movey) |
|
892 |
# Move all the elements of the group |
|
893 |
for element in self.Elements: |
|
894 |
if not isinstance(element, Wire): |
|
895 |
element.Move(movex, movey, self.WireExcluded) |
|
896 |
elif element in self.WireExcluded: |
|
897 |
element.Move(movex, movey, True) |
|
898 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
899 |
|
814 | 900 |
# Refreshes the bounding box of this group of elements |
901 |
def RefreshBoundingBox(self): |
|
902 |
if len(self.Elements) > 0: |
|
903 |
bbox = self.Elements[0].GetBoundingBox() |
|
904 |
minx, miny = bbox.x, bbox.y |
|
905 |
maxx = bbox.x + bbox.width |
|
906 |
maxy = bbox.y + bbox.height |
|
907 |
for element in self.Elements[1:]: |
|
908 |
bbox = element.GetBoundingBox() |
|
909 |
minx = min(minx, bbox.x) |
|
910 |
miny = min(miny, bbox.y) |
|
911 |
maxx = max(maxx, bbox.x + bbox.width) |
|
912 |
maxy = max(maxy, bbox.y + bbox.height) |
|
913 |
self.BoundingBox = wx.Rect(minx, miny, maxx - minx, maxy - miny) |
|
914 |
else: |
|
915 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
|
916 |
self.Pos = wx.Point(self.BoundingBox.x, self.BoundingBox.y) |
|
917 |
self.Size = wx.Size(self.BoundingBox.width, self.BoundingBox.height) |
|
918 |
||
919 |
# Forbids to change the group position |
|
1868
616c3f4bcbcb
fix pylint error '(no-self-argument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset
|
920 |
def SetPosition(self, x, y): |
814 | 921 |
pass |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
922 |
|
814 | 923 |
# Returns the position of this group |
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
924 |
def GetPosition(self, exclude_wires=False): |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
925 |
if exclude_wires: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
926 |
posx = posy = None |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
927 |
for element in self.Elements: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
928 |
if not isinstance(element, Wire) or element in self.WireExcluded: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
929 |
bbox = element.GetBoundingBox() |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
930 |
if posx is None and posy is None: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
931 |
posx, posy = bbox.x, bbox.y |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
932 |
else: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
933 |
posx = min(posx, bbox.x) |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
934 |
posy = min(posy, bbox.y) |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
935 |
if posx is None and posy is None: |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
936 |
return 0, 0 |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
937 |
return posx, posy |
814 | 938 |
return self.BoundingBox.x, self.BoundingBox.y |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
939 |
|
814 | 940 |
# Forbids to change the group size |
1868
616c3f4bcbcb
fix pylint error '(no-self-argument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset
|
941 |
def SetSize(self, width, height): |
814 | 942 |
pass |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
943 |
|
814 | 944 |
# Returns the size of this group |
945 |
def GetSize(self): |
|
946 |
return self.BoundingBox.width, self.BoundingBox.height |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
947 |
|
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
948 |
# Set size of the group elements to their minimum size |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
949 |
def SetBestSize(self, scaling): |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
950 |
max_movex = max_movey = 0 |
814 | 951 |
for element in self.Elements: |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
952 |
movex, movey = element.SetBestSize(scaling) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
953 |
max_movex = max(max_movex, movex) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
954 |
max_movey = max(max_movey, movey) |
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
955 |
return max_movex, max_movey |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
956 |
|
814 | 957 |
# Refreshes the group elements to move defined and handle selected |
958 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
959 |
handle_type, _handle = self.Handle |
814 | 960 |
# If it is a move handle, Move this group elements |
961 |
if handle_type == HANDLE_MOVE: |
|
962 |
movex = max(-self.BoundingBox.x, movex) |
|
963 |
movey = max(-self.BoundingBox.y, movey) |
|
964 |
if scaling is not None: |
|
965 |
movex = round_scaling(movex, scaling[0]) |
|
966 |
movey = round_scaling(movey, scaling[1]) |
|
967 |
if event.ControlDown(): |
|
968 |
self.CurrentDrag.x = self.CurrentDrag.x + movex |
|
969 |
self.CurrentDrag.y = self.CurrentDrag.y + movey |
|
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
970 |
posx, posy = self.GetPosition(True) |
814 | 971 |
if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y): |
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
972 |
movex = self.StartPos.x + self.CurrentDrag.x - posx |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
973 |
movey = self.StartPos.y - posy |
814 | 974 |
else: |
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
975 |
movex = self.StartPos.x - posx |
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
976 |
movey = self.StartPos.y + self.CurrentDrag.y - posy |
814 | 977 |
self.Move(movex, movey) |
978 |
return movex, movey |
|
979 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
980 |
|
814 | 981 |
# Change the variable that indicates if this element is highlighted |
982 |
def SetHighlighted(self, highlighted): |
|
983 |
for element in self.Elements: |
|
984 |
element.SetHighlighted(highlighted) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
985 |
|
814 | 986 |
def HighlightPoint(self, pos): |
987 |
for element in self.Elements: |
|
988 |
if isinstance(element, Wire): |
|
989 |
element.HighlightPoint(pos) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
990 |
|
814 | 991 |
# Method called when a LeftDown event have been generated |
992 |
def OnLeftDown(self, event, dc, scaling): |
|
993 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
825
0623820aa14a
Fix bug in Viewer when dragging element group with control down and group contains wires connected to blocks not in group
laurent
parents:
814
diff
changeset
|
994 |
self.StartPos = wx.Point(*self.GetPosition(True)) |
814 | 995 |
for element in self.Elements: |
996 |
element.Handle = self.Handle |
|
997 |
||
998 |
# Change the variable that indicates if the elemente is selected |
|
999 |
def SetSelected(self, selected): |
|
1000 |
for element in self.Elements: |
|
1001 |
element.SetSelected(selected) |
|
1002 |
||
1003 |
# Method called when a RightUp event has been generated |
|
1004 |
def OnRightUp(self, event, dc, scaling): |
|
1005 |
# Popup the menu with special items for a group |
|
1006 |
self.Parent.PopupGroupMenu() |
|
1007 |
||
1008 |
# Refreshes the model of all the elements of this group |
|
1047
efcc2283dd77
Fixed bug when using 'Adjust Block Size' contextual menu item on a group of selected elements
Laurent Bessard
parents:
993
diff
changeset
|
1009 |
def RefreshModel(self, move=True): |
814 | 1010 |
for element in self.Elements: |
1047
efcc2283dd77
Fixed bug when using 'Adjust Block Size' contextual menu item on a group of selected elements
Laurent Bessard
parents:
993
diff
changeset
|
1011 |
element.RefreshModel(move) |
814 | 1012 |
|
1069
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1013 |
# Draws the handles of this element if it is selected |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1014 |
def Draw(self, dc): |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1015 |
for element in self.Elements: |
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset
|
1016 |
element.Draw(dc) |
814 | 1017 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1018 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1019 |
# ------------------------------------------------------------------------------- |
814 | 1020 |
# Connector for all types of blocks |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1021 |
# ------------------------------------------------------------------------------- |
814 | 1022 |
|
1023 |
||
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1024 |
class Connector(DebugDataConsumer, ToolTipProducer): |
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1025 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1026 |
Class that implements a connector for any type of block |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1027 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1028 |
|
814 | 1029 |
# Create a new connector |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1030 |
def __init__(self, parent, name, type, position, direction, negated=False, edge="none", onlyone=False): |
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1031 |
DebugDataConsumer.__init__(self) |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1032 |
ToolTipProducer.__init__(self, parent.Parent) |
814 | 1033 |
self.ParentBlock = parent |
1034 |
self.Name = name |
|
1035 |
self.Type = type |
|
1036 |
self.Pos = position |
|
1037 |
self.Direction = direction |
|
1038 |
self.Wires = [] |
|
1039 |
if self.ParentBlock.IsOfType("BOOL", type): |
|
1040 |
self.Negated = negated |
|
1041 |
self.Edge = edge |
|
1042 |
else: |
|
1043 |
self.Negated = False |
|
1044 |
self.Edge = "none" |
|
1045 |
self.OneConnected = onlyone |
|
1046 |
self.Valid = True |
|
1047 |
self.Value = None |
|
1048 |
self.Forced = False |
|
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1049 |
self.ValueSize = None |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1050 |
self.ComputedValue = None |
814 | 1051 |
self.Selected = False |
1052 |
self.Highlights = [] |
|
1053 |
self.RefreshNameSize() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1054 |
|
814 | 1055 |
def Flush(self): |
1056 |
self.ParentBlock = None |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1057 |
for wire, _handle in self.Wires: |
814 | 1058 |
wire.Flush() |
1059 |
self.Wires = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1060 |
|
814 | 1061 |
# 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
|
1062 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 1063 |
parent_pos = self.ParentBlock.GetPosition() |
1064 |
x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE) |
|
1065 |
y = min(parent_pos[1] + self.Pos.y, parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE) |
|
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1066 |
has_modifier = self.Negated or self.Edge != "none" |
814 | 1067 |
if self.Direction[0] == 0: |
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1068 |
width = 10 if has_modifier else 5 |
814 | 1069 |
else: |
1070 |
width = CONNECTOR_SIZE |
|
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1071 |
if self.Edge == "rising" and self.Direction[0] == 1: |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1072 |
x -= 5 |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1073 |
width += 5 |
814 | 1074 |
if self.Direction[1] == 0: |
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1075 |
height = 10 if has_modifier else 5 |
814 | 1076 |
else: |
1077 |
height = CONNECTOR_SIZE |
|
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1078 |
if self.Edge == "rising" and self.Direction[1] == 1: |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1079 |
y -= 5 |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1080 |
height += 5 |
1758
845ca626db09
clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1754
diff
changeset
|
1081 |
rect = wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey)) |
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1082 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1083 |
self.ValueSize = self.ParentBlock.Parent.GetMiniTextExtent(self.ComputedValue) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1084 |
if self.ValueSize is not None: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1085 |
width, height = self.ValueSize |
1777
c46ec818bdd7
clean-up: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
1086 |
rect = rect.Union( |
c46ec818bdd7
clean-up: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
1087 |
wx.Rect( |
1764
d5df428640ff
clean-up: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1759
diff
changeset
|
1088 |
parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] + |
1777
c46ec818bdd7
clean-up: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
1089 |
width * (self.Direction[0] - 1) / 2, |
1764
d5df428640ff
clean-up: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1759
diff
changeset
|
1090 |
parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] + |
1777
c46ec818bdd7
clean-up: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset
|
1091 |
height * (self.Direction[1] - 1), |
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1092 |
width, height)) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1093 |
return rect |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1094 |
|
814 | 1095 |
# Change the connector selection |
1096 |
def SetSelected(self, selected): |
|
1097 |
self.Selected = selected |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1098 |
|
814 | 1099 |
# Make a clone of the connector |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1100 |
def Clone(self, parent=None): |
814 | 1101 |
if parent is None: |
1102 |
parent = self.ParentBlock |
|
1103 |
return Connector(parent, self.Name, self.Type, wx.Point(self.Pos[0], self.Pos[1]), |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1104 |
self.Direction, self.Negated) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1105 |
|
814 | 1106 |
# Returns the connector parent block |
1107 |
def GetParentBlock(self): |
|
1108 |
return self.ParentBlock |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1109 |
|
814 | 1110 |
# Returns the connector type |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1111 |
def GetType(self, raw=False): |
814 | 1112 |
if self.ParentBlock.IsEndType(self.Type) or raw: |
1113 |
return self.Type |
|
1114 |
elif (self.Negated or self.Edge != "none") and self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1115 |
return "BOOL" |
|
1116 |
else: |
|
1117 |
return self.ParentBlock.GetConnectionResultType(self, self.Type) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1118 |
|
814 | 1119 |
# Returns the connector type |
1120 |
def GetConnectedType(self): |
|
1121 |
if self.ParentBlock.IsEndType(self.Type): |
|
1122 |
return self.Type |
|
1123 |
elif len(self.Wires) == 1: |
|
1124 |
return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1]) |
|
1125 |
return self.Type |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1126 |
|
814 | 1127 |
# Returns the connector type |
1128 |
def GetConnectedRedrawRect(self, movex, movey): |
|
1129 |
rect = None |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1130 |
for wire, _handle in self.Wires: |
814 | 1131 |
if rect is None: |
1132 |
rect = wire.GetRedrawRect() |
|
1133 |
else: |
|
1134 |
rect = rect.Union(wire.GetRedrawRect()) |
|
1135 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1136 |
|
814 | 1137 |
# Returns if connector type is compatible with type given |
1138 |
def IsCompatible(self, type): |
|
1139 |
reference = self.GetType() |
|
1140 |
return self.ParentBlock.IsOfType(type, reference) or self.ParentBlock.IsOfType(reference, type) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1141 |
|
814 | 1142 |
# Changes the connector name |
1143 |
def SetType(self, type): |
|
1144 |
self.Type = type |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1145 |
for wire, _handle in self.Wires: |
814 | 1146 |
wire.SetValid(wire.IsConnectedCompatible()) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1147 |
|
814 | 1148 |
# Returns the connector name |
1149 |
def GetName(self): |
|
1150 |
return self.Name |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1151 |
|
814 | 1152 |
# Changes the connector name |
1153 |
def SetName(self, name): |
|
1154 |
self.Name = name |
|
1155 |
self.RefreshNameSize() |
|
1156 |
||
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1157 |
def SetForced(self, forced): |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1158 |
if self.Forced != forced: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1159 |
self.Forced = forced |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1160 |
if self.Visible: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1161 |
self.Parent.ElementNeedRefresh(self) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1162 |
|
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1163 |
def GetComputedValue(self): |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1164 |
if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
1165 |
return self.Value |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1166 |
return None |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1167 |
|
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1168 |
def GetToolTipValue(self): |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1169 |
return self.GetComputedValue() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1170 |
|
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1171 |
def SetValue(self, value): |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1172 |
if self.Value != value: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1173 |
self.Value = value |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1174 |
computed_value = self.GetComputedValue() |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1175 |
if computed_value is not None: |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1176 |
self.ComputedValue = computed_value |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1177 |
self.SetToolTipText(self.ComputedValue) |
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1178 |
if len(self.ComputedValue) > 4: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1179 |
self.ComputedValue = self.ComputedValue[:4] + "..." |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1180 |
self.ValueSize = None |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1181 |
if self.ParentBlock.Visible: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1182 |
self.ParentBlock.Parent.ElementNeedRefresh(self) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1183 |
|
814 | 1184 |
def RefreshForced(self): |
1185 |
self.Forced = False |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1186 |
for wire, _handle in self.Wires: |
814 | 1187 |
self.Forced |= wire.IsForced() |
1188 |
||
1189 |
def RefreshValue(self): |
|
1190 |
self.Value = self.ReceivingCurrent() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1191 |
|
814 | 1192 |
def RefreshValid(self): |
1193 |
self.Valid = True |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1194 |
for wire, _handle in self.Wires: |
814 | 1195 |
self.Valid &= wire.GetValid() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1196 |
|
814 | 1197 |
def ReceivingCurrent(self): |
1198 |
current = False |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1199 |
for wire, _handle in self.Wires: |
814 | 1200 |
value = wire.GetValue() |
1201 |
if current != "undefined" and isinstance(value, BooleanType): |
|
1202 |
current |= wire.GetValue() |
|
1203 |
elif value == "undefined": |
|
1204 |
current = "undefined" |
|
1205 |
return current |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1206 |
|
814 | 1207 |
def SpreadCurrent(self, spreading): |
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1208 |
for wire, _handle in self.Wires: |
814 | 1209 |
wire.SetValue(spreading) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1210 |
|
814 | 1211 |
# Changes the connector name size |
1212 |
def RefreshNameSize(self): |
|
1213 |
if self.Name != "": |
|
1214 |
self.NameSize = self.ParentBlock.Parent.GetTextExtent(self.Name) |
|
1215 |
else: |
|
1216 |
self.NameSize = 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1217 |
|
814 | 1218 |
# Returns the connector name size |
1219 |
def GetNameSize(self): |
|
1220 |
return self.NameSize |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1221 |
|
814 | 1222 |
# Returns the wires connected to the connector |
1223 |
def GetWires(self): |
|
1224 |
return self.Wires |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1225 |
|
814 | 1226 |
# Returns the parent block Id |
1227 |
def GetBlockId(self): |
|
1228 |
return self.ParentBlock.GetId() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1229 |
|
814 | 1230 |
# Returns the connector relative position |
1231 |
def GetRelPosition(self): |
|
1232 |
return self.Pos |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1233 |
|
814 | 1234 |
# Returns the connector absolute position |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1235 |
def GetPosition(self, size=True): |
814 | 1236 |
parent_pos = self.ParentBlock.GetPosition() |
1237 |
# If the position of the end of the connector is asked |
|
1238 |
if size: |
|
1239 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
1240 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
1241 |
else: |
|
1242 |
x = parent_pos[0] + self.Pos.x |
|
1243 |
y = parent_pos[1] + self.Pos.y |
|
1244 |
return wx.Point(x, y) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1245 |
|
814 | 1246 |
# Change the connector relative position |
1247 |
def SetPosition(self, pos): |
|
1248 |
self.Pos = pos |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1249 |
|
814 | 1250 |
# Returns the connector direction |
1251 |
def GetDirection(self): |
|
1252 |
return self.Direction |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1253 |
|
814 | 1254 |
# Change the connector direction |
1255 |
def SetDirection(self, direction): |
|
1256 |
self.Direction = direction |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1257 |
|
814 | 1258 |
# Connect a wire to this connector at the last place |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1259 |
def Connect(self, wire, refresh=True): |
814 | 1260 |
self.InsertConnect(len(self.Wires), wire, refresh) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1261 |
|
814 | 1262 |
# Connect a wire to this connector at the place given |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1263 |
def InsertConnect(self, idx, wire, refresh=True): |
814 | 1264 |
if wire not in self.Wires: |
1265 |
self.Wires.insert(idx, wire) |
|
1054
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1266 |
if wire[1] == 0: |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1267 |
wire[0].ConnectStartPoint(None, self) |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1268 |
else: |
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset
|
1269 |
wire[0].ConnectEndPoint(None, self) |
814 | 1270 |
if refresh: |
1271 |
self.ParentBlock.RefreshModel(False) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1272 |
|
814 | 1273 |
# Returns the index of the wire given in the list of connected |
1274 |
def GetWireIndex(self, wire): |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1275 |
for i, (tmp_wire, _handle) in enumerate(self.Wires): |
814 | 1276 |
if tmp_wire == wire: |
1277 |
return i |
|
1278 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1279 |
|
814 | 1280 |
# Unconnect a wire or all wires connected to the connector |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1281 |
def UnConnect(self, wire=None, unconnect=True, delete=False): |
814 | 1282 |
i = 0 |
1283 |
found = False |
|
1284 |
while i < len(self.Wires) and not found: |
|
1285 |
if not wire or self.Wires[i][0] == wire: |
|
1286 |
# If Unconnect haven't been called from a wire, disconnect the connector in the wire |
|
1287 |
if unconnect: |
|
1288 |
if self.Wires[i][1] == 0: |
|
1289 |
self.Wires[i][0].UnConnectStartPoint(delete) |
|
1290 |
else: |
|
1291 |
self.Wires[i][0].UnConnectEndPoint(delete) |
|
1292 |
# Remove wire from connected |
|
1293 |
if wire: |
|
1294 |
self.Wires.pop(i) |
|
1295 |
found = True |
|
1296 |
i += 1 |
|
1297 |
# If no wire defined, unconnect all wires |
|
1298 |
if not wire: |
|
1299 |
self.Wires = [] |
|
857
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1300 |
if not delete: |
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1301 |
self.RefreshValid() |
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset
|
1302 |
self.ParentBlock.RefreshModel(False) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1303 |
|
814 | 1304 |
# Returns if connector has one or more wire connected |
1305 |
def IsConnected(self): |
|
1306 |
return len(self.Wires) > 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1307 |
|
814 | 1308 |
# Move the wires connected |
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
1309 |
def MoveConnected(self, exclude=None): |
814 | 1310 |
if len(self.Wires) > 0: |
1311 |
# Calculate the new position of the end point |
|
1312 |
parent_pos = self.ParentBlock.GetPosition() |
|
1313 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
1314 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
1315 |
# Move the corresponding point on all the wires connected |
|
1316 |
for wire, index in self.Wires: |
|
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
1317 |
if (exclude is None) or (wire not in exclude): |
814 | 1318 |
if index == 0: |
1319 |
wire.MoveStartPoint(wx.Point(x, y)) |
|
1320 |
else: |
|
1321 |
wire.MoveEndPoint(wx.Point(x, y)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1322 |
|
814 | 1323 |
# Refreshes the model of all the wires connected |
1324 |
def RefreshWires(self): |
|
1325 |
for wire in self.Wires: |
|
1326 |
wire[0].RefreshModel() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1327 |
|
814 | 1328 |
# Refreshes the parent block model |
1329 |
def RefreshParentBlock(self): |
|
1330 |
self.ParentBlock.RefreshModel(False) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1331 |
|
814 | 1332 |
# Highlight the parent block |
1333 |
def HighlightParentBlock(self, highlight): |
|
1334 |
self.ParentBlock.SetHighlighted(highlight) |
|
1335 |
self.ParentBlock.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1336 |
|
814 | 1337 |
# Returns all the blocks connected to this connector |
1338 |
def GetConnectedBlocks(self): |
|
1339 |
blocks = [] |
|
1340 |
for wire, handle in self.Wires: |
|
1341 |
# Get other connector connected to each wire |
|
1342 |
if handle == 0: |
|
1343 |
connector = wire.GetEndConnected() |
|
1344 |
else: |
|
1345 |
connector = wire.GetStartConnected() |
|
1346 |
# Get parent block for this connector |
|
1347 |
if connector: |
|
1348 |
block = connector.GetParentBlock() |
|
1349 |
if block not in blocks: |
|
1350 |
blocks.append(block) |
|
1351 |
return blocks |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1352 |
|
814 | 1353 |
# Returns the connector negated property |
1354 |
def IsNegated(self): |
|
1355 |
return self.Negated |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1356 |
|
814 | 1357 |
# Changes the connector negated property |
1358 |
def SetNegated(self, negated): |
|
1359 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1360 |
self.Negated = negated |
|
1361 |
self.Edge = "none" |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1362 |
|
814 | 1363 |
# Returns the connector edge property |
1364 |
def GetEdge(self): |
|
1365 |
return self.Edge |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1366 |
|
814 | 1367 |
# Changes the connector edge property |
1368 |
def SetEdge(self, edge): |
|
1369 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1370 |
self.Edge = edge |
814 | 1371 |
self.Negated = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1372 |
|
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1373 |
# assume that pointer is already inside of this connector |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1374 |
def ConnectionAvailable(self, direction=None, exclude=True): |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1375 |
wire_nums = len(self.Wires) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1376 |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1741
diff
changeset
|
1377 |
connector_free = (wire_nums <= 0) |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1378 |
connector_max_used = ((wire_nums > 0) and self.OneConnected) |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1379 |
if (self.Parent.CurrentLanguage in ["SFC", "LD"]) and (self.Type == "BOOL"): |
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
1380 |
connector_max_used = False |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1381 |
|
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1382 |
# connector is available for new connection |
1754
63f4af6bf6d9
clean-up: fix most PEP8 E221 multiple spaces before operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1752
diff
changeset
|
1383 |
connect = connector_free or not connector_max_used |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1384 |
return connect, connector_max_used |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1385 |
|
814 | 1386 |
# Tests if the point given is near from the end point of this connector |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1387 |
def TestPoint(self, pt, direction=None, exclude=True): |
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
1388 |
inside = False |
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
1389 |
check_point = (not exclude) and (direction is None or self.Direction == direction) |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1390 |
|
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1391 |
if check_point: |
814 | 1392 |
# Calculate a square around the end point of this connector |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1393 |
parent_pos = self.ParentBlock.GetPosition() |
814 | 1394 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
1395 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
|
1396 |
width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE |
|
1397 |
height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE |
|
1398 |
rect = wx.Rect(x, y, width, height) |
|
1752
d14ff9d7eb76
clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1749
diff
changeset
|
1399 |
inside = rect.InsideXY(pt.x, pt.y) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1400 |
|
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1401 |
return inside |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1402 |
|
814 | 1403 |
# Draws the highlightment of this element if it is highlighted |
1404 |
def DrawHighlightment(self, dc): |
|
1405 |
scalex, scaley = dc.GetUserScale() |
|
1406 |
dc.SetUserScale(1, 1) |
|
1407 |
pen = MiterPen(HIGHLIGHTCOLOR, 2 * scalex + 5) |
|
1408 |
pen.SetCap(wx.CAP_BUTT) |
|
1409 |
dc.SetPen(pen) |
|
1410 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1411 |
dc.SetLogicalFunction(wx.AND) |
|
1412 |
parent_pos = self.ParentBlock.GetPosition() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1413 |
xstart = parent_pos[0] + self.Pos.x |
814 | 1414 |
ystart = parent_pos[1] + self.Pos.y |
1415 |
if self.Direction[0] < 0: |
|
1416 |
xstart += 1 |
|
1417 |
if self.Direction[1] < 0: |
|
1418 |
ystart += 1 |
|
1419 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
1420 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1421 |
dc.DrawLine(round((xstart + self.Direction[0]) * scalex), round((ystart + self.Direction[1]) * scaley), |
814 | 1422 |
round(xend * scalex), round(yend * scaley)) |
1423 |
dc.SetLogicalFunction(wx.COPY) |
|
1424 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1425 |
|
814 | 1426 |
# Adds an highlight to the connector |
1427 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1428 |
if highlight_type == ERROR_HIGHLIGHT: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1429 |
for wire, _handle in self.Wires: |
814 | 1430 |
wire.SetValid(False) |
1431 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1432 |
|
814 | 1433 |
# Removes an highlight from the connector |
1434 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1435 |
error = False |
|
1436 |
highlights = [] |
|
1437 |
for highlight in self.Highlights: |
|
1438 |
if highlight != (start, end, highlight_type): |
|
1439 |
highlights.append(highlight) |
|
1440 |
error |= highlight == ERROR_HIGHLIGHT |
|
1441 |
self.Highlights = highlights |
|
1442 |
if not error: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1443 |
for wire, _handle in self.Wires: |
814 | 1444 |
wire.SetValid(wire.IsConnectedCompatible()) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1445 |
|
814 | 1446 |
# Removes all the highlights of one particular type from the connector |
1447 |
def ClearHighlight(self, highlight_type=None): |
|
1448 |
error = False |
|
1449 |
if highlight_type is None: |
|
1450 |
self.Highlights = [] |
|
1451 |
else: |
|
1452 |
highlights = [] |
|
1453 |
for highlight in self.Highlights: |
|
1454 |
if highlight[2] != highlight_type: |
|
1455 |
highlights.append(highlight) |
|
1456 |
error |= highlight == ERROR_HIGHLIGHT |
|
1457 |
self.Highlights = highlights |
|
1458 |
if not error: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1459 |
for wire, _handle in self.Wires: |
814 | 1460 |
wire.SetValid(wire.IsConnectedCompatible()) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1461 |
|
814 | 1462 |
# Draws the connector |
1463 |
def Draw(self, dc): |
|
1464 |
if self.Selected: |
|
1465 |
dc.SetPen(MiterPen(wx.BLUE, 3)) |
|
1466 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1467 |
# elif len(self.Highlights) > 0: |
814 | 1468 |
# dc.SetPen(MiterPen(self.Highlights[-1][1])) |
1469 |
# dc.SetBrush(wx.Brush(self.Highlights[-1][0])) |
|
1470 |
else: |
|
1471 |
if not self.Valid: |
|
1472 |
dc.SetPen(MiterPen(wx.RED)) |
|
1473 |
elif isinstance(self.Value, BooleanType) and self.Value: |
|
1474 |
if self.Forced: |
|
1475 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
1476 |
else: |
|
1477 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1478 |
elif self.Value == "undefined": |
|
1479 |
dc.SetPen(MiterPen(wx.NamedColour("orange"))) |
|
1480 |
elif self.Forced: |
|
1481 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
1482 |
else: |
|
1483 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1484 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
1485 |
parent_pos = self.ParentBlock.GetPosition() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1486 |
|
814 | 1487 |
if getattr(dc, "printing", False): |
1488 |
name_size = dc.GetTextExtent(self.Name) |
|
1489 |
else: |
|
1490 |
name_size = self.NameSize |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1491 |
|
814 | 1492 |
if self.Negated: |
1493 |
# If connector is negated, draw a circle |
|
1494 |
xcenter = parent_pos[0] + self.Pos.x + (CONNECTOR_SIZE * self.Direction[0]) / 2 |
|
1495 |
ycenter = parent_pos[1] + self.Pos.y + (CONNECTOR_SIZE * self.Direction[1]) / 2 |
|
1496 |
dc.DrawCircle(xcenter, ycenter, CONNECTOR_SIZE / 2) |
|
1497 |
else: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1498 |
xstart = parent_pos[0] + self.Pos.x |
814 | 1499 |
ystart = parent_pos[1] + self.Pos.y |
1500 |
if self.Edge == "rising": |
|
1501 |
# If connector has a rising edge, draw a right arrow |
|
1502 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart - 4) |
|
1503 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart + 4) |
|
1504 |
elif self.Edge == "falling": |
|
1505 |
# If connector has a falling edge, draw a left arrow |
|
1506 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4) |
|
1507 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4) |
|
1508 |
if self.Direction[0] < 0: |
|
1509 |
xstart += 1 |
|
1510 |
if self.Direction[1] < 0: |
|
1511 |
ystart += 1 |
|
1512 |
if self.Selected: |
|
1513 |
xend = xstart + (CONNECTOR_SIZE - 2) * self.Direction[0] |
|
1514 |
yend = ystart + (CONNECTOR_SIZE - 2) * self.Direction[1] |
|
1515 |
dc.DrawLine(xstart + 2 * self.Direction[0], ystart + 2 * self.Direction[1], xend, yend) |
|
1516 |
else: |
|
1517 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
1518 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
1519 |
dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend) |
|
1520 |
if self.Direction[0] != 0: |
|
1521 |
ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2 |
|
1522 |
if self.Direction[0] < 0: |
|
1523 |
xtext = parent_pos[0] + self.Pos.x + 5 |
|
1524 |
else: |
|
1525 |
xtext = parent_pos[0] + self.Pos.x - (name_size[0] + 5) |
|
1526 |
if self.Direction[1] != 0: |
|
1527 |
xtext = parent_pos[0] + self.Pos.x - name_size[0] / 2 |
|
1528 |
if self.Direction[1] < 0: |
|
1529 |
ytext = parent_pos[1] + self.Pos.y + 5 |
|
1530 |
else: |
|
1531 |
ytext = parent_pos[1] + self.Pos.y - (name_size[1] + 5) |
|
1532 |
# Draw the text |
|
1533 |
dc.DrawText(self.Name, xtext, ytext) |
|
1534 |
if not getattr(dc, "printing", False): |
|
1535 |
DrawHighlightedText(dc, self.Name, self.Highlights, xtext, ytext) |
|
1536 |
||
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1537 |
if self.Value is not None and not isinstance(self.Value, BooleanType) and self.Value != "undefined": |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1538 |
dc.SetFont(self.ParentBlock.Parent.GetMiniFont()) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1539 |
dc.SetTextForeground(wx.NamedColour("purple")) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1540 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1541 |
self.ValueSize = self.ParentBlock.Parent.GetMiniTextExtent(self.ComputedValue) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1542 |
if self.ValueSize is not None: |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1543 |
width, height = self.ValueSize |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1544 |
dc.DrawText(self.ComputedValue, |
1767
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
1545 |
parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] + |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
1546 |
width * (self.Direction[0] - 1) / 2, |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
1547 |
parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] + |
c74815729afd
clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1764
diff
changeset
|
1548 |
height * (self.Direction[1] - 1)) |
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1549 |
dc.SetFont(self.ParentBlock.Parent.GetFont()) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1550 |
dc.SetTextForeground(wx.BLACK) |
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset
|
1551 |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1552 |
|
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1553 |
# ------------------------------------------------------------------------------- |
814 | 1554 |
# Common Wire Element |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
1555 |
# ------------------------------------------------------------------------------- |
814 | 1556 |
|
1557 |
||
1558 |
class Wire(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
|
1559 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1560 |
Class that implements a wire for connecting two blocks |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
1561 |
""" |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1562 |
|
814 | 1563 |
# Create a new wire |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
1564 |
def __init__(self, parent, start=None, end=None): |
814 | 1565 |
Graphic_Element.__init__(self, parent) |
1566 |
DebugDataConsumer.__init__(self) |
|
1567 |
self.StartPoint = start |
|
1568 |
self.EndPoint = end |
|
1569 |
self.StartConnected = None |
|
1570 |
self.EndConnected = None |
|
1571 |
# If the start and end points are defined, calculate the wire |
|
1572 |
if start and end: |
|
1573 |
self.ResetPoints() |
|
1574 |
self.GeneratePoints() |
|
1575 |
else: |
|
1576 |
self.Points = [] |
|
1577 |
self.Segments = [] |
|
1578 |
self.SelectedSegment = None |
|
1579 |
self.Valid = True |
|
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1580 |
self.Modifier = "none" |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1581 |
self.PreviousValue = None |
814 | 1582 |
self.ValueSize = None |
1583 |
self.ComputedValue = None |
|
1584 |
self.OverStart = False |
|
1585 |
self.OverEnd = False |
|
1586 |
self.ComputingType = False |
|
1587 |
self.Font = parent.GetMiniFont() |
|
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
1588 |
self.ErrHighlight = False |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1589 |
|
814 | 1590 |
def GetDefinition(self): |
1591 |
if self.StartConnected is not None and self.EndConnected is not None: |
|
1592 |
startblock = self.StartConnected.GetParentBlock() |
|
1593 |
endblock = self.EndConnected.GetParentBlock() |
|
1594 |
return [], [(startblock.GetId(), endblock.GetId())] |
|
1595 |
return [], [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1596 |
|
814 | 1597 |
def Flush(self): |
1598 |
self.StartConnected = None |
|
1599 |
self.EndConnected = None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1600 |
|
814 | 1601 |
# 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
|
1602 |
def GetRedrawRect(self, movex=0, movey=0): |
814 | 1603 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
1604 |
if self.StartConnected: |
|
1605 |
rect = rect.Union(self.StartConnected.GetRedrawRect(movex, movey)) |
|
1606 |
if self.EndConnected: |
|
1607 |
rect = rect.Union(self.EndConnected.GetRedrawRect(movex, movey)) |
|
1608 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
|
1609 |
self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue) |
|
1610 |
if self.ValueSize is not None: |
|
1611 |
width, height = self.ValueSize |
|
1612 |
if self.BoundingBox[2] > width * 4 or self.BoundingBox[3] > height * 4: |
|
1613 |
x = self.Points[0].x + width * self.StartPoint[1][0] / 2 |
|
1614 |
y = self.Points[0].y + height * (self.StartPoint[1][1] - 1) |
|
1615 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
1616 |
x = self.Points[-1].x + width * self.EndPoint[1][0] / 2 |
|
1617 |
y = self.Points[-1].y + height * (self.EndPoint[1][1] - 1) |
|
1618 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
1619 |
else: |
|
1620 |
middle = len(self.Segments) / 2 + len(self.Segments) % 2 - 1 |
|
1621 |
x = (self.Points[middle].x + self.Points[middle + 1].x - width) / 2 |
|
1622 |
if self.BoundingBox[3] > height and self.Segments[middle] in [NORTH, SOUTH]: |
|
1623 |
y = (self.Points[middle].y + self.Points[middle + 1].y - height) / 2 |
|
1624 |
else: |
|
1625 |
y = self.Points[middle].y - height |
|
1626 |
rect = rect.Union(wx.Rect(x, y, width, height)) |
|
1627 |
return rect |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1628 |
|
1852
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
1629 |
def Clone(self, parent, connectors=None, dx=0, dy=0): |
70c1cc354a8f
fix pylint warning '(dangerous-default-value) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset
|
1630 |
connectors = {} if connectors is None else connectors |
814 | 1631 |
start_connector = connectors.get(self.StartConnected, None) |
1632 |
end_connector = connectors.get(self.EndConnected, None) |
|
1633 |
if start_connector is not None and end_connector is not None: |
|
1634 |
wire = Wire(parent) |
|
1635 |
wire.SetPoints([(point.x + dx, point.y + dy) for point in self.Points]) |
|
1636 |
start_connector.Connect((wire, 0), False) |
|
1637 |
end_connector.Connect((wire, -1), False) |
|
1638 |
wire.ConnectStartPoint(start_connector.GetPosition(), start_connector) |
|
1639 |
wire.ConnectEndPoint(end_connector.GetPosition(), end_connector) |
|
1640 |
return wire |
|
1641 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1642 |
|
814 | 1643 |
# Forbids to change the wire position |
1868
616c3f4bcbcb
fix pylint error '(no-self-argument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset
|
1644 |
def SetPosition(self, x, y): |
814 | 1645 |
pass |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1646 |
|
814 | 1647 |
# Forbids to change the wire size |
1868
616c3f4bcbcb
fix pylint error '(no-self-argument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset
|
1648 |
def SetSize(self, width, height): |
814 | 1649 |
pass |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1650 |
|
814 | 1651 |
# Moves and Resizes the element for fitting scaling |
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset
|
1652 |
def SetBestSize(self, scaling): |
814 | 1653 |
if scaling is not None: |
1654 |
movex_max = movey_max = 0 |
|
1655 |
for idx, point in enumerate(self.Points): |
|
1656 |
if 0 < idx < len(self.Points) - 1: |
|
1657 |
movex = round_scaling(point.x, scaling[0]) - point.x |
|
1658 |
movey = round_scaling(point.y, scaling[1]) - point.y |
|
1659 |
if idx == 1: |
|
1660 |
if self.Segments[0][0] == 0: |
|
1661 |
movex = 0 |
|
1662 |
elif (point.x + movex - self.Points[0].x) * self.Segments[0][0] < MIN_SEGMENT_SIZE: |
|
1663 |
movex = round_scaling(self.Points[0].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - point.x |
|
1664 |
if self.Segments[0][1] == 0: |
|
1665 |
movey = 0 |
|
1666 |
elif (point.y + movey - self.Points[0].y) * self.Segments[0][1] < MIN_SEGMENT_SIZE: |
|
1667 |
movey = round_scaling(self.Points[0].y + MIN_SEGMENT_SIZE * self.Segments[0][1], scaling[0], self.Segments[0][1]) - point.y |
|
1668 |
elif idx == len(self.Points) - 2: |
|
1669 |
if self.Segments[-1][0] == 0: |
|
1670 |
movex = 0 |
|
1671 |
elif (self.Points[-1].x - (point.x + movex)) * self.Segments[-1][0] < MIN_SEGMENT_SIZE: |
|
1672 |
movex = round_scaling(self.Points[-1].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - point.x |
|
1673 |
if self.Segments[-1][1] == 0: |
|
1674 |
movey = 0 |
|
1675 |
elif (self.Points[-1].y - (point.y + movey)) * self.Segments[-1][1] < MIN_SEGMENT_SIZE: |
|
1676 |
movey = round_scaling(self.Points[-1].y - MIN_SEGMENT_SIZE * self.Segments[-1][1], scaling[1], -self.Segments[-1][1]) - point.y |
|
1677 |
movex_max = max(movex_max, movex) |
|
1678 |
movey_max = max(movey_max, movey) |
|
1679 |
point.x += movex |
|
1680 |
point.y += movey |
|
1681 |
return movex_max, movey_max |
|
1682 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1683 |
|
814 | 1684 |
# Returns connector to which start point is connected |
1685 |
def GetStartConnected(self): |
|
1686 |
return self.StartConnected |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1687 |
|
814 | 1688 |
# Returns connector to which start point is connected |
1689 |
def GetStartConnectedType(self): |
|
1690 |
if self.StartConnected and not self.ComputingType: |
|
1691 |
self.ComputingType = True |
|
1692 |
computed_type = self.StartConnected.GetType() |
|
1693 |
self.ComputingType = False |
|
1694 |
return computed_type |
|
1695 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1696 |
|
814 | 1697 |
# Returns connector to which end point is connected |
1698 |
def GetEndConnected(self): |
|
1699 |
return self.EndConnected |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1700 |
|
814 | 1701 |
# Returns connector to which end point is connected |
1702 |
def GetEndConnectedType(self): |
|
1703 |
if self.EndConnected and not self.ComputingType: |
|
1704 |
self.ComputingType = True |
|
1705 |
computed_type = self.EndConnected.GetType() |
|
1706 |
self.ComputingType = False |
|
1707 |
return computed_type |
|
1708 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1709 |
|
814 | 1710 |
def GetConnectionDirection(self): |
1711 |
if self.StartConnected is None and self.EndConnected is None: |
|
1712 |
return None |
|
1713 |
elif self.StartConnected is not None and self.EndConnected is None: |
|
1714 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
|
1715 |
elif self.StartConnected is None and self.EndConnected is not None: |
|
1716 |
return self.EndPoint |
|
1717 |
elif self.Handle is not None: |
|
1718 |
handle_type, handle = self.Handle |
|
1719 |
# A point has been handled |
|
1720 |
if handle_type == HANDLE_POINT: |
|
1721 |
if handle == 0: |
|
1722 |
return self.EndPoint |
|
1723 |
else: |
|
1724 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
|
1725 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1726 |
|
814 | 1727 |
def GetOtherConnected(self, connector): |
1728 |
if self.StartConnected == connector: |
|
1729 |
return self.EndConnected |
|
1730 |
else: |
|
1731 |
return self.StartConnected |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1732 |
|
814 | 1733 |
def GetOtherConnectedType(self, handle): |
1734 |
if handle == 0: |
|
1735 |
return self.GetEndConnectedType() |
|
1736 |
else: |
|
1737 |
return self.GetStartConnectedType() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1738 |
|
814 | 1739 |
def IsConnectedCompatible(self): |
1740 |
if self.StartConnected: |
|
1741 |
return self.StartConnected.IsCompatible(self.GetEndConnectedType()) |
|
1742 |
elif self.EndConnected: |
|
1743 |
return True |
|
1744 |
return False |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1745 |
|
814 | 1746 |
def SetForced(self, forced): |
1747 |
if self.Forced != forced: |
|
1748 |
self.Forced = forced |
|
1749 |
if self.StartConnected: |
|
1750 |
self.StartConnected.RefreshForced() |
|
1751 |
if self.EndConnected: |
|
1752 |
self.EndConnected.RefreshForced() |
|
1753 |
if self.Visible: |
|
1754 |
self.Parent.ElementNeedRefresh(self) |
|
1755 |
||
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1756 |
def GetComputedValue(self): |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1757 |
if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType): |
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset
|
1758 |
return self.Value |
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1759 |
return None |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1760 |
|
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1761 |
def GetToolTipValue(self): |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1762 |
return self.GetComputedValue() |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1763 |
|
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1764 |
def SetModifier(self, modifier): |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1765 |
self.Modifier = modifier |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1766 |
|
814 | 1767 |
def SetValue(self, value): |
1377
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1768 |
if self.Modifier == "rising": |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1769 |
value, self.PreviousValue = value and not self.PreviousValue, value |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1770 |
elif self.Modifier == "falling": |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1771 |
value, self.PreviousValue = not value and self.PreviousValue, value |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1772 |
elif self.Modifier == "negated": |
cc8f9177d41c
Fixed bug when debugging wire connected to output connector with modifiers even if connector/continuation is used to replace long wires
Laurent Bessard
parents:
1258
diff
changeset
|
1773 |
value = not value |
814 | 1774 |
if self.Value != value: |
1775 |
self.Value = value |
|
1170
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1776 |
computed_value = self.GetComputedValue() |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1777 |
if computed_value is not None: |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1778 |
self.ComputedValue = computed_value |
074e46cdedbc
Added support for displaying ToolTip, starting drag'n drop and Double click on Block connectors when debugging
Laurent Bessard
parents:
1169
diff
changeset
|
1779 |
self.SetToolTipText(self.ComputedValue) |
814 | 1780 |
if len(self.ComputedValue) > 4: |
1781 |
self.ComputedValue = self.ComputedValue[:4] + "..." |
|
1782 |
self.ValueSize = None |
|
1783 |
if self.StartConnected: |
|
1784 |
self.StartConnected.RefreshValue() |
|
1785 |
if self.EndConnected: |
|
1786 |
self.EndConnected.RefreshValue() |
|
1787 |
if self.Visible: |
|
1788 |
self.Parent.ElementNeedRefresh(self) |
|
1789 |
if isinstance(value, BooleanType) and self.StartConnected is not None: |
|
1790 |
block = self.StartConnected.GetParentBlock() |
|
1791 |
block.SpreadCurrent() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1792 |
|
814 | 1793 |
# Unconnect the start and end points |
1794 |
def Clean(self): |
|
1795 |
if self.StartConnected: |
|
1796 |
self.UnConnectStartPoint() |
|
1797 |
if self.EndConnected: |
|
1798 |
self.UnConnectEndPoint() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1799 |
|
814 | 1800 |
# Delete this wire by calling the corresponding method |
1801 |
def Delete(self): |
|
1802 |
self.Parent.DeleteWire(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1803 |
|
814 | 1804 |
# Select a segment and not the whole wire. It's useful for Ladder Diagram |
1805 |
def SetSelectedSegment(self, segment): |
|
1806 |
# The last segment is indicated |
|
1807 |
if segment == -1: |
|
1808 |
segment = len(self.Segments) - 1 |
|
1809 |
# The selected segment is reinitialised |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
1810 |
if segment is None: |
814 | 1811 |
if self.StartConnected: |
1812 |
self.StartConnected.SetSelected(False) |
|
1813 |
if self.EndConnected: |
|
1814 |
self.EndConnected.SetSelected(False) |
|
1815 |
# The segment selected is the first |
|
1816 |
elif segment == 0: |
|
1817 |
if self.StartConnected: |
|
1818 |
self.StartConnected.SetSelected(True) |
|
1819 |
if self.EndConnected: |
|
1820 |
# There is only one segment |
|
1821 |
if len(self.Segments) == 1: |
|
1822 |
self.EndConnected.SetSelected(True) |
|
1823 |
else: |
|
1824 |
self.EndConnected.SetSelected(False) |
|
1825 |
# The segment selected is the last |
|
1826 |
elif segment == len(self.Segments) - 1: |
|
1827 |
if self.StartConnected: |
|
1828 |
self.StartConnected.SetSelected(False) |
|
1829 |
if self.EndConnected: |
|
1830 |
self.EndConnected.SetSelected(True) |
|
1831 |
self.SelectedSegment = segment |
|
1832 |
self.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1833 |
|
814 | 1834 |
def SetValid(self, valid): |
1835 |
self.Valid = valid |
|
1836 |
if self.StartConnected: |
|
1837 |
self.StartConnected.RefreshValid() |
|
1838 |
if self.EndConnected: |
|
1839 |
self.EndConnected.RefreshValid() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1840 |
|
814 | 1841 |
def GetValid(self): |
1842 |
return self.Valid |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1843 |
|
814 | 1844 |
# Reinitialize the wire points |
1845 |
def ResetPoints(self): |
|
1846 |
if self.StartPoint and self.EndPoint: |
|
1847 |
self.Points = [self.StartPoint[0], self.EndPoint[0]] |
|
1848 |
self.Segments = [self.StartPoint[1]] |
|
1849 |
else: |
|
1850 |
self.Points = [] |
|
1851 |
self.Segments = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1852 |
|
814 | 1853 |
# Refresh the wire bounding box |
1854 |
def RefreshBoundingBox(self): |
|
1855 |
if len(self.Points) > 0: |
|
1856 |
# If startpoint or endpoint is connected, save the point radius |
|
1857 |
start_radius = end_radius = 0 |
|
1858 |
if not self.StartConnected: |
|
1859 |
start_radius = POINT_RADIUS |
|
1860 |
if not self.EndConnected: |
|
1861 |
end_radius = POINT_RADIUS |
|
1862 |
# Initialize minimum and maximum from the first point |
|
1863 |
minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius |
|
1864 |
maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius |
|
1865 |
miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius |
|
1866 |
maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius |
|
1867 |
# Actualize minimum and maximum with the other points |
|
1868 |
for point in self.Points[1:-1]: |
|
1869 |
minx, minbbxx = min(minx, point.x), min(minbbxx, point.x) |
|
1870 |
maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x) |
|
1871 |
miny, minbbxy = min(miny, point.y), min(minbbxy, point.y) |
|
1872 |
maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y) |
|
1873 |
if len(self.Points) > 1: |
|
1874 |
minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius) |
|
1875 |
maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius) |
|
1876 |
miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius) |
|
1877 |
maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius) |
|
1878 |
self.Pos.x, self.Pos.y = minx, miny |
|
1879 |
self.Size = wx.Size(maxx - minx, maxy - miny) |
|
1880 |
self.BoundingBox = wx.Rect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1881 |
|
814 | 1882 |
# Refresh the realpoints that permits to keep the proportionality in wire during resizing |
1883 |
def RefreshRealPoints(self): |
|
1884 |
if len(self.Points) > 0: |
|
1885 |
self.RealPoints = [] |
|
1886 |
# Calculate float relative position of each point with the minimum point |
|
1887 |
for point in self.Points: |
|
1888 |
self.RealPoints.append([float(point.x - self.Pos.x), float(point.y - self.Pos.y)]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1889 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1890 |
# Returns the wire minimum size |
814 | 1891 |
def GetMinSize(self): |
1892 |
width = 1 |
|
1893 |
height = 1 |
|
1894 |
dir_product = product(self.StartPoint[1], self.EndPoint[1]) |
|
1895 |
# The directions are opposed |
|
1896 |
if dir_product < 0: |
|
1897 |
if self.StartPoint[0] != 0: |
|
1898 |
width = MIN_SEGMENT_SIZE * 2 |
|
1899 |
if self.StartPoint[1] != 0: |
|
1900 |
height = MIN_SEGMENT_SIZE * 2 |
|
1901 |
# The directions are the same |
|
1902 |
elif dir_product > 0: |
|
1903 |
if self.StartPoint[0] != 0: |
|
1904 |
width = MIN_SEGMENT_SIZE |
|
1905 |
if self.StartPoint[1] != 0: |
|
1906 |
height = MIN_SEGMENT_SIZE |
|
1907 |
# The directions are perpendiculars |
|
1908 |
else: |
|
1909 |
width = MIN_SEGMENT_SIZE |
|
1910 |
height = MIN_SEGMENT_SIZE |
|
1911 |
return width + 1, height + 1 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1912 |
|
814 | 1913 |
# Returns if the point given is on one of the wire segments |
1914 |
def HitTest(self, pt, connectors=True): |
|
1915 |
test = False |
|
1916 |
for i in xrange(len(self.Points) - 1): |
|
1917 |
rect = wx.Rect(0, 0, 0, 0) |
|
1918 |
if i == 0 and self.StartConnected is not None: |
|
1919 |
x1 = self.Points[i].x - self.Segments[0][0] * CONNECTOR_SIZE |
|
1920 |
y1 = self.Points[i].y - self.Segments[0][1] * CONNECTOR_SIZE |
|
1921 |
else: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1922 |
x1, y1 = self.Points[i].x, self.Points[i].y |
814 | 1923 |
if i == len(self.Points) - 2 and self.EndConnected is not None: |
1924 |
x2 = self.Points[i + 1].x + self.Segments[-1][0] * CONNECTOR_SIZE |
|
1925 |
y2 = self.Points[i + 1].y + self.Segments[-1][1] * CONNECTOR_SIZE |
|
1926 |
else: |
|
1927 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1928 |
# Calculate a rectangle around the segment |
|
1929 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1930 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1931 |
test |= rect.InsideXY(pt.x, pt.y) |
814 | 1932 |
return test |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1933 |
|
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1934 |
# Returns the wire start or end point if the point given is on one of them |
814 | 1935 |
def TestPoint(self, pt): |
1936 |
# Test the wire start point |
|
1937 |
rect = wx.Rect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE, |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1938 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
814 | 1939 |
if rect.InsideXY(pt.x, pt.y): |
1940 |
return 0 |
|
1941 |
# Test the wire end point |
|
1942 |
if len(self.Points) > 1: |
|
1943 |
rect = wx.Rect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE, |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1944 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
814 | 1945 |
if rect.InsideXY(pt.x, pt.y): |
1946 |
return -1 |
|
1947 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1948 |
|
814 | 1949 |
# Returns the wire segment if the point given is on it |
1950 |
def TestSegment(self, pt, all=False): |
|
1951 |
for i in xrange(len(self.Segments)): |
|
1952 |
# If wire is not in a Ladder Diagram, first and last segments are excluded |
|
1953 |
if all or 0 < i < len(self.Segments) - 1: |
|
1954 |
x1, y1 = self.Points[i].x, self.Points[i].y |
|
1955 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1956 |
# Calculate a rectangle around the segment |
|
1957 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1958 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
814 | 1959 |
if rect.InsideXY(pt.x, pt.y): |
1960 |
return i, self.Segments[i] |
|
1961 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1962 |
|
814 | 1963 |
# Define the wire points |
1964 |
def SetPoints(self, points, verify=True): |
|
1965 |
if len(points) > 1: |
|
1966 |
self.Points = [wx.Point(x, y) for x, y in points] |
|
1967 |
# Calculate the start and end directions |
|
1968 |
self.StartPoint = [None, vector(self.Points[0], self.Points[1])] |
|
1969 |
self.EndPoint = [None, vector(self.Points[-1], self.Points[-2])] |
|
1970 |
# Calculate the start and end points |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1971 |
self.StartPoint[0] = wx.Point(self.Points[0].x + CONNECTOR_SIZE * self.StartPoint[1][0], |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1972 |
self.Points[0].y + CONNECTOR_SIZE * self.StartPoint[1][1]) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1973 |
self.EndPoint[0] = wx.Point(self.Points[-1].x + CONNECTOR_SIZE * self.EndPoint[1][0], |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
1974 |
self.Points[-1].y + CONNECTOR_SIZE * self.EndPoint[1][1]) |
814 | 1975 |
self.Points[0] = self.StartPoint[0] |
1976 |
self.Points[-1] = self.EndPoint[0] |
|
1977 |
# Calculate the segments directions |
|
1978 |
self.Segments = [] |
|
1979 |
i = 0 |
|
1980 |
while i < len(self.Points) - 1: |
|
1981 |
if verify and 0 < i < len(self.Points) - 2 and \ |
|
1982 |
self.Points[i] == self.Points[i + 1] and \ |
|
1983 |
self.Segments[-1] == vector(self.Points[i + 1], self.Points[i + 2]): |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
1984 |
for dummy in xrange(2): |
814 | 1985 |
self.Points.pop(i) |
1986 |
else: |
|
1987 |
segment = vector(self.Points[i], self.Points[i + 1]) |
|
1988 |
if is_null_vector(segment) and i > 0: |
|
1989 |
segment = (self.Segments[-1][1], self.Segments[-1][0]) |
|
1990 |
if i < len(self.Points) - 2: |
|
1991 |
next = vector(self.Points[i + 1], self.Points[i + 2]) |
|
1992 |
if next == segment or is_null_vector(add_vectors(segment, next)): |
|
1993 |
self.Points.insert(i + 1, wx.Point(self.Points[i + 1].x, self.Points[i + 1].y)) |
|
1994 |
self.Segments.append(segment) |
|
1995 |
i += 1 |
|
1996 |
self.RefreshBoundingBox() |
|
1997 |
self.RefreshRealPoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
1998 |
|
814 | 1999 |
# Returns the position of the point indicated |
2000 |
def GetPoint(self, index): |
|
2001 |
if index < len(self.Points): |
|
2002 |
return self.Points[index].x, self.Points[index].y |
|
2003 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2004 |
|
814 | 2005 |
# Returns a list of the position of all wire points |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2006 |
def GetPoints(self, invert=False): |
814 | 2007 |
points = self.VerifyPoints() |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2008 |
points[0] = wx.Point(points[0].x - CONNECTOR_SIZE * self.StartPoint[1][0], |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2009 |
points[0].y - CONNECTOR_SIZE * self.StartPoint[1][1]) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2010 |
points[-1] = wx.Point(points[-1].x - CONNECTOR_SIZE * self.EndPoint[1][0], |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2011 |
points[-1].y - CONNECTOR_SIZE * self.EndPoint[1][1]) |
814 | 2012 |
# An inversion of the list is asked |
2013 |
if invert: |
|
2014 |
points.reverse() |
|
2015 |
return points |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2016 |
|
814 | 2017 |
# Returns the position of the two selected segment points |
2018 |
def GetSelectedSegmentPoints(self): |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
2019 |
if self.SelectedSegment is not None and len(self.Points) > 1: |
814 | 2020 |
return self.Points[self.SelectedSegment:self.SelectedSegment + 2] |
2021 |
return [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2022 |
|
814 | 2023 |
# Returns if the selected segment is the first and/or the last of the wire |
2024 |
def GetSelectedSegmentConnections(self): |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
2025 |
if self.SelectedSegment is not None and len(self.Points) > 1: |
814 | 2026 |
return self.SelectedSegment == 0, self.SelectedSegment == len(self.Segments) - 1 |
2027 |
return (True, True) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2028 |
|
814 | 2029 |
# Returns the connectors on which the wire is connected |
2030 |
def GetConnected(self): |
|
2031 |
connected = [] |
|
2032 |
if self.StartConnected and self.StartPoint[1] == WEST: |
|
2033 |
connected.append(self.StartConnected) |
|
2034 |
if self.EndConnected and self.EndPoint[1] == WEST: |
|
2035 |
connected.append(self.EndConnected) |
|
2036 |
return connected |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2037 |
|
814 | 2038 |
# Returns the id of the block connected to the first or the last wire point |
2039 |
def GetConnectedInfos(self, index): |
|
2040 |
if index == 0 and self.StartConnected: |
|
2041 |
return self.StartConnected.GetBlockId(), self.StartConnected.GetName() |
|
2042 |
elif index == -1 and self.EndConnected: |
|
2043 |
return self.EndConnected.GetBlockId(), self.EndConnected.GetName() |
|
2044 |
return None |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2045 |
|
814 | 2046 |
# Update the wire points position by keeping at most possible the current positions |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2047 |
def GeneratePoints(self, realpoints=True): |
814 | 2048 |
i = 0 |
2049 |
# Calculate the start enad end points with the minimum segment size in the right direction |
|
2050 |
end = wx.Point(self.EndPoint[0].x + self.EndPoint[1][0] * MIN_SEGMENT_SIZE, |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2051 |
self.EndPoint[0].y + self.EndPoint[1][1] * MIN_SEGMENT_SIZE) |
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2052 |
start = wx.Point(self.StartPoint[0].x + self.StartPoint[1][0] * MIN_SEGMENT_SIZE, |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2053 |
self.StartPoint[0].y + self.StartPoint[1][1] * MIN_SEGMENT_SIZE) |
814 | 2054 |
# Evaluate the point till it's the last |
2055 |
while i < len(self.Points) - 1: |
|
2056 |
# The next point is the last |
|
2057 |
if i + 1 == len(self.Points) - 1: |
|
2058 |
# Calculate the direction from current point to end point |
|
2059 |
v_end = vector(self.Points[i], end) |
|
2060 |
# The current point is the first |
|
2061 |
if i == 0: |
|
2062 |
# If the end point is not in the start direction, a point is added |
|
2063 |
if v_end != self.Segments[0] or v_end == self.EndPoint[1]: |
|
2064 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2065 |
self.Segments.insert(1, DirectionChoice( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2066 |
(self.Segments[0][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2067 |
self.Segments[0][0]), v_end, self.EndPoint[1])) |
814 | 2068 |
# The current point is the second |
2069 |
elif i == 1: |
|
2070 |
# The previous direction and the target direction are mainly opposed, a point is added |
|
2071 |
if product(v_end, self.Segments[0]) < 0: |
|
2072 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2073 |
self.Segments.insert(2, DirectionChoice( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2074 |
(self.Segments[1][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2075 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
814 | 2076 |
# The previous direction and the end direction are the same or they are |
2077 |
# perpendiculars and the end direction points towards current segment |
|
2078 |
elif product(self.Segments[0], self.EndPoint[1]) >= 0 and product(self.Segments[1], self.EndPoint[1]) <= 0: |
|
2079 |
# Current point and end point are aligned |
|
2080 |
if self.Segments[0][0] != 0: |
|
2081 |
self.Points[1].x = end.x |
|
2082 |
if self.Segments[0][1] != 0: |
|
2083 |
self.Points[1].y = end.y |
|
2084 |
# If the previous direction and the end direction are the same, a point is added |
|
2085 |
if product(self.Segments[0], self.EndPoint[1]) > 0: |
|
2086 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2087 |
self.Segments.insert(2, DirectionChoice( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2088 |
(self.Segments[1][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2089 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
814 | 2090 |
else: |
2091 |
# Current point is positioned in the middle of start point |
|
2092 |
# and end point on the current direction and a point is added |
|
2093 |
if self.Segments[0][0] != 0: |
|
2094 |
self.Points[1].x = (end.x + start.x) / 2 |
|
2095 |
if self.Segments[0][1] != 0: |
|
2096 |
self.Points[1].y = (end.y + start.y) / 2 |
|
2097 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2098 |
self.Segments.insert(2, DirectionChoice( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2099 |
(self.Segments[1][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2100 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
814 | 2101 |
else: |
2102 |
# The previous direction and the end direction are perpendiculars |
|
2103 |
if product(self.Segments[i - 1], self.EndPoint[1]) == 0: |
|
2104 |
# The target direction and the end direction aren't mainly the same |
|
2105 |
if product(v_end, self.EndPoint[1]) <= 0: |
|
2106 |
# Current point and end point are aligned |
|
2107 |
if self.Segments[i - 1][0] != 0: |
|
2108 |
self.Points[i].x = end.x |
|
2109 |
if self.Segments[i - 1][1] != 0: |
|
2110 |
self.Points[i].y = end.y |
|
2111 |
# Previous direction is updated from the new point |
|
2112 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
2113 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
2114 |
else: |
|
2115 |
test = True |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2116 |
# If the current point is the third, test if the second |
814 | 2117 |
# point can be aligned with the end point |
2118 |
if i == 2: |
|
2119 |
test_point = wx.Point(self.Points[1].x, self.Points[1].y) |
|
2120 |
if self.Segments[1][0] != 0: |
|
2121 |
test_point.y = end.y |
|
2122 |
if self.Segments[1][1] != 0: |
|
2123 |
test_point.x = end.x |
|
2124 |
vector_test = vector(self.Points[0], test_point, False) |
|
2125 |
test = norm(vector_test) > MIN_SEGMENT_SIZE and product(self.Segments[0], vector_test) > 0 |
|
2126 |
# The previous point can be aligned |
|
2127 |
if test: |
|
2128 |
self.Points[i].x, self.Points[i].y = end.x, end.y |
|
2129 |
if self.Segments[i - 1][0] != 0: |
|
2130 |
self.Points[i - 1].y = end.y |
|
2131 |
if self.Segments[i - 1][1] != 0: |
|
2132 |
self.Points[i - 1].x = end.x |
|
2133 |
self.Segments[i] = (-self.EndPoint[1][0], -self.EndPoint[1][1]) |
|
2134 |
else: |
|
2135 |
# Current point is positioned in the middle of previous point |
|
2136 |
# and end point on the current direction and a point is added |
|
2137 |
if self.Segments[1][0] != 0: |
|
2138 |
self.Points[2].x = (self.Points[1].x + end.x) / 2 |
|
2139 |
if self.Segments[1][1] != 0: |
|
2140 |
self.Points[2].y = (self.Points[1].y + end.y) / 2 |
|
2141 |
self.Points.insert(3, wx.Point(self.Points[2].x, self.Points[2].y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2142 |
self.Segments.insert( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2143 |
3, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2144 |
DirectionChoice((self.Segments[2][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2145 |
self.Segments[2][0]), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2146 |
v_end, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2147 |
self.EndPoint[1])) |
814 | 2148 |
else: |
2149 |
# Current point is aligned with end point |
|
2150 |
if self.Segments[i - 1][0] != 0: |
|
2151 |
self.Points[i].x = end.x |
|
2152 |
if self.Segments[i - 1][1] != 0: |
|
2153 |
self.Points[i].y = end.y |
|
2154 |
# Previous direction is updated from the new point |
|
2155 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
2156 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
2157 |
# If previous direction and end direction are opposed |
|
2158 |
if product(self.Segments[i - 1], self.EndPoint[1]) < 0: |
|
2159 |
# Current point is positioned in the middle of previous point |
|
2160 |
# and end point on the current direction |
|
2161 |
if self.Segments[i - 1][0] != 0: |
|
2162 |
self.Points[i].x = (end.x + self.Points[i - 1].x) / 2 |
|
2163 |
if self.Segments[i - 1][1] != 0: |
|
2164 |
self.Points[i].y = (end.y + self.Points[i - 1].y) / 2 |
|
2165 |
# A point is added |
|
2166 |
self.Points.insert(i + 1, wx.Point(self.Points[i].x, self.Points[i].y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2167 |
self.Segments.insert( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2168 |
i + 1, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2169 |
DirectionChoice((self.Segments[i][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2170 |
self.Segments[i][0]), v_end, self.EndPoint[1])) |
814 | 2171 |
else: |
2172 |
# Current point is the first, and second is not mainly in the first direction |
|
2173 |
if i == 0 and product(vector(start, self.Points[1]), self.Segments[0]) < 0: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2174 |
# If first and second directions aren't perpendiculars, a point is added |
814 | 2175 |
if product(self.Segments[0], self.Segments[1]) != 0: |
2176 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2177 |
self.Segments.insert( |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2178 |
1, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2179 |
DirectionChoice((self.Segments[0][1], |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2180 |
self.Segments[0][0]), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2181 |
vector(start, self.Points[1]), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2182 |
self.Segments[1])) |
814 | 2183 |
else: |
2184 |
self.Points[1].x, self.Points[1].y = start.x, start.y |
|
2185 |
else: |
|
2186 |
# Next point is aligned with current point |
|
2187 |
if self.Segments[i][0] != 0: |
|
2188 |
self.Points[i + 1].y = self.Points[i].y |
|
2189 |
if self.Segments[i][1] != 0: |
|
2190 |
self.Points[i + 1].x = self.Points[i].x |
|
2191 |
# Current direction is updated from the new point |
|
2192 |
if product(vector(self.Points[i], self.Points[i + 1]), self.Segments[i]) < 0: |
|
2193 |
self.Segments[i] = (-self.Segments[i][0], -self.Segments[i][1]) |
|
2194 |
i += 1 |
|
2195 |
self.RefreshBoundingBox() |
|
2196 |
if realpoints: |
|
2197 |
self.RefreshRealPoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2198 |
|
814 | 2199 |
# Verify that two consecutive points haven't the same position |
2200 |
def VerifyPoints(self): |
|
2201 |
points = [point for point in self.Points] |
|
2202 |
segments = [segment for segment in self.Segments] |
|
2203 |
i = 1 |
|
2204 |
while i < len(points) - 1: |
|
2205 |
if points[i] == points[i + 1] and segments[i - 1] == segments[i + 1]: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
2206 |
for dummy in xrange(2): |
814 | 2207 |
points.pop(i) |
2208 |
segments.pop(i) |
|
2209 |
else: |
|
2210 |
i += 1 |
|
2211 |
# If the wire isn't in a Ladder Diagram, save the new point list |
|
2212 |
if self.Parent.__class__.__name__ != "LD_Viewer": |
|
2213 |
self.Points = [point for point in points] |
|
2214 |
self.Segments = [segment for segment in segments] |
|
2215 |
self.RefreshBoundingBox() |
|
2216 |
self.RefreshRealPoints() |
|
2217 |
return points |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2218 |
|
814 | 2219 |
# Moves all the wire points except the first and the last if they are connected |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2220 |
def Move(self, dx, dy, endpoints=False): |
814 | 2221 |
for i, point in enumerate(self.Points): |
2222 |
if endpoints or not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2223 |
point.x += dx |
|
2224 |
point.y += dy |
|
2225 |
self.StartPoint[0] = self.Points[0] |
|
2226 |
self.EndPoint[0] = self.Points[-1] |
|
2227 |
self.GeneratePoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2228 |
|
814 | 2229 |
# Resize the wire from position and size given |
2230 |
def Resize(self, x, y, width, height): |
|
2231 |
if len(self.Points) > 1: |
|
2232 |
# Calculate the new position of each point for testing the new size |
|
2233 |
minx, miny = self.Pos.x, self.Pos.y |
|
2234 |
lastwidth, lastheight = self.Size.width, self.Size.height |
|
2235 |
for i, point in enumerate(self.RealPoints): |
|
2236 |
# If start or end point is connected, it's not calculate |
|
2237 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2238 |
if i == 0: |
|
2239 |
dir = self.StartPoint[1] |
|
2240 |
elif i == len(self.Points) - 1: |
|
2241 |
dir = self.EndPoint[1] |
|
2242 |
else: |
|
2243 |
dir = (0, 0) |
|
2244 |
pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * width / float(max(lastwidth, 1)))), |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2245 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
814 | 2246 |
pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * height / float(max(lastheight, 1)))), |
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2247 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
814 | 2248 |
self.Points[i] = wx.Point(minx + x + pointx, miny + y + pointy) |
2249 |
self.StartPoint[0] = self.Points[0] |
|
2250 |
self.EndPoint[0] = self.Points[-1] |
|
2251 |
self.GeneratePoints(False) |
|
2252 |
# Test if the wire position or size have changed |
|
2253 |
if x != 0 and minx == self.Pos.x: |
|
2254 |
x = 0 |
|
2255 |
width = lastwidth |
|
2256 |
if y != 0 and miny == self.Pos.y: |
|
2257 |
y = 0 |
|
2258 |
height = lastwidth |
|
2259 |
if width != lastwidth and lastwidth == self.Size.width: |
|
2260 |
width = lastwidth |
|
2261 |
if height != lastheight and lastheight == self.Size.height: |
|
2262 |
height = lastheight |
|
2263 |
# Calculate the real points from the new size, it's important for |
|
2264 |
# keeping a proportionality in the points position with the size |
|
2265 |
# during a resize dragging |
|
2266 |
for i, point in enumerate(self.RealPoints): |
|
2267 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2268 |
point[0] = point[0] * width / float(max(lastwidth, 1)) |
|
2269 |
point[1] = point[1] * height / float(max(lastheight, 1)) |
|
2270 |
# Calculate the correct position of the points from real points |
|
2271 |
for i, point in enumerate(self.RealPoints): |
|
2272 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
2273 |
if i == 0: |
|
2274 |
dir = self.StartPoint[1] |
|
2275 |
elif i == len(self.Points) - 1: |
|
2276 |
dir = self.EndPoint[1] |
|
2277 |
else: |
|
2278 |
dir = (0, 0) |
|
1768
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2279 |
realpointx = max(-dir[0] * MIN_SEGMENT_SIZE, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2280 |
min(int(round(point[0])), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2281 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2282 |
realpointy = max(-dir[1] * MIN_SEGMENT_SIZE, |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2283 |
min(int(round(point[1])), |
691083b5682a
clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset
|
2284 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
814 | 2285 |
self.Points[i] = wx.Point(minx + x + realpointx, miny + y + realpointy) |
2286 |
self.StartPoint[0] = self.Points[0] |
|
2287 |
self.EndPoint[0] = self.Points[-1] |
|
2288 |
self.GeneratePoints(False) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2289 |
|
814 | 2290 |
# Moves the wire start point and update the wire points |
2291 |
def MoveStartPoint(self, point): |
|
2292 |
if len(self.Points) > 1: |
|
2293 |
self.StartPoint[0] = point |
|
2294 |
self.Points[0] = point |
|
2295 |
self.GeneratePoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2296 |
|
814 | 2297 |
# Changes the wire start direction and update the wire points |
2298 |
def SetStartPointDirection(self, dir): |
|
2299 |
if len(self.Points) > 1: |
|
2300 |
self.StartPoint[1] = dir |
|
2301 |
self.Segments[0] = dir |
|
2302 |
self.GeneratePoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2303 |
|
814 | 2304 |
# Rotates the wire start direction by an angle of 90 degrees anticlockwise |
2305 |
def RotateStartPoint(self): |
|
2306 |
self.SetStartPointDirection((self.StartPoint[1][1], -self.StartPoint[1][0])) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2307 |
|
814 | 2308 |
# Connects wire start point to the connector given and moves wire start point |
2309 |
# to given point |
|
2310 |
def ConnectStartPoint(self, point, connector): |
|
2311 |
if point: |
|
2312 |
self.MoveStartPoint(point) |
|
2313 |
self.StartConnected = connector |
|
2314 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2315 |
|
814 | 2316 |
# Unconnects wire start point |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2317 |
def UnConnectStartPoint(self, delete=False): |
814 | 2318 |
if delete: |
2319 |
self.StartConnected = None |
|
2320 |
self.Delete() |
|
2321 |
elif self.StartConnected: |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2322 |
self.StartConnected.UnConnect(self, unconnect=False) |
814 | 2323 |
self.StartConnected = None |
2324 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2325 |
|
814 | 2326 |
# Moves the wire end point and update the wire points |
2327 |
def MoveEndPoint(self, point): |
|
2328 |
if len(self.Points) > 1: |
|
2329 |
self.EndPoint[0] = point |
|
2330 |
self.Points[-1] = point |
|
2331 |
self.GeneratePoints() |
|
2332 |
||
2333 |
# Changes the wire end direction and update the wire points |
|
2334 |
def SetEndPointDirection(self, dir): |
|
2335 |
if len(self.Points) > 1: |
|
2336 |
self.EndPoint[1] = dir |
|
2337 |
self.GeneratePoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2338 |
|
814 | 2339 |
# Rotates the wire end direction by an angle of 90 degrees anticlockwise |
2340 |
def RotateEndPoint(self): |
|
2341 |
self.SetEndPointDirection((self.EndPoint[1][1], -self.EndPoint[1][0])) |
|
2342 |
||
2343 |
# Connects wire end point to the connector given and moves wire end point |
|
2344 |
# to given point |
|
2345 |
def ConnectEndPoint(self, point, connector): |
|
2346 |
if point: |
|
2347 |
self.MoveEndPoint(point) |
|
2348 |
self.EndConnected = connector |
|
2349 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2350 |
|
814 | 2351 |
# Unconnects wire end point |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2352 |
def UnConnectEndPoint(self, delete=False): |
814 | 2353 |
if delete: |
2354 |
self.EndConnected = None |
|
2355 |
self.Delete() |
|
2356 |
elif self.EndConnected: |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2357 |
self.EndConnected.UnConnect(self, unconnect=False) |
814 | 2358 |
self.EndConnected = None |
2359 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2360 |
|
814 | 2361 |
# Moves the wire segment given by its index |
2362 |
def MoveSegment(self, idx, movex, movey, scaling): |
|
2363 |
if 0 < idx < len(self.Segments) - 1: |
|
2364 |
if self.Segments[idx] in (NORTH, SOUTH): |
|
2365 |
start_x = self.Points[idx].x |
|
2366 |
if scaling is not None: |
|
2367 |
movex = round_scaling(self.Points[idx].x + movex, scaling[0]) - self.Points[idx].x |
|
2368 |
if idx == 1 and (self.Points[1].x + movex - self.Points[0].x) * self.Segments[0][0] < MIN_SEGMENT_SIZE: |
|
2369 |
movex = round_scaling(self.Points[0].x + MIN_SEGMENT_SIZE * self.Segments[0][0], scaling[0], self.Segments[0][0]) - self.Points[idx].x |
|
2370 |
elif idx == len(self.Segments) - 2 and (self.Points[-1].x - (self.Points[-2].x + movex)) * self.Segments[-1][0] < MIN_SEGMENT_SIZE: |
|
2371 |
movex = round_scaling(self.Points[-1].x - MIN_SEGMENT_SIZE * self.Segments[-1][0], scaling[0], -self.Segments[-1][0]) - self.Points[idx].x |
|
2372 |
self.Points[idx].x += movex |
|
2373 |
self.Points[idx + 1].x += movex |
|
2374 |
self.GeneratePoints() |
|
2375 |
if start_x != self.Points[idx].x: |
|
2376 |
return self.Points[idx].x - start_x, 0 |
|
2377 |
elif self.Segments[idx] in (EAST, WEST): |
|
2378 |
start_y = self.Points[idx].y |
|
2379 |
if scaling is not None: |
|
2380 |
movey = round_scaling(self.Points[idx].y + movey, scaling[1]) - self.Points[idx].y |
|
2381 |
if idx == 1 and (self.Points[1].y + movey - self.Points[0].y) * self.Segments[0][1] < MIN_SEGMENT_SIZE: |
|
2382 |
movex = round_scaling(self.Points[0].y + MIN_SEGMENT_SIZE * self.Segments[0][1], scaling[0], self.Segments[0][1]) - self.Points[idx].y |
|
2383 |
elif idx == len(self.Segments) - 2 and (self.Points[-1].y - (self.Points[-2].y + movey)) * self.Segments[-1][1] < MIN_SEGMENT_SIZE: |
|
2384 |
movey = round_scaling(self.Points[idx].y - MIN_SEGMENT_SIZE * self.Segments[-1][1], scaling[1], -self.Segments[-1][1]) - self.Points[idx].y |
|
2385 |
self.Points[idx].y += movey |
|
2386 |
self.Points[idx + 1].y += movey |
|
2387 |
self.GeneratePoints() |
|
2388 |
if start_y != self.Points[idx].y: |
|
2389 |
return 0, self.Points[idx].y - start_y |
|
2390 |
return 0, 0 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2391 |
|
814 | 2392 |
# Adds two points in the middle of the handled segment |
2393 |
def AddSegment(self): |
|
2394 |
handle_type, handle = self.Handle |
|
2395 |
if handle_type == HANDLE_SEGMENT: |
|
2396 |
segment, dir = handle |
|
2397 |
if len(self.Segments) > 1: |
|
2398 |
pointx = self.Points[segment].x |
|
2399 |
pointy = self.Points[segment].y |
|
2400 |
if dir[0] != 0: |
|
2401 |
pointx = (self.Points[segment].x + self.Points[segment + 1].x) / 2 |
|
2402 |
if dir[1] != 0: |
|
2403 |
pointy = (self.Points[segment].y + self.Points[segment + 1].y) / 2 |
|
2404 |
self.Points.insert(segment + 1, wx.Point(pointx, pointy)) |
|
2405 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
|
2406 |
self.Points.insert(segment + 2, wx.Point(pointx, pointy)) |
|
2407 |
self.Segments.insert(segment + 2, dir) |
|
2408 |
else: |
|
2409 |
p1x = p2x = self.Points[segment].x |
|
2410 |
p1y = p2y = self.Points[segment].y |
|
2411 |
if dir[0] != 0: |
|
2412 |
p1x = (2 * self.Points[segment].x + self.Points[segment + 1].x) / 3 |
|
2413 |
p2x = (self.Points[segment].x + 2 * self.Points[segment + 1].x) / 3 |
|
2414 |
if dir[1] != 0: |
|
2415 |
p1y = (2 * self.Points[segment].y + self.Points[segment + 1].y) / 3 |
|
2416 |
p2y = (self.Points[segment].y + 2 * self.Points[segment + 1].y) / 3 |
|
2417 |
self.Points.insert(segment + 1, wx.Point(p1x, p1y)) |
|
2418 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
|
2419 |
self.Points.insert(segment + 2, wx.Point(p1x, p1y)) |
|
2420 |
self.Segments.insert(segment + 2, dir) |
|
2421 |
self.Points.insert(segment + 3, wx.Point(p2x, p2y)) |
|
2422 |
self.Segments.insert(segment + 3, (dir[1], dir[0])) |
|
2423 |
self.Points.insert(segment + 4, wx.Point(p2x, p2y)) |
|
2424 |
self.Segments.insert(segment + 4, dir) |
|
2425 |
self.GeneratePoints() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2426 |
|
814 | 2427 |
# Delete the handled segment by removing the two segment points |
2428 |
def DeleteSegment(self): |
|
2429 |
handle_type, handle = self.Handle |
|
2430 |
if handle_type == HANDLE_SEGMENT: |
|
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
2431 |
segment, _dir = handle |
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset
|
2432 |
for dummy in xrange(2): |
814 | 2433 |
self.Points.pop(segment) |
2434 |
self.Segments.pop(segment) |
|
2435 |
self.GeneratePoints() |
|
2436 |
self.RefreshModel() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2437 |
|
814 | 2438 |
# Method called when a LeftDown event have been generated |
2439 |
def OnLeftDown(self, event, dc, scaling): |
|
2440 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2441 |
# Test if a point have been handled |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
2442 |
# result = self.TestPoint(pos) |
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
2443 |
# if result != None: |
814 | 2444 |
# self.Handle = (HANDLE_POINT, result) |
2445 |
# wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
|
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
2446 |
# else: |
814 | 2447 |
# Test if a segment have been handled |
2448 |
result = self.TestSegment(pos) |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
2449 |
if result is not None: |
814 | 2450 |
if result[1] in (NORTH, SOUTH): |
2451 |
wx.CallAfter(self.Parent.SetCurrentCursor, 4) |
|
2452 |
elif result[1] in (EAST, WEST): |
|
2453 |
wx.CallAfter(self.Parent.SetCurrentCursor, 5) |
|
2454 |
self.Handle = (HANDLE_SEGMENT, result) |
|
2455 |
# Execute the default method for a graphic element |
|
2456 |
else: |
|
2457 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
2458 |
self.oldPos = pos |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2459 |
|
814 | 2460 |
# Method called when a RightUp event has been generated |
2461 |
def OnRightUp(self, event, dc, scaling): |
|
2462 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2463 |
# Test if a segment has been handled |
|
2464 |
result = self.TestSegment(pos, True) |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
2465 |
if result is not None: |
814 | 2466 |
self.Handle = (HANDLE_SEGMENT, result) |
2467 |
# Popup the menu with special items for a wire |
|
2468 |
self.Parent.PopupWireMenu(0 < result[0] < len(self.Segments) - 1) |
|
2469 |
else: |
|
2470 |
# Execute the default method for a graphic element |
|
2471 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2472 |
|
814 | 2473 |
# Method called when a LeftDClick event has been generated |
2474 |
def OnLeftDClick(self, event, dc, scaling): |
|
2475 |
rect = self.GetRedrawRect() |
|
2476 |
if event.ControlDown(): |
|
2477 |
direction = (self.StartPoint[1], self.EndPoint[1]) |
|
2478 |
if direction in [(EAST, WEST), (WEST, EAST)]: |
|
2479 |
avgy = (self.StartPoint[0].y + self.EndPoint[0].y) / 2 |
|
2480 |
if scaling is not None: |
|
2481 |
avgy = round(float(avgy) / scaling[1]) * scaling[1] |
|
2482 |
if self.StartConnected is not None: |
|
2483 |
movey = avgy - self.StartPoint[0].y |
|
2484 |
startblock = self.StartConnected.GetParentBlock() |
|
2485 |
startblock.Move(0, movey) |
|
2486 |
startblock.RefreshModel() |
|
2487 |
rect.Union(startblock.GetRedrawRect(0, movey)) |
|
2488 |
else: |
|
2489 |
self.MoveStartPoint(wx.Point(self.StartPoint[0].x, avgy)) |
|
2490 |
if self.EndConnected is not None: |
|
2491 |
movey = avgy - self.EndPoint[0].y |
|
2492 |
endblock = self.EndConnected.GetParentBlock() |
|
2493 |
endblock.Move(0, movey) |
|
2494 |
endblock.RefreshModel() |
|
2495 |
rect.Union(endblock.GetRedrawRect(0, movey)) |
|
2496 |
else: |
|
2497 |
self.MoveEndPoint(wx.Point(self.EndPoint[0].x, avgy)) |
|
2498 |
self.Parent.RefreshBuffer() |
|
2499 |
elif direction in [(NORTH, SOUTH), (SOUTH, NORTH)]: |
|
2500 |
avgx = (self.StartPoint[0].x + self.EndPoint[0].x) / 2 |
|
2501 |
if scaling is not None: |
|
2502 |
avgx = round(float(avgx) / scaling[0]) * scaling[0] |
|
2503 |
if self.StartConnected is not None: |
|
2504 |
movex = avgx - self.StartPoint[0].x |
|
2505 |
startblock = self.StartConnected.GetParentBlock() |
|
2506 |
startblock.Move(movex, 0) |
|
2507 |
startblock.RefreshModel() |
|
2508 |
rect.Union(startblock.GetRedrawRect(movex, 0)) |
|
2509 |
else: |
|
2510 |
self.MoveStartPoint(wx.Point(avgx, self.StartPoint[0].y)) |
|
2511 |
if self.EndConnected is not None: |
|
2512 |
movex = avgx - self.EndPoint[0].x |
|
2513 |
endblock = self.EndConnected.GetParentBlock() |
|
2514 |
endblock.Move(movex, 0) |
|
2515 |
endblock.RefreshModel() |
|
2516 |
rect.Union(endblock.GetRedrawRect(movex, 0)) |
|
2517 |
else: |
|
2518 |
self.MoveEndPoint(wx.Point(avgx, self.EndPoint[0].y)) |
|
2519 |
self.Parent.RefreshBuffer() |
|
2520 |
else: |
|
2521 |
self.ResetPoints() |
|
2522 |
self.GeneratePoints() |
|
2523 |
self.RefreshModel() |
|
2524 |
self.Parent.RefreshBuffer() |
|
2525 |
rect.Union(self.GetRedrawRect()) |
|
2526 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2527 |
|
814 | 2528 |
# Method called when a Motion event has been generated |
2529 |
def OnMotion(self, event, dc, scaling): |
|
2530 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
2531 |
if not event.Dragging(): |
|
2532 |
# Test if a segment has been handled |
|
2533 |
result = self.TestSegment(pos) |
|
2534 |
if result: |
|
2535 |
if result[1] in (NORTH, SOUTH): |
|
2536 |
wx.CallAfter(self.Parent.SetCurrentCursor, 4) |
|
2537 |
elif result[1] in (EAST, WEST): |
|
2538 |
wx.CallAfter(self.Parent.SetCurrentCursor, 5) |
|
2539 |
return 0, 0 |
|
2540 |
else: |
|
2541 |
# Execute the default method for a graphic element |
|
2542 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
|
2543 |
else: |
|
2544 |
# Execute the default method for a graphic element |
|
2545 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2546 |
|
814 | 2547 |
# Refreshes the wire state according to move defined and handle selected |
2548 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
2549 |
handle_type, handle = self.Handle |
|
2550 |
# A point has been handled |
|
2551 |
if handle_type == HANDLE_POINT: |
|
2552 |
movex = max(-self.Points[handle].x + POINT_RADIUS, movex) |
|
2553 |
movey = max(-self.Points[handle].y + POINT_RADIUS, movey) |
|
2554 |
if scaling is not None: |
|
2555 |
movex = round_scaling(self.Points[handle].x + movex, scaling[0]) - self.Points[handle].x |
|
2556 |
movey = round_scaling(self.Points[handle].y + movey, scaling[1]) - self.Points[handle].y |
|
2557 |
# Try to connect point to a connector |
|
2558 |
new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey) |
|
2559 |
connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection()) |
|
2560 |
if connector: |
|
2561 |
if handle == 0 and self.EndConnected != connector: |
|
2562 |
connector.HighlightParentBlock(True) |
|
2563 |
connector.Connect((self, handle)) |
|
2564 |
self.SetStartPointDirection(connector.GetDirection()) |
|
2565 |
self.ConnectStartPoint(connector.GetPosition(), connector) |
|
2566 |
pos = connector.GetPosition() |
|
2567 |
movex = pos.x - self.oldPos.x |
|
2568 |
movey = pos.y - self.oldPos.y |
|
2569 |
if not connector.IsCompatible(self.GetEndConnectedType()): |
|
2570 |
self.SetValid(False) |
|
2571 |
self.Dragging = False |
|
2572 |
elif handle != 0 and self.StartConnected != connector: |
|
2573 |
connector.HighlightParentBlock(True) |
|
2574 |
connector.Connect((self, handle)) |
|
2575 |
self.SetEndPointDirection(connector.GetDirection()) |
|
2576 |
self.ConnectEndPoint(connector.GetPosition(), connector) |
|
2577 |
pos = connector.GetPosition() |
|
2578 |
movex = pos.x - self.oldPos.x |
|
2579 |
movey = pos.y - self.oldPos.y |
|
2580 |
if not connector.IsCompatible(self.GetStartConnectedType()): |
|
2581 |
self.SetValid(False) |
|
2582 |
self.Dragging = False |
|
2583 |
elif handle == 0: |
|
2584 |
self.MoveStartPoint(new_pos) |
|
2585 |
else: |
|
2586 |
self.MoveEndPoint(new_pos) |
|
2587 |
# If there is no connector, move the point |
|
2588 |
elif handle == 0: |
|
2589 |
self.SetValid(True) |
|
2590 |
if self.StartConnected: |
|
2591 |
self.StartConnected.HighlightParentBlock(False) |
|
2592 |
self.UnConnectStartPoint() |
|
2593 |
self.MoveStartPoint(new_pos) |
|
2594 |
else: |
|
2595 |
self.SetValid(True) |
|
2596 |
if self.EndConnected: |
|
2597 |
self.EndConnected.HighlightParentBlock(False) |
|
2598 |
self.UnConnectEndPoint() |
|
2599 |
self.MoveEndPoint(new_pos) |
|
2600 |
return movex, movey |
|
2601 |
# A segment has been handled, move a segment |
|
2602 |
elif handle_type == HANDLE_SEGMENT: |
|
2603 |
return self.MoveSegment(handle[0], movex, movey, scaling) |
|
2604 |
# Execute the default method for a graphic element |
|
2605 |
else: |
|
2606 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2607 |
|
814 | 2608 |
# Refreshes the wire model |
2609 |
def RefreshModel(self, move=True): |
|
2610 |
if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]: |
|
2611 |
self.StartConnected.RefreshParentBlock() |
|
2612 |
if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]: |
|
2613 |
self.EndConnected.RefreshParentBlock() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2614 |
|
814 | 2615 |
# Change the variable that indicates if this element is highlighted |
2616 |
def SetHighlighted(self, highlighted): |
|
2617 |
self.Highlighted = highlighted |
|
2618 |
if not highlighted: |
|
2619 |
self.OverStart = False |
|
2620 |
self.OverEnd = False |
|
2621 |
self.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2622 |
|
814 | 2623 |
def HighlightPoint(self, pos): |
2624 |
start, end = self.OverStart, self.OverEnd |
|
2625 |
self.OverStart = False |
|
2626 |
self.OverEnd = False |
|
2627 |
# Test if a point has been handled |
|
2628 |
result = self.TestPoint(pos) |
|
1743
c3c3d1318130
clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
2629 |
if result is not None: |
814 | 2630 |
if result == 0 and self.StartConnected is not None: |
2631 |
self.OverStart = True |
|
2632 |
elif result != 0 and self.EndConnected is not None: |
|
2633 |
self.OverEnd = True |
|
2634 |
if start != self.OverStart or end != self.OverEnd: |
|
2635 |
self.Refresh() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2636 |
|
814 | 2637 |
# Draws the highlightment of this element if it is highlighted |
2638 |
def DrawHighlightment(self, dc): |
|
2639 |
scalex, scaley = dc.GetUserScale() |
|
2640 |
dc.SetUserScale(1, 1) |
|
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2641 |
# If user trying to connect wire with wrong input, highlight will become red. |
1828
396da88d7b5c
fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset
|
2642 |
if self.ErrHighlight and not self.EndConnected: |
1544
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2643 |
highlightcolor = wx.RED |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2644 |
else: |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2645 |
highlightcolor = HIGHLIGHTCOLOR |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2646 |
dc.SetPen(MiterPen(highlightcolor, (2 * scalex + 5))) |
2969c2123105
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Sergey Surkov <surkovsv93@gmail.com>
parents:
1377
diff
changeset
|
2647 |
dc.SetBrush(wx.Brush(highlightcolor)) |
814 | 2648 |
dc.SetLogicalFunction(wx.AND) |
2649 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
2650 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2651 |
dc.DrawCircle(round(self.Points[0].x * scalex), |
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2652 |
round(self.Points[0].y * scaley), |
814 | 2653 |
(POINT_RADIUS + 1) * scalex + 2) |
2654 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
2655 |
dc.DrawCircle(self.Points[-1].x * scalex, self.Points[-1].y * scaley, (POINT_RADIUS + 1) * scalex + 2) |
|
2656 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
2657 |
if len(self.Points) > 1: |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2658 |
points = [wx.Point(round((self.Points[0].x - self.Segments[0][0]) * scalex), |
814 | 2659 |
round((self.Points[0].y - self.Segments[0][1]) * scaley))] |
2660 |
points.extend([wx.Point(round(point.x * scalex), round(point.y * scaley)) for point in self.Points[1:-1]]) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2661 |
points.append(wx.Point(round((self.Points[-1].x + self.Segments[-1][0]) * scalex), |
814 | 2662 |
round((self.Points[-1].y + self.Segments[-1][1]) * scaley))) |
2663 |
else: |
|
2664 |
points = [] |
|
2665 |
dc.DrawLines(points) |
|
2666 |
dc.SetLogicalFunction(wx.COPY) |
|
2667 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2668 |
|
814 | 2669 |
if self.StartConnected is not None: |
2670 |
self.StartConnected.DrawHighlightment(dc) |
|
2671 |
self.StartConnected.Draw(dc) |
|
2672 |
if self.EndConnected is not None: |
|
2673 |
self.EndConnected.DrawHighlightment(dc) |
|
2674 |
self.EndConnected.Draw(dc) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2675 |
|
814 | 2676 |
# Draws the wire lines and points |
2677 |
def Draw(self, dc): |
|
2678 |
Graphic_Element.Draw(self, dc) |
|
2679 |
if not self.Valid: |
|
2680 |
dc.SetPen(MiterPen(wx.RED)) |
|
2681 |
dc.SetBrush(wx.RED_BRUSH) |
|
2682 |
elif isinstance(self.Value, BooleanType) and self.Value: |
|
2683 |
if self.Forced: |
|
2684 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
2685 |
dc.SetBrush(wx.CYAN_BRUSH) |
|
2686 |
else: |
|
2687 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
2688 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
2689 |
elif self.Value == "undefined": |
|
2690 |
dc.SetPen(MiterPen(wx.NamedColour("orange"))) |
|
2691 |
dc.SetBrush(wx.Brush(wx.NamedColour("orange"))) |
|
2692 |
elif self.Forced: |
|
2693 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
2694 |
dc.SetBrush(wx.BLUE_BRUSH) |
|
2695 |
else: |
|
2696 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
2697 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
2698 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
2699 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
2700 |
dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS) |
|
2701 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
2702 |
dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS) |
|
2703 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
2704 |
if len(self.Points) > 1: |
|
2705 |
points = [wx.Point(self.Points[0].x - self.Segments[0][0], self.Points[0].y - self.Segments[0][1])] |
|
2706 |
points.extend([point for point in self.Points[1:-1]]) |
|
2707 |
points.append(wx.Point(self.Points[-1].x + self.Segments[-1][0], self.Points[-1].y + self.Segments[-1][1])) |
|
2708 |
else: |
|
2709 |
points = [] |
|
2710 |
dc.DrawLines(points) |
|
2711 |
# Draw the segment selected in red |
|
2712 |
if not getattr(dc, "printing", False) and self.SelectedSegment is not None: |
|
2713 |
dc.SetPen(MiterPen(wx.BLUE, 3)) |
|
2714 |
if self.SelectedSegment == len(self.Segments) - 1: |
|
2715 |
end = 0 |
|
2716 |
else: |
|
2717 |
end = 1 |
|
2718 |
dc.DrawLine(self.Points[self.SelectedSegment].x - 1, self.Points[self.SelectedSegment].y, |
|
2719 |
self.Points[self.SelectedSegment + 1].x + end, self.Points[self.SelectedSegment + 1].y) |
|
2720 |
if self.Value is not None and not isinstance(self.Value, BooleanType) and self.Value != "undefined": |
|
2721 |
dc.SetFont(self.Parent.GetMiniFont()) |
|
2722 |
dc.SetTextForeground(wx.NamedColour("purple")) |
|
2723 |
if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)): |
|
2724 |
self.ValueSize = self.Parent.GetMiniTextExtent(self.ComputedValue) |
|
2725 |
if self.ValueSize is not None: |
|
2726 |
width, height = self.ValueSize |
|
2727 |
if self.BoundingBox[2] > width * 4 or self.BoundingBox[3] > height * 4: |
|
2728 |
x = self.Points[0].x + width * (self.StartPoint[1][0] - 1) / 2 |
|
2729 |
y = self.Points[0].y + height * (self.StartPoint[1][1] - 1) |
|
2730 |
dc.DrawText(self.ComputedValue, x, y) |
|
2731 |
x = self.Points[-1].x + width * (self.EndPoint[1][0] - 1) / 2 |
|
2732 |
y = self.Points[-1].y + height * (self.EndPoint[1][1] - 1) |
|
2733 |
dc.DrawText(self.ComputedValue, x, y) |
|
2734 |
else: |
|
2735 |
middle = len(self.Segments) / 2 + len(self.Segments) % 2 - 1 |
|
2736 |
x = (self.Points[middle].x + self.Points[middle + 1].x - width) / 2 |
|
2737 |
if self.BoundingBox[3] > height and self.Segments[middle] in [NORTH, SOUTH]: |
|
2738 |
y = (self.Points[middle].y + self.Points[middle + 1].y - height) / 2 |
|
2739 |
else: |
|
2740 |
y = self.Points[middle].y - height |
|
2741 |
dc.DrawText(self.ComputedValue, x, y) |
|
2742 |
dc.SetFont(self.Parent.GetFont()) |
|
2743 |
dc.SetTextForeground(wx.BLACK) |
|
2744 |
||
2745 |
||
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
2746 |
# ------------------------------------------------------------------------------- |
814 | 2747 |
# Graphic comment element |
1782
5b6ad7a7fd9d
clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset
|
2748 |
# ------------------------------------------------------------------------------- |
814 | 2749 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
2750 |
|
814 | 2751 |
def FilterHighlightsByRow(highlights, row, length): |
2752 |
_highlights = [] |
|
2753 |
for start, end, highlight_type in highlights: |
|
2754 |
if start[0] <= row and end[0] >= row: |
|
2755 |
if start[0] < row: |
|
2756 |
start = (row, 0) |
|
2757 |
if end[0] > row: |
|
2758 |
end = (row, length) |
|
2759 |
_highlights.append((start, end, highlight_type)) |
|
2760 |
return _highlights |
|
2761 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
2762 |
|
814 | 2763 |
def FilterHighlightsByColumn(highlights, start_col, end_col): |
2764 |
_highlights = [] |
|
2765 |
for start, end, highlight_type in highlights: |
|
2766 |
if end[1] > start_col and start[1] < end_col: |
|
2767 |
start = (start[0], max(start[1], start_col) - start_col) |
|
2768 |
end = (end[0], min(end[1], end_col) - start_col) |
|
2769 |
_highlights.append((start, end, highlight_type)) |
|
2770 |
return _highlights |
|
2771 |
||
2772 |
||
2773 |
class Comment(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
|
2774 |
""" |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
2775 |
Class that implements a comment |
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
2776 |
""" |
814 | 2777 |
|
2778 |
# Create a new comment |
|
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2779 |
def __init__(self, parent, content, id=None): |
814 | 2780 |
Graphic_Element.__init__(self, parent) |
2781 |
self.Id = id |
|
2782 |
self.Content = content |
|
2783 |
self.Pos = wx.Point(0, 0) |
|
2784 |
self.Size = wx.Size(0, 0) |
|
2785 |
self.Highlights = [] |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2786 |
|
814 | 2787 |
# Make a clone of this comment |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2788 |
def Clone(self, parent, id=None, pos=None): |
814 | 2789 |
comment = Comment(parent, self.Content, id) |
2790 |
if pos is not None: |
|
2791 |
comment.SetPosition(pos.x, pos.y) |
|
2792 |
comment.SetSize(self.Size[0], self.Size[1]) |
|
2793 |
return comment |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2794 |
|
814 | 2795 |
# Method for keeping compatibility with others |
2796 |
def Clean(self): |
|
2797 |
pass |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2798 |
|
814 | 2799 |
# Delete this comment by calling the corresponding method |
2800 |
def Delete(self): |
|
2801 |
self.Parent.DeleteComment(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2802 |
|
814 | 2803 |
# Refresh the comment bounding box |
2804 |
def RefreshBoundingBox(self): |
|
2805 |
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:
1631
diff
changeset
|
2806 |
|
814 | 2807 |
# Changes the comment size |
2808 |
def SetSize(self, width, height): |
|
2809 |
self.Size.SetWidth(width) |
|
2810 |
self.Size.SetHeight(height) |
|
2811 |
self.RefreshBoundingBox() |
|
2812 |
||
2813 |
# Returns the comment size |
|
2814 |
def GetSize(self): |
|
2815 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2816 |
|
814 | 2817 |
# Returns the comment minimum size |
2818 |
def GetMinSize(self): |
|
2819 |
dc = wx.ClientDC(self.Parent) |
|
2820 |
min_width = 0 |
|
2821 |
min_height = 0 |
|
2822 |
# The comment minimum size is the maximum size of words in the content |
|
2823 |
for line in self.Content.splitlines(): |
|
2824 |
for word in line.split(" "): |
|
2825 |
wordwidth, wordheight = dc.GetTextExtent(word) |
|
2826 |
min_width = max(min_width, wordwidth) |
|
2827 |
min_height = max(min_height, wordheight) |
|
2828 |
return min_width + 20, min_height + 20 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2829 |
|
814 | 2830 |
# Changes the comment position |
2831 |
def SetPosition(self, x, y): |
|
2832 |
self.Pos.x = x |
|
2833 |
self.Pos.y = y |
|
2834 |
self.RefreshBoundingBox() |
|
2835 |
||
2836 |
# Changes the comment content |
|
2837 |
def SetContent(self, content): |
|
2838 |
self.Content = content |
|
2839 |
min_width, min_height = self.GetMinSize() |
|
2840 |
self.Size[0] = max(self.Size[0], min_width) |
|
2841 |
self.Size[1] = max(self.Size[1], min_height) |
|
2842 |
self.RefreshBoundingBox() |
|
2843 |
||
2844 |
# Returns the comment content |
|
2845 |
def GetContent(self): |
|
2846 |
return self.Content |
|
2847 |
||
2848 |
# Returns the comment position |
|
2849 |
def GetPosition(self): |
|
2850 |
return self.Pos.x, self.Pos.y |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2851 |
|
814 | 2852 |
# Moves the comment |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset
|
2853 |
def Move(self, dx, dy, connected=True): |
814 | 2854 |
self.Pos.x += dx |
2855 |
self.Pos.y += dy |
|
2856 |
self.RefreshBoundingBox() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2857 |
|
814 | 2858 |
# Resizes the comment with the position and the size given |
2859 |
def Resize(self, x, y, width, height): |
|
2860 |
self.Move(x, y) |
|
2861 |
self.SetSize(width, height) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2862 |
|
814 | 2863 |
# Method called when a RightUp event have been generated |
2864 |
def OnRightUp(self, event, dc, scaling): |
|
2865 |
# Popup the default menu |
|
2866 |
self.Parent.PopupDefaultMenu() |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2867 |
|
814 | 2868 |
# Refreshes the wire state according to move defined and handle selected |
2869 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
2870 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE and self.Parent.CurrentLanguage == "LD": |
|
2871 |
movex = movey = 0 |
|
2872 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2873 |
|
814 | 2874 |
# Refreshes the comment model |
2875 |
def RefreshModel(self, move=True): |
|
2876 |
self.Parent.RefreshCommentModel(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2877 |
|
814 | 2878 |
# Method called when a LeftDClick event have been generated |
2879 |
def OnLeftDClick(self, event, dc, scaling): |
|
2880 |
# Edit the comment content |
|
2881 |
self.Parent.EditCommentContent(self) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2882 |
|
814 | 2883 |
# Adds an highlight to the comment |
2884 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
2885 |
if infos[0] == "content": |
|
2886 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2887 |
|
814 | 2888 |
# Removes an highlight from the comment |
2889 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
2890 |
RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2891 |
|
814 | 2892 |
# Removes all the highlights of one particular type from the comment |
2893 |
def ClearHighlight(self, highlight_type=None): |
|
2894 |
self.Highlights = ClearHighlights(self.Highlights, highlight_type) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2895 |
|
814 | 2896 |
# Draws the highlightment of this element if it is highlighted |
2897 |
def DrawHighlightment(self, dc): |
|
2898 |
scalex, scaley = dc.GetUserScale() |
|
2899 |
dc.SetUserScale(1, 1) |
|
2900 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
2901 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
2902 |
dc.SetLogicalFunction(wx.AND) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2903 |
|
814 | 2904 |
left = (self.Pos.x - 1) * scalex - 2 |
2905 |
right = (self.Pos.x + self.Size[0] + 1) * scalex + 2 |
|
2906 |
top = (self.Pos.y - 1) * scaley - 2 |
|
2907 |
bottom = (self.Pos.y + self.Size[1] + 1) * scaley + 2 |
|
2908 |
angle_top = (self.Pos.x + self.Size[0] - 9) * scalex + 2 |
|
2909 |
angle_right = (self.Pos.y + 9) * scaley - 2 |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2910 |
|
814 | 2911 |
polygon = [wx.Point(left, top), wx.Point(angle_top, top), |
2912 |
wx.Point(right, angle_right), wx.Point(right, bottom), |
|
2913 |
wx.Point(left, bottom)] |
|
2914 |
dc.DrawPolygon(polygon) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2915 |
|
814 | 2916 |
dc.SetLogicalFunction(wx.COPY) |
2917 |
dc.SetUserScale(scalex, scaley) |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2918 |
|
814 | 2919 |
# Draws the comment and its content |
2920 |
def Draw(self, dc): |
|
2921 |
Graphic_Element.Draw(self, dc) |
|
2922 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
2923 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
2924 |
# Draws the comment shape |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset
|
2925 |
polygon = [wx.Point(self.Pos.x, self.Pos.y), |
814 | 2926 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
2927 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10), |
|
2928 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1]), |
|
2929 |
wx.Point(self.Pos.x, self.Pos.y + self.Size[1])] |
|
2930 |
dc.DrawPolygon(polygon) |
|
1631
940e20a8865b
fix issue with printing scheme (FBD, LD or SFC) with comment element on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
2931 |
|
940e20a8865b
fix issue with printing scheme (FBD, LD or SFC) with comment element on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
2932 |
# dc.SetBrush call is workaround for the issue with wx.PrinterDC |
940e20a8865b
fix issue with printing scheme (FBD, LD or SFC) with comment element on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
2933 |
# with wxPython 3.0 on GNU/Linux (don't remove it) |
940e20a8865b
fix issue with printing scheme (FBD, LD or SFC) with comment element on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
2934 |
dc.SetBrush(wx.WHITE_BRUSH) |
814 | 2935 |
lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
2936 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10), |
|
2937 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)] |
|
2938 |
dc.DrawLines(lines) |
|
1631
940e20a8865b
fix issue with printing scheme (FBD, LD or SFC) with comment element on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1571
diff
changeset
|
2939 |
|
814 | 2940 |
# Draws the comment content |
2941 |
y = self.Pos.y + 10 |
|
2942 |
for idx, line in enumerate(self.Content.splitlines()): |
|
2943 |
first = True |
|
2944 |
linetext = "" |
|
2945 |
words = line.split(" ") |
|
2946 |
if not getattr(dc, "printing", False): |
|
2947 |
highlights = FilterHighlightsByRow(self.Highlights, idx, len(line)) |
|
2948 |
highlights_offset = 0 |
|
2949 |
for i, word in enumerate(words): |
|
2950 |
if first: |
|
2951 |
text = word |
|
2952 |
else: |
|
2953 |
text = linetext + " " + word |
|
2954 |
wordwidth, wordheight = dc.GetTextExtent(text) |
|
2955 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2956 |
break |
|
2957 |
if wordwidth < self.Size[0] - 20: |
|
2958 |
if i < len(words) - 1: |
|
2959 |
linetext = text |
|
2960 |
first = False |
|
2961 |
else: |
|
2962 |
dc.DrawText(text, self.Pos.x + 10, y) |
|
2963 |
if not getattr(dc, "printing", False): |
|
2964 |
DrawHighlightedText(dc, text, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(text)), self.Pos.x + 10, y) |
|
2965 |
highlights_offset += len(text) + 1 |
|
2966 |
y += wordheight + 5 |
|
2967 |
else: |
|
2968 |
if not first: |
|
2969 |
dc.DrawText(linetext, self.Pos.x + 10, y) |
|
2970 |
if not getattr(dc, "printing", False): |
|
2971 |
DrawHighlightedText(dc, linetext, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(linetext)), self.Pos.x + 10, y) |
|
2972 |
highlights_offset += len(linetext) + 1 |
|
2973 |
if first or i == len(words) - 1: |
|
2974 |
if not first: |
|
2975 |
y += wordheight + 5 |
|
2976 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2977 |
break |
|
2978 |
dc.DrawText(word, self.Pos.x + 10, y) |
|
2979 |
if not getattr(dc, "printing", False): |
|
2980 |
DrawHighlightedText(dc, word, FilterHighlightsByColumn(highlights, highlights_offset, highlights_offset + len(word)), self.Pos.x + 10, y) |
|
2981 |
highlights_offset += len(word) + 1 |
|
2982 |
else: |
|
2983 |
linetext = word |
|
2984 |
y += wordheight + 5 |
|
2985 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2986 |
break |