author | lbessard |
Fri, 20 Jul 2007 23:04:50 +0200 | |
changeset 51 | 47c72c77590a |
parent 42 | 4a8400732001 |
child 56 | 7187e1c00975 |
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 |
# |
|
7 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
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 |
|
19 |
#Lesser General Public License for more details. |
|
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 |
from wxPython.wx import * |
|
26 |
import wx |
|
27 |
from math import * |
|
28 |
||
29 |
||
30 |
#------------------------------------------------------------------------------- |
|
31 |
# Common constants |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
||
34 |
""" |
|
35 |
Definition of constants for dimensions of graphic elements |
|
36 |
""" |
|
37 |
||
38 |
# FBD and SFC constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
39 |
MIN_MOVE = 5 # Minimum move before starting a element dragging |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
40 |
CONNECTOR_SIZE = 8 # Size of connectors |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
41 |
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
|
42 |
HANDLE_SIZE = 6 # Size of the squares for handles |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
43 |
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
|
44 |
POINT_RADIUS = 2 # Radius of the point of wire ends |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
45 |
MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire |
0 | 46 |
|
47 |
# LD constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
LD_WIRE_SIZE = 30 # Size of a wire between two contact |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
51 |
LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
52 |
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
|
53 |
LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box |
0 | 54 |
|
55 |
# SFC constants |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
56 |
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
|
57 |
SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line |
0 | 63 |
|
64 |
# Type definition constants for graphic elements |
|
65 |
[INPUT, OUTPUT, INOUT] = range(3) |
|
66 |
[CONNECTOR, CONTINUATION] = range(2) |
|
67 |
[LEFTRAIL, RIGHTRAIL] = range(2) |
|
68 |
[CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) |
|
69 |
[COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET] = range(4) |
|
70 |
[SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) |
|
71 |
||
72 |
# Constants for defining the type of dragging that has been selected |
|
73 |
[HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) |
|
74 |
||
75 |
# List of value for resize handle that are valid |
|
76 |
VALID_HANDLES = [(1,1), (1,2), (1,3), (2,3), (3,3), (3,2), (3,1), (2,1)] |
|
77 |
||
78 |
# Contants for defining the direction of a connector |
|
79 |
[EAST, NORTH, WEST, SOUTH] = [(1,0), (0,-1), (-1,0), (0,1)] |
|
80 |
||
81 |
# Contants for defining which mode is selected for each view |
|
82 |
[MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, MODE_WIRE, |
|
27 | 83 |
MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, MODE_TRANSITION, |
84 |
MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION] = range(15) |
|
85 |
||
86 |
# Contants for defining which drawing mode is selected for app |
|
87 |
[FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] |
|
88 |
||
0 | 89 |
|
90 |
""" |
|
91 |
Basic vector operations for calculate wire points |
|
92 |
""" |
|
93 |
||
94 |
# Calculate the scalar product of two vectors |
|
95 |
def product(v1, v2): |
|
96 |
return v1[0] * v2[0] + v1[1] * v2[1] |
|
97 |
||
98 |
# Create a vector from two points and define if vector must be normal |
|
99 |
def vector(p1, p2, normal = True): |
|
100 |
vector = (p2.x - p1.x, p2.y - p1.y) |
|
101 |
if normal: |
|
102 |
return normalize(vector) |
|
103 |
return vector |
|
104 |
||
105 |
# Calculate the norm of a given vector |
|
106 |
def norm(v): |
|
107 |
return sqrt(v[0] * v[0] + v[1] * v[1]) |
|
108 |
||
109 |
# Normalize a given vector |
|
110 |
def normalize(v): |
|
111 |
v_norm = norm(v) |
|
112 |
# Verifie if it is not a null vector |
|
113 |
if v_norm > 0: |
|
114 |
return (v[0] / v_norm, v[1] / v_norm) |
|
115 |
else: |
|
116 |
return v |
|
117 |
||
118 |
||
119 |
""" |
|
120 |
Function that calculates the nearest point of the grid defined by scaling for the given point |
|
121 |
""" |
|
122 |
||
27 | 123 |
def GetScaledEventPosition(event, dc, scaling): |
124 |
pos = event.GetLogicalPosition(dc) |
|
0 | 125 |
if scaling: |
126 |
pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0] |
|
127 |
pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1] |
|
128 |
return pos |
|
129 |
||
130 |
||
131 |
""" |
|
132 |
Function that choose a direction during the wire points generation |
|
133 |
""" |
|
134 |
||
135 |
def DirectionChoice(v_base, v_target, dir_target): |
|
136 |
dir_product = product(v_base, v_target) |
|
137 |
if dir_product < 0: |
|
138 |
return (-v_base[0], -v_base[1]) |
|
139 |
elif dir_product == 0 and product(v_base, dir_target) != 0: |
|
140 |
return dir_target |
|
141 |
return v_base |
|
142 |
||
143 |
||
144 |
#------------------------------------------------------------------------------- |
|
145 |
# Viewer Rubberband |
|
146 |
#------------------------------------------------------------------------------- |
|
147 |
||
148 |
""" |
|
149 |
Class that implements a rubberband |
|
150 |
""" |
|
151 |
||
152 |
class RubberBand: |
|
153 |
||
154 |
# Create a rubberband by indicated on which window it must be drawn |
|
155 |
def __init__(self, drawingSurface): |
|
156 |
self.drawingSurface = drawingSurface |
|
157 |
self.Reset() |
|
158 |
||
159 |
# Method that initializes the internal attributes of the rubberband |
|
160 |
def Reset(self): |
|
161 |
self.startPoint = None |
|
162 |
self.currentBox = None |
|
163 |
self.lastBox = None |
|
164 |
||
165 |
# Method that return if a box is currently edited |
|
166 |
def IsShown(self): |
|
167 |
return self.currentBox != None |
|
168 |
||
169 |
# Method that returns the currently edited box |
|
170 |
def GetCurrentExtent(self): |
|
171 |
return self.currentBox |
|
172 |
||
173 |
# Method called when a new box starts to be edited |
|
27 | 174 |
def OnLeftDown(self, event, dc, scaling): |
175 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 176 |
# Save the point for calculate the box position and size |
177 |
self.startPoint = pos |
|
178 |
self.currentBox = wxRect(pos.x, pos.y, 0, 0) |
|
179 |
self.drawingSurface.SetCursor(wxStockCursor(wxCURSOR_CROSS)) |
|
180 |
self.Redraw() |
|
181 |
||
182 |
# Method called when dragging with a box edited |
|
27 | 183 |
def OnMotion(self, event, dc, scaling): |
184 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 185 |
# Save the last position and size of the box for erasing it |
186 |
self.lastBox = wxRect(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
187 |
self.currentBox.height) |
|
188 |
# Calculate new position and size of the box |
|
189 |
if pos.x >= self.startPoint.x: |
|
190 |
self.currentBox.x = self.startPoint.x |
|
191 |
self.currentBox.width = pos.x - self.startPoint.x + 1 |
|
192 |
else: |
|
193 |
self.currentBox.x = pos.x |
|
194 |
self.currentBox.width = self.startPoint.x - pos.x + 1 |
|
195 |
if pos.y >= self.startPoint.y: |
|
196 |
self.currentBox.y = self.startPoint.y |
|
197 |
self.currentBox.height = pos.y - self.startPoint.y + 1 |
|
198 |
else: |
|
199 |
self.currentBox.y = pos.y |
|
200 |
self.currentBox.height = self.startPoint.y - pos.y + 1 |
|
201 |
self.Redraw() |
|
202 |
||
203 |
# Method called when dragging is stopped |
|
27 | 204 |
def OnLeftUp(self, event, dc, scaling): |
0 | 205 |
self.drawingSurface.SetCursor(wxNullCursor) |
206 |
self.lastBox = self.currentBox |
|
207 |
self.currentBox = None |
|
208 |
self.Redraw() |
|
209 |
||
210 |
# Method that erase the last box and draw the new box |
|
211 |
def Redraw(self): |
|
27 | 212 |
dc = self.drawingSurface.GetLogicalDC() |
0 | 213 |
dc.SetPen(wxPen(wxWHITE, 1, wxDOT)) |
214 |
dc.SetBrush(wxTRANSPARENT_BRUSH) |
|
215 |
dc.SetLogicalFunction(wxXOR) |
|
216 |
if self.lastBox: |
|
217 |
# Erase last box |
|
218 |
dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, |
|
219 |
self.lastBox.height) |
|
220 |
if self.currentBox: |
|
221 |
# Draw current box |
|
222 |
dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
223 |
self.currentBox.height) |
|
224 |
||
27 | 225 |
# Erase last box |
226 |
def Erase(self): |
|
227 |
dc = self.drawingSurface.GetLogicalDC() |
|
228 |
dc.SetPen(wxPen(wxWHITE, 1, wxDOT)) |
|
229 |
dc.SetBrush(wxTRANSPARENT_BRUSH) |
|
230 |
dc.SetLogicalFunction(wxXOR) |
|
231 |
if self.lastBox: |
|
232 |
dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, |
|
233 |
self.lastBox.height) |
|
234 |
||
235 |
# Draw current box |
|
236 |
def Draw(self): |
|
237 |
dc = self.drawingSurface.GetLogicalDC() |
|
238 |
dc.SetPen(wxPen(wxWHITE, 1, wxDOT)) |
|
239 |
dc.SetBrush(wxTRANSPARENT_BRUSH) |
|
240 |
dc.SetLogicalFunction(wxXOR) |
|
241 |
if self.currentBox: |
|
242 |
# Draw current box |
|
243 |
dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, |
|
244 |
self.currentBox.height) |
|
245 |
||
0 | 246 |
|
247 |
#------------------------------------------------------------------------------- |
|
248 |
# Graphic element base class |
|
249 |
#------------------------------------------------------------------------------- |
|
250 |
||
251 |
""" |
|
252 |
Class that implements a generic graphic element |
|
253 |
""" |
|
254 |
||
255 |
class Graphic_Element: |
|
256 |
||
257 |
# Create a new graphic element |
|
258 |
def __init__(self, parent, id = None): |
|
259 |
self.Parent = parent |
|
260 |
self.Id = id |
|
261 |
self.oldPos = None |
|
262 |
self.Handle = False |
|
263 |
self.Dragging = False |
|
264 |
self.Selected = False |
|
265 |
self.Pos = wxPoint(0, 0) |
|
266 |
self.Size = wxSize(0, 0) |
|
267 |
self.BoundingBox = wxRect(0, 0, 0, 0) |
|
268 |
||
269 |
# Make a clone of this element |
|
270 |
def Clone(self): |
|
271 |
return Graphic_Element(self.Parent, self.Id) |
|
272 |
||
273 |
# Changes the block position |
|
274 |
def SetPosition(self, x, y): |
|
275 |
self.Pos.x = x |
|
276 |
self.Pos.y = y |
|
277 |
self.RefreshConnected() |
|
278 |
self.RefreshBoundingBox() |
|
279 |
||
280 |
# Returns the block position |
|
281 |
def GetPosition(self): |
|
282 |
return self.Pos.x, self.Pos.y |
|
283 |
||
284 |
# Changes the element size |
|
285 |
def SetSize(self, width, height): |
|
286 |
self.Size.SetWidth(width) |
|
287 |
self.Size.SetHeight(height) |
|
288 |
self.RefreshConnectors() |
|
289 |
self.RefreshBoundingBox() |
|
290 |
||
291 |
# Returns the element size |
|
292 |
def GetSize(self): |
|
293 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
294 |
||
295 |
# Refresh the element Bounding Box |
|
296 |
def RefreshBoundingBox(self): |
|
297 |
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
|
298 |
||
299 |
# Refresh the element connectors position |
|
300 |
def RefreshConnectors(self): |
|
301 |
pass |
|
302 |
||
303 |
# Refresh the position of wires connected to element inputs and outputs |
|
304 |
def RefreshConnected(self): |
|
305 |
pass |
|
306 |
||
307 |
# Change the parent |
|
308 |
def SetParent(self, parent): |
|
309 |
self.Parent = parent |
|
310 |
||
311 |
# Override this method for defining the method to call for deleting this element |
|
312 |
def Delete(self): |
|
313 |
pass |
|
314 |
||
315 |
# Returns the Id |
|
316 |
def GetId(self): |
|
317 |
return self.Id |
|
318 |
||
319 |
# Returns if the point given is in the bounding box |
|
320 |
def HitTest(self, pt): |
|
321 |
rect = self.BoundingBox |
|
322 |
return rect.InsideXY(pt.x, pt.y) |
|
323 |
||
42 | 324 |
# Returns if the point given is in the bounding box |
325 |
def IsInSelection(self, rect): |
|
326 |
return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) |
|
327 |
||
0 | 328 |
# Override this method for refreshing the bounding box |
329 |
def RefreshBoundingBox(self): |
|
330 |
pass |
|
331 |
||
332 |
# Returns the bounding box |
|
333 |
def GetBoundingBox(self): |
|
334 |
return self.BoundingBox |
|
335 |
||
336 |
# Change the variable that indicates if this element is selected |
|
337 |
def SetSelected(self, selected): |
|
338 |
self.Selected = selected |
|
339 |
||
340 |
# Test if the point is on a handle of this element |
|
341 |
def TestHandle(self, pt): |
|
342 |
# Verify that this element is selected |
|
343 |
if self.Selected: |
|
344 |
# Find if point is on a handle horizontally |
|
345 |
if self.BoundingBox.x - HANDLE_SIZE - 2 <= pt.x < self.BoundingBox.x - 2: |
|
346 |
handle_x = 1 |
|
347 |
elif self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2 <= pt.x < self.BoundingBox.x + (self.BoundingBox.width + HANDLE_SIZE) / 2: |
|
348 |
handle_x = 2 |
|
349 |
elif self.BoundingBox.x + self.BoundingBox.width + 2 <= pt.x < self.BoundingBox.x + self.BoundingBox.width + HANDLE_SIZE + 2: |
|
350 |
handle_x = 3 |
|
351 |
else: |
|
352 |
handle_x = 0 |
|
353 |
# Find if point is on a handle vertically |
|
354 |
if self.BoundingBox.y - HANDLE_SIZE - 2 <= pt.y < self.BoundingBox.y - 2: |
|
355 |
handle_y = 1 |
|
356 |
elif self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2 <= pt.y < self.BoundingBox.y + (self.BoundingBox.height + HANDLE_SIZE) / 2: |
|
357 |
handle_y = 2 |
|
358 |
elif self.BoundingBox.y + self.BoundingBox.height - 2 <= pt.y < self.BoundingBox.y + self.BoundingBox.height + HANDLE_SIZE + 2: |
|
359 |
handle_y = 3 |
|
360 |
else: |
|
361 |
handle_y = 0 |
|
362 |
# Verify that the result is valid |
|
363 |
if (handle_x, handle_y) in VALID_HANDLES: |
|
364 |
return handle_x, handle_y |
|
365 |
return 0, 0 |
|
366 |
||
367 |
# Method called when a LeftDown event have been generated |
|
27 | 368 |
def OnLeftDown(self, event, dc, scaling): |
369 |
pos = event.GetLogicalPosition(dc) |
|
0 | 370 |
# Test if an handle have been clicked |
371 |
result = self.TestHandle(pos) |
|
372 |
# Find which type of handle have been clicked, |
|
373 |
# Save a resize event and change the cursor |
|
374 |
if result == (1, 1) or result == (3, 3): |
|
375 |
self.Handle = (HANDLE_RESIZE, result) |
|
376 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZENWSE)) |
|
377 |
elif result == (1, 3) or result == (3, 1): |
|
378 |
self.Handle = (HANDLE_RESIZE, result) |
|
379 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZENESW)) |
|
380 |
elif result == (1, 2) or result == (3, 2): |
|
381 |
self.Handle = (HANDLE_RESIZE, result) |
|
382 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZEWE)) |
|
383 |
elif result == (2, 1) or result == (2, 3): |
|
384 |
self.Handle = (HANDLE_RESIZE, result) |
|
385 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZENS)) |
|
386 |
# If no handle have been clicked, save a move event, and change the cursor |
|
387 |
else: |
|
388 |
self.Handle = (HANDLE_MOVE, None) |
|
389 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND)) |
|
390 |
self.SetSelected(False) |
|
391 |
# Initializes the last position |
|
27 | 392 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
0 | 393 |
|
394 |
# Method called when a LeftUp event have been generated |
|
27 | 395 |
def OnLeftUp(self, event, dc, scaling): |
0 | 396 |
# If a dragging have been initiated |
397 |
if self.Dragging and self.oldPos: |
|
398 |
# Calculate the movement of cursor and refreshes the element state |
|
27 | 399 |
pos = GetScaledEventPosition(event, dc, scaling) |
0 | 400 |
movex = pos.x - self.oldPos.x |
401 |
movey = pos.y - self.oldPos.y |
|
402 |
self.ProcessDragging(movex, movey) |
|
403 |
self.RefreshModel() |
|
404 |
self.SetSelected(True) |
|
405 |
self.oldPos = None |
|
406 |
||
407 |
# Method called when a RightUp event have been generated |
|
27 | 408 |
def OnRightUp(self, event, dc, scaling): |
0 | 409 |
self.SetSelected(True) |
410 |
self.oldPos = None |
|
411 |
||
412 |
# Method called when a LeftDClick event have been generated |
|
27 | 413 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 414 |
pass |
415 |
||
416 |
# Method called when a Motion event have been generated |
|
27 | 417 |
def OnMotion(self, event, dc, scaling): |
0 | 418 |
# If the cursor is dragging and the element have been clicked |
419 |
if event.Dragging() and self.oldPos: |
|
420 |
# Calculate the movement of cursor |
|
27 | 421 |
pos = GetScaledEventPosition(event, dc, scaling) |
0 | 422 |
movex = pos.x - self.oldPos.x |
423 |
movey = pos.y - self.oldPos.y |
|
424 |
# If movement is greater than MIN_MOVE then a dragging is initiated |
|
425 |
if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): |
|
426 |
self.Dragging = True |
|
427 |
# If a dragging have been initiated, refreshes the element state |
|
428 |
if self.Dragging: |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
429 |
self.oldPos = pos |
0 | 430 |
self.ProcessDragging(movex, movey) |
431 |
# If cursor just pass over the element, changes the cursor if it is on a handle |
|
432 |
else: |
|
27 | 433 |
pos = event.GetLogicalPosition(dc) |
0 | 434 |
handle = self.TestHandle(pos) |
435 |
if handle == (1, 1) or handle == (3, 3): |
|
436 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZENWSE)) |
|
437 |
elif handle == (1, 3) or handle == (3, 1): |
|
438 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZENESW)) |
|
439 |
elif handle == (1, 2) or handle == (3, 2): |
|
440 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZEWE)) |
|
441 |
elif handle == (2, 1) or handle == (2, 3): |
|
442 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZENS)) |
|
443 |
else: |
|
444 |
wxCallAfter(self.Parent.SetCursor, wxNullCursor) |
|
445 |
||
446 |
# Moves the element |
|
447 |
def Move(self, dx, dy, exclude = []): |
|
448 |
self.Pos.x += dx |
|
449 |
self.Pos.y += dy |
|
450 |
self.RefreshConnected(exclude) |
|
451 |
self.RefreshBoundingBox() |
|
452 |
||
453 |
# Resizes the element from position and size given |
|
454 |
def Resize(self, x, y, width, height): |
|
455 |
self.Move(x, y) |
|
456 |
self.SetSize(width, height) |
|
457 |
||
458 |
# Refreshes the element state according to move defined and handle selected |
|
459 |
def ProcessDragging(self, movex, movey): |
|
460 |
handle_type, handle = self.Handle |
|
461 |
# If it is a resize handle, calculate the values from resizing |
|
462 |
if handle_type == HANDLE_RESIZE: |
|
463 |
x, y = 0, 0 |
|
464 |
width, height = self.GetSize() |
|
465 |
if handle[0] == 1: |
|
466 |
x = movex |
|
467 |
width -= movex |
|
468 |
elif handle[0] == 3: |
|
469 |
width += movex |
|
470 |
if handle[1] == 1: |
|
471 |
y = movey |
|
472 |
height -= movey |
|
473 |
elif handle[1] == 3: |
|
474 |
height += movey |
|
475 |
# Verify that new size is not lesser than minimum |
|
476 |
min_width, min_height = self.GetMinSize() |
|
477 |
if width >= min_width and height >= min_height: |
|
478 |
self.Resize(x, y, width, height) |
|
479 |
# If it is a move handle, Move this element |
|
480 |
elif handle_type == HANDLE_MOVE: |
|
481 |
self.Move(movex, movey) |
|
482 |
||
483 |
# Override this method for defining the method to call for refreshing the model of this element |
|
484 |
def RefreshModel(self, move=True): |
|
485 |
pass |
|
486 |
||
487 |
# Draws the handles of this element if it is selected |
|
488 |
def Draw(self, dc): |
|
489 |
if self.Selected: |
|
490 |
dc.SetPen(wxBLACK_PEN) |
|
491 |
dc.SetBrush(wxBLACK_BRUSH) |
|
492 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
|
493 |
dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, |
|
494 |
self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
|
495 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
496 |
self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) |
|
497 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
498 |
self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) |
|
499 |
dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, |
|
500 |
self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
501 |
dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, |
|
502 |
self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
503 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) |
|
504 |
dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) |
|
505 |
dc.SetBrush(wxWHITE_BRUSH) |
|
506 |
||
507 |
||
508 |
#------------------------------------------------------------------------------- |
|
509 |
# Group of graphic elements |
|
510 |
#------------------------------------------------------------------------------- |
|
511 |
||
512 |
""" |
|
513 |
Class that implements a group of graphic elements |
|
514 |
""" |
|
515 |
||
516 |
class Graphic_Group(Graphic_Element): |
|
517 |
||
518 |
# Create a new group of graphic elements |
|
519 |
def __init__(self, parent): |
|
520 |
Graphic_Element.__init__(self, parent) |
|
521 |
self.Elements = [] |
|
42 | 522 |
self.RefreshWireExclusion() |
0 | 523 |
self.RefreshBoundingBox() |
524 |
||
525 |
# Destructor |
|
526 |
def __del__(self): |
|
527 |
self.Elements = [] |
|
528 |
||
42 | 529 |
# Refresh the list of wire excluded |
530 |
def RefreshWireExclusion(self): |
|
531 |
self.WireExcluded = [] |
|
532 |
for element in self.Elements: |
|
533 |
if isinstance(element, Wire): |
|
534 |
startblock = element.StartConnected.GetParentBlock() |
|
535 |
endblock = element.EndConnected.GetParentBlock() |
|
536 |
if startblock in self.Elements and endblock in self.Elements: |
|
537 |
self.WireExcluded.append(element) |
|
538 |
||
0 | 539 |
# Make a clone of this group |
540 |
def Clone(self): |
|
541 |
clone = Graphic_Group(self.Parent) |
|
542 |
elements = [] |
|
543 |
# Makes a clone of all the elements in this group |
|
544 |
for element in self.Elements: |
|
545 |
elements.append(element.Clone()) |
|
546 |
clone.SetElements(elements) |
|
547 |
return clone |
|
548 |
||
549 |
# Clean this group of elements |
|
550 |
def Clean(self): |
|
551 |
# Clean all the elements of the group |
|
552 |
for element in self.Elements: |
|
553 |
element.Clean() |
|
554 |
||
555 |
# Delete this group of elements |
|
556 |
def Delete(self): |
|
557 |
# Delete all the elements of the group |
|
558 |
for element in self.Elements: |
|
559 |
element.Delete() |
|
42 | 560 |
self.WireExcluded = [] |
0 | 561 |
|
562 |
# Returns if the point given is in the bounding box of one of the elements of this group |
|
563 |
def HitTest(self, pt): |
|
564 |
result = False |
|
565 |
for element in self.Elements: |
|
566 |
result |= element.HitTest(pt) |
|
567 |
return result |
|
568 |
||
569 |
# Returns if the element given is in this group |
|
570 |
def IsElementIn(self, element): |
|
571 |
return element in self.Elements |
|
572 |
||
573 |
# Change the elements of the group |
|
574 |
def SetElements(self, elements): |
|
575 |
self.Elements = elements |
|
42 | 576 |
self.RefreshWireExclusion() |
0 | 577 |
self.RefreshBoundingBox() |
578 |
||
579 |
# Returns the elements of the group |
|
580 |
def GetElements(self): |
|
581 |
return self.Elements |
|
582 |
||
583 |
# Remove or select the given element if it is or not in the group |
|
584 |
def SelectElement(self, element): |
|
585 |
if element in self.Elements: |
|
586 |
self.Elements.remove(element) |
|
587 |
else: |
|
588 |
self.Elements.append(element) |
|
42 | 589 |
self.RefreshWireExclusion() |
0 | 590 |
self.RefreshBoundingBox() |
591 |
||
592 |
# Move this group of elements |
|
593 |
def Move(self, movex, movey): |
|
594 |
# Move all the elements of the group |
|
595 |
for element in self.Elements: |
|
42 | 596 |
if not isinstance(element, Wire): |
597 |
element.Move(movex, movey, self.WireExcluded) |
|
598 |
elif element in self.WireExcluded: |
|
0 | 599 |
element.Move(movex, movey, True) |
600 |
self.RefreshBoundingBox() |
|
601 |
||
602 |
# Refreshes the bounding box of this group of elements |
|
603 |
def RefreshBoundingBox(self): |
|
604 |
if len(self.Elements) > 0: |
|
605 |
bbox = self.Elements[0].GetBoundingBox() |
|
606 |
minx, miny = bbox.x, bbox.y |
|
607 |
maxx = bbox.x + bbox.width |
|
608 |
maxy = bbox.y + bbox.height |
|
609 |
for element in self.Elements[1:]: |
|
610 |
bbox = element.GetBoundingBox() |
|
611 |
minx = min(minx, bbox.x) |
|
612 |
miny = min(miny, bbox.y) |
|
613 |
maxx = max(maxx, bbox.x + bbox.width) |
|
614 |
maxy = max(maxy, bbox.y + bbox.height) |
|
615 |
self.BoundingBox = wxRect(minx, miny, maxx - minx, maxy - miny) |
|
616 |
else: |
|
617 |
self.BoundingBox = wxRect(0, 0, 0, 0) |
|
618 |
||
619 |
# Forbids to change the group position |
|
620 |
def SetPosition(x, y): |
|
621 |
pass |
|
622 |
||
623 |
# Returns the position of this group |
|
624 |
def GetPosition(self): |
|
625 |
return self.BoundingBox.x, self.BoundingBox.y |
|
626 |
||
627 |
# Forbids to change the group size |
|
628 |
def SetSize(width, height): |
|
629 |
pass |
|
630 |
||
631 |
# Returns the size of this group |
|
632 |
def GetSize(self): |
|
633 |
return self.BoundingBox.width, self.BoundingBox.height |
|
634 |
||
635 |
# Change the variable that indicates if the elemente is selected |
|
636 |
def SetSelected(self, selected): |
|
637 |
for element in self.Elements: |
|
638 |
element.SetSelected(selected) |
|
639 |
||
640 |
# Refreshes the model of all the elements of this group |
|
641 |
def RefreshModel(self): |
|
642 |
for element in self.Elements: |
|
643 |
element.RefreshModel() |
|
644 |
||
645 |
||
646 |
#------------------------------------------------------------------------------- |
|
647 |
# Connector for all types of blocks |
|
648 |
#------------------------------------------------------------------------------- |
|
649 |
||
650 |
""" |
|
651 |
Class that implements a connector for any type of block |
|
652 |
""" |
|
653 |
||
654 |
class Connector: |
|
655 |
||
656 |
# Create a new connector |
|
27 | 657 |
def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False): |
0 | 658 |
self.ParentBlock = parent |
659 |
self.Name = name |
|
660 |
self.Type = type |
|
661 |
self.Pos = position |
|
662 |
self.Direction = direction |
|
663 |
self.Wires = [] |
|
664 |
self.Negated = negated |
|
665 |
self.Edge = edge |
|
27 | 666 |
self.OneConnected = onlyone |
0 | 667 |
self.Pen = wxBLACK_PEN |
42 | 668 |
self.RefreshNameSize() |
0 | 669 |
|
670 |
# Change the connector pen |
|
671 |
def SetPen(self, pen): |
|
672 |
self.Pen = pen |
|
673 |
||
674 |
# Make a clone of the connector |
|
675 |
def Clone(self): |
|
42 | 676 |
return Connector(self.ParentBlock, self.Name, self.Type, wxPoint(self.Pos[0], self.Pos[1]), |
0 | 677 |
self.Direction, self.Negated) |
678 |
||
679 |
# Returns the connector parent block |
|
680 |
def GetParentBlock(self): |
|
681 |
return self.ParentBlock |
|
682 |
||
683 |
# Returns the connector name |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
684 |
def GetType(self): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
685 |
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
|
686 |
|
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
687 |
# 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
|
688 |
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
|
689 |
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
|
690 |
|
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
691 |
# Returns the connector name |
0 | 692 |
def GetName(self): |
693 |
return self.Name |
|
694 |
||
695 |
# Changes the connector name |
|
696 |
def SetName(self, name): |
|
697 |
self.Name = name |
|
42 | 698 |
self.RefreshNameSize() |
699 |
||
700 |
# Changes the connector name size |
|
701 |
def RefreshNameSize(self): |
|
702 |
if self.Name != "": |
|
703 |
dc = wxClientDC(self.ParentBlock.Parent) |
|
704 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
705 |
else: |
|
706 |
self.NameSize = 0, 0 |
|
707 |
||
708 |
# Returns the connector name size |
|
709 |
def GetNameSize(self): |
|
710 |
return self.NameSize |
|
0 | 711 |
|
712 |
# Returns the wires connected to the connector |
|
713 |
def GetWires(self): |
|
714 |
return self.Wires |
|
715 |
||
716 |
# Returns the parent block Id |
|
717 |
def GetBlockId(self): |
|
718 |
return self.ParentBlock.GetId() |
|
719 |
||
720 |
# Returns the connector relative position |
|
721 |
def GetRelPosition(self): |
|
722 |
return self.Pos |
|
723 |
||
724 |
# Returns the connector absolute position |
|
725 |
def GetPosition(self, size = True): |
|
726 |
parent_pos = self.ParentBlock.GetPosition() |
|
727 |
# If the position of the end of the connector is asked |
|
728 |
if size: |
|
729 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
730 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
731 |
else: |
|
732 |
x = parent_pos[0] + self.Pos.x |
|
733 |
y = parent_pos[1] + self.Pos.y |
|
734 |
return wxPoint(x, y) |
|
735 |
||
736 |
# Change the connector relative position |
|
737 |
def SetPosition(self, pos): |
|
738 |
self.Pos = pos |
|
739 |
||
740 |
# Returns the connector direction |
|
741 |
def GetDirection(self): |
|
742 |
return self.Direction |
|
743 |
||
744 |
# Change the connector direction |
|
745 |
def SetDirection(self, direction): |
|
746 |
self.Direction = direction |
|
747 |
||
748 |
# Connect a wire to this connector at the last place |
|
749 |
def Connect(self, wire, refresh = True): |
|
750 |
self.InsertConnect(len(self.Wires), wire, refresh) |
|
751 |
||
752 |
# Connect a wire to this connector at the place given |
|
753 |
def InsertConnect(self, idx, wire, refresh = True): |
|
754 |
if wire not in self.Wires: |
|
755 |
self.Wires.insert(idx, wire) |
|
756 |
if refresh: |
|
757 |
self.ParentBlock.RefreshModel(False) |
|
758 |
||
759 |
# Returns the index of the wire given in the list of connected |
|
760 |
def GetWireIndex(self, wire): |
|
761 |
for i, (tmp_wire, handle) in enumerate(self.Wires): |
|
762 |
if tmp_wire == wire: |
|
763 |
return i |
|
764 |
return None |
|
765 |
||
766 |
# Unconnect a wire or all wires connected to the connector |
|
2 | 767 |
def UnConnect(self, wire = None, unconnect = True, delete = False): |
0 | 768 |
i = 0 |
769 |
found = False |
|
770 |
while i < len(self.Wires) and not found: |
|
771 |
if not wire or self.Wires[i][0] == wire: |
|
772 |
# If Unconnect haven't been called from a wire, disconnect the connector in the wire |
|
773 |
if unconnect: |
|
774 |
if self.Wires[i][1] == 0: |
|
2 | 775 |
self.Wires[i][0].UnConnectStartPoint(delete) |
0 | 776 |
else: |
2 | 777 |
self.Wires[i][0].UnConnectEndPoint(delete) |
0 | 778 |
# Remove wire from connected |
779 |
if wire: |
|
780 |
self.Wires.pop(i) |
|
781 |
found = True |
|
782 |
i += 1 |
|
783 |
# If no wire defined, unconnect all wires |
|
784 |
if not wire: |
|
785 |
self.Wires = [] |
|
786 |
self.ParentBlock.RefreshModel(False) |
|
787 |
||
788 |
# Returns if connector has one or more wire connected |
|
789 |
def IsConnected(self): |
|
790 |
return len(self.Wires) > 0 |
|
791 |
||
792 |
# Move the wires connected |
|
793 |
def MoveConnected(self, exclude = []): |
|
794 |
if len(self.Wires) > 0: |
|
795 |
# Calculate the new position of the end point |
|
796 |
parent_pos = self.ParentBlock.GetPosition() |
|
797 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE |
|
798 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE |
|
799 |
# Move the corresponding point on all the wires connected |
|
800 |
for wire, index in self.Wires: |
|
801 |
if wire not in exclude: |
|
802 |
if index == 0: |
|
803 |
wire.MoveStartPoint(wxPoint(x, y)) |
|
804 |
else: |
|
805 |
wire.MoveEndPoint(wxPoint(x, y)) |
|
806 |
||
807 |
# Refreshes the model of all the wires connected |
|
808 |
def RefreshWires(self): |
|
809 |
for wire in self.Wires: |
|
810 |
wire[0].RefreshModel() |
|
811 |
||
812 |
# Refreshes the parent block model |
|
813 |
def RefreshParentBlock(self): |
|
814 |
self.ParentBlock.RefreshModel(False) |
|
815 |
||
27 | 816 |
# Returns all the blocks connected to this connector |
817 |
def GetConnectedBlocks(self): |
|
818 |
blocks = [] |
|
819 |
for wire, handle in self.Wires: |
|
820 |
# Get other connector connected to each wire |
|
821 |
if handle == 0: |
|
822 |
connector = blocks.GetEndConnected() |
|
823 |
else: |
|
824 |
connector = blocks.GetStartConnected() |
|
825 |
# Get parent block for this connector |
|
826 |
if connector: |
|
827 |
block = connector.GetParentBlock() |
|
828 |
if block not in blocks: |
|
829 |
blocks.append(block) |
|
830 |
return blocks |
|
831 |
||
0 | 832 |
# Returns the connector negated property |
833 |
def IsNegated(self): |
|
834 |
return self.Negated |
|
835 |
||
836 |
# Changes the connector negated property |
|
837 |
def SetNegated(self, negated): |
|
838 |
self.Negated = negated |
|
839 |
self.Edge = "none" |
|
840 |
||
841 |
# Returns the connector edge property |
|
842 |
def GetEdge(self): |
|
843 |
return self.Edge |
|
844 |
||
845 |
# Changes the connector edge property |
|
846 |
def SetEdge(self, edge): |
|
847 |
self.Edge = edge |
|
848 |
self.Negated = False |
|
849 |
||
850 |
# Tests if the point given is near from the end point of this connector |
|
851 |
def TestPoint(self, pt, exclude = True): |
|
852 |
parent_pos = self.ParentBlock.GetPosition() |
|
27 | 853 |
if not (len(self.Wires) > 0 and self.OneConnected and exclude): |
0 | 854 |
# Calculate a square around the end point of this connector |
855 |
x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
|
856 |
y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE - ANCHOR_DISTANCE |
|
857 |
width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE |
|
858 |
height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE |
|
859 |
rect = wxRect(x, y, width, height) |
|
860 |
return rect.InsideXY(pt.x, pt.y) |
|
861 |
return False |
|
862 |
||
863 |
# Draws the connector |
|
864 |
def Draw(self, dc): |
|
865 |
dc.SetPen(self.Pen) |
|
866 |
dc.SetBrush(wxWHITE_BRUSH) |
|
867 |
parent_pos = self.ParentBlock.GetPosition() |
|
868 |
if self.Negated: |
|
869 |
# If connector is negated, draw a circle |
|
870 |
xcenter = parent_pos[0] + self.Pos.x + (CONNECTOR_SIZE * self.Direction[0]) / 2 |
|
871 |
ycenter = parent_pos[1] + self.Pos.y + (CONNECTOR_SIZE * self.Direction[1]) / 2 |
|
872 |
dc.DrawCircle(xcenter, ycenter, CONNECTOR_SIZE / 2) |
|
873 |
else: |
|
874 |
xstart = parent_pos[0] + self.Pos.x |
|
875 |
ystart = parent_pos[1] + self.Pos.y |
|
876 |
if self.Edge == "rising": |
|
877 |
# If connector has a rising edge, draw a right arrow |
|
878 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart - 4) |
|
879 |
dc.DrawLine(xstart, ystart, xstart - 4, ystart + 4) |
|
880 |
elif self.Edge == "falling": |
|
881 |
# If connector has a falling edge, draw a left arrow |
|
882 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4) |
|
883 |
dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4) |
|
884 |
xend = xstart + CONNECTOR_SIZE * self.Direction[0] |
|
885 |
yend = ystart + CONNECTOR_SIZE * self.Direction[1] |
|
886 |
dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend) |
|
887 |
if self.Direction[0] != 0: |
|
42 | 888 |
ytext = parent_pos[1] + self.Pos.y - self.NameSize[1] / 2 |
0 | 889 |
if self.Direction[0] < 0: |
890 |
xtext = parent_pos[0] + self.Pos.x + 5 |
|
891 |
else: |
|
42 | 892 |
xtext = parent_pos[0] + self.Pos.x - (self.NameSize[0] + 5) |
0 | 893 |
if self.Direction[1] != 0: |
42 | 894 |
xtext = parent_pos[0] + self.Pos.x - self.NameSize[0] / 2 |
0 | 895 |
if self.Direction[1] < 0: |
896 |
ytext = parent_pos[1] + self.Pos.y + 5 |
|
897 |
else: |
|
42 | 898 |
ytext = parent_pos[1] + self.Pos.y - (self.NameSize[1] + 5) |
0 | 899 |
# Draw the text |
900 |
dc.DrawText(self.Name, xtext, ytext) |
|
901 |
||
902 |
||
903 |
#------------------------------------------------------------------------------- |
|
904 |
# Common Wire Element |
|
905 |
#------------------------------------------------------------------------------- |
|
906 |
||
907 |
""" |
|
908 |
Class that implements a wire for connecting two blocks |
|
909 |
""" |
|
910 |
||
911 |
class Wire(Graphic_Element): |
|
912 |
||
913 |
# Create a new wire |
|
914 |
def __init__(self, parent, start = None, end = None): |
|
915 |
Graphic_Element.__init__(self, parent) |
|
916 |
self.StartPoint = start |
|
917 |
self.EndPoint = end |
|
918 |
self.StartConnected = None |
|
919 |
self.EndConnected = None |
|
920 |
# If the start and end points are defined, calculate the wire |
|
921 |
if start and end: |
|
922 |
self.ResetPoints() |
|
923 |
self.GeneratePoints() |
|
924 |
else: |
|
925 |
self.Points = [] |
|
926 |
self.Segments = [] |
|
927 |
self.SelectedSegment = None |
|
928 |
self.OverStart = False |
|
929 |
self.OverEnd = False |
|
930 |
||
931 |
# Destructor of a wire |
|
932 |
def __del__(self): |
|
933 |
self.StartConnected = None |
|
934 |
self.EndConnected = None |
|
935 |
||
936 |
# Forbids to change the wire position |
|
937 |
def SetPosition(x, y): |
|
938 |
pass |
|
939 |
||
940 |
# Forbids to change the wire size |
|
941 |
def SetSize(width, height): |
|
942 |
pass |
|
943 |
||
27 | 944 |
# Returns connector to which start point is connected |
945 |
def GetStartConnected(self): |
|
946 |
return self.StartConnected |
|
947 |
||
948 |
# Returns connector to which end point is connected |
|
949 |
def GetEndConnected(self): |
|
950 |
return self.EndConnected |
|
951 |
||
0 | 952 |
# Unconnect the start and end points |
953 |
def Clean(self): |
|
954 |
if self.StartConnected: |
|
955 |
self.UnConnectStartPoint() |
|
956 |
if self.EndConnected: |
|
957 |
self.UnConnectEndPoint() |
|
958 |
||
959 |
# Delete this wire by calling the corresponding method |
|
960 |
def Delete(self): |
|
961 |
self.Parent.DeleteWire(self) |
|
962 |
||
963 |
# Select a segment and not the whole wire. It's useful for Ladder Diagram |
|
964 |
def SetSelectedSegment(self, segment): |
|
965 |
# The last segment is indicated |
|
966 |
if segment == -1: |
|
967 |
segment = len(self.Segments) - 1 |
|
968 |
# The selected segment is reinitialised |
|
969 |
if segment == None: |
|
970 |
if self.StartConnected: |
|
971 |
self.StartConnected.SetPen(wxBLACK_PEN) |
|
972 |
if self.EndConnected: |
|
973 |
self.EndConnected.SetPen(wxBLACK_PEN) |
|
974 |
# The segment selected is the first |
|
975 |
elif segment == 0: |
|
976 |
if self.StartConnected: |
|
977 |
self.StartConnected.SetPen(wxRED_PEN) |
|
978 |
if self.EndConnected: |
|
979 |
# There is only one segment |
|
980 |
if len(self.Segments) == 1: |
|
981 |
self.EndConnected.SetPen(wxRED_PEN) |
|
982 |
else: |
|
983 |
self.EndConnected.SetPen(wxBLACK_PEN) |
|
984 |
# The segment selected is the last |
|
985 |
elif segment == len(self.Segments) - 1: |
|
986 |
if self.StartConnected: |
|
987 |
self.StartConnected.SetPen(wxBLACK_PEN) |
|
988 |
if self.EndConnected: |
|
989 |
self.EndConnected.SetPen(wxRED_PEN) |
|
990 |
self.SelectedSegment = segment |
|
991 |
||
992 |
# Reinitialize the wire points |
|
993 |
def ResetPoints(self): |
|
994 |
if self.StartPoint and self.EndPoint: |
|
995 |
self.Points = [self.StartPoint[0], self.EndPoint[0]] |
|
996 |
self.Segments = [self.StartPoint[1]] |
|
997 |
else: |
|
998 |
self.Points = [] |
|
999 |
self.Segments = [] |
|
1000 |
||
1001 |
# Refresh the wire bounding box |
|
1002 |
def RefreshBoundingBox(self): |
|
1003 |
if len(self.Points) > 0: |
|
1004 |
# If startpoint or endpoint is connected, save the point radius |
|
1005 |
start_radius = end_radius = 0 |
|
1006 |
if not self.StartConnected: |
|
1007 |
start_radius = POINT_RADIUS |
|
1008 |
if not self.EndConnected: |
|
1009 |
end_radius = POINT_RADIUS |
|
1010 |
# Initialize minimum and maximum from the first point |
|
1011 |
minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius |
|
1012 |
maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius |
|
1013 |
miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius |
|
1014 |
maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius |
|
1015 |
# Actualize minimum and maximum with the other points |
|
1016 |
for point in self.Points[1:-1]: |
|
1017 |
minx, minbbxx = min(minx, point.x), min(minbbxx, point.x) |
|
1018 |
maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x) |
|
1019 |
miny, minbbxy = min(miny, point.y), min(minbbxy, point.y) |
|
1020 |
maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y) |
|
1021 |
if len(self.Points) > 1: |
|
1022 |
minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius) |
|
1023 |
maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius) |
|
1024 |
miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius) |
|
1025 |
maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius) |
|
1026 |
self.Pos = wxPoint(minx, miny) |
|
1027 |
self.Size = wxSize(maxx -minx + 1, maxy - miny + 1) |
|
1028 |
self.BoundingBox = wxRect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1) |
|
1029 |
||
1030 |
# Refresh the realpoints that permits to keep the proportionality in wire during resizing |
|
1031 |
def RefreshRealPoints(self): |
|
1032 |
if len(self.Points) > 0: |
|
1033 |
self.RealPoints = [] |
|
1034 |
# Calculate float relative position of each point with the minimum point |
|
1035 |
for point in self.Points: |
|
1036 |
self.RealPoints.append([float(point.x - self.Pos.x), float(point.y - self.Pos.y)]) |
|
1037 |
||
1038 |
# Returns the wire minimum size |
|
1039 |
def GetMinSize(self): |
|
1040 |
width = 1 |
|
1041 |
height = 1 |
|
1042 |
dir_product = product(self.StartPoint[1], self.EndPoint[1]) |
|
1043 |
# The directions are opposed |
|
1044 |
if dir_product < 0: |
|
1045 |
if self.StartPoint[0] != 0: |
|
1046 |
width = MIN_SEGMENT_SIZE * 2 |
|
1047 |
if self.StartPoint[1] != 0: |
|
1048 |
height = MIN_SEGMENT_SIZE * 2 |
|
1049 |
# The directions are the same |
|
1050 |
elif dir_product > 0: |
|
1051 |
if self.StartPoint[0] != 0: |
|
1052 |
width = MIN_SEGMENT_SIZE |
|
1053 |
if self.StartPoint[1] != 0: |
|
1054 |
height = MIN_SEGMENT_SIZE |
|
1055 |
# The directions are perpendiculars |
|
1056 |
else: |
|
1057 |
width = MIN_SEGMENT_SIZE |
|
1058 |
height = MIN_SEGMENT_SIZE |
|
1059 |
return width + 1, height + 1 |
|
1060 |
||
1061 |
# Returns if the point given is on one of the wire segments |
|
1062 |
def HitTest(self, pt): |
|
1063 |
test = False |
|
1064 |
for i in xrange(len(self.Points) - 1): |
|
1065 |
rect = wxRect(0, 0, 0, 0) |
|
1066 |
x1, y1 = self.Points[i].x, self.Points[i].y |
|
1067 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1068 |
# Calculate a rectangle around the segment |
|
1069 |
rect = wxRect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
1070 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
|
1071 |
test |= rect.InsideXY(pt.x, pt.y) |
|
1072 |
return test |
|
1073 |
||
1074 |
# Returns the wire start or end point if the point given is on one of them |
|
1075 |
def TestPoint(self, pt): |
|
1076 |
# Test the wire start point |
|
1077 |
rect = wxRect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE, |
|
1078 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
|
1079 |
if rect.InsideXY(pt.x, pt.y): |
|
1080 |
return 0 |
|
1081 |
# Test the wire end point |
|
1082 |
if len(self.Points) > 1: |
|
1083 |
rect = wxRect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE, |
|
1084 |
2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) |
|
1085 |
if rect.InsideXY(pt.x, pt.y): |
|
1086 |
return -1 |
|
1087 |
return None |
|
1088 |
||
1089 |
# Returns the wire segment if the point given is on it |
|
1090 |
def TestSegment(self, pt, all=False): |
|
1091 |
for i in xrange(len(self.Segments)): |
|
1092 |
# If wire is not in a Ladder Diagram, first and last segments are excluded |
|
1093 |
if 0 < i < len(self.Segments) - 1 or all: |
|
1094 |
x1, y1 = self.Points[i].x, self.Points[i].y |
|
1095 |
x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y |
|
1096 |
# Calculate a rectangle around the segment |
|
1097 |
rect = wxRect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, |
|
1098 |
abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) |
|
1099 |
if rect.InsideXY(pt.x, pt.y): |
|
1100 |
return i, self.Segments[i] |
|
1101 |
return None |
|
1102 |
||
1103 |
# Define the wire points |
|
1104 |
def SetPoints(self, points): |
|
1105 |
if len(points) > 1: |
|
1106 |
self.Points = [wxPoint(x, y) for x, y in points] |
|
1107 |
# Calculate the start and end directions |
|
1108 |
self.StartPoint = [None, vector(self.Points[0], self.Points[1])] |
|
1109 |
self.EndPoint = [None, vector(self.Points[-1], self.Points[-2])] |
|
1110 |
# Calculate the start and end points |
|
1111 |
self.StartPoint[0] = wxPoint(self.Points[0].x + CONNECTOR_SIZE * self.StartPoint[1][0], |
|
1112 |
self.Points[0].y + CONNECTOR_SIZE * self.StartPoint[1][1]) |
|
1113 |
self.EndPoint[0] = wxPoint(self.Points[-1].x + CONNECTOR_SIZE * self.EndPoint[1][0], |
|
1114 |
self.Points[-1].y + CONNECTOR_SIZE * self.EndPoint[1][1]) |
|
1115 |
self.Points[0] = self.StartPoint[0] |
|
1116 |
self.Points[-1] = self.EndPoint[0] |
|
1117 |
# Calculate the segments directions |
|
1118 |
self.Segments = [] |
|
1119 |
for i in xrange(len(self.Points) - 1): |
|
1120 |
self.Segments.append(vector(self.Points[i], self.Points[i + 1])) |
|
1121 |
self.RefreshBoundingBox() |
|
1122 |
self.RefreshRealPoints() |
|
1123 |
||
1124 |
# Returns the position of the point indicated |
|
1125 |
def GetPoint(self, index): |
|
1126 |
if index < len(self.Points): |
|
1127 |
return self.Points[index].x, self.Points[index].y |
|
1128 |
return None |
|
1129 |
||
1130 |
# Returns a list of the position of all wire points |
|
1131 |
def GetPoints(self, invert = False): |
|
1132 |
points = self.VerifyPoints() |
|
1133 |
points[0] = wxPoint(points[0].x - CONNECTOR_SIZE * self.StartPoint[1][0], |
|
1134 |
points[0].y - CONNECTOR_SIZE * self.StartPoint[1][1]) |
|
1135 |
points[-1] = wxPoint(points[-1].x - CONNECTOR_SIZE * self.EndPoint[1][0], |
|
1136 |
points[-1].y - CONNECTOR_SIZE * self.EndPoint[1][1]) |
|
1137 |
# An inversion of the list is asked |
|
1138 |
if invert: |
|
1139 |
points.reverse() |
|
1140 |
return points |
|
1141 |
||
1142 |
# Returns the position of the two selected segment points |
|
1143 |
def GetSelectedSegmentPoints(self): |
|
1144 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
1145 |
return self.Points[self.SelectedSegment:self.SelectedSegment + 2] |
|
1146 |
return [] |
|
1147 |
||
1148 |
# Returns if the selected segment is the first and/or the last of the wire |
|
1149 |
def GetSelectedSegmentConnections(self): |
|
1150 |
if self.SelectedSegment != None and len(self.Points) > 1: |
|
1151 |
return self.SelectedSegment == 0, self.SelectedSegment == len(self.Segments) - 1 |
|
1152 |
return (True, True) |
|
1153 |
||
1154 |
# Returns the connectors on which the wire is connected |
|
1155 |
def GetConnected(self): |
|
1156 |
connected = [] |
|
1157 |
if self.StartConnected and self.StartPoint[1] == WEST: |
|
1158 |
connected.append(self.StartConnected) |
|
1159 |
if self.EndConnected and self.EndPoint[1] == WEST: |
|
1160 |
connected.append(self.EndConnected) |
|
1161 |
return connected |
|
1162 |
||
1163 |
# Returns the id of the block connected to the first or the last wire point |
|
27 | 1164 |
def GetConnectedInfos(self, index): |
0 | 1165 |
if index == 0 and self.StartConnected: |
27 | 1166 |
return self.StartConnected.GetBlockId(), self.StartConnected.GetName() |
0 | 1167 |
elif index == -1 and self.EndConnected: |
42 | 1168 |
return self.EndConnected.GetBlockId(), self.EndConnected.GetName() |
0 | 1169 |
return None |
1170 |
||
1171 |
# Update the wire points position by keeping at most possible the current positions |
|
1172 |
def GeneratePoints(self, realpoints = True): |
|
1173 |
i = 0 |
|
1174 |
# Calculate the start enad end points with the minimum segment size in the right direction |
|
1175 |
end = wxPoint(self.EndPoint[0].x + self.EndPoint[1][0] * MIN_SEGMENT_SIZE, |
|
1176 |
self.EndPoint[0].y + self.EndPoint[1][1] * MIN_SEGMENT_SIZE) |
|
1177 |
start = wxPoint(self.StartPoint[0].x + self.StartPoint[1][0] * MIN_SEGMENT_SIZE, |
|
1178 |
self.StartPoint[0].y + self.StartPoint[1][1] * MIN_SEGMENT_SIZE) |
|
1179 |
# Evaluate the point till it's the last |
|
1180 |
while i < len(self.Points) - 1: |
|
1181 |
# The next point is the last |
|
1182 |
if i + 1 == len(self.Points) - 1: |
|
1183 |
# Calculate the direction from current point to end point |
|
1184 |
v_end = vector(self.Points[i], end) |
|
1185 |
# The current point is the first |
|
1186 |
if i == 0: |
|
1187 |
# If the end point is not in the start direction, a point is added |
|
1188 |
if v_end != self.Segments[0] or v_end == self.EndPoint[1]: |
|
1189 |
self.Points.insert(1, wxPoint(start.x, start.y)) |
|
1190 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
|
1191 |
self.Segments[0][0]), v_end, self.EndPoint[1])) |
|
1192 |
# The current point is the second |
|
1193 |
elif i == 1: |
|
1194 |
# The previous direction and the target direction are mainly opposed, a point is added |
|
1195 |
if product(v_end, self.Segments[0]) < 0: |
|
1196 |
self.Points.insert(2, wxPoint(self.Points[1].x, self.Points[1].y)) |
|
1197 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
1198 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1199 |
# The previous direction and the end direction are the same or they are |
|
1200 |
# perpendiculars and the end direction points towards current segment |
|
1201 |
elif product(self.Segments[0], self.EndPoint[1]) >= 0 and product(self.Segments[1], self.EndPoint[1]) <= 0: |
|
1202 |
# Current point and end point are aligned |
|
1203 |
if self.Segments[0][0] != 0: |
|
1204 |
self.Points[1].x = end.x |
|
1205 |
if self.Segments[0][1] != 0: |
|
1206 |
self.Points[1].y = end.y |
|
1207 |
# If the previous direction and the end direction are the same, a point is added |
|
1208 |
if product(self.Segments[0], self.EndPoint[1]) > 0: |
|
1209 |
self.Points.insert(2, wxPoint(self.Points[1].x, self.Points[1].y)) |
|
1210 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
1211 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1212 |
else: |
|
1213 |
# Current point is positioned in the middle of start point |
|
1214 |
# and end point on the current direction and a point is added |
|
1215 |
if self.Segments[0][0] != 0: |
|
1216 |
self.Points[1].x = (end.x + start.x) / 2 |
|
1217 |
if self.Segments[0][1] != 0: |
|
1218 |
self.Points[1].y = (end.y + start.y) / 2 |
|
1219 |
self.Points.insert(2, wxPoint(self.Points[1].x, self.Points[1].y)) |
|
1220 |
self.Segments.insert(2, DirectionChoice((self.Segments[1][1], |
|
1221 |
self.Segments[1][0]), v_end, self.EndPoint[1])) |
|
1222 |
else: |
|
1223 |
# The previous direction and the end direction are perpendiculars |
|
1224 |
if product(self.Segments[i - 1], self.EndPoint[1]) == 0: |
|
1225 |
# The target direction and the end direction aren't mainly the same |
|
1226 |
if product(v_end, self.EndPoint[1]) <= 0: |
|
1227 |
# Current point and end point are aligned |
|
1228 |
if self.Segments[i - 1][0] != 0: |
|
1229 |
self.Points[i].x = end.x |
|
1230 |
if self.Segments[i - 1][1] != 0: |
|
1231 |
self.Points[i].y = end.y |
|
1232 |
# Previous direction is updated from the new point |
|
1233 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
1234 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
1235 |
else: |
|
1236 |
test = True |
|
1237 |
# If the current point is the third, test if the second |
|
1238 |
# point can be aligned with the end point |
|
1239 |
if i == 2: |
|
1240 |
test_point = wxPoint(self.Points[1].x, self.Points[1].y) |
|
1241 |
if self.Segments[1][0] != 0: |
|
1242 |
test_point.y = end.y |
|
1243 |
if self.Segments[1][1] != 0: |
|
1244 |
test_point.x = end.x |
|
1245 |
test = norm(vector(self.Points[0], test_point, False)) > MIN_SEGMENT_SIZE |
|
1246 |
# The previous point can be aligned |
|
1247 |
if test: |
|
1248 |
self.Points[i].x, self.Points[i].y = end.x, end.y |
|
1249 |
if self.Segments[i - 1][0] != 0: |
|
1250 |
self.Points[i - 1].y = end.y |
|
1251 |
if self.Segments[i - 1][1] != 0: |
|
1252 |
self.Points[i - 1].x = end.x |
|
1253 |
self.Segments[i] = (-self.EndPoint[1][0], -self.EndPoint[1][1]) |
|
1254 |
else: |
|
1255 |
# Current point is positioned in the middle of previous point |
|
1256 |
# and end point on the current direction and a point is added |
|
1257 |
if self.Segments[1][0] != 0: |
|
1258 |
self.Points[2].x = (self.Points[1].x + end.x) / 2 |
|
1259 |
if self.Segments[1][1] != 0: |
|
1260 |
self.Points[2].y = (self.Points[1].y + end.y) / 2 |
|
1261 |
self.Points.insert(3, wxPoint(self.Points[2].x, self.Points[2].y)) |
|
1262 |
self.Segments.insert(3, DirectionChoice((self.Segments[2][1], |
|
1263 |
self.Segments[2][0]), v_end, self.EndPoint[1])) |
|
1264 |
else: |
|
1265 |
# Current point is aligned with end point |
|
1266 |
if self.Segments[i - 1][0] != 0: |
|
1267 |
self.Points[i].x = end.x |
|
1268 |
if self.Segments[i - 1][1] != 0: |
|
1269 |
self.Points[i].y = end.y |
|
1270 |
# Previous direction is updated from the new point |
|
1271 |
if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: |
|
1272 |
self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) |
|
1273 |
# If previous direction and end direction are opposed |
|
1274 |
if product(self.Segments[i - 1], self.EndPoint[1]) < 0: |
|
1275 |
# Current point is positioned in the middle of previous point |
|
1276 |
# and end point on the current direction |
|
1277 |
if self.Segments[i - 1][0] != 0: |
|
1278 |
self.Points[i].x = (end.x + self.Points[i - 1].x) / 2 |
|
1279 |
if self.Segments[i - 1][1] != 0: |
|
1280 |
self.Points[i].y = (end.y + self.Points[i - 1].y) / 2 |
|
1281 |
# A point is added |
|
1282 |
self.Points.insert(i + 1, wxPoint(self.Points[i].x, self.Points[i].y)) |
|
1283 |
self.Segments.insert(i + 1, DirectionChoice((self.Segments[i][1], |
|
1284 |
self.Segments[i][0]), v_end, self.EndPoint[1])) |
|
1285 |
else: |
|
1286 |
# Current point is the first, and second is not mainly in the first direction |
|
1287 |
if i == 0 and product(vector(start, self.Points[1]), self.Segments[0]) < 0: |
|
1288 |
# If first and second directions aren't perpendiculars, a point is added |
|
1289 |
if product(self.Segments[0], self.Segments[1]) != 0: |
|
1290 |
self.Points.insert(1, wxPoint(start.x, start.y)) |
|
1291 |
self.Segments.insert(1, DirectionChoice((self.Segments[0][1], |
|
1292 |
self.Segments[0][0]), vector(start, self.Points[1]), self.Segments[1])) |
|
1293 |
else: |
|
1294 |
self.Points[1].x, self.Points[1].y = start.x, start.y |
|
1295 |
else: |
|
1296 |
# Next point is aligned with current point |
|
1297 |
if self.Segments[i][0] != 0: |
|
1298 |
self.Points[i + 1].y = self.Points[i].y |
|
1299 |
if self.Segments[i][1] != 0: |
|
1300 |
self.Points[i + 1].x = self.Points[i].x |
|
1301 |
# Current direction is updated from the new point |
|
1302 |
if product(vector(self.Points[i], self.Points[i + 1]), self.Segments[i]) < 0: |
|
1303 |
self.Segments[i] = (-self.Segments[i][0], -self.Segments[i][1]) |
|
1304 |
i += 1 |
|
1305 |
self.RefreshBoundingBox() |
|
1306 |
if realpoints: |
|
1307 |
self.RefreshRealPoints() |
|
1308 |
||
1309 |
# Verify that two consecutive points haven't the same position |
|
1310 |
def VerifyPoints(self): |
|
1311 |
points = [point for point in self.Points] |
|
1312 |
segments = [segment for segment in self.Segments] |
|
1313 |
i = 1 |
|
1314 |
while i < len(points) - 1: |
|
1315 |
if points[i] == points[i + 1] and segments[i - 1] == segments[i + 1]: |
|
1316 |
for j in xrange(2): |
|
1317 |
points.pop(i) |
|
1318 |
segments.pop(i) |
|
1319 |
else: |
|
1320 |
i += 1 |
|
1321 |
# If the wire isn't in a Ladder Diagram, save the new point list |
|
1322 |
if self.Parent.__class__.__name__ != "LD_Viewer": |
|
1323 |
self.Points = [point for point in points] |
|
1324 |
self.Segments = [segment for segment in segments] |
|
1325 |
self.RefreshBoundingBox() |
|
1326 |
self.RefreshRealPoints() |
|
1327 |
return points |
|
1328 |
||
1329 |
# Moves all the wire points except the first and the last if they are connected |
|
1330 |
def Move(self, dx, dy, endpoints = False): |
|
1331 |
for i, point in enumerate(self.Points): |
|
1332 |
if endpoints or not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1333 |
point.x += dx |
|
1334 |
point.y += dy |
|
1335 |
self.StartPoint[0] = self.Points[0] |
|
1336 |
self.EndPoint[0] = self.Points[-1] |
|
1337 |
self.GeneratePoints() |
|
1338 |
||
1339 |
# Resize the wire from position and size given |
|
1340 |
def Resize(self, x, y, width, height): |
|
1341 |
if len(self.Points) > 1: |
|
1342 |
# Calculate the new position of each point for testing the new size |
|
1343 |
minx, miny = self.Pos.x, self.Pos.y |
|
1344 |
lastwidth, lastheight = self.Size.width, self.Size.height |
|
1345 |
for i, point in enumerate(self.RealPoints): |
|
1346 |
# If start or end point is connected, it's not calculate |
|
1347 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1348 |
if i == 0: |
|
1349 |
dir = self.StartPoint[1] |
|
1350 |
elif i == len(self.Points) - 1: |
|
1351 |
dir = self.EndPoint[1] |
|
1352 |
else: |
|
1353 |
dir = (0, 0) |
|
1354 |
pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * (width - 1) / float(lastwidth - 1))), |
|
1355 |
width - dir[0] * MIN_SEGMENT_SIZE - 1)) |
|
1356 |
pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * (height - 1) / float(lastheight - 1))), |
|
1357 |
height - dir[1] * MIN_SEGMENT_SIZE - 1)) |
|
1358 |
self.Points[i] = wxPoint(minx + x + pointx, miny + y + pointy) |
|
1359 |
self.StartPoint[0] = self.Points[0] |
|
1360 |
self.EndPoint[0] = self.Points[-1] |
|
1361 |
self.GeneratePoints(False) |
|
1362 |
# Test if the wire position or size have changed |
|
1363 |
if x != 0 and minx == self.Pos.x: |
|
1364 |
x = 0 |
|
1365 |
width = lastwidth |
|
1366 |
if y != 0 and miny == self.Pos.y: |
|
1367 |
y = 0 |
|
1368 |
height = lastwidth |
|
1369 |
if width != lastwidth and lastwidth == self.Size.width: |
|
1370 |
width = lastwidth |
|
1371 |
if height != lastheight and lastheight == self.Size.height: |
|
1372 |
height = lastheight |
|
1373 |
# Calculate the real points from the new size, it's important for |
|
1374 |
# keeping a proportionality in the points position with the size |
|
1375 |
# duringa resize dragging |
|
1376 |
for i, point in enumerate(self.RealPoints): |
|
1377 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1378 |
point[0] = point[0] * (width - 1) / float(lastwidth - 1) |
|
1379 |
point[1] = point[1] * (height - 1) / float(lastheight - 1) |
|
1380 |
# Calculate the correct position of the points from real points |
|
1381 |
for i, point in enumerate(self.RealPoints): |
|
1382 |
if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): |
|
1383 |
if i == 0: |
|
1384 |
dir = self.StartPoint[1] |
|
1385 |
elif i == len(self.Points) - 1: |
|
1386 |
dir = self.EndPoint[1] |
|
1387 |
else: |
|
1388 |
dir = (0, 0) |
|
1389 |
realpointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0])), |
|
1390 |
width - dir[0] * MIN_SEGMENT_SIZE - 1)) |
|
1391 |
realpointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1])), |
|
1392 |
height - dir[1] * MIN_SEGMENT_SIZE - 1)) |
|
1393 |
self.Points[i] = wxPoint(minx + x + realpointx, miny + y + realpointy) |
|
1394 |
self.StartPoint[0] = self.Points[0] |
|
1395 |
self.EndPoint[0] = self.Points[-1] |
|
1396 |
self.GeneratePoints(False) |
|
1397 |
||
1398 |
# Moves the wire start point and update the wire points |
|
1399 |
def MoveStartPoint(self, point): |
|
1400 |
if len(self.Points) > 1: |
|
1401 |
self.StartPoint[0] = point |
|
1402 |
self.Points[0] = point |
|
1403 |
self.GeneratePoints() |
|
1404 |
||
1405 |
# Changes the wire start direction and update the wire points |
|
1406 |
def SetStartPointDirection(self, dir): |
|
1407 |
if len(self.Points) > 1: |
|
1408 |
self.StartPoint[1] = dir |
|
1409 |
self.Segments[0] = dir |
|
1410 |
self.GeneratePoints() |
|
1411 |
||
1412 |
# Rotates the wire start direction by an angle of 90 degrees anticlockwise |
|
1413 |
def RotateStartPoint(self): |
|
1414 |
self.SetStartPointDirection((self.StartPoint[1][1], -self.StartPoint[1][0])) |
|
1415 |
||
1416 |
# Connects wire start point to the connector given and moves wire start point |
|
1417 |
# to given point |
|
1418 |
def ConnectStartPoint(self, point, connector): |
|
1419 |
if point: |
|
1420 |
self.MoveStartPoint(point) |
|
1421 |
self.StartConnected = connector |
|
1422 |
||
1423 |
# Unconnects wire start point |
|
2 | 1424 |
def UnConnectStartPoint(self, delete = False): |
0 | 1425 |
self.StartConnected.UnConnect(self, False) |
2 | 1426 |
if delete: |
1427 |
self.Delete() |
|
1428 |
else: |
|
1429 |
self.StartConnected = None |
|
0 | 1430 |
|
1431 |
# Moves the wire end point and update the wire points |
|
1432 |
def MoveEndPoint(self, point): |
|
1433 |
if len(self.Points) > 1: |
|
1434 |
self.EndPoint[0] = point |
|
1435 |
self.Points[-1] = point |
|
1436 |
self.GeneratePoints() |
|
1437 |
||
1438 |
# Changes the wire end direction and update the wire points |
|
1439 |
def SetEndPointDirection(self, dir): |
|
1440 |
if len(self.Points) > 1: |
|
1441 |
self.EndPoint[1] = dir |
|
1442 |
self.GeneratePoints() |
|
1443 |
||
1444 |
# Rotates the wire end direction by an angle of 90 degrees anticlockwise |
|
1445 |
def RotateEndPoint(self): |
|
1446 |
self.SetEndPointDirection((self.EndPoint[1][1], -self.EndPoint[1][0])) |
|
1447 |
||
1448 |
# Connects wire end point to the connector given and moves wire end point |
|
1449 |
# to given point |
|
1450 |
def ConnectEndPoint(self, point, connector): |
|
1451 |
if point: |
|
1452 |
self.MoveEndPoint(point) |
|
1453 |
self.EndConnected = connector |
|
1454 |
||
1455 |
# Unconnects wire end point |
|
2 | 1456 |
def UnConnectEndPoint(self, delete = False): |
0 | 1457 |
self.EndConnected.UnConnect(self, False) |
2 | 1458 |
if delete: |
1459 |
self.Delete() |
|
1460 |
else: |
|
1461 |
self.EndConnected = None |
|
0 | 1462 |
|
1463 |
# Moves the wire segment given by its index |
|
1464 |
def MoveSegment(self, idx, movex, movey): |
|
1465 |
if 0 < idx < len(self.Segments) - 1: |
|
1466 |
if self.Segments[idx] in (NORTH, SOUTH): |
|
1467 |
self.Points[idx].x += movex |
|
1468 |
self.Points[idx + 1].x += movex |
|
1469 |
elif self.Segments[idx] in (EAST, WEST): |
|
1470 |
self.Points[idx].y += movey |
|
1471 |
self.Points[idx + 1].y += movey |
|
1472 |
self.GeneratePoints() |
|
1473 |
||
1474 |
# Adds two points in the middle of the handled segment |
|
1475 |
def AddSegment(self): |
|
1476 |
handle_type, handle = self.Handle |
|
1477 |
if handle_type == HANDLE_SEGMENT: |
|
1478 |
segment, dir = handle |
|
1479 |
pointx = self.Points[segment].x |
|
1480 |
pointy = self.Points[segment].y |
|
1481 |
if dir[0] != 0: |
|
1482 |
pointx = (self.Points[segment].x + self.Points[segment + 1].x) / 2 |
|
1483 |
if dir[1] != 0: |
|
1484 |
pointy = (self.Points[segment].y + self.Points[segment + 1].y) / 2 |
|
1485 |
self.Points.insert(segment + 1, wxPoint(pointx, pointy)) |
|
1486 |
self.Segments.insert(segment + 1, (dir[1], dir[0])) |
|
1487 |
self.Points.insert(segment + 2, wxPoint(pointx, pointy)) |
|
1488 |
self.Segments.insert(segment + 2, dir) |
|
1489 |
self.GeneratePoints() |
|
1490 |
||
1491 |
# Delete the handled segment by removing the two segment points |
|
1492 |
def DeleteSegment(self): |
|
1493 |
handle_type, handle = self.Handle |
|
1494 |
if handle_type == HANDLE_SEGMENT: |
|
1495 |
segment, dir = handle |
|
1496 |
for i in xrange(2): |
|
1497 |
self.Points.pop(segment) |
|
1498 |
self.Segments.pop(segment) |
|
1499 |
self.GeneratePoints() |
|
1500 |
self.RefreshModel() |
|
1501 |
||
1502 |
# Method called when a LeftDown event have been generated |
|
27 | 1503 |
def OnLeftDown(self, event, dc, scaling): |
1504 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1505 |
# 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
|
1506 |
#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
|
1507 |
#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
|
1508 |
# self.Handle = (HANDLE_POINT, result) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1509 |
# self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND)) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1510 |
#else: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1511 |
# 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
|
1512 |
result = self.TestSegment(pos) |
0 | 1513 |
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
|
1514 |
if result[1] in (NORTH, SOUTH): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1515 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZEWE)) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1516 |
elif result[1] in (EAST, WEST): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1517 |
self.Parent.SetCursor(wxStockCursor(wxCURSOR_SIZENS)) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1518 |
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
|
1519 |
# Execute the default method for a graphic element |
0 | 1520 |
else: |
27 | 1521 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
0 | 1522 |
self.oldPos = pos |
1523 |
||
1524 |
# Method called when a RightUp event have been generated |
|
27 | 1525 |
def OnRightUp(self, event, dc, scaling): |
1526 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1527 |
# Test if a segment has been handled |
1528 |
result = self.TestSegment(pos) |
|
1529 |
if result != None: |
|
1530 |
self.Handle = (HANDLE_SEGMENT, result) |
|
1531 |
# Popup the menu with special items for a wire |
|
1532 |
self.Parent.PopupWireMenu() |
|
1533 |
else: |
|
1534 |
# Execute the default method for a graphic element |
|
27 | 1535 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
0 | 1536 |
|
1537 |
# Method called when a LeftDClick event have been generated |
|
27 | 1538 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 1539 |
self.ResetPoints() |
1540 |
self.GeneratePoints() |
|
1541 |
||
1542 |
# Method called when a Motion event have been generated |
|
27 | 1543 |
def OnMotion(self, event, dc, scaling): |
1544 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1545 |
if not event.Dragging(): |
1546 |
# Test if a segment has been handled |
|
1547 |
result = self.TestSegment(pos) |
|
1548 |
if result: |
|
1549 |
if result[1] in (NORTH, SOUTH): |
|
1550 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZEWE)) |
|
1551 |
elif result[1] in (EAST, WEST): |
|
1552 |
wxCallAfter(self.Parent.SetCursor, wxStockCursor(wxCURSOR_SIZENS)) |
|
1553 |
else: |
|
1554 |
# 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
|
1555 |
#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
|
1556 |
#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
|
1557 |
# 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
|
1558 |
# 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
|
1559 |
# 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
|
1560 |
# 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
|
1561 |
#else: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1562 |
# 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
|
1563 |
# 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
|
1564 |
# Execute the default method for a graphic element |
27 | 1565 |
Graphic_Element.OnMotion(self, event, dc, scaling) |
0 | 1566 |
else: |
1567 |
# Execute the default method for a graphic element |
|
27 | 1568 |
Graphic_Element.OnMotion(self, event, dc, scaling) |
0 | 1569 |
|
1570 |
# Refreshes the wire state according to move defined and handle selected |
|
1571 |
def ProcessDragging(self, movex, movey): |
|
1572 |
handle_type, handle = self.Handle |
|
1573 |
# A point has been handled |
|
1574 |
if handle_type == HANDLE_POINT: |
|
1575 |
# Try to connect point to a connector |
|
1576 |
new_pos = wxPoint(self.Points[handle].x + movex, self.Points[handle].y + movey) |
|
1577 |
connector = self.Parent.FindBlockConnector(new_pos) |
|
1578 |
if connector: |
|
1579 |
if handle == 0 and self.EndConnected != connector: |
|
1580 |
connector.Connect((self, handle)) |
|
1581 |
self.SetStartPointDirection(connector.GetDirection()) |
|
1582 |
self.ConnectStartPoint(connector.GetPosition(), connector) |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1583 |
self.oldPos = connector.GetPosition() |
0 | 1584 |
self.Dragging = False |
1585 |
elif handle != 0 and self.StartConnected != connector: |
|
1586 |
connector.Connect((self, handle)) |
|
1587 |
self.SetEndPointDirection(connector.GetDirection()) |
|
1588 |
self.ConnectEndPoint(connector.GetPosition(), connector) |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
1589 |
self.oldPos = connector.GetPosition() |
0 | 1590 |
self.Dragging = False |
1591 |
elif handle == 0: |
|
1592 |
self.MoveStartPoint(new_pos) |
|
1593 |
else: |
|
1594 |
self.MoveEndPoint(new_pos) |
|
1595 |
# If there is no connector, move the point |
|
1596 |
elif handle == 0: |
|
1597 |
if self.StartConnected: |
|
1598 |
self.UnConnectStartPoint() |
|
1599 |
self.MoveStartPoint(new_pos) |
|
1600 |
else: |
|
1601 |
if self.EndConnected: |
|
1602 |
self.UnConnectEndPoint() |
|
1603 |
self.MoveEndPoint(new_pos) |
|
1604 |
self.RefreshModel() |
|
1605 |
# A segment has been handled, move a segment |
|
1606 |
elif handle_type == HANDLE_SEGMENT: |
|
1607 |
self.MoveSegment(handle[0], movex, movey) |
|
1608 |
# Execute the default method for a graphic element |
|
1609 |
else: |
|
1610 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
1611 |
||
1612 |
# Refreshes the wire model |
|
1613 |
def RefreshModel(self, move=True): |
|
1614 |
if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]: |
|
1615 |
self.StartConnected.RefreshParentBlock() |
|
1616 |
if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]: |
|
1617 |
self.EndConnected.RefreshParentBlock() |
|
1618 |
||
1619 |
# Draws the wire lines and points |
|
1620 |
def Draw(self, dc): |
|
1621 |
dc.SetPen(wxBLACK_PEN) |
|
1622 |
dc.SetBrush(wxBLACK_BRUSH) |
|
1623 |
# Draw the start and end points if they are not connected or the mouse is over them |
|
1624 |
if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): |
|
1625 |
dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS) |
|
1626 |
if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): |
|
1627 |
dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS) |
|
1628 |
# Draw the wire lines and the last point (it seems that DrawLines stop before the last point) |
|
1629 |
dc.DrawLines(self.Points) |
|
1630 |
dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) |
|
1631 |
# Draw the segment selected in red |
|
1632 |
if self.SelectedSegment != None: |
|
1633 |
dc.SetPen(wxRED_PEN) |
|
1634 |
dc.DrawLine(self.Points[self.SelectedSegment].x, self.Points[self.SelectedSegment].y, |
|
1635 |
self.Points[self.SelectedSegment + 1].x, self.Points[self.SelectedSegment + 1].y) |
|
1636 |
if self.SelectedSegment == len(self.Segments) - 1: |
|
1637 |
dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) |
|
1638 |
Graphic_Element.Draw(self, dc) |
|
1639 |
||
1640 |
||
1641 |
#------------------------------------------------------------------------------- |
|
1642 |
# Graphic comment element |
|
1643 |
#------------------------------------------------------------------------------- |
|
1644 |
||
1645 |
""" |
|
1646 |
Class that implements a comment |
|
1647 |
""" |
|
1648 |
||
1649 |
class Comment(Graphic_Element): |
|
1650 |
||
1651 |
# Create a new comment |
|
1652 |
def __init__(self, parent, content, id = None): |
|
1653 |
Graphic_Element.__init__(self, parent) |
|
1654 |
self.Id = id |
|
1655 |
self.Content = content |
|
1656 |
self.Pos = wxPoint(0, 0) |
|
1657 |
self.Size = wxSize(0, 0) |
|
1658 |
||
1659 |
# Method for keeping compatibility with others |
|
1660 |
def Clean(self): |
|
1661 |
pass |
|
1662 |
||
1663 |
# Delete this comment by calling the corresponding method |
|
1664 |
def Delete(self): |
|
1665 |
self.Parent.DeleteComment(self) |
|
1666 |
||
1667 |
# Refresh the comment bounding box |
|
1668 |
def RefreshBoundingBox(self): |
|
1669 |
self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1670 |
||
1671 |
# Changes the comment size |
|
1672 |
def SetSize(self, width, height): |
|
1673 |
self.Size.SetWidth(width) |
|
1674 |
self.Size.SetHeight(height) |
|
1675 |
self.RefreshBoundingBox() |
|
1676 |
||
1677 |
# Returns the comment size |
|
1678 |
def GetSize(self): |
|
1679 |
return self.Size.GetWidth(), self.Size.GetHeight() |
|
1680 |
||
1681 |
# Returns the comment minimum size |
|
1682 |
def GetMinSize(self): |
|
1683 |
dc = wxClientDC(self.Parent) |
|
1684 |
min_width = 0 |
|
1685 |
min_height = 0 |
|
1686 |
# The comment minimum size is the maximum size of words in the content |
|
1687 |
for line in self.Content.splitlines(): |
|
1688 |
for word in line.split(" "): |
|
1689 |
wordwidth, wordheight = dc.GetTextExtent(word) |
|
1690 |
min_width = max(min_width, wordwidth) |
|
1691 |
min_height = max(min_height, wordheight) |
|
1692 |
return min_width + 20, min_height + 20 |
|
1693 |
||
1694 |
# Changes the comment position |
|
1695 |
def SetPosition(self, x, y): |
|
1696 |
self.Pos.x = x |
|
1697 |
self.Pos.y = y |
|
1698 |
self.RefreshBoundingBox() |
|
1699 |
||
1700 |
# Changes the comment content |
|
1701 |
def SetContent(self, content): |
|
1702 |
self.Content = content |
|
1703 |
min_width, min_height = self.GetMinSize() |
|
1704 |
self.Size[0] = max(self.Size[0], min_width) |
|
1705 |
self.Size[1] = max(self.Size[1], min_height) |
|
1706 |
self.RefreshBoundingBox() |
|
1707 |
||
1708 |
# Returns the comment content |
|
1709 |
def GetContent(self): |
|
1710 |
return self.Content |
|
1711 |
||
1712 |
# Returns the comment position |
|
1713 |
def GetPosition(self): |
|
1714 |
return self.Pos.x, self.Pos.y |
|
1715 |
||
1716 |
# Moves the comment |
|
1717 |
def Move(self, dx, dy, connected = True): |
|
1718 |
self.Pos.x += dx |
|
1719 |
self.Pos.y += dy |
|
1720 |
self.RefreshBoundingBox() |
|
1721 |
||
1722 |
# Resizes the comment with the position and the size given |
|
1723 |
def Resize(self, x, y, width, height): |
|
1724 |
self.Move(x, y) |
|
1725 |
self.SetSize(width, height) |
|
1726 |
||
1727 |
# Method called when a RightUp event have been generated |
|
27 | 1728 |
def OnRightUp(self, event, dc, scaling): |
0 | 1729 |
# Popup the default menu |
1730 |
self.Parent.PopupDefaultMenu() |
|
1731 |
||
1732 |
# Refreshes the comment model |
|
1733 |
def RefreshModel(self, move=True): |
|
1734 |
self.Parent.RefreshCommentModel(self) |
|
1735 |
||
1736 |
# Method called when a LeftDClick event have been generated |
|
27 | 1737 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 1738 |
# Edit the comment content |
1739 |
self.Parent.EditCommentContent(self) |
|
1740 |
||
1741 |
# Draws the comment and its content |
|
1742 |
def Draw(self, dc): |
|
1743 |
dc.SetPen(wxBLACK_PEN) |
|
1744 |
dc.SetBrush(wxWHITE_BRUSH) |
|
1745 |
# Draws the comment shape |
|
1746 |
polygon = [wxPoint(self.Pos.x, self.Pos.y), |
|
1747 |
wxPoint(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
|
1748 |
wxPoint(self.Pos.x + self.Size[0], self.Pos.y + 10), |
|
1749 |
wxPoint(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] + 1), |
|
1750 |
wxPoint(self.Pos.x, self.Pos.y + self.Size[1] + 1)] |
|
1751 |
dc.DrawPolygon(polygon) |
|
1752 |
lines = [wxPoint(self.Pos.x + self.Size[0] - 10, self.Pos.y), |
|
1753 |
wxPoint(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10), |
|
1754 |
wxPoint(self.Pos.x + self.Size[0], self.Pos.y + 10)] |
|
1755 |
dc.DrawLines(lines) |
|
1756 |
# Draws the comment content |
|
1757 |
y = self.Pos.y + 10 |
|
1758 |
for line in self.Content.splitlines(): |
|
1759 |
first = True |
|
1760 |
words = line.split(" ") |
|
1761 |
for i, word in enumerate(words): |
|
1762 |
if first: |
|
1763 |
test = word |
|
1764 |
else: |
|
1765 |
test = linetext + " " + word |
|
1766 |
wordwidth, wordheight = dc.GetTextExtent(test) |
|
1767 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
1768 |
break |
|
1769 |
if wordwidth < self.Size[0] - 20 and i < len(words) - 1: |
|
1770 |
linetext = test |
|
1771 |
first = False |
|
1772 |
else: |
|
1773 |
if wordwidth < self.Size[0] - 20 and i == len(words) - 1: |
|
1774 |
dc.DrawText(test, self.Pos.x + 10, y) |
|
1775 |
else: |
|
1776 |
dc.DrawText(linetext, self.Pos.x + 10, y) |
|
1777 |
if i == len(words) - 1: |
|
1778 |
y += wordheight + 5 |
|
1779 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
1780 |
break |
|
1781 |
dc.DrawText(word, self.Pos.x + 10, y) |
|
1782 |
else: |
|
1783 |
linetext = word |
|
1784 |
y += wordheight + 5 |
|
1785 |
if y + wordheight > self.Pos.y + self.Size[1] - 10: |
|
1786 |
break |
|
1787 |
Graphic_Element.Draw(self, dc) |