author | Laurent Bessard |
Wed, 29 May 2013 22:27:20 +0200 | |
changeset 1193 | 59c196884fec |
parent 1176 | f4b434672204 |
child 1258 | 441f31474b50 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
||
1176
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset
|
27 |
from graphics.GraphicCommons import * |
f4b434672204
Moved and rewrote DebugViewer and DebusDataConsumer classes
Laurent Bessard
parents:
814
diff
changeset
|
28 |
from graphics.DebugDataConsumer import DebugDataConsumer |
814 | 29 |
from plcopen.structures import * |
30 |
||
31 |
def GetWireSize(block): |
|
32 |
if isinstance(block, SFC_Step): |
|
33 |
return SFC_WIRE_MIN_SIZE + block.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1] |
|
34 |
else: |
|
35 |
return SFC_WIRE_MIN_SIZE |
|
36 |
||
37 |
#------------------------------------------------------------------------------- |
|
38 |
# Sequencial Function Chart Step |
|
39 |
#------------------------------------------------------------------------------- |
|
40 |
||
41 |
""" |
|
42 |
Class that implements the graphic representation of a step |
|
43 |
""" |
|
44 |
||
45 |
class SFC_Step(Graphic_Element, DebugDataConsumer): |
|
46 |
||
47 |
# Create a new step |
|
48 |
def __init__(self, parent, name, initial = False, id = None): |
|
49 |
Graphic_Element.__init__(self, parent) |
|
50 |
DebugDataConsumer.__init__(self) |
|
51 |
self.SetName(name) |
|
52 |
self.Initial = initial |
|
53 |
self.Id = id |
|
54 |
self.Highlights = [] |
|
55 |
self.Size = wx.Size(SFC_STEP_DEFAULT_SIZE[0], SFC_STEP_DEFAULT_SIZE[1]) |
|
56 |
# Create an input and output connector |
|
57 |
if not self.Initial: |
|
58 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
|
59 |
else: |
|
60 |
self.Input = None |
|
61 |
self.Output = None |
|
62 |
self.Action = None |
|
63 |
self.PreviousValue = None |
|
64 |
self.PreviousSpreading = False |
|
65 |
||
66 |
def Flush(self): |
|
67 |
if self.Input is not None: |
|
68 |
self.Input.Flush() |
|
69 |
self.Input = None |
|
70 |
if self.Output is not None: |
|
71 |
self.Output.Flush() |
|
72 |
self.Output = None |
|
73 |
if self.Output is not None: |
|
74 |
self.Action.Flush() |
|
75 |
self.Action = None |
|
76 |
||
77 |
def SetForced(self, forced): |
|
78 |
if self.Forced != forced: |
|
79 |
self.Forced = forced |
|
80 |
if self.Visible: |
|
81 |
self.Parent.ElementNeedRefresh(self) |
|
82 |
||
83 |
def SetValue(self, value): |
|
84 |
self.PreviousValue = self.Value |
|
85 |
self.Value = value |
|
86 |
if self.Value != self.PreviousValue: |
|
87 |
if self.Visible: |
|
88 |
self.Parent.ElementNeedRefresh(self) |
|
89 |
self.SpreadCurrent() |
|
90 |
||
91 |
def SpreadCurrent(self): |
|
92 |
if self.Parent.Debug: |
|
93 |
spreading = self.Value |
|
94 |
if spreading and not self.PreviousSpreading: |
|
95 |
if self.Output is not None: |
|
96 |
self.Output.SpreadCurrent(True) |
|
97 |
if self.Action is not None: |
|
98 |
self.Action.SpreadCurrent(True) |
|
99 |
elif not spreading and self.PreviousSpreading: |
|
100 |
if self.Output is not None: |
|
101 |
self.Output.SpreadCurrent(False) |
|
102 |
if self.Action is not None: |
|
103 |
self.Action.SpreadCurrent(False) |
|
104 |
self.PreviousSpreading = spreading |
|
105 |
||
106 |
# Make a clone of this SFC_Step |
|
107 |
def Clone(self, parent, id = None, name = "Step", pos = None): |
|
108 |
step = SFC_Step(parent, name, self.Initial, id) |
|
109 |
step.SetSize(self.Size[0], self.Size[1]) |
|
110 |
if pos is not None: |
|
111 |
step.SetPosition(pos.x, pos.y) |
|
112 |
else: |
|
113 |
step.SetPosition(self.Pos.x, self.Pos.y) |
|
114 |
if self.Input: |
|
115 |
step.Input = self.Input.Clone(step) |
|
116 |
if self.Output: |
|
117 |
step.Output = self.Output.Clone(step) |
|
118 |
if self.Action: |
|
119 |
step.Action = self.Action.Clone(step) |
|
120 |
return step |
|
121 |
||
122 |
def GetConnectorTranslation(self, element): |
|
123 |
connectors = {} |
|
124 |
if self.Input is not None: |
|
125 |
connectors[self.Input] = element.Input |
|
126 |
if self.Output is not None: |
|
127 |
connectors[self.Output] = element.Output |
|
128 |
if self.Action is not None: |
|
129 |
connectors[self.Action] = element.Action |
|
130 |
return connectors |
|
131 |
||
132 |
# Returns the RedrawRect |
|
133 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
134 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
135 |
if self.Input: |
|
136 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
137 |
if self.Output: |
|
138 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
139 |
if self.Action: |
|
140 |
rect = rect.Union(self.Action.GetRedrawRect(movex, movey)) |
|
141 |
if movex != 0 or movey != 0: |
|
142 |
if self.Input and self.Input.IsConnected(): |
|
143 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
144 |
if self.Output and self.Output.IsConnected(): |
|
145 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
146 |
if self.Action and self.Action.IsConnected(): |
|
147 |
rect = rect.Union(self.Action.GetConnectedRedrawRect(movex, movey)) |
|
148 |
return rect |
|
149 |
||
150 |
# Delete this step by calling the appropriate method |
|
151 |
def Delete(self): |
|
152 |
self.Parent.DeleteStep(self) |
|
153 |
||
154 |
# Unconnect input and output |
|
155 |
def Clean(self): |
|
156 |
if self.Input: |
|
157 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
158 |
if self.Output: |
|
159 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
160 |
if self.Action: |
|
161 |
self.Action.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
162 |
||
163 |
# Refresh the size of text for name |
|
164 |
def RefreshNameSize(self): |
|
165 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
|
166 |
||
167 |
# Add output connector to step |
|
168 |
def AddInput(self): |
|
169 |
if not self.Input: |
|
170 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH) |
|
171 |
self.RefreshBoundingBox() |
|
172 |
||
173 |
# Remove output connector from step |
|
174 |
def RemoveInput(self): |
|
175 |
if self.Input: |
|
176 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
177 |
self.Input = None |
|
178 |
self.RefreshBoundingBox() |
|
179 |
||
180 |
# Add output connector to step |
|
181 |
def AddOutput(self): |
|
182 |
if not self.Output: |
|
183 |
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH, onlyone = True) |
|
184 |
self.RefreshBoundingBox() |
|
185 |
||
186 |
# Remove output connector from step |
|
187 |
def RemoveOutput(self): |
|
188 |
if self.Output: |
|
189 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
190 |
self.Output = None |
|
191 |
self.RefreshBoundingBox() |
|
192 |
||
193 |
# Add action connector to step |
|
194 |
def AddAction(self): |
|
195 |
if not self.Action: |
|
196 |
self.Action = Connector(self, "", None, wx.Point(self.Size[0], self.Size[1] / 2), EAST, onlyone = True) |
|
197 |
self.RefreshBoundingBox() |
|
198 |
||
199 |
# Remove action connector from step |
|
200 |
def RemoveAction(self): |
|
201 |
if self.Action: |
|
202 |
self.Action.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
203 |
self.Action = None |
|
204 |
self.RefreshBoundingBox() |
|
205 |
||
206 |
# Refresh the step bounding box |
|
207 |
def RefreshBoundingBox(self): |
|
208 |
# Calculate the bounding box size |
|
209 |
if self.Action: |
|
210 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
211 |
else: |
|
212 |
bbx_width = self.Size[0] |
|
213 |
if self.Initial: |
|
214 |
bbx_y = self.Pos.y |
|
215 |
bbx_height = self.Size[1] |
|
216 |
if self.Output: |
|
217 |
bbx_height += CONNECTOR_SIZE |
|
218 |
else: |
|
219 |
bbx_y = self.Pos.y - CONNECTOR_SIZE |
|
220 |
bbx_height = self.Size[1] + CONNECTOR_SIZE |
|
221 |
if self.Output: |
|
222 |
bbx_height += CONNECTOR_SIZE |
|
223 |
#self.BoundingBox = wx.Rect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
224 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
225 |
||
226 |
# Refresh the positions of the step connectors |
|
227 |
def RefreshConnectors(self): |
|
228 |
scaling = self.Parent.GetScaling() |
|
229 |
horizontal_pos = self.Size[0] / 2 |
|
230 |
vertical_pos = self.Size[1] / 2 |
|
231 |
if scaling is not None: |
|
232 |
horizontal_pos = round(float(self.Pos.x + horizontal_pos) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
233 |
vertical_pos = round(float(self.Pos.y + vertical_pos) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
234 |
# Update input position if it exists |
|
235 |
if self.Input: |
|
236 |
self.Input.SetPosition(wx.Point(horizontal_pos, 0)) |
|
237 |
# Update output position |
|
238 |
if self.Output: |
|
239 |
self.Output.SetPosition(wx.Point(horizontal_pos, self.Size[1])) |
|
240 |
# Update action position if it exists |
|
241 |
if self.Action: |
|
242 |
self.Action.SetPosition(wx.Point(self.Size[0], vertical_pos)) |
|
243 |
self.RefreshConnected() |
|
244 |
||
245 |
# Refresh the position of wires connected to step |
|
246 |
def RefreshConnected(self, exclude = []): |
|
247 |
if self.Input: |
|
248 |
self.Input.MoveConnected(exclude) |
|
249 |
if self.Output: |
|
250 |
self.Output.MoveConnected(exclude) |
|
251 |
if self.Action: |
|
252 |
self.Action.MoveConnected(exclude) |
|
253 |
||
254 |
# Returns the step connector that starts with the point given if it exists |
|
255 |
def GetConnector(self, position, name = None): |
|
256 |
# if a name is given |
|
257 |
if name is not None: |
|
258 |
# Test input, output and action connector if they exists |
|
259 |
#if self.Input and name == self.Input.GetName(): |
|
260 |
# return self.Input |
|
261 |
if self.Output and name == self.Output.GetName(): |
|
262 |
return self.Output |
|
263 |
if self.Action and name == self.Action.GetName(): |
|
264 |
return self.Action |
|
265 |
connectors = [] |
|
266 |
# Test input connector if it exists |
|
267 |
if self.Input: |
|
268 |
connectors.append(self.Input) |
|
269 |
# Test output connector if it exists |
|
270 |
if self.Output: |
|
271 |
connectors.append(self.Output) |
|
272 |
# Test action connector if it exists |
|
273 |
if self.Action: |
|
274 |
connectors.append(self.Action) |
|
275 |
return self.FindNearestConnector(position, connectors) |
|
276 |
||
277 |
# Returns action step connector |
|
278 |
def GetActionConnector(self): |
|
279 |
return self.Action |
|
280 |
||
281 |
# Returns input and output step connectors |
|
282 |
def GetConnectors(self): |
|
283 |
connectors = {"inputs": [], "outputs": []} |
|
284 |
if self.Input: |
|
285 |
connectors["inputs"].append(self.Input) |
|
286 |
if self.Output: |
|
287 |
connectors["outputs"].append(self.Output) |
|
288 |
return connectors |
|
289 |
||
290 |
# Test if point given is on step input or output connector |
|
291 |
def TestConnector(self, pt, direction = None, exclude=True): |
|
292 |
# Test input connector if it exists |
|
293 |
if self.Input and self.Input.TestPoint(pt, direction, exclude): |
|
294 |
return self.Input |
|
295 |
# Test output connector |
|
296 |
if self.Output and self.Output.TestPoint(pt, direction, exclude): |
|
297 |
return self.Output |
|
298 |
# Test action connector |
|
299 |
if self.Action and self.Action.TestPoint(pt, direction, exclude): |
|
300 |
return self.Action |
|
301 |
return None |
|
302 |
||
303 |
# Changes the step name |
|
304 |
def SetName(self, name): |
|
305 |
self.Name = name |
|
306 |
self.RefreshNameSize() |
|
307 |
||
308 |
# Returns the step name |
|
309 |
def GetName(self): |
|
310 |
return self.Name |
|
311 |
||
312 |
# Returns the step initial property |
|
313 |
def GetInitial(self): |
|
314 |
return self.Initial |
|
315 |
||
316 |
# Returns the connector connected to input |
|
317 |
def GetPreviousConnector(self): |
|
318 |
if self.Input: |
|
319 |
wires = self.Input.GetWires() |
|
320 |
if len(wires) == 1: |
|
321 |
return wires[0][0].GetOtherConnected(self.Input) |
|
322 |
return None |
|
323 |
||
324 |
# Returns the connector connected to output |
|
325 |
def GetNextConnector(self): |
|
326 |
if self.Output: |
|
327 |
wires = self.Output.GetWires() |
|
328 |
if len(wires) == 1: |
|
329 |
return wires[0][0].GetOtherConnected(self.Output) |
|
330 |
return None |
|
331 |
||
332 |
# Returns the connector connected to action |
|
333 |
def GetActionConnected(self): |
|
334 |
if self.Action: |
|
335 |
wires = self.Action.GetWires() |
|
336 |
if len(wires) == 1: |
|
337 |
return wires[0][0].GetOtherConnected(self.Action) |
|
338 |
return None |
|
339 |
||
340 |
# Returns the number of action line |
|
341 |
def GetActionExtraLineNumber(self): |
|
342 |
if self.Action: |
|
343 |
wires = self.Action.GetWires() |
|
344 |
if len(wires) != 1: |
|
345 |
return 0 |
|
346 |
action_block = wires[0][0].GetOtherConnected(self.Action).GetParentBlock() |
|
347 |
return max(0, action_block.GetLineNumber() - 1) |
|
348 |
return 0 |
|
349 |
||
350 |
# Returns the step minimum size |
|
351 |
def GetMinSize(self): |
|
352 |
text_width, text_height = self.Parent.GetTextExtent(self.Name) |
|
353 |
if self.Initial: |
|
354 |
return text_width + 14, text_height + 14 |
|
355 |
else: |
|
356 |
return text_width + 10, text_height + 10 |
|
357 |
||
358 |
# Updates the step size |
|
359 |
def UpdateSize(self, width, height): |
|
360 |
diffx = self.Size.GetWidth() / 2 - width / 2 |
|
361 |
diffy = height - self.Size.GetHeight() |
|
362 |
self.Move(diffx, 0) |
|
363 |
Graphic_Element.SetSize(self, width, height) |
|
364 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
365 |
self.RefreshConnected() |
|
366 |
else: |
|
367 |
self.RefreshOutputPosition((0, diffy)) |
|
368 |
||
369 |
# Align input element with this step |
|
370 |
def RefreshInputPosition(self): |
|
371 |
if self.Input: |
|
372 |
current_pos = self.Input.GetPosition(False) |
|
373 |
input = self.GetPreviousConnector() |
|
374 |
if input: |
|
375 |
input_pos = input.GetPosition(False) |
|
376 |
diffx = current_pos.x - input_pos.x |
|
377 |
input_block = input.GetParentBlock() |
|
378 |
if isinstance(input_block, SFC_Divergence): |
|
379 |
input_block.MoveConnector(input, diffx) |
|
380 |
else: |
|
381 |
if isinstance(input_block, SFC_Step): |
|
382 |
input_block.MoveActionBlock((diffx, 0)) |
|
383 |
input_block.Move(diffx, 0) |
|
384 |
input_block.RefreshInputPosition() |
|
385 |
||
386 |
# Align output element with this step |
|
387 |
def RefreshOutputPosition(self, move = None): |
|
388 |
if self.Output: |
|
389 |
wires = self.Output.GetWires() |
|
390 |
if len(wires) != 1: |
|
391 |
return |
|
392 |
current_pos = self.Output.GetPosition(False) |
|
393 |
output = wires[0][0].GetOtherConnected(self.Output) |
|
394 |
output_pos = output.GetPosition(False) |
|
395 |
diffx = current_pos.x - output_pos.x |
|
396 |
output_block = output.GetParentBlock() |
|
397 |
wire_size = SFC_WIRE_MIN_SIZE + self.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1] |
|
398 |
diffy = wire_size - output_pos.y + current_pos.y |
|
399 |
if diffy != 0: |
|
400 |
if isinstance(output_block, SFC_Step): |
|
401 |
output_block.MoveActionBlock((diffx, diffy)) |
|
402 |
wires[0][0].SetPoints([wx.Point(current_pos.x, current_pos.y + wire_size), |
|
403 |
wx.Point(current_pos.x, current_pos.y)]) |
|
404 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
405 |
output_block.Move(diffx, diffy, self.Parent.Wires) |
|
406 |
output_block.RefreshOutputPosition((diffx, diffy)) |
|
407 |
else: |
|
408 |
output_block.RefreshPosition() |
|
409 |
elif move: |
|
410 |
if isinstance(output_block, SFC_Step): |
|
411 |
output_block.MoveActionBlock(move) |
|
412 |
wires[0][0].Move(move[0], move[1], True) |
|
413 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
414 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
415 |
output_block.RefreshOutputPosition(move) |
|
416 |
else: |
|
417 |
output_block.RefreshPosition() |
|
418 |
elif isinstance(output_block, SFC_Divergence): |
|
419 |
output_block.MoveConnector(output, diffx) |
|
420 |
else: |
|
421 |
if isinstance(output_block, SFC_Step): |
|
422 |
output_block.MoveActionBlock((diffx, 0)) |
|
423 |
output_block.Move(diffx, 0) |
|
424 |
output_block.RefreshOutputPosition() |
|
425 |
||
426 |
# Refresh action element with this step |
|
427 |
def MoveActionBlock(self, move): |
|
428 |
if self.Action: |
|
429 |
wires = self.Action.GetWires() |
|
430 |
if len(wires) != 1: |
|
431 |
return |
|
432 |
action_block = wires[0][0].GetOtherConnected(self.Action).GetParentBlock() |
|
433 |
action_block.Move(move[0], move[1], self.Parent.Wires) |
|
434 |
wires[0][0].Move(move[0], move[1], True) |
|
435 |
||
436 |
# Resize the divergence from position and size given |
|
437 |
def Resize(self, x, y, width, height): |
|
438 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
439 |
self.UpdateSize(width, height) |
|
440 |
else: |
|
441 |
Graphic_Element.Resize(self, x, y, width, height) |
|
442 |
||
443 |
# Method called when a LeftDClick event have been generated |
|
444 |
def OnLeftDClick(self, event, dc, scaling): |
|
445 |
# Edit the step properties |
|
446 |
self.Parent.EditStepContent(self) |
|
447 |
||
448 |
# Method called when a RightUp event have been generated |
|
449 |
def OnRightUp(self, event, dc, scaling): |
|
450 |
# Popup the menu with special items for a step |
|
451 |
self.Parent.PopupDefaultMenu() |
|
452 |
||
453 |
# Refreshes the step state according to move defined and handle selected |
|
454 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
455 |
handle_type, handle = self.Handle |
|
456 |
if handle_type == HANDLE_MOVE: |
|
457 |
movex = max(-self.BoundingBox.x, movex) |
|
458 |
movey = max(-self.BoundingBox.y, movey) |
|
459 |
if scaling is not None: |
|
460 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
461 |
movey = round(float(self.Pos.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
462 |
action_block = None |
|
463 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
464 |
self.Move(movex, movey) |
|
465 |
self.RefreshConnected() |
|
466 |
return movex, movey |
|
467 |
elif self.Initial: |
|
468 |
self.MoveActionBlock((movex, movey)) |
|
469 |
self.Move(movex, movey, self.Parent.Wires) |
|
470 |
self.RefreshOutputPosition((movex, movey)) |
|
471 |
return movex, movey |
|
472 |
else: |
|
473 |
self.MoveActionBlock((movex, 0)) |
|
474 |
self.Move(movex, 0) |
|
475 |
self.RefreshInputPosition() |
|
476 |
self.RefreshOutputPosition() |
|
477 |
return movex, 0 |
|
478 |
else: |
|
479 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
480 |
||
481 |
# Refresh input element model |
|
482 |
def RefreshInputModel(self): |
|
483 |
if self.Input: |
|
484 |
input = self.GetPreviousConnector() |
|
485 |
if input: |
|
486 |
input_block = input.GetParentBlock() |
|
487 |
input_block.RefreshModel(False) |
|
488 |
if not isinstance(input_block, SFC_Divergence): |
|
489 |
input_block.RefreshInputModel() |
|
490 |
||
491 |
# Refresh output element model |
|
492 |
def RefreshOutputModel(self, move=False): |
|
493 |
if self.Output: |
|
494 |
output = self.GetNextConnector() |
|
495 |
if output: |
|
496 |
output_block = output.GetParentBlock() |
|
497 |
output_block.RefreshModel(False) |
|
498 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
499 |
output_block.RefreshOutputModel(move) |
|
500 |
||
501 |
# Refreshes the step model |
|
502 |
def RefreshModel(self, move=True): |
|
503 |
self.Parent.RefreshStepModel(self) |
|
504 |
if self.Action: |
|
505 |
action = self.GetActionConnected() |
|
506 |
if action: |
|
507 |
action_block = action.GetParentBlock() |
|
508 |
action_block.RefreshModel(False) |
|
509 |
# If step has moved, refresh the model of wires connected to output |
|
510 |
if move: |
|
511 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
512 |
self.RefreshInputModel() |
|
513 |
self.RefreshOutputModel(self.Initial) |
|
514 |
elif self.Output: |
|
515 |
self.Output.RefreshWires() |
|
516 |
||
517 |
# Adds an highlight to the connection |
|
518 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
519 |
if infos[0] == "name" and start[0] == 0 and end[0] == 0: |
|
520 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
521 |
||
522 |
# Removes an highlight from the connection |
|
523 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
524 |
if infos[0] == "name": |
|
525 |
RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
|
526 |
||
527 |
# Removes all the highlights of one particular type from the connection |
|
528 |
def ClearHighlight(self, highlight_type=None): |
|
529 |
ClearHighlights(self.Highlights, highlight_type) |
|
530 |
||
531 |
# Draws step |
|
532 |
def Draw(self, dc): |
|
533 |
Graphic_Element.Draw(self, dc) |
|
534 |
if self.Value: |
|
535 |
if self.Forced: |
|
536 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
537 |
else: |
|
538 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
539 |
elif self.Forced: |
|
540 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
541 |
else: |
|
542 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
543 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
544 |
||
545 |
if getattr(dc, "printing", False): |
|
546 |
name_size = dc.GetTextExtent(self.Name) |
|
547 |
else: |
|
548 |
name_size = self.NameSize |
|
549 |
||
550 |
# Draw two rectangles for representing the step |
|
551 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
552 |
if self.Initial: |
|
553 |
dc.DrawRectangle(self.Pos.x + 2, self.Pos.y + 2, self.Size[0] - 3, self.Size[1] - 3) |
|
554 |
# Draw step name |
|
555 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
|
556 |
self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
|
557 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
|
558 |
# Draw input and output connectors |
|
559 |
if self.Input: |
|
560 |
self.Input.Draw(dc) |
|
561 |
if self.Output: |
|
562 |
self.Output.Draw(dc) |
|
563 |
if self.Action: |
|
564 |
self.Action.Draw(dc) |
|
565 |
||
566 |
if not getattr(dc, "printing", False): |
|
567 |
DrawHighlightedText(dc, self.Name, self.Highlights, name_pos[0], name_pos[1]) |
|
568 |
||
569 |
||
570 |
#------------------------------------------------------------------------------- |
|
571 |
# Sequencial Function Chart Transition |
|
572 |
#------------------------------------------------------------------------------- |
|
573 |
||
574 |
""" |
|
575 |
Class that implements the graphic representation of a transition |
|
576 |
""" |
|
577 |
||
578 |
class SFC_Transition(Graphic_Element, DebugDataConsumer): |
|
579 |
||
580 |
# Create a new transition |
|
581 |
def __init__(self, parent, type = "reference", condition = None, priority = 0, id = None): |
|
582 |
Graphic_Element.__init__(self, parent) |
|
583 |
DebugDataConsumer.__init__(self) |
|
584 |
self.Type = None |
|
585 |
self.Id = id |
|
586 |
self.Priority = 0 |
|
587 |
self.Size = wx.Size(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1]) |
|
588 |
# Create an input and output connector |
|
589 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone = True) |
|
590 |
self.Output = Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH, onlyone = True) |
|
591 |
self.SetType(type, condition) |
|
592 |
self.SetPriority(priority) |
|
593 |
self.Highlights = {} |
|
594 |
self.PreviousValue = None |
|
595 |
self.PreviousSpreading = False |
|
596 |
||
597 |
def Flush(self): |
|
598 |
if self.Input is not None: |
|
599 |
self.Input.Flush() |
|
600 |
self.Input = None |
|
601 |
if self.Output is not None: |
|
602 |
self.Output.Flush() |
|
603 |
self.Output = None |
|
604 |
if self.Type == "connection" and self.Condition is not None: |
|
605 |
self.Condition.Flush() |
|
606 |
self.Condition = None |
|
607 |
||
608 |
def SetForced(self, forced): |
|
609 |
if self.Forced != forced: |
|
610 |
self.Forced = forced |
|
611 |
if self.Visible: |
|
612 |
self.Parent.ElementNeedRefresh(self) |
|
613 |
||
614 |
def SetValue(self, value): |
|
615 |
self.PreviousValue = self.Value |
|
616 |
self.Value = value |
|
617 |
if self.Value != self.PreviousValue: |
|
618 |
if self.Visible: |
|
619 |
self.Parent.ElementNeedRefresh(self) |
|
620 |
self.SpreadCurrent() |
|
621 |
||
622 |
def SpreadCurrent(self): |
|
623 |
if self.Parent.Debug: |
|
624 |
if self.Value is None: |
|
625 |
self.Value = False |
|
626 |
spreading = self.Input.ReceivingCurrent() & self.Value |
|
627 |
if spreading and not self.PreviousSpreading: |
|
628 |
self.Output.SpreadCurrent(True) |
|
629 |
elif not spreading and self.PreviousSpreading: |
|
630 |
self.Output.SpreadCurrent(False) |
|
631 |
self.PreviousSpreading = spreading |
|
632 |
||
633 |
# Make a clone of this SFC_Transition |
|
634 |
def Clone(self, parent, id = None, pos = None): |
|
635 |
transition = SFC_Transition(parent, self.Type, self.Condition, self.Priority, id) |
|
636 |
transition.SetSize(self.Size[0], self.Size[1]) |
|
637 |
if pos is not None: |
|
638 |
transition.SetPosition(pos.x, pos.y) |
|
639 |
else: |
|
640 |
transition.SetPosition(self.Pos.x, self.Pos.y) |
|
641 |
transition.Input = self.Input.Clone(transition) |
|
642 |
transition.Output = self.Output.Clone(transition) |
|
643 |
if self.Type == "connection": |
|
644 |
transition.Condition = self.Condition.Clone(transition) |
|
645 |
return transition |
|
646 |
||
647 |
def GetConnectorTranslation(self, element): |
|
648 |
connectors = {self.Input : element.Input, self.Output : element.Output} |
|
649 |
if self.Type == "connection" and self.Condition is not None: |
|
650 |
connectors[self.Condition] = element.Condition |
|
651 |
return connectors |
|
652 |
||
653 |
# Returns the RedrawRect |
|
654 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
655 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
656 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
657 |
rect = rect.Union(self.Output.GetRedrawRect(movex, movey)) |
|
658 |
if movex != 0 or movey != 0: |
|
659 |
if self.Input.IsConnected(): |
|
660 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
661 |
if self.Output.IsConnected(): |
|
662 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
663 |
if self.Type == "connection" and self.Condition.IsConnected(): |
|
664 |
rect = rect.Union(self.Condition.GetConnectedRedrawRect(movex, movey)) |
|
665 |
return rect |
|
666 |
||
667 |
# Forbids to change the transition size |
|
668 |
def SetSize(self, width, height): |
|
669 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
670 |
Graphic_Element.SetSize(self, width, height) |
|
671 |
||
672 |
# Forbids to resize the transition |
|
673 |
def Resize(self, x, y, width, height): |
|
674 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
675 |
Graphic_Element.Resize(self, x, y, width, height) |
|
676 |
||
677 |
# Refresh the size of text for name |
|
678 |
def RefreshConditionSize(self): |
|
679 |
if self.Type != "connection": |
|
680 |
if self.Condition != "": |
|
681 |
self.ConditionSize = self.Parent.GetTextExtent(self.Condition) |
|
682 |
else: |
|
683 |
self.ConditionSize = self.Parent.GetTextExtent("Transition") |
|
684 |
||
685 |
# Refresh the size of text for name |
|
686 |
def RefreshPrioritySize(self): |
|
687 |
if self.Priority != "": |
|
688 |
self.PrioritySize = self.Parent.GetTextExtent(str(self.Priority)) |
|
689 |
else: |
|
690 |
self.PrioritySize = None |
|
691 |
||
692 |
# Delete this transition by calling the appropriate method |
|
693 |
def Delete(self): |
|
694 |
self.Parent.DeleteTransition(self) |
|
695 |
||
696 |
# Unconnect input and output |
|
697 |
def Clean(self): |
|
698 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
699 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
700 |
if self.Type == "connection": |
|
701 |
self.Condition.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
702 |
||
703 |
# Returns if the point given is in the bounding box |
|
704 |
def HitTest(self, pt, connectors=True): |
|
705 |
if self.Type != "connection": |
|
706 |
# Calculate the bounding box of the condition outside the transition |
|
707 |
text_width, text_height = self.ConditionSize |
|
708 |
text_bbx = wx.Rect(self.Pos.x + self.Size[0] + 5, |
|
709 |
self.Pos.y + (self.Size[1] - text_height) / 2, |
|
710 |
text_width, |
|
711 |
text_height) |
|
712 |
test_text = text_bbx.InsideXY(pt.x, pt.y) |
|
713 |
else: |
|
714 |
test_text = False |
|
715 |
return test_text or Graphic_Element.HitTest(self, pt, connectors) |
|
716 |
||
717 |
# Refresh the transition bounding box |
|
718 |
def RefreshBoundingBox(self): |
|
719 |
bbx_x, bbx_y, bbx_width, bbx_height = self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] |
|
720 |
if self.Priority != 0: |
|
721 |
bbx_y = self.Pos.y - self.PrioritySize[1] - 2 |
|
722 |
bbx_width = max(self.Size[0], self.PrioritySize[0]) |
|
723 |
bbx_height = self.Size[1] + self.PrioritySize[1] + 2 |
|
724 |
if self.Type == "connection": |
|
725 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
726 |
bbx_width = bbx_width + CONNECTOR_SIZE |
|
727 |
else: |
|
728 |
text_width, text_height = self.ConditionSize |
|
729 |
# Calculate the bounding box size |
|
730 |
bbx_width = max(bbx_width, self.Size[0] + 5 + text_width) |
|
731 |
bbx_y = min(bbx_y, self.Pos.y - max(0, (text_height - self.Size[1]) / 2)) |
|
732 |
bbx_height = max(bbx_height, self.Pos.y - bbx_y + (self.Size[1] + text_height) / 2) |
|
733 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
734 |
||
735 |
# Returns the connector connected to input |
|
736 |
def GetPreviousConnector(self): |
|
737 |
wires = self.Input.GetWires() |
|
738 |
if len(wires) == 1: |
|
739 |
return wires[0][0].GetOtherConnected(self.Input) |
|
740 |
return None |
|
741 |
||
742 |
# Returns the connector connected to output |
|
743 |
def GetNextConnector(self): |
|
744 |
wires = self.Output.GetWires() |
|
745 |
if len(wires) == 1: |
|
746 |
return wires[0][0].GetOtherConnected(self.Output) |
|
747 |
return None |
|
748 |
||
749 |
# Refresh the positions of the transition connectors |
|
750 |
def RefreshConnectors(self): |
|
751 |
scaling = self.Parent.GetScaling() |
|
752 |
horizontal_pos = self.Size[0] / 2 |
|
753 |
vertical_pos = self.Size[1] / 2 |
|
754 |
if scaling is not None: |
|
755 |
horizontal_pos = round(float(self.Pos.x + horizontal_pos) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
756 |
vertical_pos = round(float(self.Pos.y + vertical_pos) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
757 |
# Update input position |
|
758 |
self.Input.SetPosition(wx.Point(horizontal_pos, 0)) |
|
759 |
# Update output position |
|
760 |
self.Output.SetPosition(wx.Point(horizontal_pos, self.Size[1])) |
|
761 |
if self.Type == "connection": |
|
762 |
self.Condition.SetPosition(wx.Point(0, vertical_pos)) |
|
763 |
self.RefreshConnected() |
|
764 |
||
765 |
# Refresh the position of the wires connected to transition |
|
766 |
def RefreshConnected(self, exclude = []): |
|
767 |
self.Input.MoveConnected(exclude) |
|
768 |
self.Output.MoveConnected(exclude) |
|
769 |
if self.Type == "connection": |
|
770 |
self.Condition.MoveConnected(exclude) |
|
771 |
||
772 |
# Returns the transition connector that starts with the point given if it exists |
|
773 |
def GetConnector(self, position, name = None): |
|
774 |
# if a name is given |
|
775 |
if name is not None: |
|
776 |
# Test input and output connector |
|
777 |
#if name == self.Input.GetName(): |
|
778 |
# return self.Input |
|
779 |
if name == self.Output.GetName(): |
|
780 |
return self.Output |
|
781 |
if self.Type == "connection" and name == self.Condition.GetName(): |
|
782 |
return self.Condition |
|
783 |
connectors = [self.Input, self.Output] |
|
784 |
if self.Type == "connection": |
|
785 |
connectors.append(self.Condition) |
|
786 |
return self.FindNearestConnector(position, connectors) |
|
787 |
||
788 |
# Returns the transition condition connector |
|
789 |
def GetConditionConnector(self): |
|
790 |
if self.Type == "connection": |
|
791 |
return self.Condition |
|
792 |
return None |
|
793 |
||
794 |
# Returns input and output transition connectors |
|
795 |
def GetConnectors(self): |
|
796 |
return {"inputs": [self.Input], "outputs": [self.Output]} |
|
797 |
||
798 |
# Test if point given is on transition input or output connector |
|
799 |
def TestConnector(self, pt, direction = None, exclude=True): |
|
800 |
# Test input connector |
|
801 |
if self.Input.TestPoint(pt, direction, exclude): |
|
802 |
return self.Input |
|
803 |
# Test output connector |
|
804 |
if self.Output.TestPoint(pt, direction, exclude): |
|
805 |
return self.Output |
|
806 |
# Test condition connector |
|
807 |
if self.Type == "connection" and self.Condition.TestPoint(pt, direction, exclude): |
|
808 |
return self.Condition |
|
809 |
return None |
|
810 |
||
811 |
# Changes the transition type |
|
812 |
def SetType(self, type, condition = None): |
|
813 |
if self.Type != type: |
|
814 |
if self.Type == "connection": |
|
815 |
self.Condition.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
816 |
self.Type = type |
|
817 |
if type == "connection": |
|
818 |
self.Condition = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2), WEST) |
|
819 |
else: |
|
820 |
if condition == None: |
|
821 |
condition = "" |
|
822 |
self.Condition = condition |
|
823 |
self.RefreshConditionSize() |
|
824 |
elif self.Type != "connection": |
|
825 |
if condition == None: |
|
826 |
condition = "" |
|
827 |
self.Condition = condition |
|
828 |
self.RefreshConditionSize() |
|
829 |
self.RefreshBoundingBox() |
|
830 |
||
831 |
# Returns the transition type |
|
832 |
def GetType(self): |
|
833 |
return self.Type |
|
834 |
||
835 |
# Changes the transition priority |
|
836 |
def SetPriority(self, priority): |
|
837 |
self.Priority = priority |
|
838 |
self.RefreshPrioritySize() |
|
839 |
self.RefreshBoundingBox() |
|
840 |
||
841 |
# Returns the transition type |
|
842 |
def GetPriority(self): |
|
843 |
return self.Priority |
|
844 |
||
845 |
# Returns the transition condition |
|
846 |
def GetCondition(self): |
|
847 |
if self.Type != "connection": |
|
848 |
return self.Condition |
|
849 |
return None |
|
850 |
||
851 |
# Returns the transition minimum size |
|
852 |
def GetMinSize(self): |
|
853 |
return SFC_TRANSITION_SIZE |
|
854 |
||
855 |
# Align input element with this step |
|
856 |
def RefreshInputPosition(self): |
|
857 |
wires = self.Input.GetWires() |
|
858 |
current_pos = self.Input.GetPosition(False) |
|
859 |
input = self.GetPreviousConnector() |
|
860 |
if input: |
|
861 |
input_pos = input.GetPosition(False) |
|
862 |
diffx = current_pos.x - input_pos.x |
|
863 |
input_block = input.GetParentBlock() |
|
864 |
if isinstance(input_block, SFC_Divergence): |
|
865 |
input_block.MoveConnector(input, diffx) |
|
866 |
else: |
|
867 |
if isinstance(input_block, SFC_Step): |
|
868 |
input_block.MoveActionBlock((diffx, 0)) |
|
869 |
input_block.Move(diffx, 0) |
|
870 |
input_block.RefreshInputPosition() |
|
871 |
||
872 |
# Align output element with this step |
|
873 |
def RefreshOutputPosition(self, move = None): |
|
874 |
wires = self.Output.GetWires() |
|
875 |
if len(wires) != 1: |
|
876 |
return |
|
877 |
current_pos = self.Output.GetPosition(False) |
|
878 |
output = wires[0][0].GetOtherConnected(self.Output) |
|
879 |
output_pos = output.GetPosition(False) |
|
880 |
diffx = current_pos.x - output_pos.x |
|
881 |
output_block = output.GetParentBlock() |
|
882 |
if move: |
|
883 |
if isinstance(output_block, SFC_Step): |
|
884 |
output_block.MoveActionBlock(move) |
|
885 |
wires[0][0].Move(move[0], move[1], True) |
|
886 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
887 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
888 |
output_block.RefreshOutputPosition(move) |
|
889 |
else: |
|
890 |
output_block.RefreshPosition() |
|
891 |
elif isinstance(output_block, SFC_Divergence): |
|
892 |
output_block.MoveConnector(output, diffx) |
|
893 |
else: |
|
894 |
if isinstance(output_block, SFC_Step): |
|
895 |
output_block.MoveActionBlock((diffx, 0)) |
|
896 |
output_block.Move(diffx, 0) |
|
897 |
output_block.RefreshOutputPosition() |
|
898 |
||
899 |
# Method called when a LeftDClick event have been generated |
|
900 |
def OnLeftDClick(self, event, dc, scaling): |
|
901 |
# Edit the transition properties |
|
902 |
self.Parent.EditTransitionContent(self) |
|
903 |
||
904 |
# Method called when a RightUp event have been generated |
|
905 |
def OnRightUp(self, event, dc, scaling): |
|
906 |
# Popup the menu with special items for a step |
|
907 |
self.Parent.PopupDefaultMenu() |
|
908 |
||
909 |
# Refreshes the transition state according to move defined and handle selected |
|
910 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
911 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
912 |
movex = max(-self.BoundingBox.x, movex) |
|
913 |
if scaling is not None: |
|
914 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
915 |
self.Move(movex, 0) |
|
916 |
self.RefreshInputPosition() |
|
917 |
self.RefreshOutputPosition() |
|
918 |
return movex, 0 |
|
919 |
else: |
|
920 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, width_fac = 2, height_fac = 2) |
|
921 |
||
922 |
# Refresh input element model |
|
923 |
def RefreshInputModel(self): |
|
924 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
925 |
input = self.GetPreviousConnector() |
|
926 |
if input: |
|
927 |
input_block = input.GetParentBlock() |
|
928 |
input_block.RefreshModel(False) |
|
929 |
if not isinstance(input_block, SFC_Divergence): |
|
930 |
input_block.RefreshInputModel() |
|
931 |
||
932 |
# Refresh output element model |
|
933 |
def RefreshOutputModel(self, move=False): |
|
934 |
output = self.GetNextConnector() |
|
935 |
if output: |
|
936 |
output_block = output.GetParentBlock() |
|
937 |
output_block.RefreshModel(False) |
|
938 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
939 |
output_block.RefreshOutputModel(move) |
|
940 |
||
941 |
# Refreshes the transition model |
|
942 |
def RefreshModel(self, move=True): |
|
943 |
self.Parent.RefreshTransitionModel(self) |
|
944 |
# If transition has moved, refresh the model of wires connected to output |
|
945 |
if move: |
|
946 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
947 |
self.RefreshInputModel() |
|
948 |
self.RefreshOutputModel() |
|
949 |
else: |
|
950 |
self.Output.RefreshWires() |
|
951 |
||
952 |
# Adds an highlight to the block |
|
953 |
def AddHighlight(self, infos, start, end ,highlight_type): |
|
954 |
if infos[0] in ["reference", "inline", "priority"] and start[0] == 0 and end[0] == 0: |
|
955 |
highlights = self.Highlights.setdefault(infos[0], []) |
|
956 |
AddHighlight(highlights, (start, end, highlight_type)) |
|
957 |
||
958 |
# Removes an highlight from the block |
|
959 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
960 |
if infos[0] in ["reference", "inline", "priority"]: |
|
961 |
highlights = self.Highlights.get(infos[0], []) |
|
962 |
if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: |
|
963 |
self.Highlights.pop(infos[0]) |
|
964 |
||
965 |
# Removes all the highlights of one particular type from the block |
|
966 |
def ClearHighlight(self, highlight_type=None): |
|
967 |
if highlight_type is None: |
|
968 |
self.Highlights = {} |
|
969 |
else: |
|
970 |
highlight_items = self.Highlights.items() |
|
971 |
for name, highlights in highlight_items: |
|
972 |
highlights = ClearHighlights(highlight, highlight_type) |
|
973 |
if len(highlights) == 0: |
|
974 |
self.Highlights.pop(name) |
|
975 |
||
976 |
# Draws transition |
|
977 |
def Draw(self, dc): |
|
978 |
Graphic_Element.Draw(self, dc) |
|
979 |
if self.Value: |
|
980 |
if self.Forced: |
|
981 |
dc.SetPen(MiterPen(wx.CYAN)) |
|
982 |
dc.SetBrush(wx.CYAN_BRUSH) |
|
983 |
else: |
|
984 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
985 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
986 |
elif self.Forced: |
|
987 |
dc.SetPen(MiterPen(wx.BLUE)) |
|
988 |
dc.SetBrush(wx.BLUE_BRUSH) |
|
989 |
else: |
|
990 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
991 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
992 |
||
993 |
if getattr(dc, "printing", False): |
|
994 |
if self.Type != "connection": |
|
995 |
condition_size = dc.GetTextExtent(self.Condition) |
|
996 |
if self.Priority != 0: |
|
997 |
priority_size = dc.GetTextExtent(str(self.Priority)) |
|
998 |
else: |
|
999 |
if self.Type != "connection": |
|
1000 |
condition_size = self.ConditionSize |
|
1001 |
if self.Priority != 0: |
|
1002 |
priority_size = self.PrioritySize |
|
1003 |
||
1004 |
# Draw plain rectangle for representing the transition |
|
1005 |
dc.DrawRectangle(self.Pos.x, |
|
1006 |
self.Pos.y + (self.Size[1] - SFC_TRANSITION_SIZE[1])/2, |
|
1007 |
self.Size[0] + 1, |
|
1008 |
SFC_TRANSITION_SIZE[1] + 1) |
|
1009 |
vertical_line_x = self.Input.GetPosition()[0] |
|
1010 |
dc.DrawLine(vertical_line_x, self.Pos.y, vertical_line_x, self.Pos.y + self.Size[1] + 1) |
|
1011 |
# Draw transition condition |
|
1012 |
if self.Type != "connection": |
|
1013 |
if self.Condition != "": |
|
1014 |
condition = self.Condition |
|
1015 |
else: |
|
1016 |
condition = "Transition" |
|
1017 |
condition_pos = (self.Pos.x + self.Size[0] + 5, |
|
1018 |
self.Pos.y + (self.Size[1] - condition_size[1]) / 2) |
|
1019 |
dc.DrawText(condition, condition_pos[0], condition_pos[1]) |
|
1020 |
# Draw priority number |
|
1021 |
if self.Priority != 0: |
|
1022 |
priority_pos = (self.Pos.x, self.Pos.y - priority_size[1] - 2) |
|
1023 |
dc.DrawText(str(self.Priority), priority_pos[0], priority_pos[1]) |
|
1024 |
# Draw input and output connectors |
|
1025 |
self.Input.Draw(dc) |
|
1026 |
self.Output.Draw(dc) |
|
1027 |
if self.Type == "connection": |
|
1028 |
self.Condition.Draw(dc) |
|
1029 |
||
1030 |
if not getattr(dc, "printing", False): |
|
1031 |
for name, highlights in self.Highlights.iteritems(): |
|
1032 |
if name == "priority": |
|
1033 |
DrawHighlightedText(dc, str(self.Priority), highlights, priority_pos[0], priority_pos[1]) |
|
1034 |
else: |
|
1035 |
DrawHighlightedText(dc, condition, highlights, condition_pos[0], condition_pos[1]) |
|
1036 |
||
1037 |
#------------------------------------------------------------------------------- |
|
1038 |
# Sequencial Function Chart Divergence and Convergence |
|
1039 |
#------------------------------------------------------------------------------- |
|
1040 |
||
1041 |
""" |
|
1042 |
Class that implements the graphic representation of a divergence or convergence, |
|
1043 |
selection or simultaneous |
|
1044 |
""" |
|
1045 |
||
1046 |
class SFC_Divergence(Graphic_Element): |
|
1047 |
||
1048 |
# Create a new divergence |
|
1049 |
def __init__(self, parent, type, number = 2, id = None): |
|
1050 |
Graphic_Element.__init__(self, parent) |
|
1051 |
self.Type = type |
|
1052 |
self.Id = id |
|
1053 |
self.RealConnectors = None |
|
1054 |
number = max(2, number) |
|
1055 |
self.Size = wx.Size((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, self.GetMinSize()[1]) |
|
1056 |
# Create an input and output connector |
|
1057 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1058 |
self.Inputs = [Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone = True)] |
|
1059 |
self.Outputs = [] |
|
1060 |
for i in xrange(number): |
|
1061 |
self.Outputs.append(Connector(self, "", None, wx.Point(i * SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH, onlyone = True)) |
|
1062 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1063 |
self.Inputs = [] |
|
1064 |
for i in xrange(number): |
|
1065 |
self.Inputs.append(Connector(self, "", None, wx.Point(i * SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH, onlyone = True)) |
|
1066 |
self.Outputs = [Connector(self, "", None, wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH, onlyone = True)] |
|
1067 |
self.Value = None |
|
1068 |
self.PreviousValue = None |
|
1069 |
||
1070 |
def Flush(self): |
|
1071 |
for input in self.Inputs: |
|
1072 |
input.Flush() |
|
1073 |
self.Inputs = [] |
|
1074 |
for output in self.Outputs: |
|
1075 |
output.Flush() |
|
1076 |
self.Outputs = [] |
|
1077 |
||
1078 |
def SpreadCurrent(self): |
|
1079 |
if self.Parent.Debug: |
|
1080 |
self.PreviousValue = self.Value |
|
1081 |
if self.Type == SELECTION_CONVERGENCE: |
|
1082 |
self.Value = False |
|
1083 |
for input in self.Inputs: |
|
1084 |
self.Value |= input.ReceivingCurrent() |
|
1085 |
elif self.Type == SIMULTANEOUS_CONVERGENCE: |
|
1086 |
self.Value = True |
|
1087 |
for input in self.Inputs: |
|
1088 |
self.Value &= input.ReceivingCurrent() |
|
1089 |
elif self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1090 |
self.Value = self.Inputs[0].ReceivingCurrent() |
|
1091 |
else: |
|
1092 |
self.Value = False |
|
1093 |
if self.Value and not self.PreviousValue: |
|
1094 |
if self.Visible: |
|
1095 |
self.Parent.ElementNeedRefresh(self) |
|
1096 |
for output in self.Outputs: |
|
1097 |
output.SpreadCurrent(True) |
|
1098 |
elif not self.Value and self.PreviousValue: |
|
1099 |
if self.Visible: |
|
1100 |
self.Parent.ElementNeedRefresh(self) |
|
1101 |
for output in self.Outputs: |
|
1102 |
output.SpreadCurrent(False) |
|
1103 |
||
1104 |
# Make a clone of this SFC_Divergence |
|
1105 |
def Clone(self, parent, id = None, pos = None): |
|
1106 |
divergence = SFC_Divergence(parent, self.Type, max(len(self.Inputs), len(self.Outputs)), id) |
|
1107 |
divergence.SetSize(self.Size[0], self.Size[1]) |
|
1108 |
if pos is not None: |
|
1109 |
divergence.SetPosition(pos.x, pos.y) |
|
1110 |
else: |
|
1111 |
divergence.SetPosition(self.Pos.x, self.Pos.y) |
|
1112 |
divergence.Inputs = [input.Clone(divergence) for input in self.Inputs] |
|
1113 |
divergence.Outputs = [output.Clone(divergence) for output in self.Outputs] |
|
1114 |
return divergence |
|
1115 |
||
1116 |
def GetConnectorTranslation(self, element): |
|
1117 |
return dict(zip(self.Inputs + self.Outputs, element.Inputs + element.Outputs)) |
|
1118 |
||
1119 |
# Returns the RedrawRect |
|
1120 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1121 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1122 |
if movex != 0 or movey != 0: |
|
1123 |
for input in self.Inputs: |
|
1124 |
if input.IsConnected(): |
|
1125 |
rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
|
1126 |
for output in self.Outputs: |
|
1127 |
if output.IsConnected(): |
|
1128 |
rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) |
|
1129 |
return rect |
|
1130 |
||
1131 |
# Forbids to resize the divergence |
|
1132 |
def Resize(self, x, y, width, height): |
|
1133 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1134 |
Graphic_Element.Resize(self, x, 0, width, self.GetMinSize()[1]) |
|
1135 |
||
1136 |
# Delete this divergence by calling the appropriate method |
|
1137 |
def Delete(self): |
|
1138 |
self.Parent.DeleteDivergence(self) |
|
1139 |
||
1140 |
# Returns the divergence type |
|
1141 |
def GetType(self): |
|
1142 |
return self.Type |
|
1143 |
||
1144 |
# Unconnect input and output |
|
1145 |
def Clean(self): |
|
1146 |
for input in self.Inputs: |
|
1147 |
input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1148 |
for output in self.Outputs: |
|
1149 |
output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1150 |
||
1151 |
# Add a branch to the divergence |
|
1152 |
def AddBranch(self): |
|
1153 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1154 |
maxx = 0 |
|
1155 |
for output in self.Outputs: |
|
1156 |
pos = output.GetRelPosition() |
|
1157 |
maxx = max(maxx, pos.x) |
|
1158 |
connector = Connector(self, "", None, wx.Point(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH, onlyone = True) |
|
1159 |
self.Outputs.append(connector) |
|
1160 |
self.MoveConnector(connector, 0) |
|
1161 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1162 |
maxx = 0 |
|
1163 |
for input in self.Inputs: |
|
1164 |
pos = input.GetRelPosition() |
|
1165 |
maxx = max(maxx, pos.x) |
|
1166 |
connector = Connector(self, "", None, wx.Point(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH, onlyone = True) |
|
1167 |
self.Inputs.append(connector) |
|
1168 |
self.MoveConnector(connector, SFC_DEFAULT_SEQUENCE_INTERVAL) |
|
1169 |
||
1170 |
# Remove a branch from the divergence |
|
1171 |
def RemoveBranch(self, connector): |
|
1172 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1173 |
if connector in self.Outputs and len(self.Outputs) > 2: |
|
1174 |
self.Outputs.remove(connector) |
|
1175 |
self.MoveConnector(self.Outputs[0], 0) |
|
1176 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1177 |
if connector in self.Inputs and len(self.Inputs) > 2: |
|
1178 |
self.Inputs.remove(connector) |
|
1179 |
self.MoveConnector(self.Inputs[0], 0) |
|
1180 |
||
1181 |
# Remove the handled branch from the divergence |
|
1182 |
def RemoveHandledBranch(self): |
|
1183 |
handle_type, handle = self.Handle |
|
1184 |
if handle_type == HANDLE_CONNECTOR: |
|
1185 |
handle.UnConnect(delete=True) |
|
1186 |
self.RemoveBranch(handle) |
|
1187 |
||
1188 |
# Return the number of branches for the divergence |
|
1189 |
def GetBranchNumber(self): |
|
1190 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1191 |
return len(self.Outputs) |
|
1192 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1193 |
return len(self.Inputs) |
|
1194 |
||
1195 |
# Returns if the point given is in the bounding box |
|
1196 |
def HitTest(self, pt, connectors=True): |
|
1197 |
return self.BoundingBox.InsideXY(pt.x, pt.y) or self.TestConnector(pt, exclude=False) != None |
|
1198 |
||
1199 |
# Refresh the divergence bounding box |
|
1200 |
def RefreshBoundingBox(self): |
|
1201 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
1202 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, |
|
1203 |
self.Size[0] + 1, self.Size[1] + 1) |
|
1204 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1205 |
self.BoundingBox = wx.Rect(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y, |
|
1206 |
self.Size[0] + 2 * SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Size[1] + 1) |
|
1207 |
||
1208 |
# Refresh the position of wires connected to divergence |
|
1209 |
def RefreshConnected(self, exclude = []): |
|
1210 |
for input in self.Inputs: |
|
1211 |
input.MoveConnected(exclude) |
|
1212 |
for output in self.Outputs: |
|
1213 |
output.MoveConnected(exclude) |
|
1214 |
||
1215 |
# Moves the divergence connector given |
|
1216 |
def MoveConnector(self, connector, movex): |
|
1217 |
position = connector.GetRelPosition() |
|
1218 |
connector.SetPosition(wx.Point(position.x + movex, position.y)) |
|
1219 |
minx = self.Size[0] |
|
1220 |
maxx = 0 |
|
1221 |
for input in self.Inputs: |
|
1222 |
input_pos = input.GetRelPosition() |
|
1223 |
minx = min(minx, input_pos.x) |
|
1224 |
maxx = max(maxx, input_pos.x) |
|
1225 |
for output in self.Outputs: |
|
1226 |
output_pos = output.GetRelPosition() |
|
1227 |
minx = min(minx, output_pos.x) |
|
1228 |
maxx = max(maxx, output_pos.x) |
|
1229 |
if minx != 0: |
|
1230 |
for input in self.Inputs: |
|
1231 |
input_pos = input.GetRelPosition() |
|
1232 |
input.SetPosition(wx.Point(input_pos.x - minx, input_pos.y)) |
|
1233 |
for output in self.Outputs: |
|
1234 |
output_pos = output.GetRelPosition() |
|
1235 |
output.SetPosition(wx.Point(output_pos.x - minx, output_pos.y)) |
|
1236 |
self.Inputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x)) |
|
1237 |
self.Outputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x)) |
|
1238 |
self.Pos.x += minx |
|
1239 |
self.Size[0] = maxx - minx |
|
1240 |
connector.MoveConnected() |
|
1241 |
self.RefreshBoundingBox() |
|
1242 |
||
1243 |
# Returns the divergence connector that starts with the point given if it exists |
|
1244 |
def GetConnector(self, position, name = None): |
|
1245 |
# if a name is given |
|
1246 |
if name is not None: |
|
1247 |
# Test each input and output connector |
|
1248 |
#for input in self.Inputs: |
|
1249 |
# if name == input.GetName(): |
|
1250 |
# return input |
|
1251 |
for output in self.Outputs: |
|
1252 |
if name == output.GetName(): |
|
1253 |
return output |
|
1254 |
return self.FindNearestConnector(position, self.Inputs + self.Outputs) |
|
1255 |
||
1256 |
# Returns input and output divergence connectors |
|
1257 |
def GetConnectors(self): |
|
1258 |
return {"inputs": self.Inputs, "outputs": self.Outputs} |
|
1259 |
||
1260 |
# Test if point given is on divergence input or output connector |
|
1261 |
def TestConnector(self, pt, direction = None, exclude=True): |
|
1262 |
# Test input connector |
|
1263 |
for input in self.Inputs: |
|
1264 |
if input.TestPoint(pt, direction, exclude): |
|
1265 |
return input |
|
1266 |
# Test output connector |
|
1267 |
for output in self.Outputs: |
|
1268 |
if output.TestPoint(pt, direction, exclude): |
|
1269 |
return output |
|
1270 |
return None |
|
1271 |
||
1272 |
# Changes the divergence size |
|
1273 |
def SetSize(self, width, height): |
|
1274 |
height = self.GetMinSize()[1] |
|
1275 |
for i, input in enumerate(self.Inputs): |
|
1276 |
position = input.GetRelPosition() |
|
1277 |
if self.RealConnectors: |
|
1278 |
input.SetPosition(wx.Point(int(round(self.RealConnectors["Inputs"][i] * width)), 0)) |
|
1279 |
else: |
|
1280 |
input.SetPosition(wx.Point(int(round(float(position.x)*float(width)/float(self.Size[0]))), 0)) |
|
1281 |
input.MoveConnected() |
|
1282 |
for i, output in enumerate(self.Outputs): |
|
1283 |
position = output.GetRelPosition() |
|
1284 |
if self.RealConnectors: |
|
1285 |
output.SetPosition(wx.Point(int(round(self.RealConnectors["Outputs"][i] * width)), height)) |
|
1286 |
else: |
|
1287 |
output.SetPosition(wx.Point(int(round(float(position.x)*float(width)/float(self.Size[0]))), height)) |
|
1288 |
output.MoveConnected() |
|
1289 |
self.Size = wx.Size(width, height) |
|
1290 |
self.RefreshBoundingBox() |
|
1291 |
||
1292 |
# Returns the divergence minimum size |
|
1293 |
def GetMinSize(self, default=False): |
|
1294 |
width = 0 |
|
1295 |
if default: |
|
1296 |
if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]: |
|
1297 |
width = (len(self.Outputs) - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL |
|
1298 |
elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1299 |
width = (len(self.Inputs) - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL |
|
1300 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
1301 |
return width, 1 |
|
1302 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1303 |
return width, 3 |
|
1304 |
return 0, 0 |
|
1305 |
||
1306 |
# Refresh the position of the block connected to connector |
|
1307 |
def RefreshConnectedPosition(self, connector): |
|
1308 |
wires = connector.GetWires() |
|
1309 |
if len(wires) != 1: |
|
1310 |
return |
|
1311 |
current_pos = connector.GetPosition(False) |
|
1312 |
next = wires[0][0].GetOtherConnected(connector) |
|
1313 |
next_pos = next.GetPosition(False) |
|
1314 |
diffx = current_pos.x - next_pos.x |
|
1315 |
next_block = next.GetParentBlock() |
|
1316 |
if isinstance(next_block, SFC_Divergence): |
|
1317 |
next_block.MoveConnector(next, diffx) |
|
1318 |
else: |
|
1319 |
next_block.Move(diffx, 0) |
|
1320 |
if connector in self.Inputs: |
|
1321 |
next_block.RefreshInputPosition() |
|
1322 |
else: |
|
1323 |
next_block.RefreshOutputPosition() |
|
1324 |
||
1325 |
# Refresh the position of this divergence |
|
1326 |
def RefreshPosition(self): |
|
1327 |
y = 0 |
|
1328 |
for input in self.Inputs: |
|
1329 |
wires = input.GetWires() |
|
1330 |
if len(wires) != 1: |
|
1331 |
return |
|
1332 |
previous = wires[0][0].GetOtherConnected(input) |
|
1333 |
previous_pos = previous.GetPosition(False) |
|
1334 |
y = max(y, previous_pos.y + GetWireSize(previous.GetParentBlock())) |
|
1335 |
diffy = y - self.Pos.y |
|
1336 |
if diffy != 0: |
|
1337 |
self.Move(0, diffy, self.Parent.Wires) |
|
1338 |
self.RefreshOutputPosition((0, diffy)) |
|
1339 |
for input in self.Inputs: |
|
1340 |
input.MoveConnected() |
|
1341 |
||
1342 |
# Align output element with this divergence |
|
1343 |
def RefreshOutputPosition(self, move = None): |
|
1344 |
if move: |
|
1345 |
for output_connector in self.Outputs: |
|
1346 |
wires = output_connector.GetWires() |
|
1347 |
if len(wires) != 1: |
|
1348 |
return |
|
1349 |
current_pos = output_connector.GetPosition(False) |
|
1350 |
output = wires[0][0].GetOtherConnected(self.Output) |
|
1351 |
output_pos = output.GetPosition(False) |
|
1352 |
diffx = current_pos.x - output_pos.x |
|
1353 |
output_block = output.GetParentBlock() |
|
1354 |
if isinstance(output_block, SFC_Step): |
|
1355 |
output_block.MoveActionBlock(move) |
|
1356 |
wires[0][0].Move(move[0], move[1], True) |
|
1357 |
if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0: |
|
1358 |
output_block.Move(move[0], move[1], self.Parent.Wires) |
|
1359 |
output_block.RefreshOutputPosition(move) |
|
1360 |
||
1361 |
# Method called when a LeftDown event have been generated |
|
1362 |
def OnLeftDown(self, event, dc, scaling): |
|
1363 |
connector = None |
|
1364 |
if event.ControlDown(): |
|
1365 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
1366 |
# Test if a connector have been handled |
|
1367 |
connector = self.TestConnector(pos, exclude=False) |
|
1368 |
if connector: |
|
1369 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
1370 |
wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
|
1371 |
self.Selected = False |
|
1372 |
# Initializes the last position |
|
1373 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
1374 |
else: |
|
1375 |
self.RealConnectors = {"Inputs":[],"Outputs":[]} |
|
1376 |
for input in self.Inputs: |
|
1377 |
position = input.GetRelPosition() |
|
1378 |
self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0])) |
|
1379 |
for output in self.Outputs: |
|
1380 |
position = output.GetRelPosition() |
|
1381 |
self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0])) |
|
1382 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
|
1383 |
||
1384 |
# Method called when a LeftUp event have been generated |
|
1385 |
def OnLeftUp(self, event, dc, scaling): |
|
1386 |
handle_type, handle = self.Handle |
|
1387 |
if handle_type == HANDLE_CONNECTOR and self.Dragging and self.oldPos: |
|
1388 |
wires = handle.GetWires() |
|
1389 |
if len(wires) == 1: |
|
1390 |
block = wires[0][0].GetOtherConnected(handle).GetParentBlock() |
|
1391 |
block.RefreshModel(False) |
|
1392 |
if not isinstance(block, SFC_Divergence): |
|
1393 |
if handle in self.Inputs: |
|
1394 |
block.RefreshInputModel() |
|
1395 |
else: |
|
1396 |
block.RefreshOutputModel() |
|
1397 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
|
1398 |
self.RealConnectors = None |
|
1399 |
||
1400 |
# Method called when a RightUp event have been generated |
|
1401 |
def OnRightUp(self, event, dc, scaling): |
|
1402 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
1403 |
# Popup the menu with special items for a block and a connector if one is handled |
|
1404 |
connector = self.TestConnector(pos, exclude=False) |
|
1405 |
if connector: |
|
1406 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
1407 |
self.Parent.PopupDivergenceMenu(True) |
|
1408 |
else: |
|
1409 |
# Popup the divergence menu without delete branch |
|
1410 |
self.Parent.PopupDivergenceMenu(False) |
|
1411 |
||
1412 |
# Refreshes the divergence state according to move defined and handle selected |
|
1413 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1414 |
handle_type, handle = self.Handle |
|
1415 |
# A connector has been handled |
|
1416 |
if handle_type == HANDLE_CONNECTOR: |
|
1417 |
movex = max(-self.BoundingBox.x, movex) |
|
1418 |
if scaling is not None: |
|
1419 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1420 |
self.MoveConnector(handle, movex) |
|
1421 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1422 |
self.RefreshConnectedPosition(handle) |
|
1423 |
return movex, 0 |
|
1424 |
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1425 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1426 |
return 0, 0 |
|
1427 |
||
1428 |
# Refresh output element model |
|
1429 |
def RefreshOutputModel(self, move=False): |
|
1430 |
if move and self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1431 |
for output in self.Outputs: |
|
1432 |
wires = output.GetWires() |
|
1433 |
if len(wires) != 1: |
|
1434 |
return |
|
1435 |
output_block = wires[0][0].GetOtherConnected(output).GetParentBlock() |
|
1436 |
output_block.RefreshModel(False) |
|
1437 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
1438 |
output_block.RefreshOutputModel(move) |
|
1439 |
||
1440 |
# Refreshes the divergence model |
|
1441 |
def RefreshModel(self, move=True): |
|
1442 |
self.Parent.RefreshDivergenceModel(self) |
|
1443 |
# If divergence has moved, refresh the model of wires connected to outputs |
|
1444 |
if move: |
|
1445 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1446 |
self.RefreshOutputModel() |
|
1447 |
else: |
|
1448 |
for output in self.Outputs: |
|
1449 |
output.RefreshWires() |
|
1450 |
||
1451 |
# Draws the highlightment of this element if it is highlighted |
|
1452 |
def DrawHighlightment(self, dc): |
|
1453 |
scalex, scaley = dc.GetUserScale() |
|
1454 |
dc.SetUserScale(1, 1) |
|
1455 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
1456 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1457 |
dc.SetLogicalFunction(wx.AND) |
|
1458 |
# Draw two rectangles for representing the contact |
|
1459 |
posx = self.Pos.x |
|
1460 |
width = self.Size[0] |
|
1461 |
if self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1462 |
posx -= SFC_SIMULTANEOUS_SEQUENCE_EXTRA |
|
1463 |
width += SFC_SIMULTANEOUS_SEQUENCE_EXTRA * 2 |
|
1464 |
dc.DrawRectangle(int(round((posx - 1) * scalex)) - 2, |
|
1465 |
int(round((self.Pos.y - 1) * scaley)) - 2, |
|
1466 |
int(round((width + 3) * scalex)) + 5, |
|
1467 |
int(round((self.Size.height + 3) * scaley)) + 5) |
|
1468 |
dc.SetLogicalFunction(wx.COPY) |
|
1469 |
dc.SetUserScale(scalex, scaley) |
|
1470 |
||
1471 |
# Draws divergence |
|
1472 |
def Draw(self, dc): |
|
1473 |
Graphic_Element.Draw(self, dc) |
|
1474 |
if self.Value: |
|
1475 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1476 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
1477 |
else: |
|
1478 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1479 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1480 |
# Draw plain rectangle for representing the divergence |
|
1481 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
1482 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1483 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1484 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y, |
|
1485 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y) |
|
1486 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y + self.Size[1], |
|
1487 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y + self.Size[1]) |
|
1488 |
# Draw inputs and outputs connectors |
|
1489 |
for input in self.Inputs: |
|
1490 |
input.Draw(dc) |
|
1491 |
for output in self.Outputs: |
|
1492 |
output.Draw(dc) |
|
1493 |
||
1494 |
||
1495 |
#------------------------------------------------------------------------------- |
|
1496 |
# Sequencial Function Chart Jump to Step |
|
1497 |
#------------------------------------------------------------------------------- |
|
1498 |
||
1499 |
""" |
|
1500 |
Class that implements the graphic representation of a jump to step |
|
1501 |
""" |
|
1502 |
||
1503 |
class SFC_Jump(Graphic_Element): |
|
1504 |
||
1505 |
# Create a new jump |
|
1506 |
def __init__(self, parent, target, id = None): |
|
1507 |
Graphic_Element.__init__(self, parent) |
|
1508 |
self.SetTarget(target) |
|
1509 |
self.Id = id |
|
1510 |
self.Size = wx.Size(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1]) |
|
1511 |
self.Highlights = [] |
|
1512 |
# Create an input and output connector |
|
1513 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone = True) |
|
1514 |
self.Value = None |
|
1515 |
self.PreviousValue = None |
|
1516 |
||
1517 |
def Flush(self): |
|
1518 |
if self.Input is not None: |
|
1519 |
self.Input.Flush() |
|
1520 |
self.Input = None |
|
1521 |
||
1522 |
def SpreadCurrent(self): |
|
1523 |
if self.Parent.Debug: |
|
1524 |
self.PreviousValue = self.Value |
|
1525 |
self.Value = self.Input.ReceivingCurrent() |
|
1526 |
if self.Value != self.PreviousValue and self.Visible: |
|
1527 |
self.Parent.ElementNeedRefresh(self) |
|
1528 |
||
1529 |
# Make a clone of this SFC_Jump |
|
1530 |
def Clone(self, parent, id = None, pos = None): |
|
1531 |
jump = SFC_Jump(parent, self.Target, id) |
|
1532 |
jump.SetSize(self.Size[0], self.Size[1]) |
|
1533 |
if pos is not None: |
|
1534 |
jump.SetPosition(pos.x, pos.y) |
|
1535 |
else: |
|
1536 |
jump.SetPosition(self.Pos.x, self.Pos.y) |
|
1537 |
jump.Input = self.Input.Clone(jump) |
|
1538 |
return jump |
|
1539 |
||
1540 |
def GetConnectorTranslation(self, element): |
|
1541 |
return {self.Input : element.Input} |
|
1542 |
||
1543 |
# Returns the RedrawRect |
|
1544 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1545 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1546 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
1547 |
if movex != 0 or movey != 0: |
|
1548 |
if self.Input.IsConnected(): |
|
1549 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
1550 |
return rect |
|
1551 |
||
1552 |
# Forbids to change the jump size |
|
1553 |
def SetSize(self, width, height): |
|
1554 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1555 |
Graphic_Element.SetSize(self, width, height) |
|
1556 |
||
1557 |
# Forbids to resize jump |
|
1558 |
def Resize(self, x, y, width, height): |
|
1559 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1560 |
Graphic_Element.Resize(self, x, y, width, height) |
|
1561 |
||
1562 |
# Delete this jump by calling the appropriate method |
|
1563 |
def Delete(self): |
|
1564 |
self.Parent.DeleteJump(self) |
|
1565 |
||
1566 |
# Unconnect input |
|
1567 |
def Clean(self): |
|
1568 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1569 |
||
1570 |
# Refresh the size of text for target |
|
1571 |
def RefreshTargetSize(self): |
|
1572 |
self.TargetSize = self.Parent.GetTextExtent(self.Target) |
|
1573 |
||
1574 |
# Returns if the point given is in the bounding box |
|
1575 |
def HitTest(self, pt, connectors=True): |
|
1576 |
# Calculate the bounding box of the condition outside the transition |
|
1577 |
text_width, text_height = self.TargetSize |
|
1578 |
text_bbx = wx.Rect(self.Pos.x + self.Size[0] + 2, |
|
1579 |
self.Pos.y + (self.Size[1] - text_height) / 2, |
|
1580 |
text_width, |
|
1581 |
text_height) |
|
1582 |
return text_bbx.InsideXY(pt.x, pt.y) or Graphic_Element.HitTest(self, pt, connectors) |
|
1583 |
||
1584 |
# Refresh the jump bounding box |
|
1585 |
def RefreshBoundingBox(self): |
|
1586 |
text_width, text_height = self.Parent.GetTextExtent(self.Target) |
|
1587 |
# Calculate the bounding box size |
|
1588 |
bbx_width = self.Size[0] + 2 + text_width |
|
1589 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE, |
|
1590 |
bbx_width + 1, self.Size[1] + CONNECTOR_SIZE + 1) |
|
1591 |
||
1592 |
# Returns the connector connected to input |
|
1593 |
def GetPreviousConnector(self): |
|
1594 |
wires = self.Input.GetWires() |
|
1595 |
if len(wires) == 1: |
|
1596 |
return wires[0][0].GetOtherConnected(self.Input) |
|
1597 |
return None |
|
1598 |
||
1599 |
# Refresh the element connectors position |
|
1600 |
def RefreshConnectors(self): |
|
1601 |
scaling = self.Parent.GetScaling() |
|
1602 |
horizontal_pos = self.Size[0] / 2 |
|
1603 |
if scaling is not None: |
|
1604 |
horizontal_pos = round(float(self.Pos.x + horizontal_pos) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1605 |
self.Input.SetPosition(wx.Point(horizontal_pos, 0)) |
|
1606 |
self.RefreshConnected() |
|
1607 |
||
1608 |
# Refresh the position of wires connected to jump |
|
1609 |
def RefreshConnected(self, exclude = []): |
|
1610 |
if self.Input: |
|
1611 |
self.Input.MoveConnected(exclude) |
|
1612 |
||
1613 |
# Returns input jump connector |
|
1614 |
def GetConnector(self, position = None, name = None): |
|
1615 |
return self.Input |
|
1616 |
||
1617 |
# Returns all the jump connectors |
|
1618 |
def GetConnectors(self): |
|
1619 |
return {"inputs": [self.Input], "outputs": []} |
|
1620 |
||
1621 |
# Test if point given is on jump input connector |
|
1622 |
def TestConnector(self, pt, direction = None, exclude = True): |
|
1623 |
# Test input connector |
|
1624 |
if self.Input and self.Input.TestPoint(pt, direction, exclude): |
|
1625 |
return self.Input |
|
1626 |
return None |
|
1627 |
||
1628 |
# Changes the jump target |
|
1629 |
def SetTarget(self, target): |
|
1630 |
self.Target = target |
|
1631 |
self.RefreshTargetSize() |
|
1632 |
self.RefreshBoundingBox() |
|
1633 |
||
1634 |
# Returns the jump target |
|
1635 |
def GetTarget(self): |
|
1636 |
return self.Target |
|
1637 |
||
1638 |
# Returns the jump minimum size |
|
1639 |
def GetMinSize(self): |
|
1640 |
return SFC_JUMP_SIZE |
|
1641 |
||
1642 |
# Align input element with this jump |
|
1643 |
def RefreshInputPosition(self): |
|
1644 |
if self.Input: |
|
1645 |
current_pos = self.Input.GetPosition(False) |
|
1646 |
input = self.GetPreviousConnector() |
|
1647 |
if input: |
|
1648 |
input_pos = input.GetPosition(False) |
|
1649 |
diffx = current_pos.x - input_pos.x |
|
1650 |
input_block = input.GetParentBlock() |
|
1651 |
if isinstance(input_block, SFC_Divergence): |
|
1652 |
input_block.MoveConnector(input, diffx) |
|
1653 |
else: |
|
1654 |
if isinstance(input_block, SFC_Step): |
|
1655 |
input_block.MoveActionBlock((diffx, 0)) |
|
1656 |
input_block.Move(diffx, 0) |
|
1657 |
input_block.RefreshInputPosition() |
|
1658 |
||
1659 |
# Can't align output element, because there is no output |
|
1660 |
def RefreshOutputPosition(self, move = None): |
|
1661 |
pass |
|
1662 |
||
1663 |
# Method called when a LeftDClick event have been generated |
|
1664 |
def OnLeftDClick(self, event, dc, scaling): |
|
1665 |
# Edit the jump properties |
|
1666 |
self.Parent.EditJumpContent(self) |
|
1667 |
||
1668 |
# Method called when a RightUp event have been generated |
|
1669 |
def OnRightUp(self, event, dc, scaling): |
|
1670 |
# Popup the default menu |
|
1671 |
self.Parent.PopupDefaultMenu() |
|
1672 |
||
1673 |
# Refreshes the jump state according to move defined and handle selected |
|
1674 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1675 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1676 |
movex = max(-self.BoundingBox.x, movex) |
|
1677 |
if scaling is not None: |
|
1678 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1679 |
self.Move(movex, 0) |
|
1680 |
self.RefreshInputPosition() |
|
1681 |
return movex, 0 |
|
1682 |
else: |
|
1683 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, width_fac = 2) |
|
1684 |
||
1685 |
# Refresh input element model |
|
1686 |
def RefreshInputModel(self): |
|
1687 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1688 |
input = self.GetPreviousConnector() |
|
1689 |
if input: |
|
1690 |
input_block = input.GetParentBlock() |
|
1691 |
input_block.RefreshModel(False) |
|
1692 |
if not isinstance(input_block, SFC_Divergence): |
|
1693 |
input_block.RefreshInputModel() |
|
1694 |
||
1695 |
# Refresh output element model |
|
1696 |
def RefreshOutputModel(self, move=False): |
|
1697 |
pass |
|
1698 |
||
1699 |
# Refreshes the jump model |
|
1700 |
def RefreshModel(self, move=True): |
|
1701 |
self.Parent.RefreshJumpModel(self) |
|
1702 |
if move: |
|
1703 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1704 |
self.RefreshInputModel() |
|
1705 |
||
1706 |
# Adds an highlight to the variable |
|
1707 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1708 |
if infos[0] == "target" and start[0] == 0 and end[0] == 0: |
|
1709 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
1710 |
||
1711 |
# Removes an highlight from the variable |
|
1712 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1713 |
if infos[0] == "target": |
|
1714 |
RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
|
1715 |
||
1716 |
# Removes all the highlights of one particular type from the variable |
|
1717 |
def ClearHighlight(self, highlight_type=None): |
|
1718 |
ClearHighlights(self.Highlights, highlight_type) |
|
1719 |
||
1720 |
# Draws the highlightment of this element if it is highlighted |
|
1721 |
def DrawHighlightment(self, dc): |
|
1722 |
scalex, scaley = dc.GetUserScale() |
|
1723 |
dc.SetUserScale(1, 1) |
|
1724 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
1725 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1726 |
dc.SetLogicalFunction(wx.AND) |
|
1727 |
points = [wx.Point(int(round((self.Pos.x - 2) * scalex)) - 3, |
|
1728 |
int(round((self.Pos.y - 2) * scaley)) - 2), |
|
1729 |
wx.Point(int(round((self.Pos.x + self.Size[0] + 2) * scalex)) + 4, |
|
1730 |
int(round((self.Pos.y - 2) * scaley)) - 2), |
|
1731 |
wx.Point(int(round((self.Pos.x + self.Size[0] / 2) * scalex)), |
|
1732 |
int(round((self.Pos.y + self.Size[1] + 3) * scaley)) + 4)] |
|
1733 |
dc.DrawPolygon(points) |
|
1734 |
dc.SetLogicalFunction(wx.COPY) |
|
1735 |
dc.SetUserScale(scalex, scaley) |
|
1736 |
||
1737 |
# Draws divergence |
|
1738 |
def Draw(self, dc): |
|
1739 |
Graphic_Element.Draw(self, dc) |
|
1740 |
if self.Value: |
|
1741 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1742 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
1743 |
else: |
|
1744 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1745 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1746 |
||
1747 |
if getattr(dc, "printing", False): |
|
1748 |
target_size = dc.GetTextExtent(self.Target) |
|
1749 |
else: |
|
1750 |
target_size = self.TargetSize |
|
1751 |
||
1752 |
# Draw plain rectangle for representing the divergence |
|
1753 |
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]) |
|
1754 |
points = [wx.Point(self.Pos.x, self.Pos.y), |
|
1755 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3), |
|
1756 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y), |
|
1757 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])] |
|
1758 |
dc.DrawPolygon(points) |
|
1759 |
target_pos = (self.Pos.x + self.Size[0] + 2, |
|
1760 |
self.Pos.y + (self.Size[1] - target_size[1]) / 2) |
|
1761 |
dc.DrawText(self.Target, target_pos[0], target_pos[1]) |
|
1762 |
# Draw input connector |
|
1763 |
if self.Input: |
|
1764 |
self.Input.Draw(dc) |
|
1765 |
||
1766 |
if not getattr(dc, "printing", False): |
|
1767 |
DrawHighlightedText(dc, self.Target, self.Highlights, target_pos[0], target_pos[1]) |
|
1768 |
||
1769 |
||
1770 |
#------------------------------------------------------------------------------- |
|
1771 |
# Sequencial Function Chart Action Block |
|
1772 |
#------------------------------------------------------------------------------- |
|
1773 |
||
1774 |
""" |
|
1775 |
Class that implements the graphic representation of an action block |
|
1776 |
""" |
|
1777 |
||
1778 |
class SFC_ActionBlock(Graphic_Element): |
|
1779 |
||
1780 |
# Create a new action block |
|
1781 |
def __init__(self, parent, actions = [], id = None): |
|
1782 |
Graphic_Element.__init__(self, parent) |
|
1783 |
self.Id = id |
|
1784 |
self.Size = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
|
1785 |
self.MinSize = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
|
1786 |
self.Highlights = {} |
|
1787 |
# Create an input and output connector |
|
1788 |
self.Input = Connector(self, "", None, wx.Point(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST, onlyone = True) |
|
1789 |
self.SetActions(actions) |
|
1790 |
self.Value = None |
|
1791 |
self.PreviousValue = None |
|
1792 |
||
1793 |
def Flush(self): |
|
1794 |
if self.Input is not None: |
|
1795 |
self.Input.Flush() |
|
1796 |
self.Input = None |
|
1797 |
||
1798 |
def SpreadCurrent(self): |
|
1799 |
if self.Parent.Debug: |
|
1800 |
self.PreviousValue = self.Value |
|
1801 |
self.Value = self.Input.ReceivingCurrent() |
|
1802 |
if self.Value != self.PreviousValue and self.Visible: |
|
1803 |
self.Parent.ElementNeedRefresh(self) |
|
1804 |
||
1805 |
# Make a clone of this SFC_ActionBlock |
|
1806 |
def Clone(self, parent, id = None, pos = None): |
|
1807 |
actions = [action.copy() for action in self.Actions] |
|
1808 |
action_block = SFC_ActionBlock(parent, actions, id) |
|
1809 |
action_block.SetSize(self.Size[0], self.Size[1]) |
|
1810 |
if pos is not None: |
|
1811 |
action_block.SetPosition(pos.x, pos.y) |
|
1812 |
else: |
|
1813 |
action_block.SetPosition(self.Pos.x, self.Pos.y) |
|
1814 |
action_block.Input = self.Input.Clone(action_block) |
|
1815 |
return action_block |
|
1816 |
||
1817 |
def GetConnectorTranslation(self, element): |
|
1818 |
return {self.Input : element.Input} |
|
1819 |
||
1820 |
# Returns the RedrawRect |
|
1821 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1822 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1823 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
1824 |
if movex != 0 or movey != 0: |
|
1825 |
if self.Input.IsConnected(): |
|
1826 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
1827 |
return rect |
|
1828 |
||
1829 |
# Returns the number of action lines |
|
1830 |
def GetLineNumber(self): |
|
1831 |
return len(self.Actions) |
|
1832 |
||
1833 |
def GetLineSize(self): |
|
1834 |
if len(self.Actions) > 0: |
|
1835 |
return self.Size[1] / len(self.Actions) |
|
1836 |
else: |
|
1837 |
return SFC_ACTION_MIN_SIZE[1] |
|
1838 |
||
1839 |
# Forbids to resize the action block |
|
1840 |
def Resize(self, x, y, width, height): |
|
1841 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1842 |
if x == 0: |
|
1843 |
self.SetSize(width, self.Size[1]) |
|
1844 |
else: |
|
1845 |
Graphic_Element.Resize(self, x, y, width, height) |
|
1846 |
||
1847 |
# Delete this action block by calling the appropriate method |
|
1848 |
def Delete(self): |
|
1849 |
self.Parent.DeleteActionBlock(self) |
|
1850 |
||
1851 |
# Unconnect input and output |
|
1852 |
def Clean(self): |
|
1853 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1854 |
||
1855 |
# Refresh the action block bounding box |
|
1856 |
def RefreshBoundingBox(self): |
|
1857 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1858 |
||
1859 |
# Refresh the position of wires connected to action block |
|
1860 |
def RefreshConnected(self, exclude = []): |
|
1861 |
self.Input.MoveConnected(exclude) |
|
1862 |
||
1863 |
# Returns input action block connector |
|
1864 |
def GetConnector(self, position = None, name = None): |
|
1865 |
return self.Input |
|
1866 |
||
1867 |
# Returns all the action block connectors |
|
1868 |
def GetConnectors(self): |
|
1869 |
return {"inputs": [self.Input], "outputs": []} |
|
1870 |
||
1871 |
# Test if point given is on action block input connector |
|
1872 |
def TestConnector(self, pt, direction = None, exclude = True): |
|
1873 |
# Test input connector |
|
1874 |
if self.Input.TestPoint(pt, direction, exclude): |
|
1875 |
return self.Input |
|
1876 |
return None |
|
1877 |
||
1878 |
# Refresh the element connectors position |
|
1879 |
def RefreshConnectors(self): |
|
1880 |
scaling = self.Parent.GetScaling() |
|
1881 |
vertical_pos = SFC_ACTION_MIN_SIZE[1] / 2 |
|
1882 |
if scaling is not None: |
|
1883 |
vertical_pos = round(float(self.Pos.y + vertical_pos) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
1884 |
self.Input.SetPosition(wx.Point(0, vertical_pos)) |
|
1885 |
self.RefreshConnected() |
|
1886 |
||
1887 |
# Changes the action block actions |
|
1888 |
def SetActions(self, actions): |
|
1889 |
self.Actions = actions |
|
1890 |
self.ColSize = [0, 0, 0] |
|
1891 |
min_height = 0 |
|
1892 |
for action in self.Actions: |
|
1893 |
width, height = self.Parent.GetTextExtent(action["qualifier"]) |
|
1894 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
|
1895 |
row_height = height |
|
1896 |
if action.has_key("duration"): |
|
1897 |
width, height = self.Parent.GetTextExtent(action["duration"]) |
|
1898 |
row_height = max(row_height, height) |
|
1899 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
|
1900 |
width, height = self.Parent.GetTextExtent(action["value"]) |
|
1901 |
row_height = max(row_height, height) |
|
1902 |
self.ColSize[1] = max(self.ColSize[1], width + 10) |
|
1903 |
if action.get("indicator", "") != "": |
|
1904 |
width, height = self.Parent.GetTextExtent(action["indicator"]) |
|
1905 |
row_height = max(row_height, height) |
|
1906 |
self.ColSize[2] = max(self.ColSize[2], width + 10) |
|
1907 |
min_height += row_height + 5 |
|
1908 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1909 |
self.Size = wx.Size(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], max(min_height, SFC_ACTION_MIN_SIZE[1], self.Size[1])) |
|
1910 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1911 |
SFC_ACTION_MIN_SIZE[0]), max(SFC_ACTION_MIN_SIZE[1], min_height) |
|
1912 |
self.RefreshBoundingBox() |
|
1913 |
else: |
|
1914 |
self.Size = wx.Size(max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1915 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1]) |
|
1916 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1917 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1] |
|
1918 |
self.RefreshBoundingBox() |
|
1919 |
if self.Input: |
|
1920 |
wires = self.Input.GetWires() |
|
1921 |
if len(wires) == 1: |
|
1922 |
input_block = wires[0][0].GetOtherConnected(self.Input).GetParentBlock() |
|
1923 |
input_block.RefreshOutputPosition() |
|
1924 |
input_block.RefreshOutputModel(True) |
|
1925 |
||
1926 |
# Returns the action block actions |
|
1927 |
def GetActions(self): |
|
1928 |
return self.Actions |
|
1929 |
||
1930 |
# Returns the action block minimum size |
|
1931 |
def GetMinSize(self): |
|
1932 |
return self.MinSize |
|
1933 |
||
1934 |
# Method called when a LeftDClick event have been generated |
|
1935 |
def OnLeftDClick(self, event, dc, scaling): |
|
1936 |
# Edit the action block properties |
|
1937 |
self.Parent.EditActionBlockContent(self) |
|
1938 |
||
1939 |
# Method called when a RightUp event have been generated |
|
1940 |
def OnRightUp(self, event, dc, scaling): |
|
1941 |
# Popup the default menu |
|
1942 |
self.Parent.PopupDefaultMenu() |
|
1943 |
||
1944 |
# Refreshes the action block state according to move defined and handle selected |
|
1945 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1946 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1947 |
handle_type, handle = self.Handle |
|
1948 |
if handle_type == HANDLE_MOVE: |
|
1949 |
movex = max(-self.BoundingBox.x, movex) |
|
1950 |
if scaling is not None: |
|
1951 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1952 |
wires = self.Input.GetWires() |
|
1953 |
if len(wires) == 1: |
|
1954 |
input_pos = wires[0][0].GetOtherConnected(self.Input).GetPosition(False) |
|
1955 |
if self.Pos.x - input_pos.x + movex >= SFC_WIRE_MIN_SIZE: |
|
1956 |
self.Move(movex, 0) |
|
1957 |
return movex, 0 |
|
1958 |
return 0, 0 |
|
1959 |
else: |
|
1960 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1961 |
else: |
|
1962 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1963 |
||
1964 |
||
1965 |
# Refreshes the action block model |
|
1966 |
def RefreshModel(self, move=True): |
|
1967 |
self.Parent.RefreshActionBlockModel(self) |
|
1968 |
||
1969 |
# Adds an highlight to the variable |
|
1970 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1971 |
if infos[0] == "action" and infos[1] < len(self.Actions): |
|
1972 |
action_highlights = self.Highlights.setdefault(infos[1], {}) |
|
1973 |
attribute_highlights = action_highlights.setdefault(infos[2], []) |
|
1974 |
AddHighlight(attribute_highlights, (start, end, highlight_type)) |
|
1975 |
||
1976 |
# Removes an highlight from the block |
|
1977 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1978 |
if infos[0] == "action" and infos[1] < len(self.Actions): |
|
1979 |
action_highlights = self.Highlights.get(infos[1], {}) |
|
1980 |
attribute_highlights = action_highlights.setdefault(infos[2], []) |
|
1981 |
if RemoveHighlight(attribute_highlights, (start, end, highlight_type)) and len(attribute_highlights) == 0: |
|
1982 |
action_highlights.pop(infos[2]) |
|
1983 |
if len(action_highlights) == 0: |
|
1984 |
self.Highlights.pop(infos[1]) |
|
1985 |
||
1986 |
# Removes all the highlights of one particular type from the block |
|
1987 |
def ClearHighlight(self, highlight_type=None): |
|
1988 |
if highlight_type is None: |
|
1989 |
self.Highlights = {} |
|
1990 |
else: |
|
1991 |
highlight_items = self.Highlights.items() |
|
1992 |
for number, action_highlights in highlight_items: |
|
1993 |
action_highlight_items = action_highlights.items() |
|
1994 |
for name, attribute_highlights in action_highlights: |
|
1995 |
attribute_highlights = ClearHighlights(attribute_highlights, highlight_type) |
|
1996 |
if len(attribute_highlights) == 0: |
|
1997 |
action_highlights.pop(name) |
|
1998 |
if len(action_highlights) == 0: |
|
1999 |
self.Highlights.pop(number) |
|
2000 |
||
2001 |
# Draws divergence |
|
2002 |
def Draw(self, dc): |
|
2003 |
Graphic_Element.Draw(self, dc) |
|
2004 |
if self.Value: |
|
2005 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
2006 |
else: |
|
2007 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
2008 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
2009 |
colsize = [self.ColSize[0], self.Size[0] - self.ColSize[0] - self.ColSize[2], self.ColSize[2]] |
|
2010 |
# Draw plain rectangle for representing the action block |
|
2011 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
2012 |
dc.DrawLine(self.Pos.x + colsize[0], self.Pos.y, |
|
2013 |
self.Pos.x + colsize[0], self.Pos.y + self.Size[1]) |
|
2014 |
dc.DrawLine(self.Pos.x + colsize[0] + colsize[1], self.Pos.y, |
|
2015 |
self.Pos.x + colsize[0] + colsize[1], self.Pos.y + self.Size[1]) |
|
2016 |
line_size = self.GetLineSize() |
|
2017 |
for i, action in enumerate(self.Actions): |
|
2018 |
if i != 0: |
|
2019 |
dc.DrawLine(self.Pos.x, self.Pos.y + i * line_size, |
|
2020 |
self.Pos.x + self.Size[0], self.Pos.y + i * line_size) |
|
2021 |
qualifier_size = dc.GetTextExtent(action["qualifier"]) |
|
2022 |
if action.has_key("duration"): |
|
2023 |
qualifier_pos = (self.Pos.x + (colsize[0] - qualifier_size[0]) / 2, |
|
2024 |
self.Pos.y + i * line_size + line_size / 2 - qualifier_size[1]) |
|
2025 |
duration_size = dc.GetTextExtent(action["duration"]) |
|
2026 |
duration_pos = (self.Pos.x + (colsize[0] - duration_size[0]) / 2, |
|
2027 |
self.Pos.y + i * line_size + line_size / 2) |
|
2028 |
dc.DrawText(action["duration"], duration_pos[0], duration_pos[1]) |
|
2029 |
else: |
|
2030 |
qualifier_pos = (self.Pos.x + (colsize[0] - qualifier_size[0]) / 2, |
|
2031 |
self.Pos.y + i * line_size + (line_size - qualifier_size[1]) / 2) |
|
2032 |
dc.DrawText(action["qualifier"], qualifier_pos[0], qualifier_pos[1]) |
|
2033 |
content_size = dc.GetTextExtent(action["value"]) |
|
2034 |
content_pos = (self.Pos.x + colsize[0] + (colsize[1] - content_size[0]) / 2, |
|
2035 |
self.Pos.y + i * line_size + (line_size - content_size[1]) / 2) |
|
2036 |
dc.DrawText(action["value"], content_pos[0], content_pos[1]) |
|
2037 |
if action.has_key("indicator"): |
|
2038 |
indicator_size = dc.GetTextExtent(action["indicator"]) |
|
2039 |
indicator_pos = (self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - indicator_size[0]) / 2, |
|
2040 |
self.Pos.y + i * line_size + (line_size - indicator_size[1]) / 2) |
|
2041 |
dc.DrawText(action["indicator"], indicator_pos[0], indicator_pos[1]) |
|
2042 |
||
2043 |
if not getattr(dc, "printing", False): |
|
2044 |
action_highlights = self.Highlights.get(i, {}) |
|
2045 |
for name, attribute_highlights in action_highlights.iteritems(): |
|
2046 |
if name == "qualifier": |
|
2047 |
DrawHighlightedText(dc, action["qualifier"], attribute_highlights, qualifier_pos[0], qualifier_pos[1]) |
|
2048 |
elif name == "duration": |
|
2049 |
DrawHighlightedText(dc, action["duration"], attribute_highlights, duration_pos[0], duration_pos[1]) |
|
2050 |
elif name in ["reference", "inline"]: |
|
2051 |
DrawHighlightedText(dc, action["value"], attribute_highlights, content_pos[0], content_pos[1]) |
|
2052 |
elif name == "indicator": |
|
2053 |
DrawHighlightedText(dc, action["indicator"], attribute_highlights, indicator_pos[0], indicator_pos[1]) |
|
2054 |
||
2055 |
# Draw input connector |
|
2056 |
self.Input.Draw(dc) |
|
2057 |