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