author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 27 Oct 2016 16:24:13 +0300 | |
changeset 1553 | abe55de5316b |
parent 1339 | 6adf05c4508d |
child 1571 | 486f94a8032c |
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): |
|
1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1363 |
self.RealConnectors = {"Inputs":[],"Outputs":[]} |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1364 |
for input in self.Inputs: |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1365 |
position = input.GetRelPosition() |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1366 |
self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0])) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1367 |
for output in self.Outputs: |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1368 |
position = output.GetRelPosition() |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1369 |
self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0])) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1370 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1371 |
|
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1372 |
# Method called when a LeftUp event have been generated |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1373 |
def OnLeftUp(self, event, dc, scaling): |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1374 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1375 |
self.RealConnectors = None |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1376 |
|
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1377 |
# Method called when a RightDown event have been generated |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1378 |
def OnRightDown(self, event, dc, scaling): |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1379 |
pos = GetScaledEventPosition(event, dc, scaling) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1380 |
# Test if a connector have been handled |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1381 |
connector = self.TestConnector(pos, exclude=False) |
814 | 1382 |
if connector: |
1383 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
1384 |
wx.CallAfter(self.Parent.SetCurrentCursor, 1) |
|
1385 |
self.Selected = False |
|
1386 |
# Initializes the last position |
|
1387 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
1388 |
else: |
|
1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1389 |
Graphic_Element.OnRightDown(self, event, dc, scaling) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1390 |
|
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1391 |
# Method called when a RightUp event have been generated |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1392 |
def OnRightUp(self, event, dc, scaling): |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1393 |
pos = GetScaledEventPosition(event, dc, scaling) |
814 | 1394 |
handle_type, handle = self.Handle |
1395 |
if handle_type == HANDLE_CONNECTOR and self.Dragging and self.oldPos: |
|
1396 |
wires = handle.GetWires() |
|
1397 |
if len(wires) == 1: |
|
1398 |
block = wires[0][0].GetOtherConnected(handle).GetParentBlock() |
|
1399 |
block.RefreshModel(False) |
|
1400 |
if not isinstance(block, SFC_Divergence): |
|
1401 |
if handle in self.Inputs: |
|
1402 |
block.RefreshInputModel() |
|
1403 |
else: |
|
1404 |
block.RefreshOutputModel() |
|
1258
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1405 |
Graphic_Element.OnRightUp(self, event, dc, scaling) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1406 |
else: |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1407 |
# Popup the menu with special items for a block and a connector if one is handled |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1408 |
connector = self.TestConnector(pos, exclude=False) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1409 |
if connector: |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1410 |
self.Handle = (HANDLE_CONNECTOR, connector) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1411 |
self.Parent.PopupDivergenceMenu(True) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1412 |
else: |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1413 |
# Popup the divergence menu without delete branch |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1414 |
self.Parent.PopupDivergenceMenu(False) |
441f31474b50
Fixed move of LD_PowerRail and SFC_Divergence connectors using Right button
Laurent Bessard
parents:
1176
diff
changeset
|
1415 |
|
814 | 1416 |
# Refreshes the divergence state according to move defined and handle selected |
1417 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1418 |
handle_type, handle = self.Handle |
|
1419 |
# A connector has been handled |
|
1420 |
if handle_type == HANDLE_CONNECTOR: |
|
1421 |
movex = max(-self.BoundingBox.x, movex) |
|
1422 |
if scaling is not None: |
|
1423 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1424 |
self.MoveConnector(handle, movex) |
|
1425 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1426 |
self.RefreshConnectedPosition(handle) |
|
1427 |
return movex, 0 |
|
1428 |
elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1429 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1430 |
return 0, 0 |
|
1431 |
||
1432 |
# Refresh output element model |
|
1433 |
def RefreshOutputModel(self, move=False): |
|
1434 |
if move and self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1435 |
for output in self.Outputs: |
|
1436 |
wires = output.GetWires() |
|
1437 |
if len(wires) != 1: |
|
1438 |
return |
|
1439 |
output_block = wires[0][0].GetOtherConnected(output).GetParentBlock() |
|
1440 |
output_block.RefreshModel(False) |
|
1441 |
if not isinstance(output_block, SFC_Divergence) or move: |
|
1442 |
output_block.RefreshOutputModel(move) |
|
1443 |
||
1444 |
# Refreshes the divergence model |
|
1445 |
def RefreshModel(self, move=True): |
|
1446 |
self.Parent.RefreshDivergenceModel(self) |
|
1447 |
# If divergence has moved, refresh the model of wires connected to outputs |
|
1448 |
if move: |
|
1449 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1450 |
self.RefreshOutputModel() |
|
1451 |
else: |
|
1452 |
for output in self.Outputs: |
|
1453 |
output.RefreshWires() |
|
1454 |
||
1455 |
# Draws the highlightment of this element if it is highlighted |
|
1456 |
def DrawHighlightment(self, dc): |
|
1457 |
scalex, scaley = dc.GetUserScale() |
|
1458 |
dc.SetUserScale(1, 1) |
|
1459 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
1460 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1461 |
dc.SetLogicalFunction(wx.AND) |
|
1462 |
# Draw two rectangles for representing the contact |
|
1463 |
posx = self.Pos.x |
|
1464 |
width = self.Size[0] |
|
1465 |
if self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1466 |
posx -= SFC_SIMULTANEOUS_SEQUENCE_EXTRA |
|
1467 |
width += SFC_SIMULTANEOUS_SEQUENCE_EXTRA * 2 |
|
1468 |
dc.DrawRectangle(int(round((posx - 1) * scalex)) - 2, |
|
1469 |
int(round((self.Pos.y - 1) * scaley)) - 2, |
|
1470 |
int(round((width + 3) * scalex)) + 5, |
|
1471 |
int(round((self.Size.height + 3) * scaley)) + 5) |
|
1472 |
dc.SetLogicalFunction(wx.COPY) |
|
1473 |
dc.SetUserScale(scalex, scaley) |
|
1474 |
||
1475 |
# Draws divergence |
|
1476 |
def Draw(self, dc): |
|
1477 |
Graphic_Element.Draw(self, dc) |
|
1478 |
if self.Value: |
|
1479 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1480 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
1481 |
else: |
|
1482 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1483 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1484 |
# Draw plain rectangle for representing the divergence |
|
1485 |
if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]: |
|
1486 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1487 |
elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]: |
|
1488 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y, |
|
1489 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y) |
|
1490 |
dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y + self.Size[1], |
|
1491 |
self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y + self.Size[1]) |
|
1492 |
# Draw inputs and outputs connectors |
|
1493 |
for input in self.Inputs: |
|
1494 |
input.Draw(dc) |
|
1495 |
for output in self.Outputs: |
|
1496 |
output.Draw(dc) |
|
1497 |
||
1498 |
||
1499 |
#------------------------------------------------------------------------------- |
|
1500 |
# Sequencial Function Chart Jump to Step |
|
1501 |
#------------------------------------------------------------------------------- |
|
1502 |
||
1503 |
""" |
|
1504 |
Class that implements the graphic representation of a jump to step |
|
1505 |
""" |
|
1506 |
||
1507 |
class SFC_Jump(Graphic_Element): |
|
1508 |
||
1509 |
# Create a new jump |
|
1510 |
def __init__(self, parent, target, id = None): |
|
1511 |
Graphic_Element.__init__(self, parent) |
|
1512 |
self.SetTarget(target) |
|
1513 |
self.Id = id |
|
1514 |
self.Size = wx.Size(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1]) |
|
1515 |
self.Highlights = [] |
|
1516 |
# Create an input and output connector |
|
1517 |
self.Input = Connector(self, "", None, wx.Point(self.Size[0] / 2, 0), NORTH, onlyone = True) |
|
1518 |
self.Value = None |
|
1519 |
self.PreviousValue = None |
|
1520 |
||
1521 |
def Flush(self): |
|
1522 |
if self.Input is not None: |
|
1523 |
self.Input.Flush() |
|
1524 |
self.Input = None |
|
1525 |
||
1526 |
def SpreadCurrent(self): |
|
1527 |
if self.Parent.Debug: |
|
1528 |
self.PreviousValue = self.Value |
|
1529 |
self.Value = self.Input.ReceivingCurrent() |
|
1530 |
if self.Value != self.PreviousValue and self.Visible: |
|
1531 |
self.Parent.ElementNeedRefresh(self) |
|
1532 |
||
1533 |
# Make a clone of this SFC_Jump |
|
1534 |
def Clone(self, parent, id = None, pos = None): |
|
1535 |
jump = SFC_Jump(parent, self.Target, id) |
|
1536 |
jump.SetSize(self.Size[0], self.Size[1]) |
|
1537 |
if pos is not None: |
|
1538 |
jump.SetPosition(pos.x, pos.y) |
|
1539 |
else: |
|
1540 |
jump.SetPosition(self.Pos.x, self.Pos.y) |
|
1541 |
jump.Input = self.Input.Clone(jump) |
|
1542 |
return jump |
|
1543 |
||
1544 |
def GetConnectorTranslation(self, element): |
|
1545 |
return {self.Input : element.Input} |
|
1546 |
||
1547 |
# Returns the RedrawRect |
|
1548 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1549 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1550 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
1551 |
if movex != 0 or movey != 0: |
|
1552 |
if self.Input.IsConnected(): |
|
1553 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
1554 |
return rect |
|
1555 |
||
1556 |
# Forbids to change the jump size |
|
1557 |
def SetSize(self, width, height): |
|
1558 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1559 |
Graphic_Element.SetSize(self, width, height) |
|
1560 |
||
1561 |
# Forbids to resize jump |
|
1562 |
def Resize(self, x, y, width, height): |
|
1563 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1564 |
Graphic_Element.Resize(self, x, y, width, height) |
|
1565 |
||
1566 |
# Delete this jump by calling the appropriate method |
|
1567 |
def Delete(self): |
|
1568 |
self.Parent.DeleteJump(self) |
|
1569 |
||
1570 |
# Unconnect input |
|
1571 |
def Clean(self): |
|
1572 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1573 |
||
1574 |
# Refresh the size of text for target |
|
1575 |
def RefreshTargetSize(self): |
|
1576 |
self.TargetSize = self.Parent.GetTextExtent(self.Target) |
|
1577 |
||
1578 |
# Returns if the point given is in the bounding box |
|
1579 |
def HitTest(self, pt, connectors=True): |
|
1580 |
# Calculate the bounding box of the condition outside the transition |
|
1581 |
text_width, text_height = self.TargetSize |
|
1582 |
text_bbx = wx.Rect(self.Pos.x + self.Size[0] + 2, |
|
1583 |
self.Pos.y + (self.Size[1] - text_height) / 2, |
|
1584 |
text_width, |
|
1585 |
text_height) |
|
1586 |
return text_bbx.InsideXY(pt.x, pt.y) or Graphic_Element.HitTest(self, pt, connectors) |
|
1587 |
||
1588 |
# Refresh the jump bounding box |
|
1589 |
def RefreshBoundingBox(self): |
|
1590 |
text_width, text_height = self.Parent.GetTextExtent(self.Target) |
|
1591 |
# Calculate the bounding box size |
|
1592 |
bbx_width = self.Size[0] + 2 + text_width |
|
1593 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE, |
|
1594 |
bbx_width + 1, self.Size[1] + CONNECTOR_SIZE + 1) |
|
1595 |
||
1596 |
# Returns the connector connected to input |
|
1597 |
def GetPreviousConnector(self): |
|
1598 |
wires = self.Input.GetWires() |
|
1599 |
if len(wires) == 1: |
|
1600 |
return wires[0][0].GetOtherConnected(self.Input) |
|
1601 |
return None |
|
1602 |
||
1603 |
# Refresh the element connectors position |
|
1604 |
def RefreshConnectors(self): |
|
1605 |
scaling = self.Parent.GetScaling() |
|
1606 |
horizontal_pos = self.Size[0] / 2 |
|
1607 |
if scaling is not None: |
|
1608 |
horizontal_pos = round(float(self.Pos.x + horizontal_pos) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1609 |
self.Input.SetPosition(wx.Point(horizontal_pos, 0)) |
|
1610 |
self.RefreshConnected() |
|
1611 |
||
1612 |
# Refresh the position of wires connected to jump |
|
1613 |
def RefreshConnected(self, exclude = []): |
|
1614 |
if self.Input: |
|
1615 |
self.Input.MoveConnected(exclude) |
|
1616 |
||
1617 |
# Returns input jump connector |
|
1618 |
def GetConnector(self, position = None, name = None): |
|
1619 |
return self.Input |
|
1620 |
||
1621 |
# Returns all the jump connectors |
|
1622 |
def GetConnectors(self): |
|
1623 |
return {"inputs": [self.Input], "outputs": []} |
|
1624 |
||
1625 |
# Test if point given is on jump input connector |
|
1626 |
def TestConnector(self, pt, direction = None, exclude = True): |
|
1627 |
# Test input connector |
|
1628 |
if self.Input and self.Input.TestPoint(pt, direction, exclude): |
|
1629 |
return self.Input |
|
1630 |
return None |
|
1631 |
||
1632 |
# Changes the jump target |
|
1633 |
def SetTarget(self, target): |
|
1634 |
self.Target = target |
|
1635 |
self.RefreshTargetSize() |
|
1636 |
self.RefreshBoundingBox() |
|
1637 |
||
1638 |
# Returns the jump target |
|
1639 |
def GetTarget(self): |
|
1640 |
return self.Target |
|
1641 |
||
1642 |
# Returns the jump minimum size |
|
1643 |
def GetMinSize(self): |
|
1644 |
return SFC_JUMP_SIZE |
|
1645 |
||
1646 |
# Align input element with this jump |
|
1647 |
def RefreshInputPosition(self): |
|
1648 |
if self.Input: |
|
1649 |
current_pos = self.Input.GetPosition(False) |
|
1650 |
input = self.GetPreviousConnector() |
|
1651 |
if input: |
|
1652 |
input_pos = input.GetPosition(False) |
|
1653 |
diffx = current_pos.x - input_pos.x |
|
1654 |
input_block = input.GetParentBlock() |
|
1655 |
if isinstance(input_block, SFC_Divergence): |
|
1656 |
input_block.MoveConnector(input, diffx) |
|
1657 |
else: |
|
1658 |
if isinstance(input_block, SFC_Step): |
|
1659 |
input_block.MoveActionBlock((diffx, 0)) |
|
1660 |
input_block.Move(diffx, 0) |
|
1661 |
input_block.RefreshInputPosition() |
|
1662 |
||
1663 |
# Can't align output element, because there is no output |
|
1664 |
def RefreshOutputPosition(self, move = None): |
|
1665 |
pass |
|
1666 |
||
1667 |
# Method called when a LeftDClick event have been generated |
|
1668 |
def OnLeftDClick(self, event, dc, scaling): |
|
1669 |
# Edit the jump properties |
|
1670 |
self.Parent.EditJumpContent(self) |
|
1671 |
||
1672 |
# Method called when a RightUp event have been generated |
|
1673 |
def OnRightUp(self, event, dc, scaling): |
|
1674 |
# Popup the default menu |
|
1675 |
self.Parent.PopupDefaultMenu() |
|
1676 |
||
1677 |
# Refreshes the jump state according to move defined and handle selected |
|
1678 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1679 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1680 |
movex = max(-self.BoundingBox.x, movex) |
|
1681 |
if scaling is not None: |
|
1682 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1683 |
self.Move(movex, 0) |
|
1684 |
self.RefreshInputPosition() |
|
1685 |
return movex, 0 |
|
1686 |
else: |
|
1687 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, width_fac = 2) |
|
1688 |
||
1689 |
# Refresh input element model |
|
1690 |
def RefreshInputModel(self): |
|
1691 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1692 |
input = self.GetPreviousConnector() |
|
1693 |
if input: |
|
1694 |
input_block = input.GetParentBlock() |
|
1695 |
input_block.RefreshModel(False) |
|
1696 |
if not isinstance(input_block, SFC_Divergence): |
|
1697 |
input_block.RefreshInputModel() |
|
1698 |
||
1699 |
# Refresh output element model |
|
1700 |
def RefreshOutputModel(self, move=False): |
|
1701 |
pass |
|
1702 |
||
1703 |
# Refreshes the jump model |
|
1704 |
def RefreshModel(self, move=True): |
|
1705 |
self.Parent.RefreshJumpModel(self) |
|
1706 |
if move: |
|
1707 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1708 |
self.RefreshInputModel() |
|
1709 |
||
1710 |
# Adds an highlight to the variable |
|
1711 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1712 |
if infos[0] == "target" and start[0] == 0 and end[0] == 0: |
|
1713 |
AddHighlight(self.Highlights, (start, end, highlight_type)) |
|
1714 |
||
1715 |
# Removes an highlight from the variable |
|
1716 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1717 |
if infos[0] == "target": |
|
1718 |
RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
|
1719 |
||
1720 |
# Removes all the highlights of one particular type from the variable |
|
1721 |
def ClearHighlight(self, highlight_type=None): |
|
1722 |
ClearHighlights(self.Highlights, highlight_type) |
|
1723 |
||
1724 |
# Draws the highlightment of this element if it is highlighted |
|
1725 |
def DrawHighlightment(self, dc): |
|
1726 |
scalex, scaley = dc.GetUserScale() |
|
1727 |
dc.SetUserScale(1, 1) |
|
1728 |
dc.SetPen(MiterPen(HIGHLIGHTCOLOR)) |
|
1729 |
dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) |
|
1730 |
dc.SetLogicalFunction(wx.AND) |
|
1731 |
points = [wx.Point(int(round((self.Pos.x - 2) * scalex)) - 3, |
|
1732 |
int(round((self.Pos.y - 2) * scaley)) - 2), |
|
1733 |
wx.Point(int(round((self.Pos.x + self.Size[0] + 2) * scalex)) + 4, |
|
1734 |
int(round((self.Pos.y - 2) * scaley)) - 2), |
|
1735 |
wx.Point(int(round((self.Pos.x + self.Size[0] / 2) * scalex)), |
|
1736 |
int(round((self.Pos.y + self.Size[1] + 3) * scaley)) + 4)] |
|
1737 |
dc.DrawPolygon(points) |
|
1738 |
dc.SetLogicalFunction(wx.COPY) |
|
1739 |
dc.SetUserScale(scalex, scaley) |
|
1740 |
||
1741 |
# Draws divergence |
|
1742 |
def Draw(self, dc): |
|
1743 |
Graphic_Element.Draw(self, dc) |
|
1744 |
if self.Value: |
|
1745 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
1746 |
dc.SetBrush(wx.GREEN_BRUSH) |
|
1747 |
else: |
|
1748 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
1749 |
dc.SetBrush(wx.BLACK_BRUSH) |
|
1750 |
||
1751 |
if getattr(dc, "printing", False): |
|
1752 |
target_size = dc.GetTextExtent(self.Target) |
|
1753 |
else: |
|
1754 |
target_size = self.TargetSize |
|
1755 |
||
1756 |
# Draw plain rectangle for representing the divergence |
|
1757 |
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]) |
|
1758 |
points = [wx.Point(self.Pos.x, self.Pos.y), |
|
1759 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3), |
|
1760 |
wx.Point(self.Pos.x + self.Size[0], self.Pos.y), |
|
1761 |
wx.Point(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])] |
|
1762 |
dc.DrawPolygon(points) |
|
1763 |
target_pos = (self.Pos.x + self.Size[0] + 2, |
|
1764 |
self.Pos.y + (self.Size[1] - target_size[1]) / 2) |
|
1765 |
dc.DrawText(self.Target, target_pos[0], target_pos[1]) |
|
1766 |
# Draw input connector |
|
1767 |
if self.Input: |
|
1768 |
self.Input.Draw(dc) |
|
1769 |
||
1770 |
if not getattr(dc, "printing", False): |
|
1771 |
DrawHighlightedText(dc, self.Target, self.Highlights, target_pos[0], target_pos[1]) |
|
1772 |
||
1773 |
||
1774 |
#------------------------------------------------------------------------------- |
|
1775 |
# Sequencial Function Chart Action Block |
|
1776 |
#------------------------------------------------------------------------------- |
|
1777 |
||
1778 |
""" |
|
1779 |
Class that implements the graphic representation of an action block |
|
1780 |
""" |
|
1781 |
||
1782 |
class SFC_ActionBlock(Graphic_Element): |
|
1783 |
||
1784 |
# Create a new action block |
|
1785 |
def __init__(self, parent, actions = [], id = None): |
|
1786 |
Graphic_Element.__init__(self, parent) |
|
1787 |
self.Id = id |
|
1788 |
self.Size = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
|
1789 |
self.MinSize = wx.Size(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1]) |
|
1790 |
self.Highlights = {} |
|
1791 |
# Create an input and output connector |
|
1792 |
self.Input = Connector(self, "", None, wx.Point(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST, onlyone = True) |
|
1793 |
self.SetActions(actions) |
|
1794 |
self.Value = None |
|
1795 |
self.PreviousValue = None |
|
1796 |
||
1797 |
def Flush(self): |
|
1798 |
if self.Input is not None: |
|
1799 |
self.Input.Flush() |
|
1800 |
self.Input = None |
|
1801 |
||
1802 |
def SpreadCurrent(self): |
|
1803 |
if self.Parent.Debug: |
|
1804 |
self.PreviousValue = self.Value |
|
1805 |
self.Value = self.Input.ReceivingCurrent() |
|
1806 |
if self.Value != self.PreviousValue and self.Visible: |
|
1807 |
self.Parent.ElementNeedRefresh(self) |
|
1808 |
||
1809 |
# Make a clone of this SFC_ActionBlock |
|
1810 |
def Clone(self, parent, id = None, pos = None): |
|
1811 |
actions = [action.copy() for action in self.Actions] |
|
1812 |
action_block = SFC_ActionBlock(parent, actions, id) |
|
1813 |
action_block.SetSize(self.Size[0], self.Size[1]) |
|
1814 |
if pos is not None: |
|
1815 |
action_block.SetPosition(pos.x, pos.y) |
|
1816 |
else: |
|
1817 |
action_block.SetPosition(self.Pos.x, self.Pos.y) |
|
1818 |
action_block.Input = self.Input.Clone(action_block) |
|
1819 |
return action_block |
|
1820 |
||
1821 |
def GetConnectorTranslation(self, element): |
|
1822 |
return {self.Input : element.Input} |
|
1823 |
||
1824 |
# Returns the RedrawRect |
|
1825 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
1826 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
1827 |
rect = rect.Union(self.Input.GetRedrawRect(movex, movey)) |
|
1828 |
if movex != 0 or movey != 0: |
|
1829 |
if self.Input.IsConnected(): |
|
1830 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
1831 |
return rect |
|
1832 |
||
1833 |
# Returns the number of action lines |
|
1834 |
def GetLineNumber(self): |
|
1835 |
return len(self.Actions) |
|
1836 |
||
1837 |
def GetLineSize(self): |
|
1838 |
if len(self.Actions) > 0: |
|
1839 |
return self.Size[1] / len(self.Actions) |
|
1840 |
else: |
|
1841 |
return SFC_ACTION_MIN_SIZE[1] |
|
1842 |
||
1843 |
# Forbids to resize the action block |
|
1844 |
def Resize(self, x, y, width, height): |
|
1845 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1846 |
if x == 0: |
|
1847 |
self.SetSize(width, self.Size[1]) |
|
1848 |
else: |
|
1849 |
Graphic_Element.Resize(self, x, y, width, height) |
|
1850 |
||
1851 |
# Delete this action block by calling the appropriate method |
|
1852 |
def Delete(self): |
|
1853 |
self.Parent.DeleteActionBlock(self) |
|
1854 |
||
1855 |
# Unconnect input and output |
|
1856 |
def Clean(self): |
|
1857 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
|
1858 |
||
1859 |
# Refresh the action block bounding box |
|
1860 |
def RefreshBoundingBox(self): |
|
1861 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
1862 |
||
1863 |
# Refresh the position of wires connected to action block |
|
1864 |
def RefreshConnected(self, exclude = []): |
|
1865 |
self.Input.MoveConnected(exclude) |
|
1866 |
||
1867 |
# Returns input action block connector |
|
1868 |
def GetConnector(self, position = None, name = None): |
|
1869 |
return self.Input |
|
1870 |
||
1871 |
# Returns all the action block connectors |
|
1872 |
def GetConnectors(self): |
|
1873 |
return {"inputs": [self.Input], "outputs": []} |
|
1874 |
||
1875 |
# Test if point given is on action block input connector |
|
1876 |
def TestConnector(self, pt, direction = None, exclude = True): |
|
1877 |
# Test input connector |
|
1878 |
if self.Input.TestPoint(pt, direction, exclude): |
|
1879 |
return self.Input |
|
1880 |
return None |
|
1881 |
||
1882 |
# Refresh the element connectors position |
|
1883 |
def RefreshConnectors(self): |
|
1884 |
scaling = self.Parent.GetScaling() |
|
1885 |
vertical_pos = SFC_ACTION_MIN_SIZE[1] / 2 |
|
1886 |
if scaling is not None: |
|
1887 |
vertical_pos = round(float(self.Pos.y + vertical_pos) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
1888 |
self.Input.SetPosition(wx.Point(0, vertical_pos)) |
|
1889 |
self.RefreshConnected() |
|
1890 |
||
1891 |
# Changes the action block actions |
|
1892 |
def SetActions(self, actions): |
|
1893 |
self.Actions = actions |
|
1894 |
self.ColSize = [0, 0, 0] |
|
1895 |
min_height = 0 |
|
1896 |
for action in self.Actions: |
|
1339 | 1897 |
width, height = self.Parent.GetTextExtent( |
1898 |
action.qualifier if action.qualifier != "" else "N") |
|
814 | 1899 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
1900 |
row_height = height |
|
1339 | 1901 |
if action.duration != "": |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
1902 |
width, height = self.Parent.GetTextExtent(action.duration) |
814 | 1903 |
row_height = max(row_height, height) |
1904 |
self.ColSize[0] = max(self.ColSize[0], width + 10) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
1905 |
width, height = self.Parent.GetTextExtent(action.value) |
814 | 1906 |
row_height = max(row_height, height) |
1907 |
self.ColSize[1] = max(self.ColSize[1], width + 10) |
|
1339 | 1908 |
if action.indicator != "": |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
1909 |
width, height = self.Parent.GetTextExtent(action.indicator) |
814 | 1910 |
row_height = max(row_height, height) |
1911 |
self.ColSize[2] = max(self.ColSize[2], width + 10) |
|
1912 |
min_height += row_height + 5 |
|
1913 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
1914 |
self.Size = wx.Size(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], max(min_height, SFC_ACTION_MIN_SIZE[1], self.Size[1])) |
|
1915 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1916 |
SFC_ACTION_MIN_SIZE[0]), max(SFC_ACTION_MIN_SIZE[1], min_height) |
|
1917 |
self.RefreshBoundingBox() |
|
1918 |
else: |
|
1919 |
self.Size = wx.Size(max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1920 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1]) |
|
1921 |
self.MinSize = max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2], |
|
1922 |
SFC_ACTION_MIN_SIZE[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1] |
|
1923 |
self.RefreshBoundingBox() |
|
1339 | 1924 |
if self.Input is not None: |
814 | 1925 |
wires = self.Input.GetWires() |
1926 |
if len(wires) == 1: |
|
1927 |
input_block = wires[0][0].GetOtherConnected(self.Input).GetParentBlock() |
|
1928 |
input_block.RefreshOutputPosition() |
|
1929 |
input_block.RefreshOutputModel(True) |
|
1930 |
||
1931 |
# Returns the action block actions |
|
1932 |
def GetActions(self): |
|
1933 |
return self.Actions |
|
1934 |
||
1935 |
# Returns the action block minimum size |
|
1936 |
def GetMinSize(self): |
|
1937 |
return self.MinSize |
|
1938 |
||
1939 |
# Method called when a LeftDClick event have been generated |
|
1940 |
def OnLeftDClick(self, event, dc, scaling): |
|
1941 |
# Edit the action block properties |
|
1942 |
self.Parent.EditActionBlockContent(self) |
|
1943 |
||
1944 |
# Method called when a RightUp event have been generated |
|
1945 |
def OnRightUp(self, event, dc, scaling): |
|
1946 |
# Popup the default menu |
|
1947 |
self.Parent.PopupDefaultMenu() |
|
1948 |
||
1949 |
# Refreshes the action block state according to move defined and handle selected |
|
1950 |
def ProcessDragging(self, movex, movey, event, scaling): |
|
1951 |
if self.Parent.GetDrawingMode() != FREEDRAWING_MODE: |
|
1952 |
handle_type, handle = self.Handle |
|
1953 |
if handle_type == HANDLE_MOVE: |
|
1954 |
movex = max(-self.BoundingBox.x, movex) |
|
1955 |
if scaling is not None: |
|
1956 |
movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x |
|
1957 |
wires = self.Input.GetWires() |
|
1958 |
if len(wires) == 1: |
|
1959 |
input_pos = wires[0][0].GetOtherConnected(self.Input).GetPosition(False) |
|
1960 |
if self.Pos.x - input_pos.x + movex >= SFC_WIRE_MIN_SIZE: |
|
1961 |
self.Move(movex, 0) |
|
1962 |
return movex, 0 |
|
1963 |
return 0, 0 |
|
1964 |
else: |
|
1965 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1966 |
else: |
|
1967 |
return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling) |
|
1968 |
||
1969 |
||
1970 |
# Refreshes the action block model |
|
1971 |
def RefreshModel(self, move=True): |
|
1972 |
self.Parent.RefreshActionBlockModel(self) |
|
1973 |
||
1974 |
# Adds an highlight to the variable |
|
1975 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
1976 |
if infos[0] == "action" and infos[1] < len(self.Actions): |
|
1977 |
action_highlights = self.Highlights.setdefault(infos[1], {}) |
|
1978 |
attribute_highlights = action_highlights.setdefault(infos[2], []) |
|
1979 |
AddHighlight(attribute_highlights, (start, end, highlight_type)) |
|
1980 |
||
1981 |
# Removes an highlight from the block |
|
1982 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
1983 |
if infos[0] == "action" and infos[1] < len(self.Actions): |
|
1984 |
action_highlights = self.Highlights.get(infos[1], {}) |
|
1985 |
attribute_highlights = action_highlights.setdefault(infos[2], []) |
|
1986 |
if RemoveHighlight(attribute_highlights, (start, end, highlight_type)) and len(attribute_highlights) == 0: |
|
1987 |
action_highlights.pop(infos[2]) |
|
1988 |
if len(action_highlights) == 0: |
|
1989 |
self.Highlights.pop(infos[1]) |
|
1990 |
||
1991 |
# Removes all the highlights of one particular type from the block |
|
1992 |
def ClearHighlight(self, highlight_type=None): |
|
1993 |
if highlight_type is None: |
|
1994 |
self.Highlights = {} |
|
1995 |
else: |
|
1996 |
highlight_items = self.Highlights.items() |
|
1997 |
for number, action_highlights in highlight_items: |
|
1998 |
action_highlight_items = action_highlights.items() |
|
1999 |
for name, attribute_highlights in action_highlights: |
|
2000 |
attribute_highlights = ClearHighlights(attribute_highlights, highlight_type) |
|
2001 |
if len(attribute_highlights) == 0: |
|
2002 |
action_highlights.pop(name) |
|
2003 |
if len(action_highlights) == 0: |
|
2004 |
self.Highlights.pop(number) |
|
2005 |
||
2006 |
# Draws divergence |
|
2007 |
def Draw(self, dc): |
|
2008 |
Graphic_Element.Draw(self, dc) |
|
2009 |
if self.Value: |
|
2010 |
dc.SetPen(MiterPen(wx.GREEN)) |
|
2011 |
else: |
|
2012 |
dc.SetPen(MiterPen(wx.BLACK)) |
|
2013 |
dc.SetBrush(wx.WHITE_BRUSH) |
|
2014 |
colsize = [self.ColSize[0], self.Size[0] - self.ColSize[0] - self.ColSize[2], self.ColSize[2]] |
|
2015 |
# Draw plain rectangle for representing the action block |
|
2016 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
2017 |
dc.DrawLine(self.Pos.x + colsize[0], self.Pos.y, |
|
2018 |
self.Pos.x + colsize[0], self.Pos.y + self.Size[1]) |
|
2019 |
dc.DrawLine(self.Pos.x + colsize[0] + colsize[1], self.Pos.y, |
|
2020 |
self.Pos.x + colsize[0] + colsize[1], self.Pos.y + self.Size[1]) |
|
2021 |
line_size = self.GetLineSize() |
|
2022 |
for i, action in enumerate(self.Actions): |
|
2023 |
if i != 0: |
|
2024 |
dc.DrawLine(self.Pos.x, self.Pos.y + i * line_size, |
|
2025 |
self.Pos.x + self.Size[0], self.Pos.y + i * line_size) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2026 |
qualifier_size = dc.GetTextExtent(action.qualifier) |
1339 | 2027 |
if action.duration != "": |
814 | 2028 |
qualifier_pos = (self.Pos.x + (colsize[0] - qualifier_size[0]) / 2, |
2029 |
self.Pos.y + i * line_size + line_size / 2 - qualifier_size[1]) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2030 |
duration_size = dc.GetTextExtent(action.duration) |
814 | 2031 |
duration_pos = (self.Pos.x + (colsize[0] - duration_size[0]) / 2, |
2032 |
self.Pos.y + i * line_size + line_size / 2) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2033 |
dc.DrawText(action.duration, duration_pos[0], duration_pos[1]) |
814 | 2034 |
else: |
2035 |
qualifier_pos = (self.Pos.x + (colsize[0] - qualifier_size[0]) / 2, |
|
2036 |
self.Pos.y + i * line_size + (line_size - qualifier_size[1]) / 2) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2037 |
dc.DrawText(action.qualifier, qualifier_pos[0], qualifier_pos[1]) |
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2038 |
content_size = dc.GetTextExtent(action.value) |
814 | 2039 |
content_pos = (self.Pos.x + colsize[0] + (colsize[1] - content_size[0]) / 2, |
2040 |
self.Pos.y + i * line_size + (line_size - content_size[1]) / 2) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2041 |
dc.DrawText(action.value, content_pos[0], content_pos[1]) |
1339 | 2042 |
if action.indicator != "": |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2043 |
indicator_size = dc.GetTextExtent(action.indicator) |
814 | 2044 |
indicator_pos = (self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - indicator_size[0]) / 2, |
2045 |
self.Pos.y + i * line_size + (line_size - indicator_size[1]) / 2) |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2046 |
dc.DrawText(action.indicator, indicator_pos[0], indicator_pos[1]) |
814 | 2047 |
|
2048 |
if not getattr(dc, "printing", False): |
|
2049 |
action_highlights = self.Highlights.get(i, {}) |
|
2050 |
for name, attribute_highlights in action_highlights.iteritems(): |
|
2051 |
if name == "qualifier": |
|
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2052 |
DrawHighlightedText(dc, action.qualifier, attribute_highlights, qualifier_pos[0], qualifier_pos[1]) |
814 | 2053 |
elif name == "duration": |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2054 |
DrawHighlightedText(dc, action.duration, attribute_highlights, duration_pos[0], duration_pos[1]) |
814 | 2055 |
elif name in ["reference", "inline"]: |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2056 |
DrawHighlightedText(dc, action.value, attribute_highlights, content_pos[0], content_pos[1]) |
814 | 2057 |
elif name == "indicator": |
1338
c1e6c712cc35
Replaced old graphic viewer blocks loading process by xslt stylesheet
Laurent Bessard
parents:
1258
diff
changeset
|
2058 |
DrawHighlightedText(dc, action.indicator, attribute_highlights, indicator_pos[0], indicator_pos[1]) |
814 | 2059 |
|
2060 |
# Draw input connector |
|
2061 |
self.Input.Draw(dc) |
|
2062 |