author | lbessard |
Fri, 19 Oct 2007 10:15:23 +0200 | |
changeset 109 | 734e02ab4018 |
parent 108 | 9aa1fdfb7cb2 |
child 110 | 29b6b70e1721 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 8 |
# |
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 13 |
#License as published by the Free Software Foundation; either |
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
58 | 19 |
#General Public License for more details. |
0 | 20 |
# |
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 22 |
#License along with this library; if not, write to the Free Software |
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
||
27 |
from GraphicCommons import * |
|
28 |
from plcopen.structures import * |
|
29 |
||
30 |
def GetWireSize(block): |
|
31 |
if isinstance(block, SFC_Step): |
|
32 |
return SFC_WIRE_MIN_SIZE + block.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1] |
|
33 |
else: |
|
34 |
return SFC_WIRE_MIN_SIZE |
|
35 |
||
36 |
#------------------------------------------------------------------------------- |
|
37 |
# Sequencial Function Chart Step |
|
38 |
#------------------------------------------------------------------------------- |
|
39 |
||
40 |
""" |
|
41 |
Class that implements the graphic representation of a step |
|
42 |
""" |
|
43 |
||
44 |
class SFC_Step(Graphic_Element): |
|
45 |
||
46 |
# Create a new step |
|
47 |
def __init__(self, parent, name, initial = False, id = None): |
|
48 |
Graphic_Element.__init__(self, parent) |
|
49 |
self.Name = name |
|
50 |
self.Initial = initial |
|
51 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
52 |
self.Size = wx.Size(SFC_STEP_DEFAULT_SIZE[0], SFC_STEP_DEFAULT_SIZE[1]) |
0 | 53 |
# Create an input and output connector |
54 |
if not self.Initial: |
|
108 | 55 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
0 | 56 |
else: |
57 |
self.Input = None |
|
58 |
self.Output = None |
|
59 |
self.Action = None |
|
60 |
||
61 |
# Destructor |
|
62 |
def __del__(self): |
|
63 |
self.Input = None |
|
64 |
self.Output = None |
|
65 |
self.Action = None |
|
66 |
||
67 |
# Delete this step by calling the appropriate method |
|
68 |
def Delete(self): |
|
69 |
self.Parent.DeleteStep(self) |
|
70 |
||
71 |
# Unconnect input and output |
|
72 |
def Clean(self): |
|
73 |
if self.Input: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
74 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 75 |
if self.Output: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
76 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 77 |
if self.Action: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
78 |
self.Action.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 79 |
|
80 |
# Add output connector to step |
|
71 | 81 |
def AddInput(self): |
82 |
if not self.Input: |
|
108 | 83 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
71 | 84 |
self.RefreshBoundingBox() |
85 |
||
86 |
# Remove output connector from step |
|
87 |
def RemoveInput(self): |
|
88 |
if self.Input: |
|
89 |
self.Input.UnConnect() |
|
90 |
self.Input = None |
|
91 |
self.RefreshBoundingBox() |
|
92 |
||
93 |
# Add output connector to step |
|
0 | 94 |
def AddOutput(self): |
95 |
if not self.Output: |
|
108 | 96 |
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH) |
0 | 97 |
self.RefreshBoundingBox() |
98 |
||
99 |
# Remove output connector from step |
|
100 |
def RemoveOutput(self): |
|
101 |
if self.Output: |
|
102 |
self.Output.UnConnect() |
|
103 |
self.Output = None |
|
104 |
self.RefreshBoundingBox() |
|
105 |
||
106 |
# Add action connector to step |
|
107 |
def AddAction(self): |
|
108 |
if not self.Action: |
|
108 | 109 |
self.Action = Connector(self, "", None, wx.Point(self.Size[0], self.Size[1] / 2), EAST) |
0 | 110 |
self.RefreshBoundingBox() |
111 |
||
112 |
# Remove action connector from step |
|
113 |
def RemoveAction(self): |
|
114 |
if self.Action: |
|
115 |
self.Action.UnConnect() |
|
116 |
self.Action = None |
|
117 |
self.RefreshBoundingBox() |
|
118 |
||
119 |
# Refresh the step bounding box |
|
120 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
121 |
dc = wx.ClientDC(self.Parent) |
0 | 122 |
# Calculate the bounding box size |
123 |
if self.Action: |
|
124 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
125 |
else: |
|
126 |
bbx_width = self.Size[0] |
|
127 |
if self.Initial: |
|
128 |
bbx_y = self.Pos.y |
|
129 |
bbx_height = self.Size[1] |
|
130 |
if self.Output: |
|
131 |
bbx_height += CONNECTOR_SIZE |
|
132 |
else: |
|
133 |
bbx_y = self.Pos.y - CONNECTOR_SIZE |
|
134 |
bbx_height = self.Size[1] + CONNECTOR_SIZE |
|
135 |
if self.Output: |
|
136 |
bbx_height += CONNECTOR_SIZE |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
137 |
#self.BoundingBox = wx.Rect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height + 1) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
138 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
0 | 139 |
|
140 |
# Refresh the positions of the step connectors |
|
141 |
def RefreshConnectors(self): |
|
142 |
# Update input position if it exists |
|
143 |
if self.Input: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
144 |
self.Input.SetPosition(wx.Point(self.Size[0] / 2, 0)) |
0 | 145 |
# Update output position |
146 |
if self.Output: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
147 |
self.Output.SetPosition(wx.Point(self.Size[0] / 2, self.Size[1])) |
0 | 148 |
# Update action position if it exists |
149 |
if self.Action: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
150 |
self.Action.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2)) |
0 | 151 |
self.RefreshConnected() |
152 |
||
153 |
# Refresh the position of wires connected to step |
|
154 |
def RefreshConnected(self, exclude = []): |
|
155 |
if self.Input: |
|
156 |
self.Input.MoveConnected(exclude) |
|
157 |
if self.Output: |
|
158 |
self.Output.MoveConnected(exclude) |
|
159 |
if self.Action: |
|
160 |
self.Action.MoveConnected(exclude) |
|
161 |
||
162 |
# Returns the step connector that starts with the point given if it exists |
|
27 | 163 |
def GetConnector(self, position, name = None): |
164 |
# if a name is given |
|
165 |
if name: |
|
166 |
# Test input, output and action connector if they exists |
|
167 |
if self.Input and name == self.Input.GetName(): |
|
168 |
return self.Input |
|
169 |
if self.Output and name == self.Output.GetName(): |
|
170 |
return self.Output |
|
171 |
if self.Action and name == self.Action.GetName(): |
|
172 |
return self.Action |
|
0 | 173 |
# Test input connector if it exists |
174 |
if self.Input: |
|
175 |
input_pos = self.Input.GetRelPosition() |
|
176 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
177 |
return self.Input |
|
178 |
# Test output connector if it exists |
|
179 |
if self.Output: |
|
180 |
output_pos = self.Output.GetRelPosition() |
|
181 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
182 |
return self.Output |
|
183 |
# Test action connector if it exists |
|
184 |
if self.Action: |
|
185 |
action_pos = self.Action.GetRelPosition() |
|
186 |
if position.x == self.Pos.x + action_pos.x and position.y == self.Pos.y + action_pos.y: |
|
187 |
return self.Action |
|
188 |
return None |
|
189 |
||
190 |
# Returns input and output step connectors |
|
191 |
def GetConnectors(self): |
|
192 |
return {"input":self.Input,"output":self.Output,"action":self.Action} |
|
193 |
||
194 |
# Test if point given is on step input or output connector |
|
195 |
def TestConnector(self, pt, exclude=True): |
|
196 |
# Test input connector if it exists |
|
197 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
198 |
return self.Input |
|
199 |
# Test output connector |
|
200 |
if self.Output and self.Output.TestPoint(pt, exclude): |
|
201 |
return self.Output |
|
108 | 202 |
# Test action connector |
203 |
if self.Action and self.Action.TestPoint(pt, exclude): |
|
204 |
return self.Action |
|
0 | 205 |
return None |
206 |
||
207 |
# Changes the step name |
|
208 |
def SetName(self, name): |
|
209 |
self.Name = name |
|
210 |
||
211 |
# Returns the step name |
|
212 |
def GetName(self): |
|
213 |
return self.Name |
|
214 |
||
215 |
# Returns the step initial property |
|
216 |
def GetInitial(self): |
|
217 |
return self.Initial |
|
218 |
||
219 |
# Returns the connector connected to input |
|
220 |
def GetPreviousConnector(self): |
|
221 |
if self.Input: |
|
222 |
wires = self.Input.GetWires() |
|
223 |
if len(wires) == 1: |
|
224 |
return wires[0][0].EndConnected |
|
225 |
return None |
|
226 |
||
227 |
# Returns the connector connected to output |
|
228 |
def GetNextConnector(self): |
|
229 |
if self.Output: |
|
230 |
wires = self.Output.GetWires() |
|
231 |
if len(wires) == 1: |
|
232 |
return wires[0][0].StartConnected |
|
233 |
return None |
|
234 |
||
235 |
# Returns the connector connected to action |
|
236 |
def GetActionConnector(self): |
|
237 |
if self.Action: |
|
238 |
wires = self.Action.GetWires() |
|
239 |
if len(wires) == 1: |
|
240 |
return wires[0][0].StartConnected |
|
241 |
return None |
|
242 |
||
243 |
# Returns the number of action line |
|
244 |
def GetActionExtraLineNumber(self): |
|
245 |
if self.Action: |
|
246 |
wires = self.Action.GetWires() |
|
247 |
if len(wires) != 1: |
|
248 |
return 0 |
|
249 |
action_block = wires[0][0].StartConnected.GetParentBlock() |
|
250 |
return max(0, action_block.GetLineNumber() - 1) |
|
251 |
return 0 |
|
252 |
||
253 |
# Returns the step minimum size |
|
254 |
def GetMinSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
255 |
dc = wx.ClientDC(self.Parent) |
0 | 256 |
text_width, text_height = dc.GetTextExtent(self.Name) |
257 |
if self.Initial: |
|
258 |
return text_width + 14, text_height + 14 |
|
259 |
else: |
|
260 |
return text_width + 10, text_height + 10 |
|
261 |
||
262 |
# Updates the step size |
|
263 |
def UpdateSize(self, width, height): |
|
264 |
diffx = self.Size.GetWidth() / 2 - width / 2 |
|
265 |
diffy = height - self.Size.GetHeight() |
|
266 |
self.Move(diffx, 0) |
|
267 |
Graphic_Element.SetSize(self, width, height) |
|
108 | 268 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
269 |
self.RefreshConnected() |
|
270 |
else: |
|
271 |
self.RefreshOutputPosition((0, diffy)) |
|
0 | 272 |
|
273 |
# Align input element with this step |
|
274 |
def RefreshInputPosition(self): |
|
275 |
if self.Input: |
|
276 |
current_pos = self.Input.GetPosition(False) |
|
277 |
input = self.GetPreviousConnector() |
|
278 |
if input: |
|
279 |
input_pos = input.GetPosition(False) |
|
280 |
diffx = current_pos.x - input_pos.x |
|
281 |
input_block = input.GetParentBlock() |
|
282 |
if isinstance(input_block, SFC_Divergence): |
|
283 |
input_block.MoveConnector(input, diffx) |
|
284 |
else: |
|
285 |
if isinstance(input_block, SFC_Step): |
|
286 |
input_block.MoveActionBlock((diffx, 0)) |
|
287 |
input_block.Move(diffx, 0) |
|
288 |
input_block.RefreshInputPosition() |
|
289 |
||
290 |
# Align output element with this step |
|
291 |
def RefreshOutputPosition(self, move = None): |
|
292 |
if self.Output: |
|
293 |
wires = self.Output.GetWires() |
|
294 |
if len(wires) != 1: |
|
295 |
return |
|
296 |
current_pos = self.Output.GetPosition(False) |
|
297 |
output = wires[0][0].StartConnected |
|
298 |
output_pos = output.GetPosition(False) |
|
299 |
diffx = current_pos.x - output_pos.x |
|
300 |
output_block = output.GetParentBlock() |
|
301 |
wire_size = SFC_WIRE_MIN_SIZE + self.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1] |
|
302 |
diffy = wire_size - output_pos.y + current_pos.y |
|
303 |
if diffy != 0: |
|
304 |
if isinstance(output_block, SFC_Step): |
|
305 |
output_block.MoveActionBlock((diffx, diffy)) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
306 |
wires[0][0].SetPoints([wx.Point(current_pos.x, current_pos.y + wire_size), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
307 |
wx.Point(current_pos.x, current_pos.y)]) |
0 | 308 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
309 |
output_block.Move(diffx, diffy, self.Parent.Wires) |
|
310 |
output_block.RefreshOutputPosition((diffx, diffy)) |
|
311 |
else: |
|
312 |
output_block.RefreshPosition() |
|
313 |
elif move: |
|
314 |
if isinstance(output_block, SFC_Step): |
|
315 |
output_block.MoveActionBlock(move) |
|
316 |
wires[0][0].Move(move[0], move[1], True) |
|
317 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
318 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
319 |
output_block.RefreshOutputPosition(move) |
|
320 |
else: |
|
321 |
output_block.RefreshPosition() |
|
322 |
elif isinstance(output_block, SFC_Divergence): |
|
323 |
output_block.MoveConnector(output, diffx) |
|
324 |
else: |
|
325 |
if isinstance(output_block, SFC_Step): |
|
326 |
output_block.MoveActionBlock((diffx, 0)) |
|
327 |
output_block.Move(diffx, 0) |
|
328 |
output_block.RefreshOutputPosition() |
|
329 |
||
330 |
# Refresh action element with this step |
|
331 |
def MoveActionBlock(self, move): |
|
332 |
if self.Action: |
|
333 |
wires = self.Action.GetWires() |
|
334 |
if len(wires) != 1: |
|
335 |
return |
|
336 |
action_block = wires[0][0].StartConnected.GetParentBlock() |
|
337 |
action_block.Move(move[0], move[1], self.Parent.Wires) |
|
338 |
wires[0][0].Move(move[0], move[1], True) |
|
339 |
||
340 |
# Resize the divergence from position and size given |
|
341 |
def Resize(self, x, y, width, height): |
|
27 | 342 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
343 |
self.UpdateSize(width, height) |
|
344 |
else: |
|
345 |
Graphic_Element.Resize(self, x, y, width, height) |
|
0 | 346 |
|
347 |
# Method called when a LeftDClick event have been generated |
|
42 | 348 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 349 |
# Edit the step properties |
350 |
self.Parent.EditStepContent(self) |
|
351 |
||
352 |
# Method called when a RightUp event have been generated |
|
27 | 353 |
def OnRightUp(self, event, dc, scaling): |
0 | 354 |
# Popup the menu with special items for a step |
355 |
self.Parent.PopupDefaultMenu() |
|
356 |
||
357 |
# Refreshes the step state according to move defined and handle selected |
|
358 |
def ProcessDragging(self, movex, movey): |
|
359 |
handle_type, handle = self.Handle |
|
360 |
if handle_type == HANDLE_MOVE: |
|
361 |
action_block = None |
|
27 | 362 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
363 |
self.Move(movex, movey) |
|
364 |
self.RefreshConnected() |
|
365 |
elif self.Initial: |
|
0 | 366 |
self.MoveActionBlock((movex, movey)) |
367 |
self.Move(movex, movey, self.Parent.Wires) |
|
368 |
self.RefreshOutputPosition((movex, movey)) |
|
369 |
else: |
|
370 |
self.MoveActionBlock((movex, 0)) |
|
371 |
self.Move(movex, 0) |
|
372 |
self.RefreshInputPosition() |
|
373 |
self.RefreshOutputPosition() |
|
374 |
else: |
|
375 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
376 |
||
377 |
# Refresh input element model |
|
378 |
def RefreshInputModel(self): |
|
379 |
if self.Input: |
|
380 |
input = self.GetPreviousConnector() |
|
381 |
if input: |
|
382 |
input_block = input.GetParentBlock() |
|
383 |
input_block.RefreshModel(False) |
|
384 |
if not isinstance(input_block, SFC_Divergence): |
|
385 |
input_block.RefreshInputModel() |
|
386 |
||
387 |
# Refresh output element model |
|
388 |
def RefreshOutputModel(self, move=False): |
|
389 |
if self.Output: |
|
390 |
output = self.GetNextConnector() |
|
391 |
if output: |
|
392 |
output_block = output.GetParentBlock() |
|
393 |
output_block.RefreshModel(False) |
|
394 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
395 |
output_block.RefreshOutputModel(move) |
|
396 |
||
397 |
# Refreshes the step model |
|
398 |
def RefreshModel(self, move=True): |
|
399 |
self.Parent.RefreshStepModel(self) |
|
400 |
if self.Action: |
|
401 |
action = self.GetActionConnector() |
|
402 |
if action: |
|
403 |
action_block = action.GetParentBlock() |
|
404 |
action_block.RefreshModel(False) |
|
405 |
# If step has moved, refresh the model of wires connected to output |
|
406 |
if move: |
|
27 | 407 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
408 |
self.RefreshInputModel() |
|
409 |
self.RefreshOutputModel(self.Initial) |
|
410 |
elif self.Output: |
|
411 |
self.Output.RefreshWires() |
|
0 | 412 |
|
413 |
# Draws step |
|
414 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
415 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
416 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 417 |
# Draw two rectangles for representing the step |
418 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
419 |
if self.Initial: |
|
420 |
dc.DrawRectangle(self.Pos.x + 2, self.Pos.y + 2, self.Size[0] - 3, self.Size[1] - 3) |
|
421 |
# Draw step name |
|
422 |
namewidth, nameheight = dc.GetTextExtent(self.Name) |
|
423 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2, |
|
424 |
self.Pos.y + (self.Size[1] - nameheight) / 2) |
|
425 |
# Draw input and output connectors |
|
426 |
if self.Input: |
|
427 |
self.Input.Draw(dc) |
|
428 |
if self.Output: |
|
429 |
self.Output.Draw(dc) |
|
430 |
if self.Action: |
|
431 |
self.Action.Draw(dc) |
|
432 |
Graphic_Element.Draw(self, dc) |
|
433 |
||
434 |
#------------------------------------------------------------------------------- |
|
435 |
# Sequencial Function Chart Transition |
|
436 |
#------------------------------------------------------------------------------- |
|
437 |
||
438 |
""" |
|
439 |
Class that implements the graphic representation of a transition |
|
440 |
""" |
|
441 |
||
442 |
class SFC_Transition(Graphic_Element): |
|
443 |
||
444 |
# Create a new transition |
|
80 | 445 |
def __init__(self, parent, type = "reference", condition = None, priority = 0, id = None): |
0 | 446 |
Graphic_Element.__init__(self, parent) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
447 |
self.Type = None |
0 | 448 |
self.Id = id |
80 | 449 |
self.Priority = 0 |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
450 |
self.Size = wx.Size(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1]) |
0 | 451 |
# Create an input and output connector |
108 | 452 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
453 |
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
454 |
self.SetType(type, condition) |
80 | 455 |
self.SetPriority(priority) |
0 | 456 |
|
457 |
# Destructor |
|
458 |
def __del__(self): |
|
459 |
self.Input = None |
|
460 |
self.Output = None |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
461 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
462 |
self.Condition = None |
0 | 463 |
|
464 |
# Forbids to change the transition size |
|
465 |
def SetSize(self, width, height): |
|
27 | 466 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
467 |
Graphic_Element.SetSize(self, width, height) |
|
0 | 468 |
|
469 |
# Forbids to resize the transition |
|
470 |
def Resize(self, x, y, width, height): |
|
27 | 471 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
472 |
Graphic_Element.Resize(self, x, y, width, height) |
|
0 | 473 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
474 |
# Refresh the size of text for name |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
475 |
def RefreshConditionSize(self): |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
476 |
if self.Type != "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
477 |
dc = wx.ClientDC(self.Parent) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
478 |
if self.Condition != "": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
479 |
self.ConditionSize = dc.GetTextExtent(self.Condition) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
480 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
481 |
self.ConditionSize = dc.GetTextExtent("Transition") |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
482 |
|
80 | 483 |
# Refresh the size of text for name |
484 |
def RefreshPrioritySize(self): |
|
485 |
if self.Priority != "": |
|
486 |
dc = wx.ClientDC(self.Parent) |
|
487 |
self.PrioritySize = dc.GetTextExtent(str(self.Priority)) |
|
488 |
else: |
|
489 |
self.PrioritySize = None |
|
490 |
||
0 | 491 |
# Delete this transition by calling the appropriate method |
492 |
def Delete(self): |
|
493 |
self.Parent.DeleteTransition(self) |
|
494 |
||
495 |
# Unconnect input and output |
|
496 |
def Clean(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
497 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
498 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
499 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
500 |
self.Condition.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 501 |
|
502 |
# Refresh the transition bounding box |
|
503 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
504 |
dc = wx.ClientDC(self.Parent) |
80 | 505 |
bbx_x, bbx_y, bbx_width, bbx_height = self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] |
506 |
if self.Priority != 0: |
|
507 |
bbx_y = self.Pos.y - self.PrioritySize[1] - 2 |
|
508 |
bbx_width = max(self.Size[0], self.PrioritySize[0]) |
|
509 |
bbx_height = self.Size[1] + self.PrioritySize[1] + 2 |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
510 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
511 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
80 | 512 |
bbx_width = bbx_width + CONNECTOR_SIZE |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
513 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
514 |
text_width, text_height = self.ConditionSize |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
515 |
# Calculate the bounding box size |
80 | 516 |
bbx_width = max(bbx_width, self.Size[0] + 5 + text_width) |
517 |
bbx_y = min(bbx_y, self.Pos.y - max(0, (text_height - self.Size[1]) / 2)) |
|
518 |
bbx_height = max(bbx_height, self.Pos.y - bbx_y + (self.Size[1] + text_height) / 2) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
519 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 520 |
|
521 |
# Returns the connector connected to input |
|
522 |
def GetPreviousConnector(self): |
|
523 |
wires = self.Input.GetWires() |
|
524 |
if len(wires) == 1: |
|
525 |
return wires[0][0].EndConnected |
|
526 |
return None |
|
527 |
||
528 |
# Returns the connector connected to output |
|
529 |
def GetNextConnector(self): |
|
530 |
wires = self.Output.GetWires() |
|
531 |
if len(wires) == 1: |
|
532 |
return wires[0][0].StartConnected |
|
533 |
return None |
|
534 |
||
535 |
# Refresh the positions of the transition connectors |
|
536 |
def RefreshConnectors(self): |
|
537 |
# Update input position |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
538 |
self.Input.SetPosition(wx.Point(self.Size[0] / 2, 0)) |
0 | 539 |
# Update output position |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
540 |
self.Output.SetPosition(wx.Point(self.Size[0] / 2, self.Size[1])) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
541 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
542 |
self.Condition.SetPosition(wx.Point(0, self.Size[1] / 2)) |
0 | 543 |
self.RefreshConnected() |
544 |
||
545 |
# Refresh the position of the wires connected to transition |
|
546 |
def RefreshConnected(self, exclude = []): |
|
547 |
self.Input.MoveConnected(exclude) |
|
548 |
self.Output.MoveConnected(exclude) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
549 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
550 |
self.Condition.MoveConnected(exclude) |
0 | 551 |
|
552 |
# Returns the transition connector that starts with the point given if it exists |
|
27 | 553 |
def GetConnector(self, position, name = None): |
554 |
# if a name is given |
|
555 |
if name: |
|
556 |
# Test input and output connector |
|
557 |
if name == self.Input.GetName(): |
|
558 |
return self.Input |
|
559 |
if name == self.Output.GetName(): |
|
560 |
return self.Output |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
561 |
if self.Type == "connection" and name == self.Condition.GetName(): |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
562 |
return self.Condition |
0 | 563 |
# Test input connector |
564 |
input_pos = self.Input.GetRelPosition() |
|
565 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
566 |
return self.Input |
|
567 |
# Test output connector |
|
568 |
output_pos = self.Output.GetRelPosition() |
|
569 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
570 |
return self.Output |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
571 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
572 |
# Test condition connector |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
573 |
condition_pos = self.Condition.GetRelPosition() |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
574 |
if position.x == self.Pos.x + condition_pos.x and position.y == self.Pos.y + condition_pos.y: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
575 |
return self.Condition |
0 | 576 |
return None |
577 |
||
578 |
# Returns input and output transition connectors |
|
579 |
def GetConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
580 |
connectors = {"input":self.Input,"output":self.Output} |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
581 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
582 |
connectors["connection"] = self.Condition |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
583 |
return connectors |
0 | 584 |
|
585 |
# Test if point given is on transition input or output connector |
|
586 |
def TestConnector(self, pt, exclude=True): |
|
587 |
# Test input connector |
|
588 |
if self.Input.TestPoint(pt, exclude): |
|
589 |
return self.Input |
|
590 |
# Test output connector |
|
591 |
if self.Output.TestPoint(pt, exclude): |
|
592 |
return self.Output |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
593 |
# Test condition connector |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
594 |
if self.Type == "connection" and self.Condition.TestPoint(pt, exclude): |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
595 |
return self.Condition |
0 | 596 |
return None |
597 |
||
598 |
# Changes the transition type |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
599 |
def SetType(self, type, condition = None): |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
600 |
if self.Type != type: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
601 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
602 |
self.Condition.UnConnect() |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
603 |
self.Type = type |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
604 |
if type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
605 |
self.Condition = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2), WEST) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
606 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
607 |
if condition == None: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
608 |
condition = "" |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
609 |
self.Condition = condition |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
610 |
self.RefreshConditionSize() |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
611 |
elif self.Type != "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
612 |
if condition == None: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
613 |
condition = "" |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
614 |
self.Condition = condition |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
615 |
self.RefreshConditionSize() |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
616 |
self.RefreshBoundingBox() |
0 | 617 |
|
618 |
# Returns the transition type |
|
619 |
def GetType(self): |
|
620 |
return self.Type |
|
621 |
||
80 | 622 |
# Changes the transition priority |
623 |
def SetPriority(self, priority): |
|
624 |
self.Priority = priority |
|
625 |
self.RefreshPrioritySize() |
|
626 |
self.RefreshBoundingBox() |
|
627 |
||
628 |
# Returns the transition type |
|
629 |
def GetPriority(self): |
|
630 |
return self.Priority |
|
631 |
||
0 | 632 |
# Returns the transition condition |
633 |
def GetCondition(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
634 |
if self.Type != "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
635 |
return self.Condition |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
636 |
return None |
0 | 637 |
|
638 |
# Returns the transition minimum size |
|
639 |
def GetMinSize(self): |
|
640 |
return SFC_TRANSITION_SIZE |
|
641 |
||
642 |
# Align input element with this step |
|
643 |
def RefreshInputPosition(self): |
|
644 |
wires = self.Input.GetWires() |
|
645 |
current_pos = self.Input.GetPosition(False) |
|
646 |
input = self.GetPreviousConnector() |
|
647 |
if input: |
|
648 |
input_pos = input.GetPosition(False) |
|
649 |
diffx = current_pos.x - input_pos.x |
|
650 |
input_block = input.GetParentBlock() |
|
651 |
if isinstance(input_block, SFC_Divergence): |
|
652 |
input_block.MoveConnector(input, diffx) |
|
653 |
else: |
|
654 |
if isinstance(input_block, SFC_Step): |
|
655 |
input_block.MoveActionBlock((diffx, 0)) |
|
656 |
input_block.Move(diffx, 0) |
|
657 |
input_block.RefreshInputPosition() |
|
658 |
||
659 |
# Align output element with this step |
|
660 |
def RefreshOutputPosition(self, move = None): |
|
661 |
wires = self.Output.GetWires() |
|
662 |
if len(wires) != 1: |
|
663 |
return |
|
664 |
current_pos = self.Output.GetPosition(False) |
|
665 |
output = wires[0][0].StartConnected |
|
666 |
output_pos = output.GetPosition(False) |
|
667 |
diffx = current_pos.x - output_pos.x |
|
668 |
output_block = output.GetParentBlock() |
|
669 |
if move: |
|
670 |
if isinstance(output_block, SFC_Step): |
|
671 |
output_block.MoveActionBlock(move) |
|
672 |
wires[0][0].Move(move[0], move[1], True) |
|
673 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
674 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
675 |
output_block.RefreshOutputPosition(move) |
|
676 |
else: |
|
677 |
output_block.RefreshPosition() |
|
678 |
elif isinstance(output_block, SFC_Divergence): |
|
679 |
output_block.MoveConnector(output, diffx) |
|
680 |
else: |
|
681 |
if isinstance(output_block, SFC_Step): |
|
682 |
output_block.MoveActionBlock((diffx, 0)) |
|
683 |
output_block.Move(diffx, 0) |
|
684 |
output_block.RefreshOutputPosition() |
|
27 | 685 |
|
0 | 686 |
# Method called when a LeftDClick event have been generated |
27 | 687 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 688 |
# Edit the transition properties |
689 |
self.Parent.EditTransitionContent(self) |
|
690 |
||
691 |
# Method called when a RightUp event have been generated |
|
27 | 692 |
def OnRightUp(self, event, dc, scaling): |
0 | 693 |
# Popup the menu with special items for a step |
694 |
self.Parent.PopupDefaultMenu() |
|
695 |
||
696 |
# Refreshes the transition state according to move defined and handle selected |
|
697 |
def ProcessDragging(self, movex, movey): |
|
27 | 698 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
699 |
self.Move(movex, 0) |
|
700 |
self.RefreshInputPosition() |
|
701 |
self.RefreshOutputPosition() |
|
702 |
else: |
|
703 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
0 | 704 |
|
705 |
# Refresh input element model |
|
706 |
def RefreshInputModel(self): |
|
27 | 707 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
708 |
input = self.GetPreviousConnector() |
|
709 |
if input: |
|
710 |
input_block = input.GetParentBlock() |
|
711 |
input_block.RefreshModel(False) |
|
712 |
if not isinstance(input_block, SFC_Divergence): |
|
713 |
input_block.RefreshInputModel() |
|
0 | 714 |
|
715 |
# Refresh output element model |
|
716 |
def RefreshOutputModel(self, move=False): |
|
717 |
output = self.GetNextConnector() |
|
718 |
if output: |
|
719 |
output_block = output.GetParentBlock() |
|
720 |
output_block.RefreshModel(False) |
|
721 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
722 |
output_block.RefreshOutputModel(move) |
|
723 |
||
724 |
# Refreshes the transition model |
|
725 |
def RefreshModel(self, move=True): |
|
726 |
self.Parent.RefreshTransitionModel(self) |
|
727 |
# If transition has moved, refresh the model of wires connected to output |
|
728 |
if move: |
|
27 | 729 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
730 |
self.RefreshInputModel() |
|
731 |
self.RefreshOutputModel() |
|
732 |
else: |
|
733 |
self.Output.RefreshWires() |
|
0 | 734 |
|
735 |
# Draws transition |
|
736 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
737 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
738 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 739 |
# Draw plain rectangle for representing the transition |
740 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
741 |
# Draw transition condition |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
742 |
if self.Type != "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
743 |
text_width, text_height = self.ConditionSize |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
744 |
if self.Condition != "": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
745 |
condition = self.Condition |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
746 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
747 |
condition = "Transition" |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
748 |
dc.DrawText(condition, self.Pos.x + self.Size[0] + 5, |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
749 |
self.Pos.y + (self.Size[1] - text_height) / 2) |
80 | 750 |
# Draw priority number |
751 |
if self.Priority != 0: |
|
752 |
priority_width, priority_height = self.PrioritySize |
|
753 |
dc.DrawText(str(self.Priority), self.Pos.x, self.Pos.y - self.PrioritySize[1] - 2) |
|
0 | 754 |
# Draw input and output connectors |
755 |
self.Input.Draw(dc) |
|
756 |
self.Output.Draw(dc) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
757 |
if self.Type == "connection": |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
758 |
self.Condition.Draw(dc) |
0 | 759 |
Graphic_Element.Draw(self, dc) |
760 |
||
761 |
#------------------------------------------------------------------------------- |
|
762 |
# Sequencial Function Chart Divergence and Convergence |
|
763 |
#------------------------------------------------------------------------------- |
|
764 |
||
765 |
""" |
|
766 |
Class that implements the graphic representation of a divergence or convergence, |
|
767 |
selection or simultaneous |
|
768 |
""" |
|
769 |
||
770 |
class SFC_Divergence(Graphic_Element): |
|
771 |
||
772 |
# Create a new divergence |
|
773 |
def __init__(self, parent, type, number = 2, id = None): |
|
774 |
Graphic_Element.__init__(self, parent) |
|
775 |
self.Type = type |
|
776 |
self.Id = id |
|
777 |
self.RealConnectors = None |
|
778 |
number = max(2, number) |
|
779 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
780 |
self.Size = wx.Size((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 1) |
0 | 781 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
782 |
self.Size = wx.Size((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 3) |
0 | 783 |
# Create an input and output connector |
784 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
108 | 785 |
self.Inputs = [Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH)] |
0 | 786 |
self.Outputs = [] |
787 |
for i in xrange(number): |
|
108 | 788 |
self.Outputs.append(Connector(self, "", None, wx.Point(i * SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH)) |
0 | 789 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
790 |
self.Inputs = [] |
|
791 |
for i in xrange(number): |
|
108 | 792 |
self.Inputs.append(Connector(self, "", None, wx.Point(i * SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH)) |
793 |
self.Outputs = [Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH)] |
|
0 | 794 |
|
795 |
# Destructor |
|
796 |
def __del__(self): |
|
797 |
self.Inputs = [] |
|
798 |
self.Outputs = [] |
|
799 |
||
800 |
# Forbids to resize the divergence |
|
801 |
def Resize(self, x, y, width, height): |
|
27 | 802 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
803 |
Graphic_Element.Resize(self, x, y, width, height) |
|
0 | 804 |
|
805 |
# Delete this divergence by calling the appropriate method |
|
806 |
def Delete(self): |
|
807 |
self.Parent.DeleteDivergence(self) |
|
808 |
||
809 |
# Returns the divergence type |
|
810 |
def GetType(self): |
|
811 |
return self.Type |
|
812 |
||
813 |
# Unconnect input and output |
|
814 |
def Clean(self): |
|
815 |
for input in self.Inputs: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
816 |
input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 817 |
for output in self.Outputs: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
818 |
output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 819 |
|
820 |
# Add a branch to the divergence |
|
821 |
def AddBranch(self): |
|
822 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
823 |
maxx = 0 |
|
824 |
for output in self.Outputs: |
|
825 |
pos = output.GetRelPosition() |
|
826 |
maxx = max(maxx, pos.x) |
|
108 | 827 |
connector = Connector(self, "", None, wx.Point(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH) |
0 | 828 |
self.Outputs.append(connector) |
829 |
self.MoveConnector(connector, 0) |
|
830 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
831 |
maxx = 0 |
|
832 |
for input in self.Inputs: |
|
833 |
pos = input.GetRelPosition() |
|
834 |
maxx = max(maxx, pos.x) |
|
108 | 835 |
connector = Connector(self, "", None, wx.Point(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH) |
0 | 836 |
self.Inputs.append(connector) |
837 |
self.MoveConnector(connector, SFC_DEFAULT_SEQUENCE_INTERVAL) |
|
838 |
||
839 |
# Remove a branch from the divergence |
|
840 |
def RemoveBranch(self, connector): |
|
841 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
842 |
if connector in self.Outputs: |
|
843 |
self.Outputs.remove(connector) |
|
844 |
self.MoveConnector(self.Outputs[0], 0) |
|
845 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
846 |
if connector in self.Inputs: |
|
847 |
self.Inputs.remove(connector) |
|
848 |
self.MoveConnector(self.Inputs[0], 0) |
|
849 |
||
80 | 850 |
# Remove the handled branch from the divergence |
851 |
def RemoveHandledBranch(self): |
|
852 |
handle_type, handle = self.Handle |
|
853 |
if handle_type == HANDLE_CONNECTOR: |
|
854 |
handle.UnConnect(delete=True) |
|
855 |
self.RemoveBranch(handle) |
|
856 |
||
0 | 857 |
# Return the number of branches for the divergence |
858 |
def GetBranchNumber(self): |
|
859 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
860 |
return len(self.Outputs) |
|
861 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
862 |
return len(self.Inputs) |
|
863 |
||
864 |
# Returns if the point given is in the bounding box |
|
865 |
def HitTest(self, pt): |
|
866 |
rect = self.BoundingBox |
|
867 |
return rect.InsideXY(pt.x, pt.y) or self.TestConnector(pt, False) != None |
|
868 |
||
869 |
# Refresh the divergence bounding box |
|
870 |
def RefreshBoundingBox(self): |
|
871 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
872 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y - 2, |
2 | 873 |
self.Size[0] + 1, self.Size[1] + 5) |
0 | 874 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
875 |
self.BoundingBox = wx.Rect(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y - 2, |
2 | 876 |
self.Size[0] + 2 * SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Size[1] + 5) |
0 | 877 |
|
878 |
# Refresh the position of wires connected to divergence |
|
879 |
def RefreshConnected(self, exclude = []): |
|
880 |
for input in self.Inputs: |
|
881 |
input.MoveConnected(exclude) |
|
882 |
for output in self.Outputs: |
|
883 |
output.MoveConnected(exclude) |
|
884 |
||
885 |
# Moves the divergence connector given |
|
886 |
def MoveConnector(self, connector, movex): |
|
887 |
position = connector.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
888 |
connector.SetPosition(wx.Point(position.x + movex, position.y)) |
0 | 889 |
minx = self.Size[0] |
890 |
maxx = 0 |
|
891 |
for input in self.Inputs: |
|
892 |
input_pos = input.GetRelPosition() |
|
893 |
minx = min(minx, input_pos.x) |
|
894 |
maxx = max(maxx, input_pos.x) |
|
895 |
for output in self.Outputs: |
|
896 |
output_pos = output.GetRelPosition() |
|
897 |
minx = min(minx, output_pos.x) |
|
898 |
maxx = max(maxx, output_pos.x) |
|
899 |
if minx != 0: |
|
900 |
for input in self.Inputs: |
|
901 |
input_pos = input.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
902 |
input.SetPosition(wx.Point(input_pos.x - minx, input_pos.y)) |
0 | 903 |
for output in self.Outputs: |
904 |
output_pos = output.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
905 |
output.SetPosition(wx.Point(output_pos.x - minx, output_pos.y)) |
0 | 906 |
self.Pos.x += minx |
907 |
self.Size[0] = maxx - minx |
|
908 |
connector.MoveConnected() |
|
909 |
self.RefreshBoundingBox() |
|
910 |
||
911 |
# Returns the divergence connector that starts with the point given if it exists |
|
27 | 912 |
def GetConnector(self, position, name = None): |
913 |
# if a name is given |
|
914 |
if name: |
|
915 |
# Test each input and output connector |
|
916 |
for input in self.Inputs: |
|
917 |
if name == input.GetName(): |
|
918 |
return input |
|
919 |
for output in self.Outputs: |
|
920 |
if name == output.GetName(): |
|
921 |
return output |
|
0 | 922 |
# Test input connector |
923 |
for input in self.Inputs: |
|
924 |
input_pos = input.GetPosition(False) |
|
925 |
if position.x == input_pos.x and position.y == input_pos.y: |
|
926 |
return input |
|
927 |
# Test output connector |
|
928 |
for output in self.Outputs: |
|
929 |
output_pos = output.GetPosition(False) |
|
930 |
if position.x == output_pos.x and position.y == output_pos.y: |
|
931 |
return output |
|
932 |
return None |
|
933 |
||
934 |
# Returns input and output divergence connectors |
|
935 |
def GetConnectors(self): |
|
936 |
return {"inputs":self.Inputs,"outputs":self.Outputs} |
|
937 |
||
938 |
# Test if point given is on divergence input or output connector |
|
939 |
def TestConnector(self, pt, exclude=True): |
|
940 |
# Test input connector |
|
941 |
for input in self.Inputs: |
|
942 |
if input.TestPoint(pt, exclude): |
|
943 |
return input |
|
944 |
# Test output connector |
|
945 |
for output in self.Outputs: |
|
946 |
if output.TestPoint(pt, exclude): |
|
947 |
return output |
|
948 |
return None |
|
949 |
||
950 |
# Changes the divergence size |
|
951 |
def SetSize(self, width, height): |
|
952 |
for i, input in enumerate(self.Inputs): |
|
953 |
position = input.GetRelPosition() |
|
954 |
if self.RealConnectors: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
955 |
input.SetPosition(wx.Point(int(round(self.RealConnectors["Inputs"][i] * width)), 0)) |
0 | 956 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
957 |
input.SetPosition(wx.Point(int(round(float(position.x)*float(width)/float(self.Size[0]))), 0)) |
0 | 958 |
input.MoveConnected() |
959 |
for i, output in enumerate(self.Outputs): |
|
960 |
position = output.GetRelPosition() |
|
961 |
if self.RealConnectors: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
962 |
output.SetPosition(wx.Point(int(round(self.RealConnectors["Outputs"][i] * width)), self.Size[1])) |
0 | 963 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
964 |
output.SetPosition(wx.Point(int(round(float(position.x)*float(width)/float(self.Size[0]))), self.Size[1])) |
0 | 965 |
output.MoveConnected() |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
966 |
self.Size = wx.Size(width, height) |
0 | 967 |
self.RefreshBoundingBox() |
968 |
||
969 |
# Returns the divergence minimum size |
|
970 |
def GetMinSize(self): |
|
27 | 971 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
972 |
return 0, 1 |
|
973 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
974 |
return 0, 3 |
|
975 |
return 0, 0 |
|
0 | 976 |
|
977 |
# Refresh the position of the block connected to connector |
|
978 |
def RefreshConnectedPosition(self, connector): |
|
979 |
wires = connector.GetWires() |
|
980 |
if len(wires) != 1: |
|
981 |
return |
|
982 |
current_pos = connector.GetPosition(False) |
|
983 |
if connector in self.Inputs: |
|
984 |
next = wires[0][0].EndConnected |
|
985 |
else: |
|
986 |
next = wires[0][0].StartConnected |
|
987 |
next_pos = next.GetPosition(False) |
|
988 |
diffx = current_pos.x - next_pos.x |
|
989 |
next_block = next.GetParentBlock() |
|
990 |
if isinstance(next_block, SFC_Divergence): |
|
991 |
next_block.MoveConnector(next, diffx) |
|
992 |
else: |
|
993 |
next_block.Move(diffx, 0) |
|
994 |
if connector in self.Inputs: |
|
995 |
next_block.RefreshInputPosition() |
|
996 |
else: |
|
997 |
next_block.RefreshOutputPosition() |
|
27 | 998 |
|
0 | 999 |
# Refresh the position of this divergence |
1000 |
def RefreshPosition(self): |
|
1001 |
y = 0 |
|
1002 |
for input in self.Inputs: |
|
1003 |
wires = input.GetWires() |
|
1004 |
if len(wires) != 1: |
|
1005 |
return |
|
1006 |
previous = wires[0][0].EndConnected |
|
1007 |
previous_pos = previous.GetPosition(False) |
|
1008 |
y = max(y, previous_pos.y + GetWireSize(previous.GetParentBlock())) |
|
1009 |
diffy = y - self.Pos.y |
|
1010 |
if diffy != 0: |
|
1011 |
self.Move(0, diffy, self.Parent.Wires) |
|
1012 |
self.RefreshOutputPosition((0, diffy)) |
|
1013 |
for input in self.Inputs: |
|
1014 |
input.MoveConnected() |
|
1015 |
||
1016 |
# Align output element with this divergence |
|
1017 |
def RefreshOutputPosition(self, move = None): |
|
1018 |
if move: |
|
1019 |
for output_connector in self.Outputs: |
|
1020 |
wires = output_connector.GetWires() |
|
1021 |
if len(wires) != 1: |
|
1022 |
return |
|
1023 |
current_pos = output_connector.GetPosition(False) |
|
1024 |
output = wires[0][0].StartConnected |
|
1025 |
output_pos = output.GetPosition(False) |
|
1026 |
diffx = current_pos.x - output_pos.x |
|
1027 |
output_block = output.GetParentBlock() |
|
1028 |
if isinstance(output_block, SFC_Step): |
|
1029 |
output_block.MoveActionBlock(move) |
|
1030 |
wires[0][0].Move(move[0], move[1], True) |
|
1031 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
1032 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
1033 |
output_block.RefreshOutputPosition(move) |
|
1034 |
||
1035 |
# Method called when a LeftDown event have been generated |
|
27 | 1036 |
def OnLeftDown(self, event, dc, scaling): |
1037 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1038 |
# Test if a connector have been handled |
1039 |
connector = self.TestConnector(pos, False) |
|
1040 |
if connector: |
|
1041 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1042 |
self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) |
0 | 1043 |
self.Selected = False |
1044 |
# Initializes the last position |
|
27 | 1045 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
0 | 1046 |
else: |
1047 |
self.RealConnectors = {"Inputs":[],"Outputs":[]} |
|
1048 |
for input in self.Inputs: |
|
1049 |
position = input.GetRelPosition() |
|
1050 |
self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0])) |
|
1051 |
for output in self.Outputs: |
|
1052 |
position = output.GetRelPosition() |
|
1053 |
self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0])) |
|
27 | 1054 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
0 | 1055 |
|
1056 |
# Method called when a LeftUp event have been generated |
|
27 | 1057 |
def OnLeftUp(self, event, dc, scaling): |
0 | 1058 |
self.RealConnectors = None |
1059 |
handle_type, handle = self.Handle |
|
1060 |
if handle_type == HANDLE_CONNECTOR: |
|
1061 |
wires = handle.GetWires() |
|
1062 |
if len(wires) != 1: |
|
1063 |
return |
|
1064 |
if handle in self.Inputs: |
|
1065 |
block = wires[0][0].EndConnected.GetParentBlock() |
|
1066 |
else: |
|
1067 |
block = wires[0][0].StartConnected.GetParentBlock() |
|
1068 |
block.RefreshModel(False) |
|
1069 |
if not isinstance(block, SFC_Divergence): |
|
1070 |
if handle in self.Inputs: |
|
1071 |
block.RefreshInputModel() |
|
1072 |
else: |
|
1073 |
block.RefreshOutputModel() |
|
27 | 1074 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
0 | 1075 |
|
1076 |
# Method called when a RightUp event have been generated |
|
27 | 1077 |
def OnRightUp(self, event, dc, scaling): |
1078 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 1079 |
# Popup the menu with special items for a block and a connector if one is handled |
1080 |
connector = self.TestConnector(pos, False) |
|
1081 |
if connector: |
|
1082 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
1083 |
self.Parent.PopupDivergenceMenu(True) |
|
1084 |
else: |
|
1085 |
# Popup the divergence menu without delete branch |
|
1086 |
self.Parent.PopupDivergenceMenu(False) |
|
1087 |
||
1088 |
# Refreshes the divergence state according to move defined and handle selected |
|
1089 |
def ProcessDragging(self, movex, movey): |
|
1090 |
handle_type, handle = self.Handle |
|
1091 |
# A connector has been handled |
|
1092 |
if handle_type == HANDLE_CONNECTOR: |
|
1093 |
self.MoveConnector(handle, movex) |
|
27 | 1094 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1095 |
self.RefreshConnectedPosition(handle) |
|
1096 |
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1097 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
0 | 1098 |
|
1099 |
# Refresh output element model |
|
1100 |
def RefreshOutputModel(self, move=False): |
|
27 | 1101 |
if move and self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
0 | 1102 |
for output in self.Outputs: |
1103 |
wires = output.GetWires() |
|
1104 |
if len(wires) != 1: |
|
1105 |
return |
|
1106 |
output_block = wires[0][0].StartConnected.GetParentBlock() |
|
1107 |
output_block.RefreshModel(False) |
|
1108 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
1109 |
output_block.RefreshOutputModel(move) |
|
1110 |
||
1111 |
# Refreshes the divergence model |
|
1112 |
def RefreshModel(self, move=True): |
|
1113 |
self.Parent.RefreshDivergenceModel(self) |
|
1114 |
# If divergence has moved, refresh the model of wires connected to outputs |
|
1115 |
if move: |
|
27 | 1116 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1117 |
self.RefreshOutputModel() |
|
1118 |
else: |
|
1119 |
for output in self.Outputs: |
|
1120 |
output.RefreshWires() |
|
0 | 1121 |
|
1122 |
# Draws divergence |
|
1123 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1124 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1125 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 1126 |
# Draw plain rectangle for representing the divergence |
1127 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
1128 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1129 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1130 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y, |
|
1131 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y) |
|
27 | 1132 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y + self.Size[1], |
1133 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y + self.Size[1]) |
|
0 | 1134 |
# Draw inputs and outputs connectors |
1135 |
for input in self.Inputs: |
|
1136 |
input.Draw(dc) |
|
1137 |
for output in self.Outputs: |
|
1138 |
output.Draw(dc) |
|
1139 |
Graphic_Element.Draw(self, dc) |
|
1140 |
||
1141 |
#------------------------------------------------------------------------------- |
|
1142 |
# Sequencial Function Chart Jump to Step |
|
1143 |
#------------------------------------------------------------------------------- |
|
1144 |
||
1145 |
""" |
|
1146 |
Class that implements the graphic representation of a jump to step |
|
1147 |
""" |
|
1148 |
||
1149 |
class SFC_Jump(Graphic_Element): |
|
1150 |
||
1151 |
# Create a new jump |
|
1152 |
def __init__(self, parent, target, id = None): |
|
1153 |
Graphic_Element.__init__(self, parent) |
|
1154 |
self.Target = target |
|
1155 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1156 |
self.Size = wx.Size(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1]) |
0 | 1157 |
# Create an input and output connector |
108 | 1158 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
0 | 1159 |
|
1160 |
# Destructor |
|
1161 |
def __del__(self): |
|
27 | 1162 |
self.Input = None |
0 | 1163 |
|
1164 |
# Forbids to change the jump size |
|
1165 |
def SetSize(self, width, height): |
|
27 | 1166 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
1167 |
Graphic_Element.SetSize(self, width, height) |
|
0 | 1168 |
|
1169 |
# Forbids to resize jump |
|
1170 |
def Resize(self, x, y, width, height): |
|
27 | 1171 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
1172 |
Graphic_Element.Resize(self, x, y, width, height) |
|
0 | 1173 |
|
1174 |
# Delete this jump by calling the appropriate method |
|
1175 |
def Delete(self): |
|
1176 |
self.Parent.DeleteJump(self) |
|
1177 |
||
1178 |
# Unconnect input |
|
1179 |
def Clean(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1180 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 1181 |
|
1182 |
# Refresh the jump bounding box |
|
1183 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1184 |
dc = wx.ClientDC(self.Parent) |
0 | 1185 |
text_width, text_height = dc.GetTextExtent(self.Target) |
1186 |
# Calculate the bounding box size |
|
1187 |
bbx_width = self.Size[0] + 2 + text_width |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1188 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE, |
0 | 1189 |
bbx_width + 1, self.Size[1] + CONNECTOR_SIZE + 1) |
1190 |
||
1191 |
# Returns the connector connected to input |
|
1192 |
def GetPreviousConnector(self): |
|
27 | 1193 |
wires = self.Input.GetWires() |
1194 |
if len(wires) == 1: |
|
1195 |
return wires[0][0].EndConnected |
|
0 | 1196 |
return None |
1197 |
||
27 | 1198 |
# Refresh the element connectors position |
1199 |
def RefreshConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1200 |
self.Input.SetPosition(wx.Point(self.Size[0] / 2, 0)) |
27 | 1201 |
self.RefreshConnected() |
1202 |
||
0 | 1203 |
# Refresh the position of wires connected to jump |
1204 |
def RefreshConnected(self, exclude = []): |
|
1205 |
if self.Input: |
|
1206 |
self.Input.MoveConnected(exclude) |
|
1207 |
||
1208 |
# Returns input jump connector |
|
27 | 1209 |
def GetConnector(self, position = None, name = None): |
0 | 1210 |
return self.Input |
1211 |
||
1212 |
# Test if point given is on jump input connector |
|
1213 |
def TestConnector(self, pt, exclude = True): |
|
1214 |
# Test input connector |
|
1215 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
1216 |
return self.Input |
|
1217 |
return None |
|
1218 |
||
1219 |
# Changes the jump target |
|
1220 |
def SetTarget(self, target): |
|
1221 |
self.Target = target |
|
1222 |
self.RefreshBoundingBox() |
|
1223 |
||
1224 |
# Returns the jump target |
|
1225 |
def GetTarget(self): |
|
1226 |
return self.Target |
|
1227 |
||
1228 |
# Returns the jump minimum size |
|
1229 |
def GetMinSize(self): |
|
1230 |
return SFC_JUMP_SIZE |
|
1231 |
||
1232 |
# Align input element with this jump |
|
1233 |
def RefreshInputPosition(self): |
|
1234 |
if self.Input: |
|
1235 |
current_pos = self.Input.GetPosition(False) |
|
1236 |
input = self.GetPreviousConnector() |
|
1237 |
if input: |
|
1238 |
input_pos = input.GetPosition(False) |
|
1239 |
diffx = current_pos.x - input_pos.x |
|
1240 |
input_block = input.GetParentBlock() |
|
1241 |
if isinstance(input_block, SFC_Divergence): |
|
1242 |
input_block.MoveConnector(input, diffx) |
|
1243 |
else: |
|
1244 |
if isinstance(input_block, SFC_Step): |
|
1245 |
input_block.MoveActionBlock((diffx, 0)) |
|
1246 |
input_block.Move(diffx, 0) |
|
1247 |
input_block.RefreshInputPosition() |
|
1248 |
||
1249 |
# Can't align output element, because there is no output |
|
1250 |
def RefreshOutputPosition(self, move = None): |
|
1251 |
pass |
|
1252 |
||
1253 |
# Method called when a LeftDClick event have been generated |
|
27 | 1254 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 1255 |
# Edit the jump properties |
1256 |
self.Parent.EditJumpContent(self) |
|
1257 |
||
1258 |
# Method called when a RightUp event have been generated |
|
27 | 1259 |
def OnRightUp(self, event, dc, scaling): |
0 | 1260 |
# Popup the default menu |
1261 |
self.Parent.PopupDefaultMenu() |
|
1262 |
||
1263 |
# Refreshes the jump state according to move defined and handle selected |
|
1264 |
def ProcessDragging(self, movex, movey): |
|
27 | 1265 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1266 |
self.Move(movex, 0) |
|
1267 |
self.RefreshInputPosition() |
|
1268 |
else: |
|
1269 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
0 | 1270 |
|
1271 |
# Refresh input element model |
|
1272 |
def RefreshInputModel(self): |
|
27 | 1273 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1274 |
input = self.GetPreviousConnector() |
|
1275 |
if input: |
|
1276 |
input_block = input.GetParentBlock() |
|
1277 |
input_block.RefreshModel(False) |
|
1278 |
if not isinstance(input_block, SFC_Divergence): |
|
1279 |
input_block.RefreshInputModel() |
|
0 | 1280 |
|
1281 |
# Refresh output element model |
|
1282 |
def RefreshOutputModel(self, move=False): |
|
1283 |
pass |
|
1284 |
||
1285 |
# Refreshes the jump model |
|
1286 |
def RefreshModel(self, move=True): |
|
1287 |
self.Parent.RefreshJumpModel(self) |
|
1288 |
if move: |
|
27 | 1289 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1290 |
self.RefreshInputModel() |
|
0 | 1291 |
|
1292 |
# Draws divergence |
|
1293 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1294 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1295 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 1296 |
# Draw plain rectangle for representing the divergence |
1297 |
dc.DrawLine(self.Pos.x + self.Size[0] / 2, self.Pos.y, self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1]) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1298 |
points = [wx.Point(self.Pos.x, self.Pos.y), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1299 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1300 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y), |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1301 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])] |
0 | 1302 |
dc.DrawPolygon(points) |
1303 |
text_width, text_height = dc.GetTextExtent(self.Target) |
|
1304 |
dc.DrawText(self.Target, self.Pos.x + self.Size[0] + 2, |
|
1305 |
self.Pos.y + (self.Size[1] - text_height) / 2) |
|
1306 |
# Draw input connector |
|
1307 |
if self.Input: |
|
1308 |
self.Input.Draw(dc) |
|
1309 |
Graphic_Element.Draw(self, dc) |
|
1310 |
||
1311 |
||
1312 |
#------------------------------------------------------------------------------- |
|
1313 |
# Sequencial Function Chart Action Block |
|
1314 |
#------------------------------------------------------------------------------- |
|
1315 |
||
1316 |
""" |
|
1317 |
Class that implements the graphic representation of an action block |
|
1318 |
""" |
|
1319 |
||
1320 |
class SFC_ActionBlock(Graphic_Element): |
|
1321 |
||
1322 |
# Create a new action block |
|
1323 |
def __init__(self, parent, actions = [], id = None): |
|
1324 |
Graphic_Element.__init__(self, parent) |
|
1325 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1326 |
self.Size = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
108 | 1327 |
self.MinSize = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
0 | 1328 |
# Create an input and output connector |
108 | 1329 |
self.Input = Connector(self, "", None, wx.Point(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST) |
0 | 1330 |
self.SetActions(actions) |
1331 |
||
1332 |
# Destructor |
|
1333 |
def __del__(self): |
|
1334 |
self.Input = None |
|
1335 |
||
1336 |
# Returns the number of action lines |
|
1337 |
def GetLineNumber(self): |
|
1338 |
return len(self.Actions) |
|
1339 |
||
27 | 1340 |
def GetLineSize(self): |
108 | 1341 |
if len(self.Actions) > 0: |
27 | 1342 |
return self.Size[1] / len(self.Actions) |
1343 |
else: |
|
1344 |
return SFC_ACTION_MIN_SIZE[1] |
|
1345 |
||
0 | 1346 |
# Forbids to resize the action block |
1347 |
def Resize(self, x, y, width, height): |
|
27 | 1348 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1349 |
if x == 0: |
|
1350 |
self.SetSize(width, self.Size[1]) |
|
1351 |
else: |
|
1352 |
Graphic_Element.Resize(self, x, y, width, height) |
|
0 | 1353 |
|
1354 |
# Delete this action block by calling the appropriate method |
|
1355 |
def Delete(self): |
|
1356 |
self.Parent.DeleteActionBlock(self) |
|
1357 |
||
1358 |
# Unconnect input and output |
|
1359 |
def Clean(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1360 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 1361 |
|
1362 |
# Refresh the action block bounding box |
|
1363 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1364 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) |
0 | 1365 |
|
1366 |
# Refresh the position of wires connected to action block |
|
1367 |
def RefreshConnected(self, exclude = []): |
|
1368 |
self.Input.MoveConnected(exclude) |
|
1369 |
||
1370 |
# Returns input action block connector |
|
27 | 1371 |
def GetConnector(self, position = None, name = None): |
0 | 1372 |
return self.Input |
1373 |
||
1374 |
# Test if point given is on action block input connector |
|
1375 |
def TestConnector(self, pt, exclude = True): |
|
1376 |
# Test input connector |
|
1377 |
if self.Input.TestPoint(pt, exclude): |
|
1378 |
return self.Input |
|
1379 |
return None |
|
1380 |
||
1381 |
# Changes the action block actions |
|
1382 |
def SetActions(self, actions): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1383 |
dc = wx.ClientDC(self.Parent) |
0 | 1384 |
self.Actions = actions |
1385 |
self.ColSize = [0, 0, 0] |
|
108 | 1386 |
min_height = 0 |
0 | 1387 |
for action in self.Actions: |
1388 |
width, height = dc.GetTextExtent(action["qualifier"]) |
|
1389 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
|
108 | 1390 |
row_height = height |
0 | 1391 |
if "duration" in action: |
2 | 1392 |
width, height = dc.GetTextExtent(action["duration"]) |
108 | 1393 |
row_height = max(row_height, height) |
0 | 1394 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
1395 |
width, height = dc.GetTextExtent(action["value"]) |
|
108 | 1396 |
row_height = max(row_height, height) |
0 | 1397 |
self.ColSize[1] = max(self.ColSize[1], width + 10) |
1398 |
if "indicator" in action and action["indicator"] != "": |
|
1399 |
width, height = dc.GetTextExtent(action["indicator"]) |
|
108 | 1400 |
row_height = max(row_height, height) |
0 | 1401 |
self.ColSize[2] = max(self.ColSize[2], width + 10) |
108 | 1402 |
min_height += row_height + 5 |
27 | 1403 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
108 | 1404 |
self.Size = wx.Size(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], max(min_height, SFC_ACTION_MIN_SIZE[1], self.Size[1])) |
1405 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1406 |
SFC_ACTION_MIN_SIZE[0]), max(SFC_ACTION_MIN_SIZE[1], min_height) |
|
1407 |
self.RefreshBoundingBox() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1408 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1409 |
self.Size = wx.Size(max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
27 | 1410 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1]) |
108 | 1411 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
1412 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1] |
|
1413 |
self.RefreshBoundingBox() |
|
1414 |
if self.Input: |
|
1415 |
wires = self.Input.GetWires() |
|
1416 |
if len(wires) == 1: |
|
1417 |
input_block = wires[0][0].EndConnected.GetParentBlock() |
|
1418 |
input_block.RefreshOutputPosition() |
|
1419 |
input_block.RefreshOutputModel(True) |
|
0 | 1420 |
|
1421 |
# Returns the action block actions |
|
1422 |
def GetActions(self): |
|
1423 |
return self.Actions |
|
1424 |
||
1425 |
# Returns the action block minimum size |
|
1426 |
def GetMinSize(self): |
|
108 | 1427 |
return self.MinSize |
0 | 1428 |
|
1429 |
# Method called when a LeftDClick event have been generated |
|
27 | 1430 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 1431 |
# Edit the action block properties |
1432 |
self.Parent.EditActionBlockContent(self) |
|
1433 |
||
1434 |
# Refreshes the action block state according to move defined and handle selected |
|
1435 |
def ProcessDragging(self, movex, movey): |
|
27 | 1436 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
1437 |
handle_type, handle = self.Handle |
|
1438 |
if handle_type == HANDLE_MOVE: |
|
1439 |
wires = self.Input.GetWires() |
|
1440 |
if len(wires) == 1: |
|
1441 |
input_pos = wires[0][0].EndConnected.GetPosition(False) |
|
1442 |
if self.Pos.x - input_pos.x + movex >= SFC_WIRE_MIN_SIZE: |
|
1443 |
self.Move(movex, 0) |
|
1444 |
else: |
|
1445 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
0 | 1446 |
else: |
1447 |
Graphic_Element.ProcessDragging(self, movex, movey) |
|
27 | 1448 |
|
0 | 1449 |
|
1450 |
# Refreshes the action block model |
|
1451 |
def RefreshModel(self, move=True): |
|
1452 |
self.Parent.RefreshActionBlockModel(self) |
|
1453 |
||
1454 |
# Draws divergence |
|
1455 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1456 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
1457 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 1458 |
colsize = [self.ColSize[0], self.Size[0] - self.ColSize[0] - self.ColSize[2], self.ColSize[2]] |
1459 |
# Draw plain rectangle for representing the action block |
|
1460 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1461 |
dc.DrawLine(self.Pos.x + colsize[0], self.Pos.y, |
|
1462 |
self.Pos.x + colsize[0], self.Pos.y + self.Size[1]) |
|
1463 |
dc.DrawLine(self.Pos.x + colsize[0] + colsize[1], self.Pos.y, |
|
1464 |
self.Pos.x + colsize[0] + colsize[1], self.Pos.y + self.Size[1]) |
|
27 | 1465 |
line_size = self.GetLineSize() |
0 | 1466 |
for i, action in enumerate(self.Actions): |
1467 |
if i != 0: |
|
27 | 1468 |
dc.DrawLine(self.Pos.x, self.Pos.y + i * line_size, |
1469 |
self.Pos.x + self.Size[0], self.Pos.y + i * line_size) |
|
0 | 1470 |
text_width, text_height = dc.GetTextExtent(action["qualifier"]) |
1471 |
if "duration" in action: |
|
1472 |
dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2, |
|
27 | 1473 |
self.Pos.y + i * line_size + line_size / 2 - text_height) |
1 | 1474 |
text_width, text_height = dc.GetTextExtent(action["duration"]) |
1475 |
dc.DrawText(action["duration"], self.Pos.x + (colsize[0] - text_width) / 2, |
|
27 | 1476 |
self.Pos.y + i * line_size + line_size / 2) |
0 | 1477 |
else: |
1478 |
dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2, |
|
27 | 1479 |
self.Pos.y + i * line_size + (line_size - text_height) / 2) |
0 | 1480 |
text_width, text_height = dc.GetTextExtent(action["value"]) |
1481 |
dc.DrawText(action["value"], self.Pos.x + colsize[0] + (colsize[1] - text_width) / 2, |
|
27 | 1482 |
self.Pos.y + i * line_size + (line_size - text_height) / 2) |
0 | 1483 |
if "indicator" in action: |
1484 |
text_width, text_height = dc.GetTextExtent(action["indicator"]) |
|
1485 |
dc.DrawText(action["indicator"], self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - text_width) / 2, |
|
27 | 1486 |
self.Pos.y + i * line_size + (line_size - text_height) / 2) |
0 | 1487 |
# Draw input connector |
1488 |
self.Input.Draw(dc) |
|
1489 |
Graphic_Element.Draw(self, dc) |
|
27 | 1490 |