author  Edouard Tisserant 
Wed, 20 Oct 2021 08:57:07 +0200  
branch  wxPython4 
changeset 3344  4a08728a2ea4 
parent 3303  0ffb41625592 
parent 3333  dd49e4055a10 
permissions  rwrr 
814  1 
#!/usr/bin/env python 
2 
# * coding: utf8 * 

3 

1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
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 611313 automates supporting plcopen standard and CanFestival. 
814  6 
# 
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
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 021101301, USA. 
814  24 

1832
0f1081928d65
fix wrongimportorder. 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: "(noabsoluteimport) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1872
diff
changeset

26 
from __future__ import absolute_import 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

27 
from __future__ import division 
814  28 
from math import * 
2457
9deec258ab1a
python3 support: pylint, W1633 # (roundbuiltin) round builtin referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2450
diff
changeset

29 
from future.builtins import round 
2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2437
diff
changeset

30 
from six import string_types 
2432
dbc065a2f7a5
python3 support: pylint, W1613 # (xrangebuiltin) xrange builtin referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1881
diff
changeset

31 
from six.moves import xrange 
814  32 

1832
0f1081928d65
fix wrongimportorder. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1828
diff
changeset

33 
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

34 
from graphics.ToolTipProducer import ToolTipProducer 
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset

35 
from graphics.DebugDataConsumer import DebugDataConsumer 
1169
53e4a2b775a7
Move CustomToolTip from GraphicCommons to controls
Laurent Bessard
parents:
1166
diff
changeset

36 

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

37 
#  
814  38 
# Common constants 
1837
c507c363625e
fix pylint warning '(pointlessstringstatement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset

39 
# 
c507c363625e
fix pylint warning '(pointlessstringstatement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset

40 
# Definition of constants for dimensions of graphic elements 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset

41 
#  
814  42 

43 
# FBD and SFC constants 

44 
MIN_MOVE = 5 # Minimum move before starting a element dragging 

45 
CONNECTOR_SIZE = 8 # Size of connectors 

46 
BLOCK_LINE_SIZE = 20 # Minimum size of each line in a block 

47 
HANDLE_SIZE = 6 # Size of the squares for handles 

48 
ANCHOR_DISTANCE = 5 # Distance where wire is automativally attached to a connector 

49 
POINT_RADIUS = 2 # Radius of the point of wire ends 

50 
MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire 

51 

52 
# LD constants 

53 
LD_LINE_SIZE = 40 # Distance between two lines in a ladder rung 

54 
LD_ELEMENT_SIZE = (21, 15) # Size (width, height) of a ladder element (contact or coil) 

55 
LD_WIRE_SIZE = 30 # Size of a wire between two contact 

56 
LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact 

57 
LD_POWERRAIL_WIDTH = 3 # Width of a Powerrail 

58 
LD_OFFSET = (10, 10) # Distance (x, y) between each comment and rung of the ladder 

59 
LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box 

60 

61 
# SFC constants 

62 
SFC_STEP_DEFAULT_SIZE = (40, 30) # Default size of a SFC step 

63 
SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition 

64 
SFC_DEFAULT_SEQUENCE_INTERVAL = 40 # Default size of the interval between two divergence branches 

65 
SFC_SIMULTANEOUS_SEQUENCE_EXTRA = 20 # Size of extra lines for simultaneous divergence and convergence 

66 
SFC_JUMP_SIZE = (12, 13) # Size of a SFC jump to step 

67 
SFC_WIRE_MIN_SIZE = 25 # Size of a wire between two elements 

68 
SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line 

69 

70 
# Type definition constants for graphic elements 

71 
[INPUT, OUTPUT, INOUT] = range(3) 

72 
[CONNECTOR, CONTINUATION] = range(2) 

73 
[LEFTRAIL, RIGHTRAIL] = range(2) 

74 
[CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) 

75 
[COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET, COIL_RISING, COIL_FALLING] = range(6) 

76 
[SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) 

77 

78 
# Constants for defining the type of dragging that has been selected 

79 
[HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) 

80 

81 
# List of value for resize handle that are valid 

1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

82 
VALID_HANDLES = [(1, 1), (1, 2), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1), (2, 1)] 
814  83 

84 
# Contants for defining the direction of a connector 

1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

85 
[EAST, NORTH, WEST, SOUTH] = [(1, 0), (0, 1), (1, 0), (0, 1)] 
814  86 

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

87 
# Contants for defining which mode is selected for each view 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

88 
[MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

89 
MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, 
814  90 
MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION, MODE_MOTION] = range(15) 
91 

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

92 
# Contants for defining alignment types for graphic group 
814  93 
[ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6) 
94 

95 
# Contants for defining which drawing mode is selected for app 

96 
[FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] 

97 

98 
# Color for Highlighting 

99 
HIGHLIGHTCOLOR = wx.CYAN 

100 

101 
# Define highlight types 

102 
ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED) 

103 
SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE) 

104 

105 
# Define highlight refresh inhibition period in second 

106 
REFRESH_HIGHLIGHT_PERIOD = 0.1 

107 

108 
HANDLE_CURSORS = { 

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

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

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

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

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

113 
(1, 2): 4, 
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

114 
(3, 2): 4, 
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

115 
(2, 1): 5, 
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

116 
(2, 3): 5 
814  117 
} 
118 

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

119 

814  120 
def round_scaling(x, n, constraint=0): 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

121 
fraction = x / n 
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

122 
if constraint == 1: 
814  123 
xround = int(fraction) 
124 
else: 

125 
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

126 
if constraint == 1 and xround < fraction: 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

127 
xround += 1 
852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

128 
return int(xround * n) 
814  129 

1749
d73b64672238
cleanup: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset

130 

1837
c507c363625e
fix pylint warning '(pointlessstringstatement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset

131 
#  
c507c363625e
fix pylint warning '(pointlessstringstatement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset

132 
# Basic vector operations for calculate wire points 
c507c363625e
fix pylint warning '(pointlessstringstatement) String statement has no effect'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset

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

134 

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

135 
def vector(p1, p2, normal=True): 
1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

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

137 
Create a vector from two points and define if vector must be normal 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

138 
""" 
814  139 
vector = (p2.x  p1.x, p2.y  p1.y) 
140 
if normal: 

141 
return normalize(vector) 

142 
return vector 

143 

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

144 

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

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

147 
Calculate the norm of a given vector 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

148 
""" 
814  149 
return sqrt(v[0] * v[0] + v[1] * v[1]) 
150 

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

151 

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

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

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

155 
""" 
814  156 
v_norm = norm(v) 
157 
# Verifie if it is not a null vector 

158 
if v_norm > 0: 

159 
return (v[0] / v_norm, v[1] / v_norm) 

160 
else: 

161 
return v 

162 

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

163 

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

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

166 
Calculate the scalar product of two vectors 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

167 
""" 
814  168 
return v == (0, 0) 
169 

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

170 

814  171 
def add_vectors(v1, v2): 
1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

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

173 
Calculate the scalar product of two vectors 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

174 
""" 
814  175 
return (v1[0] + v2[0], v1[1] + v2[1]) 
176 

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

177 

814  178 
def product(v1, v2): 
1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

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

180 
Calculate the scalar product of two vectors 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

181 
""" 
814  182 
return v1[0] * v2[0] + v1[1] * v2[1] 
183 

184 

185 
def GetScaledEventPosition(event, dc, scaling): 

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

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

187 
Function that calculates the nearest point of the grid defined by scaling for the given point 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

188 
""" 
814  189 
pos = event.GetLogicalPosition(dc) 
190 
if scaling: 

2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

191 
pos.x = round(pos.x / scaling[0]) * scaling[0] 
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

192 
pos.y = round(pos.y / scaling[1]) * scaling[1] 
814  193 
return pos 
194 

195 

196 
def DirectionChoice(v_base, v_target, dir_target): 

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

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

198 
Function that choose a direction during the wire points generation 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

199 
""" 
814  200 
dir_product = product(v_base, v_target) 
201 
if dir_product < 0: 

202 
return (v_base[0], v_base[1]) 

203 
elif dir_product == 0 and product(v_base, dir_target) != 0: 

204 
return dir_target 

205 
return v_base 

206 

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

207 

814  208 
def MiterPen(colour, width=1, style=wx.SOLID): 
209 
pen = wx.Pen(colour, width, style) 

210 
pen.SetJoin(wx.JOIN_MITER) 

211 
pen.SetCap(wx.CAP_PROJECTING) 

212 
return pen 

213 

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

214 

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

215 
#  
814  216 
# Helpers for highlighting text 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset

217 
#  
814  218 

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

219 

814  220 
def AddHighlight(highlights, infos): 
221 
RemoveHighlight(highlights, infos) 

222 
highlights.append(infos) 

223 

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

224 

814  225 
def RemoveHighlight(highlights, infos): 
226 
if infos in highlights: 

227 
highlights.remove(infos) 

228 
return True 

229 
return False 

230 

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

231 

1872
866fb3ab8778
fix pylint error "(undefinedvariable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1870
diff
changeset

232 
def ClearHighlights(highlights, highlight_type=None): 
814  233 
if highlight_type is not None: 
234 
return [highlight for highlight in highlights if highlight[2] != highlight_type] 

235 
return [] 

236 

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

237 

814  238 
def DrawHighlightedText(dc, text, highlights, x, y): 
239 
current_pen = dc.GetPen() 

240 
dc.SetPen(wx.TRANSPARENT_PEN) 

241 
for start, end, highlight_type in highlights: 

242 
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

243 
offset_width, _offset_height = dc.GetTextExtent(text[:start[1]]) 
814  244 
part = text[start[1]:end[1] + 1] 
245 
part_width, part_height = dc.GetTextExtent(part) 

246 
dc.DrawRectangle(x + offset_width, y, part_width, part_height) 

247 
dc.SetTextForeground(highlight_type[1]) 

248 
dc.DrawText(part, x + offset_width, y) 

249 
dc.SetPen(current_pen) 

250 
dc.SetTextForeground(wx.BLACK) 

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

251 

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

252 

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

253 
#  
814  254 
# Graphic element base class 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset

255 
#  
814  256 

257 

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

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

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

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

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

262 

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

264 
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

265 
ToolTipProducer.__init__(self, parent) 
814  266 
self.Parent = parent 
267 
self.Id = id 

268 
self.oldPos = None 

269 
self.StartPos = None 

270 
self.CurrentDrag = None 

1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

271 
self.Handle = (None, None) 
814  272 
self.Dragging = False 
273 
self.Selected = False 

274 
self.Highlighted = False 

275 
self.Pos = wx.Point(0, 0) 

276 
self.Size = wx.Size(0, 0) 

277 
self.BoundingBox = wx.Rect(0, 0, 0, 0) 

278 
self.Visible = False 

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

279 

814  280 
def GetDefinition(self): 
281 
return [self.Id], [] 

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

282 

814  283 
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

284 
self.Visible = self.Selected or self.GetRedrawRect().Intersects(screen) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

285 

814  286 
def IsVisible(self): 
287 
return self.Visible 

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

288 

814  289 
def SpreadCurrent(self): 
290 
pass 

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

291 

814  292 
def GetConnectorTranslation(self, element): 
293 
return {} 

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

294 

814  295 
def FindNearestConnector(self, position, connectors): 
296 
distances = [] 

297 
for connector in connectors: 

298 
connector_pos = connector.GetRelPosition() 

299 
distances.append((sqrt((self.Pos.x + connector_pos.x  position.x) ** 2 + 

300 
(self.Pos.y + connector_pos.y  position.y) ** 2), 

301 
connector)) 

302 
distances.sort() 

303 
if len(distances) > 0: 

304 
return distances[0][1] 

305 
return None 

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

306 

814  307 
def IsOfType(self, type, reference): 
308 
return self.Parent.IsOfType(type, reference) 

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

309 

814  310 
def IsEndType(self, type): 
311 
return self.Parent.IsEndType(type) 

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

312 

814  313 
def GetDragging(self): 
314 
return self.Dragging 

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

315 

814  316 
# Make a clone of this element 
317 
def Clone(self, parent): 

318 
return Graphic_Element(parent, self.Id) 

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

319 

814  320 
# Changes the block position 
321 
def SetPosition(self, x, y): 

322 
self.Pos.x = x 

323 
self.Pos.y = y 

324 
self.RefreshConnected() 

325 
self.RefreshBoundingBox() 

326 

327 
# Returns the block position 

328 
def GetPosition(self): 

329 
return self.Pos.x, self.Pos.y 

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

330 

814  331 
# Changes the element size 
332 
def SetSize(self, width, height): 

333 
self.Size.SetWidth(width) 

334 
self.Size.SetHeight(height) 

335 
self.RefreshConnectors() 

336 
self.RefreshBoundingBox() 

337 

338 
# Returns the element size 

339 
def GetSize(self): 

340 
return self.Size.GetWidth(), self.Size.GetHeight() 

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

341 

814  342 
# Returns the minimum element size 
343 
def GetMinSize(self): 

344 
return 0, 0 

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

345 

852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

346 
# 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

347 
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

348 
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

349 
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

350 
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

351 
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

352 
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

353 
width = min_width 
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

354 
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

355 
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

356 
height = min_height 
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

357 
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

358 
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

359 
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

360 
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

361 
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

362 
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

363 
return self.Pos.x  posx, self.Pos.y  posy 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

364 

814  365 
# Refresh the element connectors position 
366 
def RefreshConnectors(self): 

367 
pass 

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

368 

814  369 
# Refresh the position of wires connected to element inputs and outputs 
1870
4d070115b552
fix pylint error '(toomanyfunctionargs) Too many positional arguments for function call'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1868
diff
changeset

370 
def RefreshConnected(self, exclude=None): 
814  371 
pass 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

372 

814  373 
# Change the parent 
374 
def SetParent(self, parent): 

375 
self.Parent = parent 

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

376 

814  377 
# Override this method for defining the method to call for deleting this element 
378 
def Delete(self): 

379 
pass 

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

380 

814  381 
# Returns the Id 
382 
def GetId(self): 

383 
return self.Id 

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

384 

814  385 
# Returns if the point given is in the bounding box 
386 
def HitTest(self, pt, connectors=True): 

387 
if connectors: 

388 
rect = self.BoundingBox 

389 
else: 

390 
rect = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) 

3303
0ffb41625592
Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset

391 
return rect.Contains(pt.x, pt.y) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

392 

814  393 
# Returns if the point given is in the bounding box 
394 
def IsInSelection(self, rect): 

3303
0ffb41625592
Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset

395 
return rect.Contains(self.BoundingBox.x, self.BoundingBox.y) and rect.Contains(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

396 

814  397 
# Override this method for refreshing the bounding box 
398 
def RefreshBoundingBox(self): 

399 
pass 

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

400 

814  401 
# Returns the bounding box 
402 
def GetBoundingBox(self): 

403 
return self.BoundingBox 

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

404 

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

406 
def GetRedrawRect(self, movex=0, movey=0): 
814  407 
scalex, scaley = self.Parent.GetViewScale() 
408 
rect = wx.Rect() 

409 
rect.x = self.BoundingBox.x  int(HANDLE_SIZE / scalex)  3  abs(movex) 

410 
rect.y = self.BoundingBox.y  int(HANDLE_SIZE / scaley)  3  abs(movey) 

411 
rect.width = self.BoundingBox.width + 2 * (int(HANDLE_SIZE / scalex) + abs(movex) + 1) + 4 

412 
rect.height = self.BoundingBox.height + 2 * (int(HANDLE_SIZE / scaley) + abs(movey) + 1) + 4 

413 
return rect 

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

414 

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

415 
def Refresh(self, rect=None): 
814  416 
if self.Visible: 
417 
if rect is not None: 

418 
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) 

419 
else: 

420 
self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False) 

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

421 

814  422 
# Change the variable that indicates if this element is selected 
423 
def SetSelected(self, selected): 

424 
self.Selected = selected 

425 
self.Refresh() 

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

426 

814  427 
# Change the variable that indicates if this element is highlighted 
428 
def SetHighlighted(self, highlighted): 

429 
self.Highlighted = highlighted 

430 
self.Refresh() 

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

431 

814  432 
# Test if the point is on a handle of this element 
433 
def TestHandle(self, event): 

434 
dc = self.Parent.GetLogicalDC() 

435 
scalex, scaley = dc.GetUserScale() 

436 
pos = event.GetPosition() 

437 
pt = wx.Point(*self.Parent.CalcUnscrolledPosition(pos.x, pos.y)) 

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

438 

814  439 
left = (self.BoundingBox.x  2) * scalex  HANDLE_SIZE 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

440 
center = (self.BoundingBox.x + self.BoundingBox.width // 2) * scalex  HANDLE_SIZE // 2 
814  441 
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

442 

814  443 
top = (self.BoundingBox.y  2) * scaley  HANDLE_SIZE 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

444 
middle = (self.BoundingBox.y + self.BoundingBox.height / 2) * scaley  HANDLE_SIZE // 2 
814  445 
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

446 

814  447 
extern_rect = wx.Rect(left, top, right + HANDLE_SIZE  left, bottom + HANDLE_SIZE  top) 
448 
intern_rect = wx.Rect(left + HANDLE_SIZE, top + HANDLE_SIZE, right  left  HANDLE_SIZE, bottom  top  HANDLE_SIZE) 

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

449 

814  450 
# Verify that this element is selected 
3303
0ffb41625592
Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2625
diff
changeset

451 
if self.Selected and extern_rect.Contains(pt.x, pt.y) and not intern_rect.Contains(pt.x, pt.y): 
814  452 
# Find if point is on a handle horizontally 
453 
if left <= pt.x < left + HANDLE_SIZE: 

454 
handle_x = 1 

455 
elif center <= pt.x < center + HANDLE_SIZE: 

456 
handle_x = 2 

457 
elif right <= pt.x < right + HANDLE_SIZE: 

458 
handle_x = 3 

459 
else: 

460 
handle_x = 0 

461 
# Find if point is on a handle vertically 

462 
if top <= pt.y < top + HANDLE_SIZE: 

463 
handle_y = 1 

464 
elif middle <= pt.y < middle + HANDLE_SIZE: 

465 
handle_y = 2 

466 
elif bottom <= pt.y < bottom + HANDLE_SIZE: 

467 
handle_y = 3 

468 
else: 

469 
handle_y = 0 

470 
# Verify that the result is valid 

471 
if (handle_x, handle_y) in VALID_HANDLES: 

472 
return handle_x, handle_y 

473 
return 0, 0 

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

474 

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

477 
# Test if an handle have been clicked 

478 
handle = self.TestHandle(event) 

479 
# Find which type of handle have been clicked, 

480 
# Save a resize event and change the cursor 

481 
cursor = HANDLE_CURSORS.get(handle, 1) 

482 
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) 

483 
if cursor > 1: 

484 
self.Handle = (HANDLE_RESIZE, handle) 

485 
else: 

486 
self.Handle = (HANDLE_MOVE, None) 

487 
self.SetSelected(False) 

488 
# Initializes the last position 

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

490 
self.StartPos = wx.Point(self.Pos.x, self.Pos.y) 

491 
self.CurrentDrag = wx.Point(0, 0) 

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

492 

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

495 
# If a dragging have been initiated 

496 
if self.Dragging and self.oldPos: 

497 
self.RefreshModel() 

498 
self.Parent.RefreshBuffer() 

499 
wx.CallAfter(self.Parent.SetCurrentCursor, 0) 

500 
self.SetSelected(True) 

501 
self.oldPos = None 

502 

503 
# Method called when a RightDown event have been generated 

504 
def OnRightDown(self, event, dc, scaling): 

505 
pass 

506 

507 
# Method called when a RightUp event have been generated 

508 
def OnRightUp(self, event, dc, scaling): 

509 
if self.Dragging and self.oldPos: 

510 
self.RefreshModel() 

511 
self.Parent.RefreshBuffer() 

512 
wx.CallAfter(self.Parent.SetCurrentCursor, 0) 

513 
self.SetSelected(True) 

514 
self.oldPos = None 

515 
if self.Parent.Debug: 

516 
self.Parent.PopupForceMenu() 

517 

518 
# Method called when a LeftDClick event have been generated 

519 
def OnLeftDClick(self, event, dc, scaling): 

520 
pass 

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

521 

814  522 
# Method called when a Motion event have been generated 
523 
def OnMotion(self, event, dc, scaling): 

524 
# If the cursor is dragging and the element have been clicked 

525 
if event.Dragging() and self.oldPos: 

526 
# 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

527 
pos = GetScaledEventPosition(event, dc, scaling) 
814  528 
movex = pos.x  self.oldPos.x 
529 
movey = pos.y  self.oldPos.y 

530 
# If movement is greater than MIN_MOVE then a dragging is initiated 

531 
if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): 

532 
self.Dragging = True 

533 
# If a dragging have been initiated, refreshes the element state 

534 
if self.Dragging: 

535 
dragx, dragy = self.ProcessDragging(movex, movey, event, scaling) 

536 
if event.ControlDown() and self.Handle[0] == HANDLE_MOVE: 

537 
self.oldPos.x = self.StartPos.x + self.CurrentDrag.x 

538 
self.oldPos.y = self.StartPos.y + self.CurrentDrag.y 

539 
else: 

540 
self.oldPos.x += dragx 

541 
self.oldPos.y += dragy 

542 
return dragx, dragy 

543 
return movex, movey 

544 
# If cursor just pass over the element, changes the cursor if it is on a handle 

545 
else: 

546 
pos = event.GetLogicalPosition(dc) 

547 
handle = self.TestHandle(event) 

548 
# Find which type of handle have been clicked, 

549 
# Save a resize event and change the cursor 

550 
cursor = HANDLE_CURSORS.get(handle, 0) 

551 
wx.CallAfter(self.Parent.SetCurrentCursor, cursor) 

552 
return 0, 0 

553 

554 
# Moves the element 

1852
70c1cc354a8f
fix pylint warning '(dangerousdefaultvalue) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset

555 
def Move(self, dx, dy, exclude=None): 
814  556 
self.Pos.x += max(self.BoundingBox.x, dx) 
557 
self.Pos.y += max(self.BoundingBox.y, dy) 

558 
self.RefreshConnected(exclude) 

559 
self.RefreshBoundingBox() 

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

560 

814  561 
# Resizes the element from position and size given 
562 
def Resize(self, x, y, width, height): 

563 
self.Move(x, y) 

564 
self.SetSize(width, height) 

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

565 

814  566 
# Refreshes the element state according to move defined and handle selected 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

567 
def ProcessDragging(self, movex, movey, event, scaling, width_fac=1, height_fac=1): 
814  568 
handle_type, handle = self.Handle 
569 
# If it is a resize handle, calculate the values from resizing 

570 
if handle_type == HANDLE_RESIZE: 

571 
if scaling is not None: 

572 
scaling = (scaling[0] * width_fac, scaling[1] * height_fac) 

573 
x = y = start_x = start_y = 0 

574 
width, height = start_width, start_height = self.GetSize() 

575 
if handle[0] == 1: 

576 
movex = max(self.BoundingBox.x, movex) 

577 
if scaling is not None: 

578 
movex = (round_scaling(width  movex, scaling[0])  width) 

579 
x = movex 

580 
if event.ShiftDown(): 

581 
width = 2 * movex 

582 
else: 

583 
width = movex 

584 
elif handle[0] == 3: 

585 
if scaling is not None: 

586 
movex = round_scaling(width + movex, scaling[0])  width 

587 
if event.ShiftDown(): 

588 
movex = min(self.BoundingBox.x, movex) 

589 
x = movex 

590 
width += 2 * movex 

591 
else: 

592 
width += movex 

593 
if handle[1] == 1: 

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

595 
if scaling is not None: 

596 
movey = (round_scaling(height  movey, scaling[1])  height) 

597 
y = movey 

598 
if event.ShiftDown(): 

599 
height = 2 * movey 

600 
else: 

601 
height = movey 

602 
elif handle[1] == 3: 

603 
if scaling is not None: 

604 
movey = round_scaling(height + movey, scaling[1])  height 

605 
if event.ShiftDown(): 

606 
movey = min(self.BoundingBox.y, movey) 

607 
y = movey 

608 
height += 2 * movey 

609 
else: 

610 
height += movey 

611 
# Verify that new size is not lesser than minimum 

612 
min_width, min_height = self.GetMinSize() 

613 
if handle[0] != 2 and (width >= min_width or width > self.Size[0]): 

614 
start_x = x 

615 
start_width = width 

616 
else: 

617 
movex = 0 

618 
if handle[1] != 2 and (height >= min_height or height > self.Size[1]): 

619 
start_y = y 

620 
start_height = height 

621 
else: 

622 
movey = 0 

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

624 
self.Resize(start_x, start_y, start_width, start_height) 

625 
return movex, movey 

626 
# If it is a move handle, Move this element 

627 
elif handle_type == HANDLE_MOVE: 

628 
movex = max(self.BoundingBox.x, movex) 

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

630 
if scaling is not None: 

631 
movex = round_scaling(self.Pos.x + movex, scaling[0])  self.Pos.x 

632 
movey = round_scaling(self.Pos.y + movey, scaling[1])  self.Pos.y 

633 
if event.ControlDown(): 

634 
self.CurrentDrag.x = self.CurrentDrag.x + movex 

635 
self.CurrentDrag.y = self.CurrentDrag.y + movey 

636 
if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y): 

637 
movex = self.StartPos.x + self.CurrentDrag.x  self.Pos.x 

638 
movey = self.StartPos.y  self.Pos.y 

639 
else: 

640 
movex = self.StartPos.x  self.Pos.x 

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

641 
movey = self.StartPos.y + self.CurrentDrag.y  self.Pos.y 
814  642 
self.Move(movex, movey) 
643 
return movex, movey 

644 
return 0, 0 

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

645 

814  646 
# Override this method for defining the method to call for adding an highlight to this element 
647 
def AddHighlight(self, infos, start, end, highlight_type): 

648 
pass 

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

649 

814  650 
# Override this method for defining the method to call for removing an highlight from this element 
651 
def RemoveHighlight(self, infos, start, end, highlight_type): 

652 
pass 

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

653 

814  654 
# Override this method for defining the method to call for removing all the highlights of one particular type from this element 
655 
def ClearHighlight(self, highlight_type=None): 

656 
pass 

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

657 

814  658 
# Override this method for defining the method to call for refreshing the model of this element 
659 
def RefreshModel(self, move=True): 

660 
pass 

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

661 

814  662 
# Draws the highlightment of this element if it is highlighted (can be overwritten) 
663 
def DrawHighlightment(self, dc): 

664 
scalex, scaley = dc.GetUserScale() 

665 
dc.SetUserScale(1, 1) 

666 
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) 

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

668 
dc.SetLogicalFunction(wx.AND) 

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

669 
dc.DrawRectangle(int(round((self.Pos.x  1) * scalex))  2, 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

670 
int(round((self.Pos.y  1) * scaley))  2, 
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

671 
int(round((self.Size.width + 3) * scalex)) + 5, 
814  672 
int(round((self.Size.height + 3) * scaley)) + 5) 
673 
dc.SetLogicalFunction(wx.COPY) 

674 
dc.SetUserScale(scalex, scaley) 

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

675 

814  676 
# Draws the handles of this element if it is selected 
677 
def Draw(self, dc): 

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

679 
if self.Highlighted: 

680 
self.DrawHighlightment(dc) 

681 
if self.Selected: 

682 
scalex, scaley = dc.GetUserScale() 

683 
dc.SetUserScale(1, 1) 

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

685 
dc.SetBrush(wx.BLACK_BRUSH) 

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

686 

814  687 
left = (self.BoundingBox.x  2) * scalex  HANDLE_SIZE 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

688 
center = (self.BoundingBox.x + self.BoundingBox.width // 2) * scalex  HANDLE_SIZE // 2 
814  689 
right = (self.BoundingBox.x + self.BoundingBox.width + 2) * scalex 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

690 

814  691 
top = (self.BoundingBox.y  2) * scaley  HANDLE_SIZE 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

692 
middle = (self.BoundingBox.y + self.BoundingBox.height // 2) * scaley  HANDLE_SIZE // 2 
814  693 
bottom = (self.BoundingBox.y + self.BoundingBox.height + 2) * scaley 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

694 

814  695 
for x, y in [(left, top), (center, top), (right, top), 
696 
(left, middle), (right, middle), 

697 
(left, bottom), (center, bottom), (right, bottom)]: 

698 
dc.DrawRectangle(x, y, HANDLE_SIZE, HANDLE_SIZE) 

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

699 

814  700 
dc.SetUserScale(scalex, scaley) 
701 

702 

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

703 
#  
814  704 
# Group of graphic elements 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset

705 
#  
814  706 

707 

708 
class Graphic_Group(Graphic_Element): 

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

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

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

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

712 

814  713 
# Create a new group of graphic elements 
714 
def __init__(self, parent): 

715 
Graphic_Element.__init__(self, parent) 

716 
self.Elements = [] 

717 
self.RefreshWireExclusion() 

718 
self.RefreshBoundingBox() 

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

719 

814  720 
# Destructor 
721 
def __del__(self): 

722 
self.Elements = [] 

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

723 

814  724 
def GetDefinition(self): 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

725 
blocks = [] 
814  726 
wires = [] 
727 
for element in self.Elements: 

728 
block, wire = element.GetDefinition() 

729 
blocks.extend(block) 

730 
wires.extend(wire) 

731 
return blocks, wires 

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

732 

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

734 
def Clone(self, parent, pos=None): 
814  735 
group = Graphic_Group(parent) 
736 
connectors = {} 

737 
exclude_names = {} 

738 
wires = [] 

739 
if pos is not None: 

740 
dx, dy = pos.x  self.BoundingBox.x, pos.y  self.BoundingBox.y 

741 
for element in self.Elements: 

742 
if isinstance(element, Wire): 

743 
wires.append(element) 

744 
else: 

745 
if pos is not None: 

746 
x, y = element.GetPosition() 

747 
new_pos = wx.Point(x + dx, y + dy) 

748 
newid = parent.GetNewId() 

749 
if parent.IsNamedElement(element): 

750 
name = parent.GenerateNewName(element, exclude_names) 

751 
exclude_names[name.upper()] = True 

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

752 
new_element = element.Clone(parent, newid, name, pos=new_pos) 
814  753 
else: 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

754 
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

755 
new_element.SetBestSize(parent.Scaling) 
814  756 
else: 
757 
new_element = element.Clone(parent) 

758 
connectors.update(element.GetConnectorTranslation(new_element)) 

759 
group.SelectElement(new_element) 

760 
for element in wires: 

761 
if pos is not None: 

762 
new_wire = element.Clone(parent, connectors, dx, dy) 

763 
else: 

764 
new_wire = element.Clone(parent, connectors) 

765 
if new_wire is not None: 

766 
if pos is not None: 

767 
parent.AddWire(new_wire) 

768 
group.SelectElement(new_wire) 

769 
if pos is not None: 

770 
for element in group.Elements: 

771 
if not isinstance(element, Wire): 

772 
parent.AddBlockInModel(element) 

773 
return group 

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

774 

814  775 
def CanAddBlocks(self, parent): 
776 
valid = True 

777 
for element in self.Elements: 

778 
if not isinstance(element, Wire): 

779 
valid &= parent.CanAddElement(element) 

780 
return valid 

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

781 

814  782 
def IsVisible(self): 
783 
for element in self.Elements: 

784 
if element.IsVisible(): 

785 
return True 

786 
return False 

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

787 

814  788 
# Refresh the list of wire excluded 
789 
def RefreshWireExclusion(self): 

790 
self.WireExcluded = [] 

791 
for element in self.Elements: 

792 
if isinstance(element, Wire): 

793 
startblock = element.StartConnected.GetParentBlock() 

794 
endblock = element.EndConnected.GetParentBlock() 

795 
if startblock in self.Elements and endblock in self.Elements: 

796 
self.WireExcluded.append(element) 

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

797 

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

799 
def GetRedrawRect(self, movex=0, movey=0): 
814  800 
rect = None 
801 
for element in self.Elements: 

802 
if rect is None: 

803 
rect = element.GetRedrawRect(movex, movey) 

804 
else: 

805 
rect = rect.Union(element.GetRedrawRect(movex, movey)) 

806 
return rect 

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

807 

814  808 
# Clean this group of elements 
809 
def Clean(self): 

810 
# Clean all the elements of the group 

811 
for element in self.Elements: 

812 
element.Clean() 

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

813 

814  814 
# Delete this group of elements 
815 
def Delete(self): 

816 
# Delete all the elements of the group 

817 
for element in self.Elements: 

818 
element.Delete() 

819 
self.WireExcluded = [] 

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

820 

814  821 
# Returns if the point given is in the bounding box of one of the elements of this group 
822 
def HitTest(self, pt, connectors=True): 

823 
result = False 

824 
for element in self.Elements: 

825 
result = element.HitTest(pt, connectors) 

826 
return result 

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

827 

814  828 
# Returns if the element given is in this group 
829 
def IsElementIn(self, element): 

830 
return element in self.Elements 

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

831 

814  832 
# Change the elements of the group 
833 
def SetElements(self, elements): 

834 
self.Elements = elements 

835 
self.RefreshWireExclusion() 

836 
self.RefreshBoundingBox() 

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

837 

814  838 
# Returns the elements of the group 
839 
def GetElements(self): 

840 
return self.Elements 

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

841 

814  842 
# Align the group elements 
843 
def AlignElements(self, horizontally, vertically): 

844 
minx = self.BoundingBox.x + self.BoundingBox.width 

845 
miny = self.BoundingBox.y + self.BoundingBox.height 

846 
maxx = self.BoundingBox.x 

847 
maxy = self.BoundingBox.y 

848 
for element in self.Elements: 

849 
if not isinstance(element, Wire): 

850 
posx, posy = element.GetPosition() 

851 
width, height = element.GetSize() 

852 
minx = min(minx, posx) 

853 
miny = min(miny, posy) 

854 
maxx = max(maxx, posx + width) 

855 
maxy = max(maxy, posy + height) 

856 
for element in self.Elements: 

857 
if not isinstance(element, Wire): 

858 
posx, posy = element.GetPosition() 

859 
width, height = element.GetSize() 

860 
movex = movey = 0 

861 
if horizontally == ALIGN_LEFT: 

862 
movex = minx  posx 

863 
elif horizontally == ALIGN_CENTER: 

2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

864 
movex = (maxx + minx  width) // 2  posx 
814  865 
elif horizontally == ALIGN_RIGHT: 
866 
movex = maxx  width  posx 

867 
if vertically == ALIGN_TOP: 

868 
movey = miny  posy 

869 
elif vertically == ALIGN_MIDDLE: 

2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

870 
movey = (maxy + miny  height) // 2  posy 
814  871 
elif vertically == ALIGN_BOTTOM: 
872 
movey = maxy  height  posy 

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

874 
element.Move(movex, movey) 

875 
element.RefreshModel() 

876 
self.RefreshBoundingBox() 

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

877 

1226
93e7a8abce5e
Fixed lag when selecting all elements using CTRL+A or after paste a lot of blocks
Laurent Bessard
parents:
1176
diff
changeset

878 
# 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

879 
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

880 
self.Elements.append(element) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

881 

814  882 
# Remove or select the given element if it is or not in the group 
883 
def SelectElement(self, element): 

884 
if element in self.Elements: 

885 
self.Elements.remove(element) 

886 
else: 

887 
self.Elements.append(element) 

888 
self.RefreshWireExclusion() 

889 
self.RefreshBoundingBox() 

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

890 

814  891 
# Move this group of elements 
892 
def Move(self, movex, movey): 

893 
movex = max(self.BoundingBox.x, movex) 

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

895 
# Move all the elements of the group 

896 
for element in self.Elements: 

897 
if not isinstance(element, Wire): 

898 
element.Move(movex, movey, self.WireExcluded) 

899 
elif element in self.WireExcluded: 

900 
element.Move(movex, movey, True) 

901 
self.RefreshBoundingBox() 

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

902 

814  903 
# Refreshes the bounding box of this group of elements 
904 
def RefreshBoundingBox(self): 

905 
if len(self.Elements) > 0: 

906 
bbox = self.Elements[0].GetBoundingBox() 

907 
minx, miny = bbox.x, bbox.y 

908 
maxx = bbox.x + bbox.width 

909 
maxy = bbox.y + bbox.height 

910 
for element in self.Elements[1:]: 

911 
bbox = element.GetBoundingBox() 

912 
minx = min(minx, bbox.x) 

913 
miny = min(miny, bbox.y) 

914 
maxx = max(maxx, bbox.x + bbox.width) 

915 
maxy = max(maxy, bbox.y + bbox.height) 

916 
self.BoundingBox = wx.Rect(minx, miny, maxx  minx, maxy  miny) 

917 
else: 

918 
self.BoundingBox = wx.Rect(0, 0, 0, 0) 

919 
self.Pos = wx.Point(self.BoundingBox.x, self.BoundingBox.y) 

920 
self.Size = wx.Size(self.BoundingBox.width, self.BoundingBox.height) 

921 

922 
# Forbids to change the group position 

1868
616c3f4bcbcb
fix pylint error '(noselfargument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset

923 
def SetPosition(self, x, y): 
814  924 
pass 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

925 

814  926 
# 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

927 
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

928 
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

929 
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

930 
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

931 
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

932 
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

933 
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

934 
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

935 
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

936 
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

937 
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

938 
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

939 
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

940 
return posx, posy 
814  941 
return self.BoundingBox.x, self.BoundingBox.y 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

942 

814  943 
# Forbids to change the group size 
1868
616c3f4bcbcb
fix pylint error '(noselfargument) Method should have "self" as first argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1857
diff
changeset

944 
def SetSize(self, width, height): 
814  945 
pass 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

946 

814  947 
# Returns the size of this group 
948 
def GetSize(self): 

949 
return self.BoundingBox.width, self.BoundingBox.height 

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

950 

852
1009f956d2ee
Fix support for adjusting block size to block minimum size and to Viewer scaling
Laurent Bessard
parents:
825
diff
changeset

951 
# 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

952 
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

953 
max_movex = max_movey = 0 
814  954 
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

955 
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

956 
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

957 
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

958 
return max_movex, max_movey 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

959 

814  960 
# Refreshes the group elements to move defined and handle selected 
961 
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

962 
handle_type, _handle = self.Handle 
814  963 
# If it is a move handle, Move this group elements 
964 
if handle_type == HANDLE_MOVE: 

965 
movex = max(self.BoundingBox.x, movex) 

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

967 
if scaling is not None: 

968 
movex = round_scaling(movex, scaling[0]) 

969 
movey = round_scaling(movey, scaling[1]) 

970 
if event.ControlDown(): 

971 
self.CurrentDrag.x = self.CurrentDrag.x + movex 

972 
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

973 
posx, posy = self.GetPosition(True) 
814  974 
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

975 
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

976 
movey = self.StartPos.y  posy 
814  977 
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

978 
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

979 
movey = self.StartPos.y + self.CurrentDrag.y  posy 
814  980 
self.Move(movex, movey) 
981 
return movex, movey 

982 
return 0, 0 

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

983 

814  984 
# Change the variable that indicates if this element is highlighted 
985 
def SetHighlighted(self, highlighted): 

986 
for element in self.Elements: 

987 
element.SetHighlighted(highlighted) 

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

988 

814  989 
def HighlightPoint(self, pos): 
990 
for element in self.Elements: 

991 
if isinstance(element, Wire): 

992 
element.HighlightPoint(pos) 

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

993 

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

996 
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

997 
self.StartPos = wx.Point(*self.GetPosition(True)) 
814  998 
for element in self.Elements: 
999 
element.Handle = self.Handle 

1000 

1001 
# Change the variable that indicates if the elemente is selected 

1002 
def SetSelected(self, selected): 

1003 
for element in self.Elements: 

1004 
element.SetSelected(selected) 

1005 

1006 
# Method called when a RightUp event has been generated 

1007 
def OnRightUp(self, event, dc, scaling): 

1008 
# Popup the menu with special items for a group 

1009 
self.Parent.PopupGroupMenu() 

1010 

1011 
# 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

1012 
def RefreshModel(self, move=True): 
814  1013 
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

1014 
element.RefreshModel(move) 
814  1015 

1069
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset

1016 
# 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

1017 
def Draw(self, dc): 
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset

1018 
for element in self.Elements: 
880ec628d490
Fixed refresh bugs when drag'n dropping of group of elements
Laurent Bessard
parents:
1054
diff
changeset

1019 
element.Draw(dc) 
814  1020 

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

1021 

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

1022 
#  
814  1023 
# Connector for all types of blocks 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1777
diff
changeset

1024 
#  
814  1025 

1026 

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

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

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

1029 
Class that implements a connector for any type of block 
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

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

1031 

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

1033 
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

1034 
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

1035 
ToolTipProducer.__init__(self, parent.Parent) 
814  1036 
self.ParentBlock = parent 
1037 
self.Name = name 

1038 
self.Type = type 

1039 
self.Pos = position 

1040 
self.Direction = direction 

1041 
self.Wires = [] 

1042 
if self.ParentBlock.IsOfType("BOOL", type): 

1043 
self.Negated = negated 

1044 
self.Edge = edge 

1045 
else: 

1046 
self.Negated = False 

1047 
self.Edge = "none" 

1048 
self.OneConnected = onlyone 

1049 
self.Valid = True 

1050 
self.Value = None 

1051 
self.Forced = False 

1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1052 
self.ValueSize = None 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1053 
self.ComputedValue = None 
814  1054 
self.Selected = False 
1055 
self.Highlights = [] 

1056 
self.RefreshNameSize() 

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

1057 

814  1058 
def Flush(self): 
1059 
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

1060 
for wire, _handle in self.Wires: 
814  1061 
wire.Flush() 
1062 
self.Wires = [] 

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

1063 

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

1065 
def GetRedrawRect(self, movex=0, movey=0): 
3333
dd49e4055a10
Fixed two exceptions happening when interacting with viewer(s) in debug mode, when debug data is still not initialized.
Edouard Tisserant
parents:
2625
diff
changeset

1066 
if self.ParentBlock == None: 
dd49e4055a10
Fixed two exceptions happening when interacting with viewer(s) in debug mode, when debug data is still not initialized.
Edouard Tisserant
parents:
2625
diff
changeset

1067 
return None 
814  1068 
parent_pos = self.ParentBlock.GetPosition() 
1069 
x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE) 

1070 
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

1071 
has_modifier = self.Negated or self.Edge != "none" 
814  1072 
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

1073 
width = 10 if has_modifier else 5 
814  1074 
else: 
1075 
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

1076 
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

1077 
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

1078 
width += 5 
814  1079 
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

1080 
height = 10 if has_modifier else 5 
814  1081 
else: 
1082 
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

1083 
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

1084 
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

1085 
height += 5 
1758
845ca626db09
cleanup: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1754
diff
changeset

1086 
rect = wx.Rect(x  abs(movex), y  abs(movey), width + 2 * abs(movex), height + 2 * abs(movey)) 
2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2437
diff
changeset

1087 
if self.ValueSize is None and isinstance(self.ComputedValue, string_types): 
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1088 
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

1089 
if self.ValueSize is not None: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1090 
width, height = self.ValueSize 
1777
c46ec818bdd7
cleanup: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset

1091 
rect = rect.Union( 
c46ec818bdd7
cleanup: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset

1092 
wx.Rect( 
1764
d5df428640ff
cleanup: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1759
diff
changeset

1093 
parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] + 
2437
105c20fdeb19
python3 support: pylint, W1619 #(olddivision) division w/o __future__ statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

1094 
width * (self.Direction[0]  1) // 2, 
1764
d5df428640ff
cleanup: fix PEP8 E502 the backslash is redundant between brackets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1759
diff
changeset

1095 
parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] + 
1777
c46ec818bdd7
cleanup: fix PEP8 E131 continuation line unaligned for hanging indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1768
diff
changeset

1096 
height * (self.Direction[1]  1), 
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1097 
width, height)) 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

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

1099 

814  1100 
# Change the connector selection 
1101 
def SetSelected(self, selected): 

1102 
self.Selected = selected 

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

1103 

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

1105 
def Clone(self, parent=None): 
814  1106 
if parent is None: 
1107 
parent = self.ParentBlock 

1108 
return Connector(parent, self.Name, self.Type, wx.Point(self.Pos[0], self.Pos[1]), 

1768
691083b5682a
cleanup: fix PEP8 E128 continuation line underindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset

1109 
self.Direction, self.Negated) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1110 

814  1111 
# Returns the connector parent block 
1112 
def GetParentBlock(self): 

1113 
return self.ParentBlock 

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

1114 

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

1116 
def GetType(self, raw=False): 
814  1117 
if self.ParentBlock.IsEndType(self.Type) or raw: 
1118 
return self.Type 

1119 
elif (self.Negated or self.Edge != "none") and self.ParentBlock.IsOfType("BOOL", self.Type): 

1120 
return "BOOL" 

1121 
else: 

1122 
return self.ParentBlock.GetConnectionResultType(self, self.Type) 

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

1123 

814  1124 
# Returns the connector type 
1125 
def GetConnectedType(self): 

1126 
if self.ParentBlock.IsEndType(self.Type): 

1127 
return self.Type 

1128 
elif len(self.Wires) == 1: 

1129 
return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1]) 

1130 
return self.Type 

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

1131 

814  1132 
# Returns the connector type 
1133 
def GetConnectedRedrawRect(self, movex, movey): 

1134 
rect = None 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

1135 
for wire, _handle in self.Wires: 
814  1136 
if rect is None: 
1137 
rect = wire.GetRedrawRect() 

1138 
else: 

1139 
rect = rect.Union(wire.GetRedrawRect()) 

1140 
return rect 

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

1141 

814  1142 
# Returns if connector type is compatible with type given 
1143 
def IsCompatible(self, type): 

1144 
reference = self.GetType() 

1145 
return self.ParentBlock.IsOfType(type, reference) or self.ParentBlock.IsOfType(reference, type) 

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

1146 

814  1147 
# Changes the connector name 
1148 
def SetType(self, type): 

1149 
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

1150 
for wire, _handle in self.Wires: 
814  1151 
wire.SetValid(wire.IsConnectedCompatible()) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1152 

814  1153 
# Returns the connector name 
1154 
def GetName(self): 

1155 
return self.Name 

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

1156 

814  1157 
# Changes the connector name 
1158 
def SetName(self, name): 

1159 
self.Name = name 

1160 
self.RefreshNameSize() 

1161 

1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1162 
def SetForced(self, forced): 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1163 
if self.Forced != forced: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1164 
self.Forced = forced 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1165 
if self.Visible: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1166 
self.Parent.ElementNeedRefresh(self) 
1730
64d8f52bc8c8
cleanup 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 GetComputedValue(self): 
2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2437
diff
changeset

1169 
if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, bool): 
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
1173
diff
changeset

1170 
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

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

1172 

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

1173 
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

1174 
return self.GetComputedValue() 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1175 

1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1176 
def SetValue(self, value): 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1177 
if self.Value != value: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1178 
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

1179 
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

1180 
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

1181 
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

1182 
self.SetToolTipText(self.ComputedValue) 
1166
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1183 
if len(self.ComputedValue) > 4: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1184 
self.ComputedValue = self.ComputedValue[:4] + "..." 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1185 
self.ValueSize = None 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1186 
if self.ParentBlock.Visible: 
2ed9675be08d
Added support for displaying value of unconnected block connectors in debug
Laurent Bessard
parents:
1120
diff
changeset

1187 
self.ParentBlock.Parent.ElementNeedRefresh(self) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1188 

814  1189 
def RefreshForced(self): 
1190 
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

1191 
for wire, _handle in self.Wires: 
814  1192 
self.Forced = wire.IsForced() 
1193 

1194 
def RefreshValue(self): 

1195 
self.Value = self.ReceivingCurrent() 

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

1196 

814  1197 
def RefreshValid(self): 
1198 
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

1199 
for wire, _handle in self.Wires: 
814  1200 
self.Valid &= wire.GetValid() 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1201 

814  1202 
def ReceivingCurrent(self): 
1203 
current = False 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

1204 
for wire, _handle in self.Wires: 
814  1205 
value = wire.GetValue() 
2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2437
diff
changeset

1206 
if current != "undefined" and isinstance(value, bool): 
814  1207 
current = wire.GetValue() 
1208 
elif value == "undefined": 

1209 
current = "undefined" 

1210 
return current 

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

1211 

814  1212 
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

1213 
for wire, _handle in self.Wires: 
814  1214 
wire.SetValue(spreading) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1215 

814  1216 
# Changes the connector name size 
1217 
def RefreshNameSize(self): 

1218 
if self.Name != "": 

1219 
self.NameSize = self.ParentBlock.Parent.GetTextExtent(self.Name) 

1220 
else: 

1221 
self.NameSize = 0, 0 

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

1222 

814  1223 
# Returns the connector name size 
1224 
def GetNameSize(self): 

1225 
return self.NameSize 

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

1226 

814  1227 
# Returns the wires connected to the connector 
1228 
def GetWires(self): 

1229 
return self.Wires 

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

1230 

814  1231 
# Returns the parent block Id 
1232 
def GetBlockId(self): 

1233 
return self.ParentBlock.GetId() 

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

1234 

814  1235 
# Returns the connector relative position 
1236 
def GetRelPosition(self): 

1237 
return self.Pos 

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

1238 

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

1240 
def GetPosition(self, size=True): 
814  1241 
parent_pos = self.ParentBlock.GetPosition() 
1242 
# If the position of the end of the connector is asked 

1243 
if size: 

1244 
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE 

1245 
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE 

1246 
else: 

1247 
x = parent_pos[0] + self.Pos.x 

1248 
y = parent_pos[1] + self.Pos.y 

1249 
return wx.Point(x, y) 

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

1250 

814  1251 
# Change the connector relative position 
1252 
def SetPosition(self, pos): 

1253 
self.Pos = pos 

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

1254 

814  1255 
# Returns the connector direction 
1256 
def GetDirection(self): 

1257 
return self.Direction 

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

1258 

814  1259 
# Change the connector direction 
1260 
def SetDirection(self, direction): 

1261 
self.Direction = direction 

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

1262 

814  1263 
# Connect a wire to this connector at the last place 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

1264 
def Connect(self, wire, refresh=True): 
814  1265 
self.InsertConnect(len(self.Wires), wire, refresh) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1266 

814  1267 
# Connect a wire to this connector at the place given 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

1268 
def InsertConnect(self, idx, wire, refresh=True): 
814  1269 
if wire not in self.Wires: 
1270 
self.Wires.insert(idx, wire) 

1054
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset

1271 
if wire[1] == 0: 
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset

1272 
wire[0].ConnectStartPoint(None, self) 
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset

1273 
else: 
ef514eaacd8c
Fixed connections of block when changing block type
Laurent Bessard
parents:
1047
diff
changeset

1274 
wire[0].ConnectEndPoint(None, self) 
814  1275 
if refresh: 
1276 
self.ParentBlock.RefreshModel(False) 

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

1277 

814  1278 
# Returns the index of the wire given in the list of connected 
1279 
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

1280 
for i, (tmp_wire, _handle) in enumerate(self.Wires): 
814  1281 
if tmp_wire == wire: 
1282 
return i 

1283 
return None 

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

1284 

814  1285 
# Unconnect a wire or all wires connected to the connector 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

1286 
def UnConnect(self, wire=None, unconnect=True, delete=False): 
814  1287 
i = 0 
1288 
found = False 

1289 
while i < len(self.Wires) and not found: 

1290 
if not wire or self.Wires[i][0] == wire: 

1291 
# If Unconnect haven't been called from a wire, disconnect the connector in the wire 

1292 
if unconnect: 

1293 
if self.Wires[i][1] == 0: 

1294 
self.Wires[i][0].UnConnectStartPoint(delete) 

1295 
else: 

1296 
self.Wires[i][0].UnConnectEndPoint(delete) 

1297 
# Remove wire from connected 

1298 
if wire: 

1299 
self.Wires.pop(i) 

1300 
found = True 

1301 
i += 1 

1302 
# If no wire defined, unconnect all wires 

1303 
if not wire: 

1304 
self.Wires = [] 

857
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset

1305 
if not delete: 
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset

1306 
self.RefreshValid() 
9695969796d0
Adding support for quickly changing variable and connection type
Laurent Bessard
parents:
852
diff
changeset

1307 
self.ParentBlock.RefreshModel(False) 
1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1631
diff
changeset

1308 

814  1309 
# Returns if connector has one or more wire connected 
1310 
def IsConnected(self): 

1311 
return len(self.Wires) > 0 

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

1312 

814  1313 
# Move the wires connected 
1852
70c1cc354a8f
fix pylint warning '(dangerousdefaultvalue) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset

1314 
def MoveConnected(self, exclude=None): 
814  1315 
if len(self.Wires) > 0: 
1316 
# Calculate the new position of the end point 

1317 
parent_pos = self.ParentBlock.GetPosition() 

1318 
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE 

1319 
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE 

1320 
# Move the corresponding point on all the wires connected 

1321 
for wire, index in self.Wires: 

1852
70c1cc354a8f
fix pylint warning '(dangerousdefaultvalue) Dangerous default value {} as argument'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1850
diff
changeset

1322 
if (exclude is None) or (wire not in exclude): 
814  1323 
if index == 0: 
1324 
wire.MoveStartPoint(wx.Point(x, y)) 

1325 
else: 

1326 
wire.MoveEndPoint(wx.Point(x, y)) 

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

1327 

814 