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