author | lbessard |
Mon, 01 Sep 2008 16:31:50 +0200 | |
changeset 247 | 4a47ccafbef7 |
parent 243 | c5da8b706cde |
child 249 | d8425712acef |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 13 |
#License as published by the Free Software Foundation; either |
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 19 |
#General Public License for more details. |
0 | 20 |
# |
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 22 |
#License along with this library; if not, write to the Free Software |
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
from math import * |
|
27 |
||
28 |
#------------------------------------------------------------------------------- |
|
29 |
# Common constants |
|
30 |
#------------------------------------------------------------------------------- |
|
31 |
||
32 |
""" |
|
33 |
Definition of constants for dimensions of graphic elements |
|
34 |
""" |
|
35 |
||
36 |
# FBD and SFC constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
37 |
MIN_MOVE = 5 # Minimum move before starting a element dragging |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
38 |
CONNECTOR_SIZE = 8 # Size of connectors |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
39 |
BLOCK_LINE_SIZE = 20 # Minimum size of each line in a block |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
40 |
HANDLE_SIZE = 6 # Size of the squares for handles |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
41 |
ANCHOR_DISTANCE = 5 # Distance where wire is automativally attached to a connector |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
42 |
POINT_RADIUS = 2 # Radius of the point of wire ends |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
43 |
MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire |
0 | 44 |
|
45 |
# LD constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
46 |
LD_LINE_SIZE = 40 # Distance between two lines in a ladder rung |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
47 |
LD_ELEMENT_SIZE = (21, 15) # Size (width, height) of a ladder element (contact or coil) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
48 |
LD_WIRE_SIZE = 30 # Size of a wire between two contact |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
49 |
LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact |
144 | 50 |
LD_POWERRAIL_WIDTH = 3 # Width of a Powerrail |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
51 |
LD_OFFSET = (10, 10) # Distance (x, y) between each comment and rung of the ladder |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
52 |
LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box |
0 | 53 |
|
54 |
# SFC constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
55 |
SFC_STEP_DEFAULT_SIZE = (40, 30) # Default size of a SFC step |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
56 |
SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
57 |
SFC_DEFAULT_SEQUENCE_INTERVAL = 40 # Default size of the interval between two divergence branches |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
58 |
SFC_SIMULTANEOUS_SEQUENCE_EXTRA = 20 # Size of extra lines for simultaneous divergence and convergence |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
59 |
SFC_JUMP_SIZE = (12, 13) # Size of a SFC jump to step |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
60 |
SFC_WIRE_MIN_SIZE = 25 # Size of a wire between two elements |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
61 |
SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line |
0 | 62 |
|
63 |
# Type definition constants for graphic elements |
|
64 |
[INPUT, OUTPUT, INOUT] = range(3) |
|
65 |
[CONNECTOR, CONTINUATION] = range(2) |
|
66 |
[LEFTRAIL, RIGHTRAIL] = range(2) |
|
67 |
[CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) |
|
68 |
[COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET] = range(4) |
|
69 |
[SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) |
|
70 |
||
71 |
# Constants for defining the type of dragging that has been selected |
|
72 |
[HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) |
|
73 |
||
74 |
# List of value for resize handle that are valid |
|
75 |
VALID_HANDLES = [(1,1), (1,2), (1,3), (2,3), (3,3), (3,2), (3,1), (2,1)] |
|
76 |
||
77 |
# Contants for defining the direction of a connector |
|
78 |
[EAST, NORTH, WEST, SOUTH] = [(1,0), (0,-1), (-1,0), (0,1)] |
|
79 |
||
80 |
# Contants for defining which mode is selected for each view |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
81 |
[MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
82 |
MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
83 |
MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION] = range(14) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
84 |
|
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
85 |
# Contants for defining alignment types for graphic group |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
86 |
[ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6) |
27 | 87 |
|
88 |
# Contants for defining which drawing mode is selected for app |
|
89 |
[FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] |
|
90 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
91 |
# Color for Highlighting |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
92 |
HIGHLIGHTCOLOR = wx.CYAN |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
93 |
|
58 | 94 |
CURSORS = None |
95 |
||
96 |
def ResetCursors(): |
|
97 |
global CURSORS |
|
98 |
if CURSORS == None: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
99 |
CURSORS = [wx.NullCursor, |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
100 |
wx.StockCursor(wx.CURSOR_HAND), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
101 |
wx.StockCursor(wx.CURSOR_SIZENWSE), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
102 |
wx.StockCursor(wx.CURSOR_SIZENESW), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
103 |
wx.StockCursor(wx.CURSOR_SIZEWE), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
104 |
wx.StockCursor(wx.CURSOR_SIZENS)] |
58 | 105 |
|
106 |
HANDLE_CURSORS = { |
|
107 |
(1, 1) : 2, |
|
108 |
(3, 3) : 2, |
|
109 |
(1, 3) : 3, |
|
110 |
(3, 1) : 3, |
|
111 |
(1, 2) : 4, |
|
112 |
(3, 2) : 4, |
|
113 |
(2, 1) : 5, |
|
114 |
(2, 3) : 5 |
|
115 |
} |
|
0 | 116 |
|
117 |
""" |
|
118 |
Basic vector operations for calculate wire points |
|
119 |
""" |
|
120 |
||
121 |
# Calculate the scalar product of two vectors |
|
122 |
def product(v1, v2): |
|
123 |
return v1[0] * v2[0] + v1[1] * v2[1] |
|
124 |
||
125 |
# Create a vector from two points and define if vector must be normal |
|
126 |
def vector(p1, p2, normal = True): |
|
127 |
vector = (p2.x - p1.x, p2.y - p1.y) |
|
128 |
if normal: |
|
129 |
return normalize(vector) |
|
130 |
return vector |
|
131 |
||
132 |
# Calculate the norm of a given vector |
|
133 |
def norm(v): |
|
134 |
return sqrt(v[0] * v[0] + v[1] * v[1]) |
|
135 |
||
136 |
# Normalize a given vector |
|
137 |
def normalize(v): |
|
138 |
v_norm = norm(v) |
|
139 |
# Verifie if it is not a null vector |
|
140 |
if v_norm > 0: |
|
141 |
return (v[0] / v_norm, v[1] / v_norm) |
|
142 |
else: |
|
143 |
return v |
|
144 |
||
145 |
||
146 |
""" |
|
147 |
Function that calculates the nearest point of the grid defined by scaling for the given point |
|
148 |
""" |
|
149 |
||
27 | 150 |
def GetScaledEventPosition(event, dc, scaling): |
151 |
pos = event.GetLogicalPosition(dc) |
|
0 | 152 |
if scaling: |
153 |
pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0] |
|
154 |
pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1] |
|
155 |
return pos |
|
156 |
||
157 |
||
158 |
""" |
|
159 |
Function that choose a direction during the wire points generation |
|
160 |
""" |
|
161 |
||
162 |
def DirectionChoice(v_base, v_target, dir_target): |
|
163 |
dir_product = product(v_base, v_target) |
|
164 |
if dir_product < 0: |
|
165 |
return (-v_base[0], -v_base[1]) |
|
166 |
elif dir_product == 0 and product(v_base, dir_target) != 0: |
|
167 |
return dir_target |
|
168 |
return v_base |
|
169 |
||
170 |
||
171 |
#------------------------------------------------------------------------------- |
|
172 |
# Viewer Rubberband |
|
173 |
#------------------------------------------------------------------------------- |
|
174 |
||
175 |
""" |
|
176 |
Class that implements a rubberband |
|
177 |
""" |
|
178 |
||
179 |
class RubberBand: |
|
180 |
||
181 |
# Create a rubberband by indicated on which window it must be drawn |
|
182 |
def __init__(self, drawingSurface): |
|
183 |
self.drawingSurface = drawingSurface |
|
184 |
self.Reset() |
|
185 |
||
186 |
# Method that initializes the internal attributes of the rubberband |
|
187 |
def Reset(self): |
|
188 |
self.startPoint = None |
|
189 |
self.currentBox = None |
|
190 |
self.lastBox = None |
|
191 |
||
192 |
# Method that return if a box is currently edited |
|
193 |
def IsShown(self): |
|
194 |
return self.currentBox != None |
|
195 |
||
196 |
# Method that returns the currently edited box |
|
197 |
def GetCurrentExtent(self): |
|
198 |
return self.currentBox |
|
199 |
||
200 |
# Method called when a new box starts to be edited |
|
27 | 201 |
def OnLeftDown(self, event, dc, scaling): |
202 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 203 |
# Save the point for calculate the box position and size |
204 |
self.startPoint = pos |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
205 |
self.currentBox = wx.Rect(pos.x, pos.y, 0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
206 |
self.drawingSurface.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) |
0 | 207 |
self.Redraw() |
208 |
||
209 |
# Method called when dragging with a box edited |
|
27 | 210 |
def OnMotion(self, event, dc, scaling): |
211 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 212 |
# Save the last position and size of the box for erasing it |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
213 |
self.lastBox = wx.Rect(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
0 | 214 |
self.currentBox.height) |
215 |
# Calculate new position and size of the box |
|
216 |
if pos.x >= self.startPoint.x: |
|
217 |
self.currentBox.x = self.startPoint.x |
|
218 |
self.currentBox.width = pos.x - self.startPoint.x + 1 |
|
219 |
else: |
|
220 |
self.currentBox.x = pos.x |
|
221 |
self.currentBox.width = self.startPoint.x - pos.x + 1 |
|
222 |
if pos.y >= self.startPoint.y: |
|
223 |
self.currentBox.y = self.startPoint.y |
|
224 |
self.currentBox.height = pos.y - self.startPoint.y + 1 |
|
225 |
else: |
|
226 |
self.currentBox.y = pos.y |
|
227 |
self.currentBox.height = self.startPoint.y - pos.y + 1 |
|
228 |
self.Redraw() |
|
229 |
||
230 |
# Method called when dragging is stopped |
|
27 | 231 |
def OnLeftUp(self, event, dc, scaling): |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
232 |
self.drawingSurface.SetCursor(wx.NullCursor) |
0 | 233 |
self.lastBox = self.currentBox |
234 |
self.currentBox = None |
|
235 |
self.Redraw() |
|
236 |
||
237 |
# Method that erase the last box and draw the new box |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
238 |
def Redraw(self, dc = None): |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
239 |
if not dc: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
240 |
dc = self.drawingSurface.GetLogicalDC() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
241 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
242 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
243 |
dc.SetLogicalFunction(wx.XOR) |
0 | 244 |
if self.lastBox: |
245 |
# Erase last box |
|
246 |
dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, |
|
247 |
self.lastBox.height) |
|
248 |
if self.currentBox: |
|
249 |
# Draw current box |
|
250 |
dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
251 |
self.currentBox.height) |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
252 |
|
27 | 253 |
# Erase last box |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
254 |
def Erase(self, dc = None): |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
255 |
if not dc: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
256 |
dc = self.drawingSurface.GetLogicalDC() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
257 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
258 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
259 |
dc.SetLogicalFunction(wx.XOR) |
27 | 260 |
if self.lastBox: |
261 |
dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, |
|
262 |
self.lastBox.height) |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
263 |
|
27 | 264 |
# Draw current box |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
265 |
def Draw(self, dc = None): |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
266 |
if not dc: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
267 |
dc = self.drawingSurface.GetLogicalDC() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
268 |
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
269 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
270 |
dc.SetLogicalFunction(wx.XOR) |
27 | 271 |
if self.currentBox: |
272 |
# Draw current box |
|
273 |
dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
274 |
self.currentBox.height) |
|
275 |
||
0 | 276 |
#------------------------------------------------------------------------------- |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
277 |
# Helper for highlighting error in drawn text |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
278 |
#------------------------------------------------------------------------------- |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
279 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
280 |
def HighlightErrorZone(dc, x, y, width, height): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
281 |
dc.SetPen(wx.TRANSPARENT_PEN) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
282 |
dc.SetLogicalFunction(wx.AND) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
283 |
dc.SetBrush(wx.Brush(wx.Colour(0,255,0))) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
284 |
dc.DrawRectangle(x, y, width, height) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
285 |
dc.SetLogicalFunction(wx.XOR) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
286 |
dc.SetBrush(wx.Brush(wx.Colour(255,0,0))) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
287 |
dc.DrawRectangle(x, y, width, height) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
288 |
dc.SetLogicalFunction(wx.COPY) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
289 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
290 |
#------------------------------------------------------------------------------- |
0 | 291 |
# Graphic element base class |
292 |
#------------------------------------------------------------------------------- |
|
293 |
||
294 |
""" |
|
295 |
Class that implements a generic graphic element |
|
296 |
""" |
|
297 |
||
298 |
class Graphic_Element: |
|
299 |
||
300 |
# Create a new graphic element |
|
301 |
def __init__(self, parent, id = None): |
|
302 |
self.Parent = parent |
|
303 |
self.Id = id |
|
304 |
self.oldPos = None |
|
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
305 |
self.Handle = (None,None) |
0 | 306 |
self.Dragging = False |
307 |
self.Selected = False |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
308 |
self.Highlighted = False |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
309 |
self.Pos = wx.Point(0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
310 |
self.Size = wx.Size(0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
311 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
58 | 312 |
self.CurrentCursor = 0 |
313 |
ResetCursors() |
|
0 | 314 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
315 |
def IsOfType(self, type, reference): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
316 |
return self.Parent.IsOfType(type, reference) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
317 |
|
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
318 |
def IsEndType(self, type): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
319 |
return self.Parent.IsEndType(type) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
320 |
|
144 | 321 |
def GetDragging(self): |
322 |
return self.Dragging |
|
323 |
||
0 | 324 |
# Make a clone of this element |
162 | 325 |
def Clone(self, parent): |
326 |
return Graphic_Element(parent, self.Id) |
|
0 | 327 |
|
328 |
# Changes the block position |
|
329 |
def SetPosition(self, x, y): |
|
330 |
self.Pos.x = x |
|
331 |
self.Pos.y = y |
|
332 |
self.RefreshConnected() |
|
333 |
self.RefreshBoundingBox() |
|
334 |
||
335 |
# Returns the block position |
|
336 |
def GetPosition(self): |
|
337 |
return self.Pos.x, self.Pos.y |
|
338 |
||
339 |
# Changes the element size |
|
340 |
def SetSize(self, width, height): |
|
341 |
self.Size.SetWidth(width) |
|
342 |
self.Size.SetHeight(height) |
|
343 |
self.RefreshConnectors() |
|
344 |
self.RefreshBoundingBox() |
|
345 |
||
346 |
# Returns the element size |
|
347 |
def GetSize(self): |
|
348 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
349 |
||
350 |
# Refresh the element Bounding Box |
|
351 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
352 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
0 | 353 |
|
354 |
# Refresh the element connectors position |
|
355 |
def RefreshConnectors(self): |
|
356 |
pass |
|
357 |
||
358 |
# Refresh the position of wires connected to element inputs and outputs |
|
359 |
def RefreshConnected(self): |
|
360 |
pass |
|
361 |
||
362 |
# Change the parent |
|
363 |
def SetParent(self, parent): |
|
364 |
self.Parent = parent |
|
365 |
||
366 |
# Override this method for defining the method to call for deleting this element |
|
367 |
def Delete(self): |
|
368 |
pass |
|
369 |
||
370 |
# Returns the Id |
|
371 |
def GetId(self): |
|
372 |
return self.Id |
|
373 |
||
374 |
# Returns if the point given is in the bounding box |
|
375 |
def HitTest(self, pt): |
|
376 |
rect = self.BoundingBox |
|
377 |
return rect.InsideXY(pt.x, pt.y) |
|
378 |
||
42 | 379 |
# Returns if the point given is in the bounding box |
380 |
def IsInSelection(self, rect): |
|
381 |
return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) |
|
382 |
||
0 | 383 |
# Override this method for refreshing the bounding box |
384 |
def RefreshBoundingBox(self): |
|
385 |
pass |
|
386 |
||
387 |
# Returns the bounding box |
|
388 |
def GetBoundingBox(self): |
|
389 |
return self.BoundingBox |
|
390 |
||
144 | 391 |
# Returns the RedrawRect |
392 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
393 |
rect = wx.Rect() |
|
394 |
rect.x = self.BoundingBox.x - HANDLE_SIZE - 2 - abs(movex) |
|
395 |
rect.y = self.BoundingBox.y - HANDLE_SIZE - 2 - abs(movey) |
|
396 |
rect.width = self.BoundingBox.width + 2 * (HANDLE_SIZE + abs(movex)) + 4 |
|
397 |
rect.height = self.BoundingBox.height + 2 * (HANDLE_SIZE + abs(movey)) + 4 |
|
398 |
return rect |
|
399 |
||
400 |
def Refresh(self, rect = None): |
|
145 | 401 |
if rect is not None: |
155 | 402 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
403 |
else: |
|
404 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False) |
|
144 | 405 |
|
0 | 406 |
# Change the variable that indicates if this element is selected |
407 |
def SetSelected(self, selected): |
|
408 |
self.Selected = selected |
|
144 | 409 |
self.Refresh() |
0 | 410 |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
411 |
# Change the variable that indicates if this element is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
412 |
def SetHighlighted(self, highlighted): |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
413 |
self.Highlighted = highlighted |
144 | 414 |
self.Refresh() |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
415 |
|
0 | 416 |
# Test if the point is on a handle of this element |
417 |
def TestHandle(self, pt): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
418 |
extern_rect = wx.Rect(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, |
58 | 419 |
self.BoundingBox.width + 2 * HANDLE_SIZE + 4, self.BoundingBox.height + 2 * HANDLE_SIZE + 4) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
420 |
intern_rect = wx.Rect(self.BoundingBox.x - 2, self.BoundingBox.y - 2, |
58 | 421 |
self.BoundingBox.width + 4, self.BoundingBox.height + 4) |
0 | 422 |
# Verify that this element is selected |
58 | 423 |
if self.Selected and extern_rect.InsideXY(pt.x, pt.y) and not intern_rect.InsideXY(pt.x, pt.y): |
0 | 424 |
# Find if point is on a handle horizontally |
425 |
if self.BoundingBox.x - HANDLE_SIZE - 2 <= pt.x < self.BoundingBox.x - 2: |
|
426 |
handle_x = 1 |
|
427 |
elif self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2 <= pt.x < self.BoundingBox.x + (self.BoundingBox.width + HANDLE_SIZE) / 2: |
|
428 |
handle_x = 2 |
|
429 |
elif self.BoundingBox.x + self.BoundingBox.width + 2 <= pt.x < self.BoundingBox.x + self.BoundingBox.width + HANDLE_SIZE + 2: |
|
430 |
handle_x = 3 |
|
431 |
else: |
|
432 |
handle_x = 0 |
|
433 |
# Find if point is on a handle vertically |
|
434 |
if self.BoundingBox.y - HANDLE_SIZE - 2 <= pt.y < self.BoundingBox.y - 2: |
|
435 |
handle_y = 1 |
|
436 |
elif self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2 <= pt.y < self.BoundingBox.y + (self.BoundingBox.height + HANDLE_SIZE) / 2: |
|
437 |
handle_y = 2 |
|
438 |
elif self.BoundingBox.y + self.BoundingBox.height - 2 <= pt.y < self.BoundingBox.y + self.BoundingBox.height + HANDLE_SIZE + 2: |
|
439 |
handle_y = 3 |
|
440 |
else: |
|
441 |
handle_y = 0 |
|
442 |
# Verify that the result is valid |
|
443 |
if (handle_x, handle_y) in VALID_HANDLES: |
|
444 |
return handle_x, handle_y |
|
445 |
return 0, 0 |
|
446 |
||
447 |
# Method called when a LeftDown event have been generated |
|
27 | 448 |
def OnLeftDown(self, event, dc, scaling): |
449 |
pos = event.GetLogicalPosition(dc) |
|
0 | 450 |
# Test if an handle have been clicked |
58 | 451 |
handle = self.TestHandle(pos) |
0 | 452 |
# Find which type of handle have been clicked, |
453 |
# Save a resize event and change the cursor |
|
58 | 454 |
cursor = HANDLE_CURSORS.get(handle, 1) |
455 |
if cursor != self.CurrentCursor: |
|
456 |
self.Parent.SetCursor(CURSORS[cursor]) |
|
457 |
self.CurrentCursor = cursor |
|
458 |
if cursor > 1: |
|
459 |
self.Handle = (HANDLE_RESIZE, handle) |
|
0 | 460 |
else: |
461 |
self.Handle = (HANDLE_MOVE, None) |
|
462 |
self.SetSelected(False) |
|
463 |
# Initializes the last position |
|
27 | 464 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
0 | 465 |
|
466 |
# Method called when a LeftUp event have been generated |
|
27 | 467 |
def OnLeftUp(self, event, dc, scaling): |
0 | 468 |
# If a dragging have been initiated |
469 |
if self.Dragging and self.oldPos: |
|
470 |
self.RefreshModel() |
|
56
7187e1c00975
Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents:
42
diff
changeset
|
471 |
self.Parent.RefreshBuffer() |
58 | 472 |
if self.CurrentCursor != 0: |
473 |
self.Parent.SetCursor(CURSORS[0]) |
|
474 |
self.CurrentCursor = 0 |
|
0 | 475 |
self.SetSelected(True) |
476 |
self.oldPos = None |
|
477 |
||
145 | 478 |
# Method called when a RightDown event have been generated |
479 |
def OnRightDown(self, event, dc, scaling): |
|
480 |
pass |
|
481 |
||
0 | 482 |
# Method called when a RightUp event have been generated |
27 | 483 |
def OnRightUp(self, event, dc, scaling): |
145 | 484 |
if self.Dragging and self.oldPos: |
485 |
self.RefreshModel() |
|
486 |
self.Parent.RefreshBuffer() |
|
487 |
if self.CurrentCursor != 0: |
|
488 |
self.Parent.SetCursor(CURSORS[0]) |
|
489 |
self.CurrentCursor = 0 |
|
0 | 490 |
self.SetSelected(True) |
491 |
self.oldPos = None |
|
492 |
||
493 |
# Method called when a LeftDClick event have been generated |
|
27 | 494 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 495 |
pass |
496 |
||
497 |
# Method called when a Motion event have been generated |
|
27 | 498 |
def OnMotion(self, event, dc, scaling): |
0 | 499 |
# If the cursor is dragging and the element have been clicked |
500 |
if event.Dragging() and self.oldPos: |
|
501 |
# Calculate the movement of cursor |
|
145 | 502 |
pos = event.GetLogicalPosition(dc) |
0 | 503 |
movex = pos.x - self.oldPos.x |
504 |
movey = pos.y - self.oldPos.y |
|
505 |
# If movement is greater than MIN_MOVE then a dragging is initiated |
|
506 |
if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): |
|
507 |
self.Dragging = True |
|
508 |
# If a dragging have been initiated, refreshes the element state |
|
509 |
if self.Dragging: |
|
165 | 510 |
dragx, dragy = self.ProcessDragging(movex, movey, event.ShiftDown(), scaling) |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
511 |
self.oldPos.x += dragx |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
512 |
self.oldPos.y += dragy |
144 | 513 |
return dragx, dragy |
514 |
return movex, movey |
|
0 | 515 |
# If cursor just pass over the element, changes the cursor if it is on a handle |
516 |
else: |
|
27 | 517 |
pos = event.GetLogicalPosition(dc) |
0 | 518 |
handle = self.TestHandle(pos) |
58 | 519 |
# Find which type of handle have been clicked, |
520 |
# Save a resize event and change the cursor |
|
521 |
cursor = HANDLE_CURSORS.get(handle, 0) |
|
522 |
if cursor != self.CurrentCursor: |
|
523 |
self.Parent.SetCursor(CURSORS[cursor]) |
|
524 |
self.CurrentCursor = cursor |
|
144 | 525 |
return 0, 0 |
58 | 526 |
|
0 | 527 |
# Moves the element |
528 |
def Move(self, dx, dy, exclude = []): |
|
529 |
self.Pos.x += dx |
|
530 |
self.Pos.y += dy |
|
531 |
self.RefreshConnected(exclude) |
|
532 |
self.RefreshBoundingBox() |
|
533 |
||
534 |
# Resizes the element from position and size given |
|
535 |
def Resize(self, x, y, width, height): |
|
536 |
self.Move(x, y) |
|
537 |
self.SetSize(width, height) |
|
538 |
||
539 |
# Refreshes the element state according to move defined and handle selected |
|
175 | 540 |
def ProcessDragging(self, movex, movey, centered, scaling, width_fac = 1, height_fac = 1): |
0 | 541 |
handle_type, handle = self.Handle |
542 |
# If it is a resize handle, calculate the values from resizing |
|
543 |
if handle_type == HANDLE_RESIZE: |
|
175 | 544 |
if scaling is not None: |
545 |
scaling = (scaling[0] * width_fac, scaling[1] * height_fac) |
|
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
546 |
x = y = start_x = start_y = 0 |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
547 |
width, height = start_width, start_height = self.GetSize() |
0 | 548 |
if handle[0] == 1: |
145 | 549 |
movex = max(-self.BoundingBox.x, movex) |
550 |
if scaling is not None: |
|
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
551 |
movex = -(round(float(width - movex) / float(scaling[0])) * scaling[0] - width) |
145 | 552 |
x = movex |
165 | 553 |
if centered: |
554 |
width -= 2 * movex |
|
555 |
else: |
|
556 |
width -= movex |
|
0 | 557 |
elif handle[0] == 3: |
145 | 558 |
if scaling is not None: |
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
559 |
movex = round(float(width + movex) / float(scaling[0])) * scaling[0] - width |
165 | 560 |
if centered: |
561 |
x = -movex |
|
562 |
width += 2 * movex |
|
563 |
else: |
|
564 |
width += movex |
|
0 | 565 |
if handle[1] == 1: |
145 | 566 |
movey = max(-self.BoundingBox.y, movey) |
567 |
if scaling is not None: |
|
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
568 |
movey = -(round(float(height - movey) / float(scaling[1])) * scaling[1] - height) |
145 | 569 |
y = movey |
165 | 570 |
if centered: |
571 |
height -= 2 * movey |
|
572 |
else: |
|
573 |
height -= movey |
|
0 | 574 |
elif handle[1] == 3: |
145 | 575 |
if scaling is not None: |
180
3b0d3ea35ee5
Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents:
175
diff
changeset
|
576 |
movey = round(float(height + movey) / float(scaling[1])) * scaling[1] - height |
165 | 577 |
if centered: |
578 |
y = -movey |
|
579 |
height += 2 * movey |
|
580 |
else: |
|
581 |
height += movey |
|
0 | 582 |
# Verify that new size is not lesser than minimum |
583 |
min_width, min_height = self.GetMinSize() |
|
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
584 |
if handle[0] != 2 and (width >= min_width or width > self.Size[0]): |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
585 |
start_x = x |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
586 |
start_width = width |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
587 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
588 |
movex = 0 |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
589 |
if handle[1] != 2 and (height >= min_height or height > self.Size[1]): |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
590 |
start_y = y |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
591 |
start_height = height |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
592 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
593 |
movey = 0 |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
594 |
if movex != 0 or movey != 0: |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
108
diff
changeset
|
595 |
self.Resize(start_x, start_y, start_width, start_height) |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
596 |
return movex, movey |
0 | 597 |
# If it is a move handle, Move this element |
598 |
elif handle_type == HANDLE_MOVE: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
599 |
movex = max(-self.BoundingBox.x, movex) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
600 |
movey = max(-self.BoundingBox.y, movey) |
145 | 601 |
if scaling is not None: |
602 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
603 |
movey = round(float(self.Pos.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
0 | 604 |
self.Move(movex, movey) |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
605 |
return movex, movey |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
606 |
return 0, 0 |
0 | 607 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
608 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
609 |
pass |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
610 |
|
0 | 611 |
# Override this method for defining the method to call for refreshing the model of this element |
612 |
def RefreshModel(self, move=True): |
|
613 |
pass |
|
614 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
615 |
# Draws the highlightment of this element if it is highlighted (can be overwritten) |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
616 |
def DrawHighlightment(self, dc): |
144 | 617 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) |
618 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
619 |
dc.SetLogicalFunction(wx.AND) |
|
620 |
dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, self.Size.width + 5, self.Size.height + 5) |
|
621 |
dc.SetLogicalFunction(wx.COPY) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
622 |
|
0 | 623 |
# Draws the handles of this element if it is selected |
624 |
def Draw(self, dc): |
|
144 | 625 |
if self.Highlighted: |
626 |
self.DrawHighlightment(dc) |
|
0 | 627 |
if self.Selected: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
628 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
629 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 630 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
631 |
dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, |
|
632 |
self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
|
633 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
634 |
self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
|
635 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
636 |
self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) |
|
637 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
638 |
self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
639 |
dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, |
|
640 |
self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
641 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
642 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) |
|
643 |
||
644 |
||
645 |
#------------------------------------------------------------------------------- |
|
646 |
# Group of graphic elements |
|
647 |
#------------------------------------------------------------------------------- |
|
648 |
||
649 |
""" |
|
650 |
Class that implements a group of graphic elements |
|
651 |
""" |
|
652 |
||
653 |
class Graphic_Group(Graphic_Element): |
|
654 |
||
655 |
# Create a new group of graphic elements |
|
656 |
def __init__(self, parent): |
|
657 |
Graphic_Element.__init__(self, parent) |
|
658 |
self.Elements = [] |
|
42 | 659 |
self.RefreshWireExclusion() |
0 | 660 |
self.RefreshBoundingBox() |
661 |
||
662 |
# Destructor |
|
663 |
def __del__(self): |
|
664 |
self.Elements = [] |
|
665 |
||
42 | 666 |
# Refresh the list of wire excluded |
667 |
def RefreshWireExclusion(self): |
|
668 |
self.WireExcluded = [] |
|
669 |
for element in self.Elements: |
|
670 |
if isinstance(element, Wire): |
|
671 |
startblock = element.StartConnected.GetParentBlock() |
|
672 |
endblock = element.EndConnected.GetParentBlock() |
|
673 |
if startblock in self.Elements and endblock in self.Elements: |
|
674 |
self.WireExcluded.append(element) |
|
675 |
||
0 | 676 |
# Make a clone of this group |
162 | 677 |
def Clone(self, parent): |
678 |
clone = Graphic_Group(parent) |
|
0 | 679 |
elements = [] |
680 |
# Makes a clone of all the elements in this group |
|
681 |
for element in self.Elements: |
|
162 | 682 |
elements.append(element.Clone(parent)) |
0 | 683 |
clone.SetElements(elements) |
684 |
return clone |
|
685 |
||
144 | 686 |
# Returns the RedrawRect |
687 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
688 |
rect = None |
|
689 |
for element in self.Elements: |
|
690 |
if rect is None: |
|
691 |
rect = element.GetRedrawRect(movex, movey) |
|
692 |
else: |
|
693 |
rect = rect.Union(element.GetRedrawRect(movex, movey)) |
|
694 |
return rect |
|
695 |
||
0 | 696 |
# Clean this group of elements |
697 |
def Clean(self): |
|
698 |
# Clean all the elements of the group |
|
699 |
for element in self.Elements: |
|
700 |
element.Clean() |
|
701 |
||
702 |
# Delete this group of elements |
|
703 |
def Delete(self): |
|
704 |
# Delete all the elements of the group |
|
705 |
for element in self.Elements: |
|
706 |
element.Delete() |
|
42 | 707 |
self.WireExcluded = [] |
0 | 708 |
|
709 |
# Returns if the point given is in the bounding box of one of the elements of this group |
|
710 |
def HitTest(self, pt): |
|
711 |
result = False |
|
712 |
for element in self.Elements: |
|
713 |
result |= element.HitTest(pt) |
|
714 |
return result |
|
715 |
||
716 |
# Returns if the element given is in this group |
|
717 |
def IsElementIn(self, element): |
|
718 |
return element in self.Elements |
|
719 |
||
720 |
# Change the elements of the group |
|
721 |
def SetElements(self, elements): |
|
722 |
self.Elements = elements |
|
42 | 723 |
self.RefreshWireExclusion() |
0 | 724 |
self.RefreshBoundingBox() |
725 |
||
726 |
# Returns the elements of the group |
|
727 |
def GetElements(self): |
|
728 |
return self.Elements |
|
729 |
||
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
730 |
# Align the group elements |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
731 |
def AlignElements(self, horizontally, vertically): |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
732 |
minx = self.BoundingBox.x + self.BoundingBox.width |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
733 |
miny = self.BoundingBox.y + self.BoundingBox.height |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
734 |
maxx = self.BoundingBox.x |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
735 |
maxy = self.BoundingBox.y |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
736 |
for element in self.Elements: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
737 |
if not isinstance(element, Wire): |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
738 |
posx, posy = element.GetPosition() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
739 |
width, height = element.GetSize() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
740 |
minx = min(minx, posx) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
741 |
miny = min(miny, posy) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
742 |
maxx = max(maxx, posx + width) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
743 |
maxy = max(maxy, posy + height) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
744 |
for element in self.Elements: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
745 |
if not isinstance(element, Wire): |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
746 |
posx, posy = element.GetPosition() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
747 |
width, height = element.GetSize() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
748 |
movex = movey = 0 |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
749 |
if horizontally == ALIGN_LEFT: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
750 |
movex = minx - posx |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
751 |
elif horizontally == ALIGN_CENTER: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
752 |
movex = (maxx + minx - width) / 2 - posx |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
753 |
elif horizontally == ALIGN_RIGHT: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
754 |
movex = maxx - width - posx |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
755 |
if vertically == ALIGN_TOP: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
756 |
movey = miny - posy |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
757 |
elif vertically == ALIGN_MIDDLE: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
758 |
movey = (maxy + miny - height) / 2 - posy |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
759 |
elif vertically == ALIGN_BOTTOM: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
760 |
movey = maxy - height - posy |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
761 |
if movex != 0 or movey != 0: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
762 |
element.Move(movex, movey) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
763 |
element.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
764 |
self.RefreshWireExclusion() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
765 |
self.RefreshBoundingBox() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
766 |
|
0 | 767 |
# Remove or select the given element if it is or not in the group |
768 |
def SelectElement(self, element): |
|
769 |
if element in self.Elements: |
|
770 |
self.Elements.remove(element) |
|
771 |
else: |
|
772 |
self.Elements.append(element) |
|
42 | 773 |
self.RefreshWireExclusion() |
0 | 774 |
self.RefreshBoundingBox() |
775 |
||
776 |
# Move this group of elements |
|
777 |
def Move(self, movex, movey): |
|
778 |
# Move all the elements of the group |
|
779 |
for element in self.Elements: |
|
42 | 780 |
if not isinstance(element, Wire): |
781 |
element.Move(movex, movey, self.WireExcluded) |
|
782 |
elif element in self.WireExcluded: |
|
0 | 783 |
element.Move(movex, movey, True) |
784 |
self.RefreshBoundingBox() |
|
785 |
||
786 |
# Refreshes the bounding box of this group of elements |
|
787 |
def RefreshBoundingBox(self): |
|
788 |
if len(self.Elements) > 0: |
|
789 |
bbox = self.Elements[0].GetBoundingBox() |
|
790 |
minx, miny = bbox.x, bbox.y |
|
791 |
maxx = bbox.x + bbox.width |
|
792 |
maxy = bbox.y + bbox.height |
|
793 |
for element in self.Elements[1:]: |
|
794 |
bbox = element.GetBoundingBox() |
|
795 |
minx = min(minx, bbox.x) |
|
796 |
miny = min(miny, bbox.y) |
|
797 |
maxx = max(maxx, bbox.x + bbox.width) |
|
798 |
maxy = max(maxy, bbox.y + bbox.height) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
799 |
self.BoundingBox = wx.Rect(minx, miny, maxx - minx, maxy - miny) |
0 | 800 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
801 |
self.BoundingBox = wx.Rect(0, 0, 0, 0) |
0 | 802 |
|
803 |
# Forbids to change the group position |
|
804 |
def SetPosition(x, y): |
|
805 |
pass |
|
806 |
||
807 |
# Returns the position of this group |
|
808 |
def GetPosition(self): |
|
809 |
return self.BoundingBox.x, self.BoundingBox.y |
|
810 |
||
811 |
# Forbids to change the group size |
|
812 |
def SetSize(width, height): |
|
813 |
pass |
|
814 |
||
815 |
# Returns the size of this group |
|
816 |
def GetSize(self): |
|
817 |
return self.BoundingBox.width, self.BoundingBox.height |
|
818 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
819 |
# Change the variable that indicates if this element is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
820 |
def SetHighlighted(self, highlighted): |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
821 |
for element in self.Elements: |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
822 |
element.SetHighlighted(highlighted) |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
823 |
|
0 | 824 |
# Change the variable that indicates if the elemente is selected |
825 |
def SetSelected(self, selected): |
|
826 |
for element in self.Elements: |
|
827 |
element.SetSelected(selected) |
|
828 |
||
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
829 |
# Method called when a RightUp event has been generated |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
830 |
def OnRightUp(self, event, dc, scaling): |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
831 |
# Popup the menu with special items for a group |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
832 |
self.Parent.PopupGroupMenu() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
833 |
|
0 | 834 |
# Refreshes the model of all the elements of this group |
835 |
def RefreshModel(self): |
|
836 |
for element in self.Elements: |
|
837 |
element.RefreshModel() |
|
838 |
||
839 |
||
840 |
#------------------------------------------------------------------------------- |
|
841 |
# Connector for all types of blocks |
|
842 |
#------------------------------------------------------------------------------- |
|
843 |
||
844 |
""" |
|
845 |
Class that implements a connector for any type of block |
|
846 |
""" |
|
847 |
||
848 |
class Connector: |
|
849 |
||
850 |
# Create a new connector |
|
27 | 851 |
def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False): |
0 | 852 |
self.ParentBlock = parent |
853 |
self.Name = name |
|
854 |
self.Type = type |
|
855 |
self.Pos = position |
|
856 |
self.Direction = direction |
|
857 |
self.Wires = [] |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
858 |
if self.ParentBlock.IsOfType("BOOL", type): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
859 |
self.Negated = negated |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
860 |
self.Edge = edge |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
861 |
else: |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
862 |
self.Negated = False |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
863 |
self.Edge = "none" |
27 | 864 |
self.OneConnected = onlyone |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
865 |
self.Pen = wx.BLACK_PEN |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
866 |
self.Errors = {} |
42 | 867 |
self.RefreshNameSize() |
0 | 868 |
|
144 | 869 |
# Returns the RedrawRect |
870 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
871 |
parent_pos = self.ParentBlock.GetPosition() |
|
872 |
x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE) |
|
873 |
y = min(parent_pos[1] + self.Pos.y, parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE) |
|
874 |
if self.Direction[0] == 0: |
|
875 |
width = 5 |
|
876 |
else: |
|
877 |
width = CONNECTOR_SIZE |
|
878 |
if self.Direction[1] == 0: |
|
879 |
height = 5 |
|
880 |
else: |
|
881 |
height = CONNECTOR_SIZE |
|
882 |
return wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey)) |
|
883 |
||
0 | 884 |
# Change the connector pen |
885 |
def SetPen(self, pen): |
|
886 |
self.Pen = pen |
|
887 |
||
888 |
# Make a clone of the connector |
|
112 | 889 |
def Clone(self, parent = None): |
890 |
if parent is None: |
|
891 |
parent = self.ParentBlock |
|
892 |
return Connector(parent, self.Name, self.Type, wx.Point(self.Pos[0], self.Pos[1]), |
|
0 | 893 |
self.Direction, self.Negated) |
894 |
||
895 |
# Returns the connector parent block |
|
896 |
def GetParentBlock(self): |
|
897 |
return self.ParentBlock |
|
898 |
||
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
899 |
# Returns the connector type |
112 | 900 |
def GetType(self, raw = False): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
901 |
if self.ParentBlock.IsEndType(self.Type) or raw: |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
902 |
return self.Type |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
903 |
elif (self.Negated or self.Edge != "none") and self.ParentBlock.IsOfType("BOOL", self.Type): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
904 |
return "BOOL" |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
905 |
else: |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
906 |
return self.ParentBlock.GetConnectionResultType(self, self.Type) |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
907 |
|
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
908 |
# Returns the connector type |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
909 |
def GetConnectedType(self): |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
910 |
if self.ParentBlock.IsEndType(self.Type): |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
911 |
return self.Type |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
912 |
elif len(self.Wires) == 1: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
913 |
return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1]) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
914 |
return self.Type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
915 |
|
144 | 916 |
# Returns the connector type |
917 |
def GetConnectedRedrawRect(self, movex, movey): |
|
918 |
rect = None |
|
919 |
for wire, handle in self.Wires: |
|
920 |
if rect is None: |
|
921 |
rect = wire.GetRedrawRect() |
|
922 |
else: |
|
923 |
rect = rect.Union(wire.GetRedrawRect()) |
|
924 |
return rect |
|
925 |
||
98 | 926 |
# Returns if connector type is compatible with type given |
927 |
def IsCompatible(self, type): |
|
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
928 |
reference = self.GetType() |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
929 |
return self.ParentBlock.IsOfType(type, reference) or self.ParentBlock.IsOfType(reference, type) |
98 | 930 |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
931 |
# Changes the connector name |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
932 |
def SetType(self, type): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
933 |
self.Type = type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
934 |
|
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
935 |
# Returns the connector name |
0 | 936 |
def GetName(self): |
937 |
return self.Name |
|
938 |
||
939 |
# Changes the connector name |
|
940 |
def SetName(self, name): |
|
941 |
self.Name = name |
|
42 | 942 |
self.RefreshNameSize() |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
943 |
|
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
944 |
def SetValue(self, value): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
945 |
for wire, handle in self.Wires: |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
946 |
wire.SetValue(value) |
42 | 947 |
|
948 |
# Changes the connector name size |
|
949 |
def RefreshNameSize(self): |
|
950 |
if self.Name != "": |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
951 |
dc = wx.ClientDC(self.ParentBlock.Parent) |
42 | 952 |
self.NameSize = dc.GetTextExtent(self.Name) |
953 |
else: |
|
954 |
self.NameSize = 0, 0 |
|
955 |
||
956 |
# Returns the connector name size |
|
957 |
def GetNameSize(self): |
|
958 |
return self.NameSize |
|
0 | 959 |
|
960 |
# Returns the wires connected to the connector |
|
961 |
def GetWires(self): |
|
962 |
return self.Wires |
|
963 |
||
964 |
# Returns the parent block Id |
|
965 |
def GetBlockId(self): |
|
966 |
return self.ParentBlock.GetId() |
|
967 |
||
968 |
# Returns the connector relative position |
|
969 |
def GetRelPosition(self): |
|
970 |
return self.Pos |
|
971 |
||
972 |
# Returns the connector absolute position |
|
973 |
def GetPosition(self, size = True): |
|
974 |
parent_pos = self.ParentBlock.GetPosition() |
|
975 |
# If the position of the end of the connector is asked |
|
976 |
if size: |
|
977 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
978 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
979 |
else: |
|
980 |
x = parent_pos[0] + self.Pos.x |
|
981 |
y = parent_pos[1] + self.Pos.y |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
982 |
return wx.Point(x, y) |
0 | 983 |
|
984 |
# Change the connector relative position |
|
985 |
def SetPosition(self, pos): |
|
986 |
self.Pos = pos |
|
987 |
||
988 |
# Returns the connector direction |
|
989 |
def GetDirection(self): |
|
990 |
return self.Direction |
|
991 |
||
992 |
# Change the connector direction |
|
993 |
def SetDirection(self, direction): |
|
994 |
self.Direction = direction |
|
995 |
||
996 |
# Connect a wire to this connector at the last place |
|
997 |
def Connect(self, wire, refresh = True): |
|
998 |
self.InsertConnect(len(self.Wires), wire, refresh) |
|
999 |
||
1000 |
# Connect a wire to this connector at the place given |
|
1001 |
def InsertConnect(self, idx, wire, refresh = True): |
|
1002 |
if wire not in self.Wires: |
|
1003 |
self.Wires.insert(idx, wire) |
|
1004 |
if refresh: |
|
1005 |
self.ParentBlock.RefreshModel(False) |
|
1006 |
||
1007 |
# Returns the index of the wire given in the list of connected |
|
1008 |
def GetWireIndex(self, wire): |
|
1009 |
for i, (tmp_wire, handle) in enumerate(self.Wires): |
|
1010 |
if tmp_wire == wire: |
|
1011 |
return i |
|
1012 |
return None |
|
1013 |
||
1014 |
# Unconnect a wire or all wires connected to the connector |
|
2 | 1015 |
def UnConnect(self, wire = None, unconnect = True, delete = False): |
0 | 1016 |
i = 0 |
1017 |
found = False |
|
1018 |
while i < len(self.Wires) and not found: |
|
1019 |
if not wire or self.Wires[i][0] == wire: |
|
1020 |
# If Unconnect haven't been called from a wire, disconnect the connector in the wire |
|
1021 |
if unconnect: |
|
1022 |
if self.Wires[i][1] == 0: |
|
2 | 1023 |
self.Wires[i][0].UnConnectStartPoint(delete) |
0 | 1024 |
else: |
2 | 1025 |
self.Wires[i][0].UnConnectEndPoint(delete) |
0 | 1026 |
# Remove wire from connected |
1027 |
if wire: |
|
1028 |
self.Wires.pop(i) |
|
1029 |
found = True |
|
1030 |
i += 1 |
|
1031 |
# If no wire defined, unconnect all wires |
|
1032 |
if not wire: |
|
1033 |
self.Wires = [] |
|
1034 |
self.ParentBlock.RefreshModel(False) |
|
1035 |
||
1036 |
# Returns if connector has one or more wire connected |
|
1037 |
def IsConnected(self): |
|
1038 |
return len(self.Wires) > 0 |
|
1039 |
||
1040 |
# Move the wires connected |
|
1041 |
def MoveConnected(self, exclude = []): |
|
1042 |
if len(self.Wires) > 0: |
|
1043 |
# Calculate the new position of the end point |
|
1044 |
parent_pos = self.ParentBlock.GetPosition() |
|
1045 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
1046 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
1047 |
# Move the corresponding point on all the wires connected |
|
1048 |
for wire, index in self.Wires: |
|
1049 |
if wire not in exclude: |
|
1050 |
if index == 0: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1051 |
wire.MoveStartPoint(wx.Point(x, y)) |
0 | 1052 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1053 |
wire.MoveEndPoint(wx.Point(x, y)) |
0 | 1054 |
|
1055 |
# Refreshes the model of all the wires connected |
|
1056 |
def RefreshWires(self): |
|
1057 |
for wire in self.Wires: |
|
1058 |
wire[0].RefreshModel() |
|
1059 |
||
1060 |
# Refreshes the parent block model |
|
1061 |
def RefreshParentBlock(self): |
|
1062 |
self.ParentBlock.RefreshModel(False) |
|
1063 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1064 |
# Highlight the parent block |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1065 |
def HighlightParentBlock(self, highlight): |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1066 |
self.ParentBlock.SetHighlighted(highlight) |
144 | 1067 |
self.ParentBlock.Refresh() |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1068 |
|
27 | 1069 |
# Returns all the blocks connected to this connector |
1070 |
def GetConnectedBlocks(self): |
|
1071 |
blocks = [] |
|
1072 |
for wire, handle in self.Wires: |
|
1073 |
# Get other connector connected to each wire |
|
1074 |
if handle == 0: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1075 |
connector = wire.GetEndConnected() |
27 | 1076 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1077 |
connector = wire.GetStartConnected() |
27 | 1078 |
# Get parent block for this connector |
1079 |
if connector: |
|
1080 |
block = connector.GetParentBlock() |
|
1081 |
if block not in blocks: |
|
1082 |
blocks.append(block) |
|
1083 |
return blocks |
|
1084 |
||
0 | 1085 |
# Returns the connector negated property |
1086 |
def IsNegated(self): |
|
1087 |
return self.Negated |
|
1088 |
||
1089 |
# Changes the connector negated property |
|
1090 |
def SetNegated(self, negated): |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1091 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1092 |
self.Negated = negated |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1093 |
self.Edge = "none" |
0 | 1094 |
|
1095 |
# Returns the connector edge property |
|
1096 |
def GetEdge(self): |
|
1097 |
return self.Edge |
|
1098 |
||
1099 |
# Changes the connector edge property |
|
1100 |
def SetEdge(self, edge): |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1101 |
if self.ParentBlock.IsOfType("BOOL", self.Type): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1102 |
self.Edge = edge |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1103 |
self.Negated = False |
0 | 1104 |
|
1105 |
# Tests if the point given is near from the end point of this connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1106 |
def TestPoint(self, pt, direction = None, exclude = True): |
0 | 1107 |
parent_pos = self.ParentBlock.GetPosition() |
27 | 1108 |
if not (len(self.Wires) > 0 and self.OneConnected and exclude): |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1109 |
if direction is None or self.Direction == direction: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1110 |
# Calculate a square around the end point of this connector |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1111 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1112 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1113 |
width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1114 |
height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1115 |
rect = wx.Rect(x, y, width, height) |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1116 |
return rect.InsideXY(pt.x, pt.y) |
0 | 1117 |
return False |
1118 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1119 |
# Draws the highlightment of this element if it is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1120 |
def DrawHighlightment(self, dc): |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1121 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1122 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
144 | 1123 |
dc.SetLogicalFunction(wx.AND) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1124 |
parent_pos = self.ParentBlock.GetPosition() |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1125 |
posx = parent_pos[0] + self.Pos.x |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1126 |
posy = parent_pos[1] + self.Pos.y |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1127 |
width = CONNECTOR_SIZE |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1128 |
height = CONNECTOR_SIZE |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1129 |
if self.Direction[0] < 0: |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1130 |
posx += CONNECTOR_SIZE * self.Direction[0] |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1131 |
elif self.Direction[0] == 0: |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1132 |
posx -= 2 |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1133 |
width = 5 |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1134 |
if self.Direction[1] < 0: |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1135 |
posy += CONNECTOR_SIZE * self.Direction[1] |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1136 |
elif self.Direction[1] == 0: |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1137 |
posy -= 2 |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1138 |
height = 5 |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1139 |
dc.DrawRectangle(posx, posy, width, height) |
144 | 1140 |
dc.SetLogicalFunction(wx.COPY) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
1141 |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1142 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1143 |
if len(infos) == 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1144 |
for wire, handle in self.Wires: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1145 |
wire.MarkAsInvalid() |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1146 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1147 |
self.Errors[infos[0]] = (start, end) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1148 |
|
0 | 1149 |
# Draws the connector |
1150 |
def Draw(self, dc): |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1151 |
if len(self.Errors) > 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1152 |
dc.SetPen(wx.RED_PEN) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1153 |
dc.SetBrush(wx.Brush(wx.Colour(255, 255, 0))) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1154 |
else: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1155 |
dc.SetPen(self.Pen) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1156 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 1157 |
parent_pos = self.ParentBlock.GetPosition() |
213 | 1158 |
|
1159 |
if getattr(dc, "printing", False): |
|
1160 |
name_size = dc.GetTextExtent(self.Name) |
|
1161 |
else: |
|
1162 |
name_size = self.NameSize |
|
1163 |
||
0 | 1164 |
if self.Negated: |
1165 |
# If connector is negated, draw a circle |
|
1166 |
xcenter = parent_pos[0] + self.Pos.x + (CONNECTOR_SIZE * self.Direction[0]) / 2 |
|
1167 |
ycenter = parent_pos[1] + self.Pos.y + (CONNECTOR_SIZE * self.Direction[1]) / 2 |
|
1168 |
dc.DrawCircle(xcenter, ycenter, CONNECTOR_SIZE / 2) |
|
1169 |
else: |
|
1170 |
xstart = parent_pos[0] + self.Pos.x |
|
1171 |
ystart = parent_pos[1] + self.Pos.y |
|
1172 |
if self.Edge == "rising": |
|
1173 |
# If connector has a rising edge, draw a right arrow |
|
1174 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart - 4) |
|
1175 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart + 4) |
|
1176 |
elif self.Edge == "falling": |
|
1177 |
# If connector has a falling edge, draw a left arrow |
|
1178 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4) |
|
1179 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4) |
|
1180 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
1181 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
1182 |
dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend) |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1183 |
if len(self.Errors) > 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1184 |
dc.SetPen(self.Pen) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
222
diff
changeset
|
1185 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 1186 |
if self.Direction[0] != 0: |
213 | 1187 |
ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2 |
0 | 1188 |
if self.Direction[0] < 0: |
1189 |
xtext = parent_pos[0] + self.Pos.x + 5 |
|
1190 |
else: |
|
213 | 1191 |
xtext = parent_pos[0] + self.Pos.x - (name_size[0] + 5) |
0 | 1192 |
if self.Direction[1] != 0: |
213 | 1193 |
xtext = parent_pos[0] + self.Pos.x - name_size[0] / 2 |
0 | 1194 |
if self.Direction[1] < 0: |
1195 |
ytext = parent_pos[1] + self.Pos.y + 5 |
|
1196 |
else: |
|
213 | 1197 |
ytext = parent_pos[1] + self.Pos.y - (name_size[1] + 5) |
0 | 1198 |
# Draw the text |
1199 |
dc.DrawText(self.Name, xtext, ytext) |
|
1200 |
||
1201 |
||
1202 |
#------------------------------------------------------------------------------- |
|
1203 |
# Common Wire Element |
|
1204 |
#------------------------------------------------------------------------------- |
|
1205 |
||
1206 |
""" |
|
1207 |
Class that implements a wire for connecting two blocks |
|
1208 |
""" |
|
1209 |
||
1210 |
class Wire(Graphic_Element): |
|
1211 |
||
1212 |
# Create a new wire |
|
1213 |
def __init__(self, parent, start = None, end = None): |
|
1214 |
Graphic_Element.__init__(self, parent) |
|
1215 |
self.StartPoint = start |
|
1216 |
self.EndPoint = end |
|
1217 |
self.StartConnected = None |
|
1218 |
self.EndConnected = None |
|
1219 |
# If the start and end points are defined, calculate the wire |
|
1220 |
if start and end: |
|
1221 |
self.ResetPoints() |
|
1222 |
self.GeneratePoints() |
|
1223 |
else: |
|
1224 |
self.Points = [] |
|
1225 |
self.Segments = [] |
|
1226 |
self.SelectedSegment = None |
|
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1227 |
self.Valid = True |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1228 |
self.Value = None |
0 | 1229 |
self.OverStart = False |
1230 |
self.OverEnd = False |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1231 |
self.ComputingType = False |
0 | 1232 |
|
1233 |
# Destructor of a wire |
|
1234 |
def __del__(self): |
|
1235 |
self.StartConnected = None |
|
1236 |
self.EndConnected = None |
|
1237 |
||
144 | 1238 |
# Returns the RedrawRect |
1239 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1240 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1241 |
if self.StartConnected: |
|
1242 |
rect = rect.Union(self.StartConnected.GetRedrawRect(movex, movey)) |
|
1243 |
if self.EndConnected: |
|
1244 |
rect = rect.Union(self.EndConnected.GetRedrawRect(movex, movey)) |
|
1245 |
return rect |
|
1246 |
||
0 | 1247 |
# Forbids to change the wire position |
1248 |
def SetPosition(x, y): |
|
1249 |
pass |
|
1250 |
||
1251 |
# Forbids to change the wire size |
|
1252 |
def SetSize(width, height): |
|
1253 |
pass |
|
1254 |
||
27 | 1255 |
# Returns connector to which start point is connected |
1256 |
def GetStartConnected(self): |
|
1257 |
return self.StartConnected |
|
1258 |
||
98 | 1259 |
# Returns connector to which start point is connected |
1260 |
def GetStartConnectedType(self): |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1261 |
if self.StartConnected and not self.ComputingType: |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1262 |
self.ComputingType = True |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1263 |
computed_type = self.StartConnected.GetType() |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1264 |
self.ComputingType = False |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1265 |
return computed_type |
98 | 1266 |
return None |
1267 |
||
27 | 1268 |
# Returns connector to which end point is connected |
1269 |
def GetEndConnected(self): |
|
1270 |
return self.EndConnected |
|
1271 |
||
98 | 1272 |
# Returns connector to which end point is connected |
1273 |
def GetEndConnectedType(self): |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1274 |
if self.EndConnected and not self.ComputingType: |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1275 |
self.ComputingType = True |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1276 |
computed_type = self.EndConnected.GetType() |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1277 |
self.ComputingType = False |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1278 |
return computed_type |
98 | 1279 |
return None |
1280 |
||
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1281 |
def GetConnectionDirection(self): |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1282 |
if self.StartConnected is None and self.EndConnected is None: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1283 |
return None |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1284 |
elif self.StartConnected is not None and self.EndConnected is None: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1285 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1286 |
elif self.StartConnected is None and self.EndConnected is not None: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1287 |
return self.EndPoint |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1288 |
elif self.Handle is not None: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1289 |
handle_type, handle = self.Handle |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1290 |
# A point has been handled |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1291 |
if handle_type == HANDLE_POINT: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1292 |
if handle == 0: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1293 |
return self.EndPoint |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1294 |
else: |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1295 |
return (-self.StartPoint[1][0], -self.StartPoint[1][1]) |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1296 |
return None |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
1297 |
|
145 | 1298 |
def GetOtherConnected(self, connector): |
1299 |
if self.StartConnected == connector: |
|
1300 |
return self.EndConnected |
|
1301 |
else: |
|
1302 |
return self.StartConnected |
|
1303 |
||
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1304 |
def GetOtherConnectedType(self, handle): |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1305 |
if handle == 0: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1306 |
return self.GetEndConnectedType() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1307 |
else: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1308 |
return self.GetStartConnectedType() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
98
diff
changeset
|
1309 |
|
98 | 1310 |
def IsConnectedCompatible(self): |
1311 |
if self.StartConnected: |
|
1312 |
return self.StartConnected.IsCompatible(self.GetEndConnectedType()) |
|
1313 |
elif self.EndConnected: |
|
1314 |
return True |
|
1315 |
return False |
|
1316 |
||
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1317 |
def SetValue(self, value): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1318 |
self.Value = value |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
1319 |
|
0 | 1320 |
# Unconnect the start and end points |
1321 |
def Clean(self): |
|
1322 |
if self.StartConnected: |
|
1323 |
self.UnConnectStartPoint() |
|
1324 |
if self.EndConnected: |
|
1325 |
self.UnConnectEndPoint() |
|
1326 |
||
1327 |
# Delete this wire by calling the corresponding method |
|
1328 |
def Delete(self): |
|
1329 |
self.Parent.DeleteWire(self) |
|
1330 |
||
1331 |
# Select a segment and not the whole wire. It's useful for Ladder Diagram |
|
1332 |
def SetSelectedSegment(self, segment): |
|
1333 |
# The last segment is indicated |
|
1334 |
if segment == -1: |
|
1335 |
segment = len(self.Segments) - 1 |
|
1336 |
# The selected segment is reinitialised |
|
1337 |
if segment == None: |
|
1338 |
if self.StartConnected: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1339 |
self.StartConnected.SetPen(wx.BLACK_PEN) |
0 | 1340 |
if self.EndConnected: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1341 |
self.EndConnected.SetPen(wx.BLACK_PEN) |
0 | 1342 |
# The segment selected is the first |
1343 |
elif segment == 0: |
|
1344 |
if self.StartConnected: |
|
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1345 |
self.StartConnected.SetPen(wx.BLUE_PEN) |
0 | 1346 |
if self.EndConnected: |
1347 |
# There is only one segment |
|
1348 |
if len(self.Segments) == 1: |
|
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1349 |
self.EndConnected.SetPen(wx.BLUE_PEN) |
0 | 1350 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1351 |
self.EndConnected.SetPen(wx.BLACK_PEN) |
0 | 1352 |
# The segment selected is the last |
1353 |
elif segment == len(self.Segments) - 1: |
|
1354 |
if self.StartConnected: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1355 |
self.StartConnected.SetPen(wx.BLACK_PEN) |
0 | 1356 |
if self.EndConnected: |
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1357 |
self.EndConnected.SetPen(wx.BLUE_PEN) |
0 | 1358 |
self.SelectedSegment = segment |
144 | 1359 |
self.Refresh() |
0 | 1360 |
|
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1361 |
# Select a segment and not the whole wire. It's useful for Ladder Diagram |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1362 |
def MarkAsInvalid(self): |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1363 |
self.Valid = False |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1364 |
if self.StartConnected: |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1365 |
self.StartConnected.SetPen(wx.RED_PEN) |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1366 |
if self.EndConnected: |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1367 |
self.EndConnected.SetPen(wx.RED_PEN) |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
1368 |
|
0 | 1369 |
# Reinitialize the wire points |
1370 |
def ResetPoints(self): |
|
1371 |
if self.StartPoint and self.EndPoint: |
|
1372 |
self.Points = [self.StartPoint[0], self.EndPoint[0]] |
|
1373 |
self.Segments = [self.StartPoint[1]] |
|
1374 |
else: |
|
1375 |
self.Points = [] |
|
1376 |
self.Segments = [] |
|
1377 |
||
1378 |
# Refresh the wire bounding box |
|
1379 |
def RefreshBoundingBox(self): |
|
1380 |
if len(self.Points) > 0: |
|
1381 |
# If startpoint or endpoint is connected, save the point radius |
|
1382 |
start_radius = end_radius = 0 |
|
1383 |
if not self.StartConnected: |
|
1384 |
start_radius = POINT_RADIUS |
|
1385 |
if not self.EndConnected: |
|
1386 |
end_radius = POINT_RADIUS |
|
1387 |
# Initialize minimum and maximum from the first point |
|
1388 |
minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius |
|
1389 |
maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius |
|
1390 |
miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius |
|
1391 |
maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius |
|
1392 |
# Actualize minimum and maximum with the other points |
|
1393 |
for point in self.Points[1:-1]: |
|
1394 |
minx, minbbxx = min(minx, point.x), min(minbbxx, point.x) |
|
1395 |
maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x) |
|
1396 |
miny, minbbxy = min(miny, point.y), min(minbbxy, point.y) |
|
1397 |
maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y) |
|
1398 |
if len(self.Points) > 1: |
|
1399 |
minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius) |
|
1400 |
maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius) |
|
1401 |
miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius) |
|
1402 |
maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius) |
|
108 | 1403 |
self.Pos.x, self.Pos.y = minx, miny |
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1404 |
self.Size = wx.Size(maxx - minx, maxy - miny) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1405 |
self.BoundingBox = wx.Rect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1) |
0 | 1406 |
|
1407 |
# Refresh the realpoints that permits to keep the proportionality in wire during resizing |
|
1408 |
def RefreshRealPoints(self): |
|
1409 |
if len(self.Points) > 0: |
|
1410 |
self.RealPoints = [] |
|
1411 |
# Calculate float relative position of each point with the minimum point |
|
1412 |
for point in self.Points: |
|
1413 |
self.RealPoints.append([float(point.x - self.Pos.x), float(point.y - self.Pos.y)]) |
|
1414 |
||
1415 |
# Returns the wire minimum size |
|
1416 |
def GetMinSize(self): |
|
1417 |
width = 1 |
|
1418 |
height = 1 |
|
1419 |
dir_product = product(self.StartPoint[1], self.EndPoint[1]) |
|
1420 |
# The directions are opposed |
|
1421 |
if dir_product < 0: |
|
1422 |
if self.StartPoint[0] != 0: |
|
1423 |
width = MIN_SEGMENT_SIZE * 2 |
|
1424 |
if self.StartPoint[1] != 0: |
|
1425 |
height = MIN_SEGMENT_SIZE * 2 |
|
1426 |
# The directions are the same |
|
1427 |
elif dir_product > 0: |
|
1428 |
if self.StartPoint[0] != 0: |
|
1429 |
width = MIN_SEGMENT_SIZE |
|
1430 |
if self.StartPoint[1] != 0: |
|
1431 |
height = MIN_SEGMENT_SIZE |
|
1432 |
# The directions are perpendiculars |
|
1433 |
else: |
|
1434 |
width = MIN_SEGMENT_SIZE |
|
1435 |
height = MIN_SEGMENT_SIZE |
|
1436 |
return width + 1, height + 1 |
|
1437 |
||
1438 |
# Returns if the point given is on one of the wire segments |
|
1439 |
def HitTest(self, pt): |
|
1440 |
test = False |
|
1441 |
for i in xrange(len(self.Points) - 1): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1442 |
rect = wx.Rect(0, 0, 0, 0) |
0 | 1443 |
x1, y1 = self.Points[i].x, self.Points[i].y |
1444 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1445 |
# Calculate a rectangle around the segment |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1446 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
0 | 1447 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
1448 |
test |= rect.InsideXY(pt.x, pt.y) |
|
1449 |
return test |
|
1450 |
||
1451 |
# Returns the wire start or end point if the point given is on one of them |
|
1452 |
def TestPoint(self, pt): |
|
1453 |
# Test the wire start point |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1454 |
rect = wx.Rect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE, |
0 | 1455 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
1456 |
if rect.InsideXY(pt.x, pt.y): |
|
1457 |
return 0 |
|
1458 |
# Test the wire end point |
|
1459 |
if len(self.Points) > 1: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1460 |
rect = wx.Rect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE, |
0 | 1461 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
1462 |
if rect.InsideXY(pt.x, pt.y): |
|
1463 |
return -1 |
|
1464 |
return None |
|
1465 |
||
1466 |
# Returns the wire segment if the point given is on it |
|
1467 |
def TestSegment(self, pt, all=False): |
|
1468 |
for i in xrange(len(self.Segments)): |
|
1469 |
# If wire is not in a Ladder Diagram, first and last segments are excluded |
|
237 | 1470 |
if all or 0 < i < len(self.Segments) - 1: |
0 | 1471 |
x1, y1 = self.Points[i].x, self.Points[i].y |
1472 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1473 |
# Calculate a rectangle around the segment |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1474 |
rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
0 | 1475 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
1476 |
if rect.InsideXY(pt.x, pt.y): |
|
1477 |
return i, self.Segments[i] |
|
1478 |
return None |
|
1479 |
||
1480 |
# Define the wire points |
|
1481 |
def SetPoints(self, points): |
|
1482 |
if len(points) > 1: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1483 |
self.Points = [wx.Point(x, y) for x, y in points] |
0 | 1484 |
# Calculate the start and end directions |
1485 |
self.StartPoint = [None, vector(self.Points[0], self.Points[1])] |
|
1486 |
self.EndPoint = [None, vector(self.Points[-1], self.Points[-2])] |
|
1487 |
# Calculate the start and end points |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1488 |
self.StartPoint[0] = wx.Point(self.Points[0].x + CONNECTOR_SIZE * self.StartPoint[1][0], |
0 | 1489 |
self.Points[0].y + CONNECTOR_SIZE * self.StartPoint[1][1]) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1490 |
self.EndPoint[0] = wx.Point(self.Points[-1].x + CONNECTOR_SIZE * self.EndPoint[1][0], |
0 | 1491 |
self.Points[-1].y + CONNECTOR_SIZE * self.EndPoint[1][1]) |
1492 |
self.Points[0] = self.StartPoint[0] |
|
1493 |
self.Points[-1] = self.EndPoint[0] |
|
1494 |
# Calculate the segments directions |
|
1495 |
self.Segments = [] |
|
1496 |
for i in xrange(len(self.Points) - 1): |
|
1497 |
self.Segments.append(vector(self.Points[i], self.Points[i + 1])) |
|
1498 |
self.RefreshBoundingBox() |
|
1499 |
self.RefreshRealPoints() |
|
1500 |
||
1501 |
# Returns the position of the point indicated |
|
1502 |
def GetPoint(self, index): |
|
1503 |
if index < len(self.Points): |
|
1504 |
return self.Points[index].x, self.Points[index].y |
|
1505 |
return None |
|
1506 |
||
1507 |
# Returns a list of the position of all wire points |
|
1508 |
def GetPoints(self, invert = False): |
|
1509 |
points = self.VerifyPoints() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1510 |
points[0] = wx.Point(points[0].x - CONNECTOR_SIZE * self.StartPoint[1][0], |
0 | 1511 |
points[0].y - CONNECTOR_SIZE * self.StartPoint[1][1]) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1512 |
points[-1] = wx.Point(points[-1].x - CONNECTOR_SIZE * self.EndPoint[1][0], |
0 | 1513 |
points[-1].y - CONNECTOR_SIZE * self.EndPoint[1][1]) |
1514 |
# An inversion of the list is asked |
|
1515 |
if invert: |
|
1516 |
points.reverse() |
|
1517 |
return points |
|
1518 |
||
1519 |
# Returns the position of the two selected segment points |
|
1520 |
def GetSelectedSegmentPoints(self): |
|
1521 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
1522 |
return self.Points[self.SelectedSegment:self.SelectedSegment + 2] |
|
1523 |
return [] |
|
1524 |
||
1525 |
# Returns if the selected segment is the first and/or the last of the wire |
|
1526 |
def GetSelectedSegmentConnections(self): |
|
1527 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
1528 |
return self.SelectedSegment == 0, self.SelectedSegment == len(self.Segments) - 1 |
|
1529 |
return (True, True) |
|
1530 |
||
1531 |
# Returns the connectors on which the wire is connected |
|
1532 |
def GetConnected(self): |
|
1533 |
connected = [] |
|
1534 |
if self.StartConnected and self.StartPoint[1] == WEST: |
|
1535 |
connected.append(self.StartConnected) |
|
1536 |
if self.EndConnected and self.EndPoint[1] == WEST: |
|
1537 |
connected.append(self.EndConnected) |
|
1538 |
return connected |
|
1539 |
||
1540 |
# Returns the id of the block connected to the first or the last wire point |
|
27 | 1541 |
def GetConnectedInfos(self, index): |
0 | 1542 |
if index == 0 and self.StartConnected: |
27 | 1543 |
return self.StartConnected.GetBlockId(), self.StartConnected.GetName() |
0 | 1544 |
elif index == -1 and self.EndConnected: |
42 | 1545 |
return self.EndConnected.GetBlockId(), self.EndConnected.GetName() |
0 | 1546 |
return None |
1547 |
||
1548 |
# Update the wire points position by keeping at most possible the current positions |
|
1549 |
def GeneratePoints(self, realpoints = True): |
|
1550 |
i = 0 |
|
1551 |
# Calculate the start enad end points with the minimum segment size in the right direction |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1552 |
end = wx.Point(self.EndPoint[0].x + self.EndPoint[1][0] * MIN_SEGMENT_SIZE, |
0 | 1553 |
self.EndPoint[0].y + self.EndPoint[1][1] * MIN_SEGMENT_SIZE) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1554 |
start = wx.Point(self.StartPoint[0].x + self.StartPoint[1][0] * MIN_SEGMENT_SIZE, |
0 | 1555 |
self.StartPoint[0].y + self.StartPoint[1][1] * MIN_SEGMENT_SIZE) |
1556 |
# Evaluate the point till it's the last |
|
1557 |
while i < len(self.Points) - 1: |
|
1558 |
# The next point is the last |
|
1559 |
if i + 1 == len(self.Points) - 1: |
|
1560 |
# Calculate the direction from current point to end point |
|
1561 |
v_end = vector(self.Points[i], end) |
|
1562 |
# The current point is the first |
|
1563 |
if i == 0: |
|
1564 |
# If the end point is not in the start direction, a point is added |
|
1565 |
if v_end != self.Segments[0] or v_end == self.EndPoint[1]: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1566 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
0 | 1567 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
1568 |
self.Segments[0][0]), v_end, self.EndPoint[1])) |
|
1569 |
# The current point is the second |
|
1570 |
elif i == 1: |
|
1571 |
# The previous direction and the target direction are mainly opposed, a point is added |
|
1572 |
if product(v_end, self.Segments[0]) < 0: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1573 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
0 | 1574 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
1575 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1576 |
# The previous direction and the end direction are the same or they are |
|
1577 |
# perpendiculars and the end direction points towards current segment |
|
1578 |
elif product(self.Segments[0], self.EndPoint[1]) >= 0 and product(self.Segments[1], self.EndPoint[1]) <= 0: |
|
1579 |
# Current point and end point are aligned |
|
1580 |
if self.Segments[0][0] != 0: |
|
1581 |
self.Points[1].x = end.x |
|
1582 |
if self.Segments[0][1] != 0: |
|
1583 |
self.Points[1].y = end.y |
|
1584 |
# If the previous direction and the end direction are the same, a point is added |
|
1585 |
if product(self.Segments[0], self.EndPoint[1]) > 0: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1586 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
0 | 1587 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
1588 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1589 |
else: |
|
1590 |
# Current point is positioned in the middle of start point |
|
1591 |
# and end point on the current direction and a point is added |
|
1592 |
if self.Segments[0][0] != 0: |
|
1593 |
self.Points[1].x = (end.x + start.x) / 2 |
|
1594 |
if self.Segments[0][1] != 0: |
|
1595 |
self.Points[1].y = (end.y + start.y) / 2 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1596 |
self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) |
0 | 1597 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
1598 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1599 |
else: |
|
1600 |
# The previous direction and the end direction are perpendiculars |
|
1601 |
if product(self.Segments[i - 1], self.EndPoint[1]) == 0: |
|
1602 |
# The target direction and the end direction aren't mainly the same |
|
1603 |
if product(v_end, self.EndPoint[1]) <= 0: |
|
1604 |
# Current point and end point are aligned |
|
1605 |
if self.Segments[i - 1][0] != 0: |
|
1606 |
self.Points[i].x = end.x |
|
1607 |
if self.Segments[i - 1][1] != 0: |
|
1608 |
self.Points[i].y = end.y |
|
1609 |
# Previous direction is updated from the new point |
|
1610 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
1611 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
1612 |
else: |
|
1613 |
test = True |
|
1614 |
# If the current point is the third, test if the second |
|
1615 |
# point can be aligned with the end point |
|
1616 |
if i == 2: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1617 |
test_point = wx.Point(self.Points[1].x, self.Points[1].y) |
0 | 1618 |
if self.Segments[1][0] != 0: |
1619 |
test_point.y = end.y |
|
1620 |
if self.Segments[1][1] != 0: |
|
1621 |
test_point.x = end.x |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1622 |
vector_test = vector(self.Points[0], test_point, False) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1623 |
test = norm(vector_test) > MIN_SEGMENT_SIZE and product(self.Segments[0], vector_test) > 0 |
0 | 1624 |
# The previous point can be aligned |
1625 |
if test: |
|
1626 |
self.Points[i].x, self.Points[i].y = end.x, end.y |
|
1627 |
if self.Segments[i - 1][0] != 0: |
|
1628 |
self.Points[i - 1].y = end.y |
|
1629 |
if self.Segments[i - 1][1] != 0: |
|
1630 |
self.Points[i - 1].x = end.x |
|
1631 |
self.Segments[i] = (-self.EndPoint[1][0], -self.EndPoint[1][1]) |
|
1632 |
else: |
|
1633 |
# Current point is positioned in the middle of previous point |
|
1634 |
# and end point on the current direction and a point is added |
|
1635 |
if self.Segments[1][0] != 0: |
|
1636 |
self.Points[2].x = (self.Points[1].x + end.x) / 2 |
|
1637 |
if self.Segments[1][1] != 0: |
|
1638 |
self.Points[2].y = (self.Points[1].y + end.y) / 2 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1639 |
self.Points.insert(3, wx.Point(self.Points[2].x, self.Points[2].y)) |
0 | 1640 |
self.Segments.insert(3, DirectionChoice((self.Segments[2][1], |
1641 |
self.Segments[2][0]), v_end, self.EndPoint[1])) |
|
1642 |
else: |
|
1643 |
# Current point is aligned with end point |
|
1644 |
if self.Segments[i - 1][0] != 0: |
|
1645 |
self.Points[i].x = end.x |
|
1646 |
if self.Segments[i - 1][1] != 0: |
|
1647 |
self.Points[i].y = end.y |
|
1648 |
# Previous direction is updated from the new point |
|
1649 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
1650 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
1651 |
# If previous direction and end direction are opposed |
|
1652 |
if product(self.Segments[i - 1], self.EndPoint[1]) < 0: |
|
1653 |
# Current point is positioned in the middle of previous point |
|
1654 |
# and end point on the current direction |
|
1655 |
if self.Segments[i - 1][0] != 0: |
|
1656 |
self.Points[i].x = (end.x + self.Points[i - 1].x) / 2 |
|
1657 |
if self.Segments[i - 1][1] != 0: |
|
1658 |
self.Points[i].y = (end.y + self.Points[i - 1].y) / 2 |
|
1659 |
# A point is added |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1660 |
self.Points.insert(i + 1, wx.Point(self.Points[i].x, self.Points[i].y)) |
0 | 1661 |
self.Segments.insert(i + 1, DirectionChoice((self.Segments[i][1], |
1662 |
self.Segments[i][0]), v_end, self.EndPoint[1])) |
|
1663 |
else: |
|
1664 |
# Current point is the first, and second is not mainly in the first direction |
|
1665 |
if i == 0 and product(vector(start, self.Points[1]), self.Segments[0]) < 0: |
|
1666 |
# If first and second directions aren't perpendiculars, a point is added |
|
1667 |
if product(self.Segments[0], self.Segments[1]) != 0: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1668 |
self.Points.insert(1, wx.Point(start.x, start.y)) |
0 | 1669 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
1670 |
self.Segments[0][0]), vector(start, self.Points[1]), self.Segments[1])) |
|
1671 |
else: |
|
1672 |
self.Points[1].x, self.Points[1].y = start.x, start.y |
|
1673 |
else: |
|
1674 |
# Next point is aligned with current point |
|
1675 |
if self.Segments[i][0] != 0: |
|
1676 |
self.Points[i + 1].y = self.Points[i].y |
|
1677 |
if self.Segments[i][1] != 0: |
|
1678 |
self.Points[i + 1].x = self.Points[i].x |
|
1679 |
# Current direction is updated from the new point |
|
1680 |
if product(vector(self.Points[i], self.Points[i + 1]), self.Segments[i]) < 0: |
|
1681 |
self.Segments[i] = (-self.Segments[i][0], -self.Segments[i][1]) |
|
1682 |
i += 1 |
|
1683 |
self.RefreshBoundingBox() |
|
1684 |
if realpoints: |
|
1685 |
self.RefreshRealPoints() |
|
1686 |
||
1687 |
# Verify that two consecutive points haven't the same position |
|
1688 |
def VerifyPoints(self): |
|
1689 |
points = [point for point in self.Points] |
|
1690 |
segments = [segment for segment in self.Segments] |
|
1691 |
i = 1 |
|
1692 |
while i < len(points) - 1: |
|
1693 |
if points[i] == points[i + 1] and segments[i - 1] == segments[i + 1]: |
|
1694 |
for j in xrange(2): |
|
1695 |
points.pop(i) |
|
1696 |
segments.pop(i) |
|
1697 |
else: |
|
1698 |
i += 1 |
|
1699 |
# If the wire isn't in a Ladder Diagram, save the new point list |
|
1700 |
if self.Parent.__class__.__name__ != "LD_Viewer": |
|
1701 |
self.Points = [point for point in points] |
|
1702 |
self.Segments = [segment for segment in segments] |
|
1703 |
self.RefreshBoundingBox() |
|
1704 |
self.RefreshRealPoints() |
|
1705 |
return points |
|
1706 |
||
1707 |
# Moves all the wire points except the first and the last if they are connected |
|
1708 |
def Move(self, dx, dy, endpoints = False): |
|
1709 |
for i, point in enumerate(self.Points): |
|
1710 |
if endpoints or not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1711 |
point.x += dx |
|
1712 |
point.y += dy |
|
1713 |
self.StartPoint[0] = self.Points[0] |
|
1714 |
self.EndPoint[0] = self.Points[-1] |
|
1715 |
self.GeneratePoints() |
|
1716 |
||
1717 |
# Resize the wire from position and size given |
|
1718 |
def Resize(self, x, y, width, height): |
|
1719 |
if len(self.Points) > 1: |
|
1720 |
# Calculate the new position of each point for testing the new size |
|
1721 |
minx, miny = self.Pos.x, self.Pos.y |
|
1722 |
lastwidth, lastheight = self.Size.width, self.Size.height |
|
1723 |
for i, point in enumerate(self.RealPoints): |
|
1724 |
# If start or end point is connected, it's not calculate |
|
1725 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1726 |
if i == 0: |
|
1727 |
dir = self.StartPoint[1] |
|
1728 |
elif i == len(self.Points) - 1: |
|
1729 |
dir = self.EndPoint[1] |
|
1730 |
else: |
|
1731 |
dir = (0, 0) |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1732 |
pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * width / float(max(lastwidth, 1)))), |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1733 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1734 |
pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * height / float(max(lastheight, 1)))), |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1735 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1736 |
self.Points[i] = wx.Point(minx + x + pointx, miny + y + pointy) |
0 | 1737 |
self.StartPoint[0] = self.Points[0] |
1738 |
self.EndPoint[0] = self.Points[-1] |
|
1739 |
self.GeneratePoints(False) |
|
1740 |
# Test if the wire position or size have changed |
|
1741 |
if x != 0 and minx == self.Pos.x: |
|
1742 |
x = 0 |
|
1743 |
width = lastwidth |
|
1744 |
if y != 0 and miny == self.Pos.y: |
|
1745 |
y = 0 |
|
1746 |
height = lastwidth |
|
1747 |
if width != lastwidth and lastwidth == self.Size.width: |
|
1748 |
width = lastwidth |
|
1749 |
if height != lastheight and lastheight == self.Size.height: |
|
1750 |
height = lastheight |
|
1751 |
# Calculate the real points from the new size, it's important for |
|
1752 |
# keeping a proportionality in the points position with the size |
|
1753 |
# duringa resize dragging |
|
1754 |
for i, point in enumerate(self.RealPoints): |
|
1755 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1756 |
point[0] = point[0] * width / float(max(lastwidth, 1)) |
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1757 |
point[1] = point[1] * height / float(max(lastheight, 1)) |
0 | 1758 |
# Calculate the correct position of the points from real points |
1759 |
for i, point in enumerate(self.RealPoints): |
|
1760 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1761 |
if i == 0: |
|
1762 |
dir = self.StartPoint[1] |
|
1763 |
elif i == len(self.Points) - 1: |
|
1764 |
dir = self.EndPoint[1] |
|
1765 |
else: |
|
1766 |
dir = (0, 0) |
|
1767 |
realpointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0])), |
|
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1768 |
width - dir[0] * MIN_SEGMENT_SIZE)) |
0 | 1769 |
realpointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1])), |
208
c70aefcadf66
Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents:
180
diff
changeset
|
1770 |
height - dir[1] * MIN_SEGMENT_SIZE)) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1771 |
self.Points[i] = wx.Point(minx + x + realpointx, miny + y + realpointy) |
0 | 1772 |
self.StartPoint[0] = self.Points[0] |
1773 |
self.EndPoint[0] = self.Points[-1] |
|
1774 |
self.GeneratePoints(False) |
|
1775 |
||
1776 |
# Moves the wire start point and update the wire points |
|
1777 |
def MoveStartPoint(self, point): |
|
1778 |
if len(self.Points) > 1: |
|
1779 |
self.StartPoint[0] = point |
|
1780 |
self.Points[0] = point |
|
1781 |
self.GeneratePoints() |
|
1782 |
||
1783 |
# Changes the wire start direction and update the wire points |
|
1784 |
def SetStartPointDirection(self, dir): |
|
1785 |
if len(self.Points) > 1: |
|
1786 |
self.StartPoint[1] = dir |
|
1787 |
self.Segments[0] = dir |
|
1788 |
self.GeneratePoints() |
|
1789 |
||
1790 |
# Rotates the wire start direction by an angle of 90 degrees anticlockwise |
|
1791 |
def RotateStartPoint(self): |
|
1792 |
self.SetStartPointDirection((self.StartPoint[1][1], -self.StartPoint[1][0])) |
|
1793 |
||
1794 |
# Connects wire start point to the connector given and moves wire start point |
|
1795 |
# to given point |
|
1796 |
def ConnectStartPoint(self, point, connector): |
|
1797 |
if point: |
|
1798 |
self.MoveStartPoint(point) |
|
1799 |
self.StartConnected = connector |
|
145 | 1800 |
self.RefreshBoundingBox() |
0 | 1801 |
|
1802 |
# Unconnects wire start point |
|
2 | 1803 |
def UnConnectStartPoint(self, delete = False): |
1804 |
if delete: |
|
60 | 1805 |
self.StartConnected = None |
2 | 1806 |
self.Delete() |
60 | 1807 |
elif self.StartConnected: |
1808 |
self.StartConnected.UnConnect(self, unconnect = False) |
|
2 | 1809 |
self.StartConnected = None |
145 | 1810 |
self.RefreshBoundingBox() |
0 | 1811 |
|
1812 |
# Moves the wire end point and update the wire points |
|
1813 |
def MoveEndPoint(self, point): |
|
1814 |
if len(self.Points) > 1: |
|
1815 |
self.EndPoint[0] = point |
|
1816 |
self.Points[-1] = point |
|
1817 |
self.GeneratePoints() |
|
1818 |
||
1819 |
# Changes the wire end direction and update the wire points |
|
1820 |
def SetEndPointDirection(self, dir): |
|
1821 |
if len(self.Points) > 1: |
|
1822 |
self.EndPoint[1] = dir |
|
1823 |
self.GeneratePoints() |
|
1824 |
||
1825 |
# Rotates the wire end direction by an angle of 90 degrees anticlockwise |
|
1826 |
def RotateEndPoint(self): |
|
1827 |
self.SetEndPointDirection((self.EndPoint[1][1], -self.EndPoint[1][0])) |
|
1828 |
||
1829 |
# Connects wire end point to the connector given and moves wire end point |
|
1830 |
# to given point |
|
1831 |
def ConnectEndPoint(self, point, connector): |
|
1832 |
if point: |
|
1833 |
self.MoveEndPoint(point) |
|
1834 |
self.EndConnected = connector |
|
145 | 1835 |
self.RefreshBoundingBox() |
0 | 1836 |
|
1837 |
# Unconnects wire end point |
|
2 | 1838 |
def UnConnectEndPoint(self, delete = False): |
1839 |
if delete: |
|
60 | 1840 |
self.EndConnected = None |
2 | 1841 |
self.Delete() |
60 | 1842 |
elif self.EndConnected: |
1843 |
self.EndConnected.UnConnect(self, unconnect = False) |
|
2 | 1844 |
self.EndConnected = None |
145 | 1845 |
self.RefreshBoundingBox() |
0 | 1846 |
|
1847 |
# Moves the wire segment given by its index |
|
145 | 1848 |
def MoveSegment(self, idx, movex, movey, scaling): |
0 | 1849 |
if 0 < idx < len(self.Segments) - 1: |
1850 |
if self.Segments[idx] in (NORTH, SOUTH): |
|
112 | 1851 |
start_x = self.Points[idx].x |
145 | 1852 |
if scaling is not None: |
1853 |
movex = round(float(self.Points[idx].x + movex) / float(scaling[0])) * scaling[0] - self.Points[idx].x |
|
0 | 1854 |
self.Points[idx].x += movex |
1855 |
self.Points[idx + 1].x += movex |
|
112 | 1856 |
self.GeneratePoints() |
1857 |
if start_x != self.Points[idx].x: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1858 |
return self.Points[idx].x - start_x, 0 |
0 | 1859 |
elif self.Segments[idx] in (EAST, WEST): |
112 | 1860 |
start_y = self.Points[idx].y |
145 | 1861 |
if scaling is not None: |
1862 |
movey = round(float(self.Points[idx].y + movey) / float(scaling[1])) * scaling[1] - self.Points[idx].y |
|
0 | 1863 |
self.Points[idx].y += movey |
1864 |
self.Points[idx + 1].y += movey |
|
112 | 1865 |
self.GeneratePoints() |
1866 |
if start_y != self.Points[idx].y: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1867 |
return 0, self.Points[idx].y - start_y |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1868 |
return 0, 0 |
0 | 1869 |
|
1870 |
# Adds two points in the middle of the handled segment |
|
1871 |
def AddSegment(self): |
|
1872 |
handle_type, handle = self.Handle |
|
1873 |
if handle_type == HANDLE_SEGMENT: |
|
1874 |
segment, dir = handle |
|
1875 |
pointx = self.Points[segment].x |
|
1876 |
pointy = self.Points[segment].y |
|
1877 |
if dir[0] != 0: |
|
1878 |
pointx = (self.Points[segment].x + self.Points[segment + 1].x) / 2 |
|
1879 |
if dir[1] != 0: |
|
1880 |
pointy = (self.Points[segment].y + self.Points[segment + 1].y) / 2 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1881 |
self.Points.insert(segment + 1, wx.Point(pointx, pointy)) |
0 | 1882 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1883 |
self.Points.insert(segment + 2, wx.Point(pointx, pointy)) |
0 | 1884 |
self.Segments.insert(segment + 2, dir) |
1885 |
self.GeneratePoints() |
|
1886 |
||
1887 |
# Delete the handled segment by removing the two segment points |
|
1888 |
def DeleteSegment(self): |
|
1889 |
handle_type, handle = self.Handle |
|
1890 |
if handle_type == HANDLE_SEGMENT: |
|
1891 |
segment, dir = handle |
|
1892 |
for i in xrange(2): |
|
1893 |
self.Points.pop(segment) |
|
1894 |
self.Segments.pop(segment) |
|
1895 |
self.GeneratePoints() |
|
1896 |
self.RefreshModel() |
|
1897 |
||
1898 |
# Method called when a LeftDown event have been generated |
|
27 | 1899 |
def OnLeftDown(self, event, dc, scaling): |
1900 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1901 |
# Test if a point have been handled |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1902 |
#result = self.TestPoint(pos) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1903 |
#if result != None: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1904 |
# self.Handle = (HANDLE_POINT, result) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1905 |
# self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1906 |
#else: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1907 |
# Test if a segment have been handled |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1908 |
result = self.TestSegment(pos) |
0 | 1909 |
if result != None: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1910 |
if result[1] in (NORTH, SOUTH): |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1911 |
self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE)) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1912 |
elif result[1] in (EAST, WEST): |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1913 |
self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZENS)) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1914 |
self.Handle = (HANDLE_SEGMENT, result) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1915 |
# Execute the default method for a graphic element |
0 | 1916 |
else: |
27 | 1917 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
0 | 1918 |
self.oldPos = pos |
1919 |
||
80 | 1920 |
# Method called when a RightUp event has been generated |
27 | 1921 |
def OnRightUp(self, event, dc, scaling): |
1922 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1923 |
# Test if a segment has been handled |
237 | 1924 |
result = self.TestSegment(pos, True) |
0 | 1925 |
if result != None: |
1926 |
self.Handle = (HANDLE_SEGMENT, result) |
|
1927 |
# Popup the menu with special items for a wire |
|
237 | 1928 |
self.Parent.PopupWireMenu(0 < result < len(self.Segments) - 1) |
0 | 1929 |
else: |
1930 |
# Execute the default method for a graphic element |
|
27 | 1931 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
0 | 1932 |
|
80 | 1933 |
# Method called when a LeftDClick event has been generated |
27 | 1934 |
def OnLeftDClick(self, event, dc, scaling): |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1935 |
if event.ControlDown(): |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1936 |
direction = (self.StartPoint[1], self.EndPoint[1]) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1937 |
if direction in [(EAST, WEST), (WEST, EAST)]: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1938 |
avgy = (self.StartPoint[0].y + self.EndPoint[0].y) / 2 |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1939 |
if self.StartConnected is not None: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1940 |
startblock = self.StartConnected.GetParentBlock() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1941 |
startblock.Move(0, avgy - self.StartPoint[0].y) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1942 |
startblock.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1943 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1944 |
self.MoveStartPoint(wx.Point(self.StartPoint[0].x, avgy)) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1945 |
if self.EndConnected is not None: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1946 |
endblock = self.EndConnected.GetParentBlock() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1947 |
endblock.Move(0, avgy - self.EndPoint[0].y) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1948 |
endblock.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1949 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1950 |
self.MoveEndPoint(wx.Point(self.EndPoint[0].x, avgy)) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1951 |
elif direction in [(NORTH, SOUTH), (SOUTH, NORTH)]: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1952 |
avgx = (self.StartPoint[0].x + self.EndPoint[0].x) / 2 |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1953 |
if self.StartConnected is not None: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1954 |
startblock = self.StartConnected.GetParentBlock() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1955 |
startblock.Move(avgx - self.StartPoint[0].x, 0) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1956 |
startblock.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1957 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1958 |
self.MoveStartPoint(wx.Point(avgx, self.StartPoint[0].y)) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1959 |
if self.EndConnected is not None: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1960 |
endblock = self.EndConnected.GetParentBlock() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1961 |
endblock.Move(avgx - self.EndPoint[0].x, 0) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1962 |
endblock.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1963 |
else: |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1964 |
self.MoveEndPoint(wx.Point(avgx, self.EndPoint[0].y)) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1965 |
self.Parent.RefreshBuffer() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1966 |
else: |
145 | 1967 |
rect = self.GetRedrawRect() |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1968 |
self.ResetPoints() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1969 |
self.GeneratePoints() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1970 |
self.RefreshModel() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
1971 |
self.Parent.RefreshBuffer() |
155 | 1972 |
self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) |
0 | 1973 |
|
80 | 1974 |
# Method called when a Motion event has been generated |
27 | 1975 |
def OnMotion(self, event, dc, scaling): |
1976 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1977 |
if not event.Dragging(): |
1978 |
# Test if a segment has been handled |
|
1979 |
result = self.TestSegment(pos) |
|
1980 |
if result: |
|
1981 |
if result[1] in (NORTH, SOUTH): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1982 |
if self.CurrentCursor != 4: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1983 |
self.CurrentCursor = 4 |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1984 |
wx.CallAfter(self.Parent.SetCursor, CURSORS[4]) |
0 | 1985 |
elif result[1] in (EAST, WEST): |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1986 |
if self.CurrentCursor != 5: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1987 |
self.CurrentCursor = 5 |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
1988 |
wx.CallAfter(self.Parent.SetCursor, CURSORS[5]) |
144 | 1989 |
return 0, 0 |
0 | 1990 |
else: |
1991 |
# Test if a point has been handled |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1992 |
#result = self.TestPoint(pos) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1993 |
#if result != None: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1994 |
# if result == 0 and self.StartConnected: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1995 |
# self.OverStart = True |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1996 |
# elif result != 0 and self.EndConnected: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1997 |
# self.OverEnd = True |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1998 |
#else: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1999 |
# self.OverStart = False |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
2000 |
# self.OverEnd = False |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
2001 |
# Execute the default method for a graphic element |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
2002 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
0 | 2003 |
else: |
2004 |
# Execute the default method for a graphic element |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
80
diff
changeset
|
2005 |
return Graphic_Element.OnMotion(self, event, dc, scaling) |
0 | 2006 |
|
2007 |
# Refreshes the wire state according to move defined and handle selected |
|
165 | 2008 |
def ProcessDragging(self, movex, movey, centered, scaling): |
0 | 2009 |
handle_type, handle = self.Handle |
2010 |
# A point has been handled |
|
2011 |
if handle_type == HANDLE_POINT: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2012 |
movex = max(-self.Points[handle].x + POINT_RADIUS, movex) |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2013 |
movey = max(-self.Points[handle].y + POINT_RADIUS, movey) |
145 | 2014 |
if scaling is not None: |
2015 |
movex = round(float(self.Points[handle].x + movex) / float(scaling[0])) * scaling[0] - self.Points[handle].x |
|
2016 |
movey = round(float(self.Points[handle].y + movey) / float(scaling[1])) * scaling[1] - self.Points[handle].y |
|
0 | 2017 |
# Try to connect point to a connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2018 |
new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey) |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
237
diff
changeset
|
2019 |
connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection()) |
0 | 2020 |
if connector: |
98 | 2021 |
if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()): |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2022 |
connector.HighlightParentBlock(True) |
0 | 2023 |
connector.Connect((self, handle)) |
2024 |
self.SetStartPointDirection(connector.GetDirection()) |
|
2025 |
self.ConnectStartPoint(connector.GetPosition(), connector) |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2026 |
pos = connector.GetPosition() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2027 |
movex = pos.x - self.oldPos.x |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2028 |
movey = pos.y - self.oldPos.y |
0 | 2029 |
self.Dragging = False |
98 | 2030 |
elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()): |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2031 |
connector.HighlightParentBlock(True) |
0 | 2032 |
connector.Connect((self, handle)) |
2033 |
self.SetEndPointDirection(connector.GetDirection()) |
|
2034 |
self.ConnectEndPoint(connector.GetPosition(), connector) |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2035 |
pos = connector.GetPosition() |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2036 |
movex = pos.x - self.oldPos.x |
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2037 |
movey = pos.y - self.oldPos.y |
0 | 2038 |
self.Dragging = False |
2039 |
elif handle == 0: |
|
2040 |
self.MoveStartPoint(new_pos) |
|
2041 |
else: |
|
2042 |
self.MoveEndPoint(new_pos) |
|
2043 |
# If there is no connector, move the point |
|
2044 |
elif handle == 0: |
|
2045 |
if self.StartConnected: |
|
2046 |
self.UnConnectStartPoint() |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2047 |
self.StartConnected.HighlightParentBlock(False) |
0 | 2048 |
self.MoveStartPoint(new_pos) |
2049 |
else: |
|
2050 |
if self.EndConnected: |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2051 |
self.EndConnected.HighlightParentBlock(False) |
0 | 2052 |
self.UnConnectEndPoint() |
2053 |
self.MoveEndPoint(new_pos) |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
112
diff
changeset
|
2054 |
return movex, movey |
0 | 2055 |
# A segment has been handled, move a segment |
2056 |
elif handle_type == HANDLE_SEGMENT: |
|
145 | 2057 |
return self.MoveSegment(handle[0], movex, movey, scaling) |
0 | 2058 |
# Execute the default method for a graphic element |
2059 |
else: |
|
165 | 2060 |
return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling) |
0 | 2061 |
|
2062 |
# Refreshes the wire model |
|
2063 |
def RefreshModel(self, move=True): |
|
2064 |
if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]: |
|
2065 |
self.StartConnected.RefreshParentBlock() |
|
2066 |
if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]: |
|
2067 |
self.EndConnected.RefreshParentBlock() |
|
2068 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2069 |
# Draws the highlightment of this element if it is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2070 |
def DrawHighlightment(self, dc): |
144 | 2071 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) |
2072 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
2073 |
dc.SetLogicalFunction(wx.AND) |
|
2074 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
2075 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
2076 |
dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS + 2) |
|
2077 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
2078 |
dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS + 2) |
|
2079 |
for i in xrange(len(self.Points) - 1): |
|
2080 |
posx = min(self.Points[i].x, self.Points[i + 1].x) - 2 |
|
2081 |
posy = min(self.Points[i].y, self.Points[i + 1].y) - 2 |
|
2082 |
width = abs(self.Points[i + 1].x - self.Points[i].x) + 5 |
|
2083 |
height = abs(self.Points[i + 1].y - self.Points[i].y) + 5 |
|
2084 |
dc.DrawRectangle(posx, posy, width, height) |
|
145 | 2085 |
dc.SetLogicalFunction(wx.COPY) |
144 | 2086 |
if self.StartConnected is not None: |
2087 |
self.StartConnected.DrawHighlightment(dc) |
|
145 | 2088 |
self.StartConnected.Draw(dc) |
144 | 2089 |
if self.EndConnected is not None: |
2090 |
self.EndConnected.DrawHighlightment(dc) |
|
145 | 2091 |
self.EndConnected.Draw(dc) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2092 |
|
0 | 2093 |
# Draws the wire lines and points |
2094 |
def Draw(self, dc): |
|
144 | 2095 |
Graphic_Element.Draw(self, dc) |
222
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
2096 |
if self.Valid: |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
2097 |
dc.SetPen(wx.BLACK_PEN) |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
2098 |
else: |
8ce5c2635976
Adding support for underlying type incompatible wire loaded instead of removing them
lbessard
parents:
213
diff
changeset
|
2099 |
dc.SetPen(wx.RED_PEN) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2100 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 2101 |
# Draw the start and end points if they are not connected or the mouse is over them |
2102 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
2103 |
dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS) |
|
2104 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
2105 |
dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS) |
|
2106 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
2107 |
dc.DrawLines(self.Points) |
|
2108 |
dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) |
|
2109 |
# Draw the segment selected in red |
|
2110 |
if self.SelectedSegment != None: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2111 |
dc.SetPen(wx.RED_PEN) |
0 | 2112 |
dc.DrawLine(self.Points[self.SelectedSegment].x, self.Points[self.SelectedSegment].y, |
2113 |
self.Points[self.SelectedSegment + 1].x, self.Points[self.SelectedSegment + 1].y) |
|
2114 |
if self.SelectedSegment == len(self.Segments) - 1: |
|
2115 |
dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) |
|
144 | 2116 |
|
0 | 2117 |
|
2118 |
#------------------------------------------------------------------------------- |
|
2119 |
# Graphic comment element |
|
2120 |
#------------------------------------------------------------------------------- |
|
2121 |
||
2122 |
""" |
|
2123 |
Class that implements a comment |
|
2124 |
""" |
|
2125 |
||
2126 |
class Comment(Graphic_Element): |
|
2127 |
||
2128 |
# Create a new comment |
|
2129 |
def __init__(self, parent, content, id = None): |
|
2130 |
Graphic_Element.__init__(self, parent) |
|
2131 |
self.Id = id |
|
2132 |
self.Content = content |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2133 |
self.Pos = wx.Point(0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2134 |
self.Size = wx.Size(0, 0) |
0 | 2135 |
|
112 | 2136 |
# Make a clone of this comment |
162 | 2137 |
def Clone(self, parent, id = None, pos = None): |
2138 |
comment = Comment(parent, self.Content, id) |
|
145 | 2139 |
if pos is not None: |
2140 |
comment.SetPosition(pos.x, pos.y) |
|
112 | 2141 |
comment.SetSize(self.Size[0], self.Size[1]) |
2142 |
return comment |
|
2143 |
||
0 | 2144 |
# Method for keeping compatibility with others |
2145 |
def Clean(self): |
|
2146 |
pass |
|
2147 |
||
2148 |
# Delete this comment by calling the corresponding method |
|
2149 |
def Delete(self): |
|
2150 |
self.Parent.DeleteComment(self) |
|
2151 |
||
2152 |
# Refresh the comment bounding box |
|
2153 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2154 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
0 | 2155 |
|
2156 |
# Changes the comment size |
|
2157 |
def SetSize(self, width, height): |
|
2158 |
self.Size.SetWidth(width) |
|
2159 |
self.Size.SetHeight(height) |
|
2160 |
self.RefreshBoundingBox() |
|
2161 |
||
2162 |
# Returns the comment size |
|
2163 |
def GetSize(self): |
|
2164 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
2165 |
||
2166 |
# Returns the comment minimum size |
|
2167 |
def GetMinSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2168 |
dc = wx.ClientDC(self.Parent) |
0 | 2169 |
min_width = 0 |
2170 |
min_height = 0 |
|
2171 |
# The comment minimum size is the maximum size of words in the content |
|
2172 |
for line in self.Content.splitlines(): |
|
2173 |
for word in line.split(" "): |
|
2174 |
wordwidth, wordheight = dc.GetTextExtent(word) |
|
2175 |
min_width = max(min_width, wordwidth) |
|
2176 |
min_height = max(min_height, wordheight) |
|
2177 |
return min_width + 20, min_height + 20 |
|
2178 |
||
2179 |
# Changes the comment position |
|
2180 |
def SetPosition(self, x, y): |
|
2181 |
self.Pos.x = x |
|
2182 |
self.Pos.y = y |
|
2183 |
self.RefreshBoundingBox() |
|
2184 |
||
2185 |
# Changes the comment content |
|
2186 |
def SetContent(self, content): |
|
2187 |
self.Content = content |
|
2188 |
min_width, min_height = self.GetMinSize() |
|
2189 |
self.Size[0] = max(self.Size[0], min_width) |
|
2190 |
self.Size[1] = max(self.Size[1], min_height) |
|
2191 |
self.RefreshBoundingBox() |
|
2192 |
||
2193 |
# Returns the comment content |
|
2194 |
def GetContent(self): |
|
2195 |
return self.Content |
|
2196 |
||
2197 |
# Returns the comment position |
|
2198 |
def GetPosition(self): |
|
2199 |
return self.Pos.x, self.Pos.y |
|
2200 |
||
2201 |
# Moves the comment |
|
2202 |
def Move(self, dx, dy, connected = True): |
|
2203 |
self.Pos.x += dx |
|
2204 |
self.Pos.y += dy |
|
2205 |
self.RefreshBoundingBox() |
|
2206 |
||
2207 |
# Resizes the comment with the position and the size given |
|
2208 |
def Resize(self, x, y, width, height): |
|
2209 |
self.Move(x, y) |
|
2210 |
self.SetSize(width, height) |
|
2211 |
||
2212 |
# Method called when a RightUp event have been generated |
|
27 | 2213 |
def OnRightUp(self, event, dc, scaling): |
0 | 2214 |
# Popup the default menu |
2215 |
self.Parent.PopupDefaultMenu() |
|
2216 |
||
2217 |
# Refreshes the comment model |
|
2218 |
def RefreshModel(self, move=True): |
|
2219 |
self.Parent.RefreshCommentModel(self) |
|
2220 |
||
2221 |
# Method called when a LeftDClick event have been generated |
|
27 | 2222 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 2223 |
# Edit the comment content |
2224 |
self.Parent.EditCommentContent(self) |
|
2225 |
||
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2226 |
# Draws the highlightment of this element if it is highlighted |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2227 |
def DrawHighlightment(self, dc): |
144 | 2228 |
dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) |
2229 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
2230 |
dc.SetLogicalFunction(wx.AND) |
|
2231 |
polygon = [wx.Point(self.Pos.x - 2, self.Pos.y - 2), |
|
2232 |
wx.Point(self.Pos.x + self.Size[0] - 8, self.Pos.y - 2), |
|
2233 |
wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + 8), |
|
2234 |
wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + self.Size[1] + 2), |
|
2235 |
wx.Point(self.Pos.x - 2, self.Pos.y + self.Size[1] + 2)] |
|
2236 |
dc.DrawPolygon(polygon) |
|
2237 |
dc.SetLogicalFunction(wx.COPY) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2238 |
|
0 | 2239 |
# Draws the comment and its content |
2240 |
def Draw(self, dc): |
|
144 | 2241 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2242 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2243 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 2244 |
# Draws the comment shape |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2245 |
polygon = [wx.Point(self.Pos.x, self.Pos.y), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2246 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2247 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10), |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2248 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1]), |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
138
diff
changeset
|
2249 |
wx.Point(self.Pos.x, self.Pos.y + self.Size[1])] |
0 | 2250 |
dc.DrawPolygon(polygon) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2251 |
lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2252 |
wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
60
diff
changeset
|
2253 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)] |
0 | 2254 |
dc.DrawLines(lines) |
2255 |
# Draws the comment content |
|
2256 |
y = self.Pos.y + 10 |
|
2257 |
for line in self.Content.splitlines(): |
|
2258 |
first = True |
|
2259 |
words = line.split(" ") |
|
2260 |
for i, word in enumerate(words): |
|
2261 |
if first: |
|
2262 |
test = word |
|
2263 |
else: |
|
2264 |
test = linetext + " " + word |
|
2265 |
wordwidth, wordheight = dc.GetTextExtent(test) |
|
2266 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2267 |
break |
|
2268 |
if wordwidth < self.Size[0] - 20 and i < len(words) - 1: |
|
2269 |
linetext = test |
|
2270 |
first = False |
|
2271 |
else: |
|
2272 |
if wordwidth < self.Size[0] - 20 and i == len(words) - 1: |
|
2273 |
dc.DrawText(test, self.Pos.x + 10, y) |
|
2274 |
else: |
|
2275 |
dc.DrawText(linetext, self.Pos.x + 10, y) |
|
2276 |
if i == len(words) - 1: |
|
2277 |
y += wordheight + 5 |
|
2278 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2279 |
break |
|
2280 |
dc.DrawText(word, self.Pos.x + 10, y) |
|
2281 |
else: |
|
2282 |
linetext = word |
|
2283 |
y += wordheight + 5 |
|
2284 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
2285 |
break |