40 """ |
40 """ |
41 Class that implements the graphic representation of a function block |
41 Class that implements the graphic representation of a function block |
42 """ |
42 """ |
43 |
43 |
44 # Create a new block |
44 # Create a new block |
45 def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}, executionControl = False, executionOrder = 0): |
45 def __init__(self, parent, type, name, id=None, extension=0, inputs=None, connectors={}, executionControl=False, executionOrder=0): |
46 Graphic_Element.__init__(self, parent) |
46 Graphic_Element.__init__(self, parent) |
47 self.Type = None |
47 self.Type = None |
48 self.Description = None |
48 self.Description = None |
49 self.Extension = None |
49 self.Extension = None |
50 self.ExecutionControl = False |
50 self.ExecutionControl = False |
57 self.Pen = MiterPen(wx.BLACK) |
57 self.Pen = MiterPen(wx.BLACK) |
58 self.SetType(type, extension, inputs, connectors, executionControl) |
58 self.SetType(type, extension, inputs, connectors, executionControl) |
59 self.Highlights = {} |
59 self.Highlights = {} |
60 |
60 |
61 # Make a clone of this FBD_Block |
61 # Make a clone of this FBD_Block |
62 def Clone(self, parent, id = None, name = "", pos = None): |
62 def Clone(self, parent, id=None, name="", pos=None): |
63 if self.Name != "" and name == "": |
63 if self.Name != "" and name == "": |
64 name = self.Name |
64 name = self.Name |
65 block = FBD_Block(parent, self.Type, name, id, self.Extension) |
65 block = FBD_Block(parent, self.Type, name, id, self.Extension) |
66 block.SetSize(self.Size[0], self.Size[1]) |
66 block.SetSize(self.Size[0], self.Size[1]) |
67 if pos is not None: |
67 if pos is not None: |
82 for output in self.Outputs: |
82 for output in self.Outputs: |
83 output.Flush() |
83 output.Flush() |
84 self.Outputs = [] |
84 self.Outputs = [] |
85 |
85 |
86 # Returns the RedrawRect |
86 # Returns the RedrawRect |
87 def GetRedrawRect(self, movex = 0, movey = 0): |
87 def GetRedrawRect(self, movex=0, movey=0): |
88 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
88 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
89 if movex != 0 or movey != 0: |
89 if movex != 0 or movey != 0: |
90 for input in self.Inputs: |
90 for input in self.Inputs: |
91 if input.IsConnected(): |
91 if input.IsConnected(): |
92 rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
92 rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
100 self.Parent.DeleteBlock(self) |
100 self.Parent.DeleteBlock(self) |
101 |
101 |
102 # Unconnect all inputs and outputs |
102 # Unconnect all inputs and outputs |
103 def Clean(self): |
103 def Clean(self): |
104 for input in self.Inputs: |
104 for input in self.Inputs: |
105 input.UnConnect(delete = True) |
105 input.UnConnect(delete=True) |
106 for output in self.Outputs: |
106 for output in self.Outputs: |
107 output.UnConnect(delete = True) |
107 output.UnConnect(delete=True) |
108 |
108 |
109 # Refresh the size of text for name |
109 # Refresh the size of text for name |
110 def RefreshNameSize(self): |
110 def RefreshNameSize(self): |
111 self.NameSize = self.Parent.GetTextExtent(self.Name) |
111 self.NameSize = self.Parent.GetTextExtent(self.Name) |
112 |
112 |
171 self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos)) |
171 self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos)) |
172 position += linesize |
172 position += linesize |
173 self.RefreshConnected() |
173 self.RefreshConnected() |
174 |
174 |
175 # Refresh the positions of wires connected to inputs and outputs |
175 # Refresh the positions of wires connected to inputs and outputs |
176 def RefreshConnected(self, exclude = []): |
176 def RefreshConnected(self, exclude=[]): |
177 for input in self.Inputs: |
177 for input in self.Inputs: |
178 input.MoveConnected(exclude) |
178 input.MoveConnected(exclude) |
179 for output in self.Outputs: |
179 for output in self.Outputs: |
180 output.MoveConnected(exclude) |
180 output.MoveConnected(exclude) |
181 |
181 |
182 # Returns the block connector that starts with the point given if it exists |
182 # Returns the block connector that starts with the point given if it exists |
183 def GetConnector(self, position, output_name = None, input_name = None): |
183 def GetConnector(self, position, output_name=None, input_name=None): |
184 if input_name is not None: |
184 if input_name is not None: |
185 # Test each input connector |
185 # Test each input connector |
186 for input in self.Inputs: |
186 for input in self.Inputs: |
187 if input_name == input.GetName(): |
187 if input_name == input.GetName(): |
188 return input |
188 return input |
221 # Returns all the block connectors |
221 # Returns all the block connectors |
222 def GetConnectors(self): |
222 def GetConnectors(self): |
223 return {"inputs": self.Inputs, "outputs": self.Outputs} |
223 return {"inputs": self.Inputs, "outputs": self.Outputs} |
224 |
224 |
225 # Test if point given is on one of the block connectors |
225 # Test if point given is on one of the block connectors |
226 def TestConnector(self, pt, direction = None, exclude = True): |
226 def TestConnector(self, pt, direction=None, exclude=True): |
227 # Test each input connector |
227 # Test each input connector |
228 for input in self.Inputs: |
228 for input in self.Inputs: |
229 if input.TestPoint(pt, direction, exclude): |
229 if input.TestPoint(pt, direction, exclude): |
230 return input |
230 return input |
231 # Test each output connector |
231 # Test each output connector |
233 if output.TestPoint(pt, direction, exclude): |
233 if output.TestPoint(pt, direction, exclude): |
234 return output |
234 return output |
235 return None |
235 return None |
236 |
236 |
237 # Changes the block type |
237 # Changes the block type |
238 def SetType(self, type, extension, inputs = None, connectors = {}, executionControl = False): |
238 def SetType(self, type, extension, inputs=None, connectors={}, executionControl=False): |
239 if type != self.Type or self.Extension != extension or executionControl != self.ExecutionControl: |
239 if type != self.Type or self.Extension != extension or executionControl != self.ExecutionControl: |
240 if type != self.Type: |
240 if type != self.Type: |
241 self.Type = type |
241 self.Type = type |
242 self.TypeSize = self.Parent.GetTextExtent(self.Type) |
242 self.TypeSize = self.Parent.GetTextExtent(self.Type) |
243 self.Extension = extension |
243 self.Extension = extension |
267 self.Pen = MiterPen(self.Colour) |
267 self.Pen = MiterPen(self.Colour) |
268 |
268 |
269 # Extract the inputs properties and create or modify the corresponding connector |
269 # Extract the inputs properties and create or modify the corresponding connector |
270 input_connectors = [] |
270 input_connectors = [] |
271 for input_name, input_type, input_modifier in inputs: |
271 for input_name, input_type, input_modifier in inputs: |
272 connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone = True) |
272 connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone=True) |
273 if input_modifier == "negated": |
273 if input_modifier == "negated": |
274 connector.SetNegated(True) |
274 connector.SetNegated(True) |
275 elif input_modifier != "none": |
275 elif input_modifier != "none": |
276 connector.SetEdge(input_modifier) |
276 connector.SetEdge(input_modifier) |
277 for input in self.Inputs: |
277 for input in self.Inputs: |
281 for wire in wires: |
281 for wire in wires: |
282 connector.Connect(wire) |
282 connector.Connect(wire) |
283 break |
283 break |
284 input_connectors.append(connector) |
284 input_connectors.append(connector) |
285 for input in self.Inputs: |
285 for input in self.Inputs: |
286 input.UnConnect(delete = True) |
286 input.UnConnect(delete=True) |
287 self.Inputs = input_connectors |
287 self.Inputs = input_connectors |
288 |
288 |
289 # Extract the outputs properties and create or modify the corresponding connector |
289 # Extract the outputs properties and create or modify the corresponding connector |
290 output_connectors = [] |
290 output_connectors = [] |
291 for output_name, output_type, output_modifier in outputs: |
291 for output_name, output_type, output_modifier in outputs: |
301 for wire in wires: |
301 for wire in wires: |
302 connector.Connect(wire) |
302 connector.Connect(wire) |
303 break |
303 break |
304 output_connectors.append(connector) |
304 output_connectors.append(connector) |
305 for output in self.Outputs: |
305 for output in self.Outputs: |
306 output.UnConnect(delete = True) |
306 output.UnConnect(delete=True) |
307 self.Outputs = output_connectors |
307 self.Outputs = output_connectors |
308 |
308 |
309 self.RefreshMinSize() |
309 self.RefreshMinSize() |
310 self.RefreshConnectors() |
310 self.RefreshConnectors() |
311 for output in self.Outputs: |
311 for output in self.Outputs: |
504 """ |
504 """ |
505 Class that implements the graphic representation of a variable |
505 Class that implements the graphic representation of a variable |
506 """ |
506 """ |
507 |
507 |
508 # Create a new variable |
508 # Create a new variable |
509 def __init__(self, parent, type, name, value_type, id = None, executionOrder = 0): |
509 def __init__(self, parent, type, name, value_type, id=None, executionOrder=0): |
510 Graphic_Element.__init__(self, parent) |
510 Graphic_Element.__init__(self, parent) |
511 self.Type = None |
511 self.Type = None |
512 self.ValueType = None |
512 self.ValueType = None |
513 self.Id = id |
513 self.Id = id |
514 self.SetName(name) |
514 self.SetName(name) |
517 self.Output = None |
517 self.Output = None |
518 self.SetType(type, value_type) |
518 self.SetType(type, value_type) |
519 self.Highlights = [] |
519 self.Highlights = [] |
520 |
520 |
521 # Make a clone of this FBD_Variable |
521 # Make a clone of this FBD_Variable |
522 def Clone(self, parent, id = None, pos = None): |
522 def Clone(self, parent, id=None, pos=None): |
523 variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id) |
523 variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id) |
524 variable.SetSize(self.Size[0], self.Size[1]) |
524 variable.SetSize(self.Size[0], self.Size[1]) |
525 if pos is not None: |
525 if pos is not None: |
526 variable.SetPosition(pos.x, pos.y) |
526 variable.SetPosition(pos.x, pos.y) |
527 else: |
527 else: |
547 if self.Output is not None: |
547 if self.Output is not None: |
548 self.Output.Flush() |
548 self.Output.Flush() |
549 self.Output = None |
549 self.Output = None |
550 |
550 |
551 # Returns the RedrawRect |
551 # Returns the RedrawRect |
552 def GetRedrawRect(self, movex = 0, movey = 0): |
552 def GetRedrawRect(self, movex=0, movey=0): |
553 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
553 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
554 if movex != 0 or movey != 0: |
554 if movex != 0 or movey != 0: |
555 if self.Input and self.Input.IsConnected(): |
555 if self.Input and self.Input.IsConnected(): |
556 rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
556 rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
557 if self.Output and self.Output.IsConnected(): |
557 if self.Output and self.Output.IsConnected(): |
559 return rect |
559 return rect |
560 |
560 |
561 # Unconnect connector |
561 # Unconnect connector |
562 def Clean(self): |
562 def Clean(self): |
563 if self.Input: |
563 if self.Input: |
564 self.Input.UnConnect(delete = True) |
564 self.Input.UnConnect(delete=True) |
565 if self.Output: |
565 if self.Output: |
566 self.Output.UnConnect(delete = True) |
566 self.Output.UnConnect(delete=True) |
567 |
567 |
568 # Delete this variable by calling the appropriate method |
568 # Delete this variable by calling the appropriate method |
569 def Delete(self): |
569 def Delete(self): |
570 self.Parent.DeleteVariable(self) |
570 self.Parent.DeleteVariable(self) |
571 |
571 |
608 if self.Output: |
608 if self.Output: |
609 self.Output.SetPosition(wx.Point(self.Size[0], position)) |
609 self.Output.SetPosition(wx.Point(self.Size[0], position)) |
610 self.RefreshConnected() |
610 self.RefreshConnected() |
611 |
611 |
612 # Refresh the position of wires connected to connector |
612 # Refresh the position of wires connected to connector |
613 def RefreshConnected(self, exclude = []): |
613 def RefreshConnected(self, exclude=[]): |
614 if self.Input: |
614 if self.Input: |
615 self.Input.MoveConnected(exclude) |
615 self.Input.MoveConnected(exclude) |
616 if self.Output: |
616 if self.Output: |
617 self.Output.MoveConnected(exclude) |
617 self.Output.MoveConnected(exclude) |
618 |
618 |
619 # Test if point given is on the variable connector |
619 # Test if point given is on the variable connector |
620 def TestConnector(self, pt, direction = None, exclude=True): |
620 def TestConnector(self, pt, direction=None, exclude=True): |
621 if self.Input and self.Input.TestPoint(pt, direction, exclude): |
621 if self.Input and self.Input.TestPoint(pt, direction, exclude): |
622 return self.Input |
622 return self.Input |
623 if self.Output and self.Output.TestPoint(pt, direction, exclude): |
623 if self.Output and self.Output.TestPoint(pt, direction, exclude): |
624 return self.Output |
624 return self.Output |
625 return None |
625 return None |
626 |
626 |
627 # Returns the block connector that starts with the point given if it exists |
627 # Returns the block connector that starts with the point given if it exists |
628 def GetConnector(self, position, name = None): |
628 def GetConnector(self, position, name=None): |
629 # if a name is given |
629 # if a name is given |
630 if name is not None: |
630 if name is not None: |
631 # Test input and output connector if they exists |
631 # Test input and output connector if they exists |
632 #if self.Input and name == self.Input.GetName(): |
632 #if self.Input and name == self.Input.GetName(): |
633 # return self.Input |
633 # return self.Input |
663 if type != self.Type: |
663 if type != self.Type: |
664 self.Type = type |
664 self.Type = type |
665 # Create an input or output connector according to variable type |
665 # Create an input or output connector according to variable type |
666 if self.Type != INPUT: |
666 if self.Type != INPUT: |
667 if self.Input is None: |
667 if self.Input is None: |
668 self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True) |
668 self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone=True) |
669 elif self.Input: |
669 elif self.Input: |
670 self.Input.UnConnect(delete = True) |
670 self.Input.UnConnect(delete=True) |
671 self.Input = None |
671 self.Input = None |
672 if self.Type != OUTPUT: |
672 if self.Type != OUTPUT: |
673 if self.Output is None: |
673 if self.Output is None: |
674 self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) |
674 self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) |
675 elif self.Output: |
675 elif self.Output: |
676 self.Output.UnConnect(delete = True) |
676 self.Output.UnConnect(delete=True) |
677 self.Output = None |
677 self.Output = None |
678 self.RefreshConnectors() |
678 self.RefreshConnectors() |
679 self.RefreshBoundingBox() |
679 self.RefreshBoundingBox() |
680 elif value_type != self.ValueType: |
680 elif value_type != self.ValueType: |
681 if self.Input: |
681 if self.Input: |
800 """ |
800 """ |
801 Class that implements the graphic representation of a connection |
801 Class that implements the graphic representation of a connection |
802 """ |
802 """ |
803 |
803 |
804 # Create a new connection |
804 # Create a new connection |
805 def __init__(self, parent, type, name, id = None): |
805 def __init__(self, parent, type, name, id=None): |
806 Graphic_Element.__init__(self, parent) |
806 Graphic_Element.__init__(self, parent) |
807 self.Type = type |
807 self.Type = type |
808 self.Id = id |
808 self.Id = id |
809 self.SetName(name) |
809 self.SetName(name) |
810 self.Pos = wx.Point(0, 0) |
810 self.Pos = wx.Point(0, 0) |
811 self.Size = wx.Size(0, 0) |
811 self.Size = wx.Size(0, 0) |
812 self.Highlights = [] |
812 self.Highlights = [] |
813 # Create an input or output connector according to connection type |
813 # Create an input or output connector according to connection type |
814 if self.Type == CONNECTOR: |
814 if self.Type == CONNECTOR: |
815 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
815 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True) |
816 else: |
816 else: |
817 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
817 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
818 self.RefreshConnectors() |
818 self.RefreshConnectors() |
819 self.RefreshNameSize() |
819 self.RefreshNameSize() |
820 |
820 |
822 if self.Connector: |
822 if self.Connector: |
823 self.Connector.Flush() |
823 self.Connector.Flush() |
824 self.Connector = None |
824 self.Connector = None |
825 |
825 |
826 # Returns the RedrawRect |
826 # Returns the RedrawRect |
827 def GetRedrawRect(self, movex = 0, movey = 0): |
827 def GetRedrawRect(self, movex=0, movey=0): |
828 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
828 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
829 if movex != 0 or movey != 0: |
829 if movex != 0 or movey != 0: |
830 if self.Connector and self.Connector.IsConnected(): |
830 if self.Connector and self.Connector.IsConnected(): |
831 rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
831 rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
832 return rect |
832 return rect |
833 |
833 |
834 # Make a clone of this FBD_Connector |
834 # Make a clone of this FBD_Connector |
835 def Clone(self, parent, id = None, pos = None): |
835 def Clone(self, parent, id=None, pos=None): |
836 connection = FBD_Connector(parent, self.Type, self.Name, id) |
836 connection = FBD_Connector(parent, self.Type, self.Name, id) |
837 connection.SetSize(self.Size[0], self.Size[1]) |
837 connection.SetSize(self.Size[0], self.Size[1]) |
838 if pos is not None: |
838 if pos is not None: |
839 connection.SetPosition(pos.x, pos.y) |
839 connection.SetPosition(pos.x, pos.y) |
840 else: |
840 else: |
846 return {self.Connector: element.Connector} |
846 return {self.Connector: element.Connector} |
847 |
847 |
848 # Unconnect connector |
848 # Unconnect connector |
849 def Clean(self): |
849 def Clean(self): |
850 if self.Connector: |
850 if self.Connector: |
851 self.Connector.UnConnect(delete = True) |
851 self.Connector.UnConnect(delete=True) |
852 |
852 |
853 # Delete this connection by calling the appropriate method |
853 # Delete this connection by calling the appropriate method |
854 def Delete(self): |
854 def Delete(self): |
855 self.Parent.DeleteConnection(self) |
855 self.Parent.DeleteConnection(self) |
856 |
856 |
879 else: |
879 else: |
880 self.Connector.SetPosition(wx.Point(self.Size[0], position)) |
880 self.Connector.SetPosition(wx.Point(self.Size[0], position)) |
881 self.RefreshConnected() |
881 self.RefreshConnected() |
882 |
882 |
883 # Refresh the position of wires connected to connector |
883 # Refresh the position of wires connected to connector |
884 def RefreshConnected(self, exclude = []): |
884 def RefreshConnected(self, exclude=[]): |
885 if self.Connector: |
885 if self.Connector: |
886 self.Connector.MoveConnected(exclude) |
886 self.Connector.MoveConnected(exclude) |
887 |
887 |
888 # Test if point given is on the connection connector |
888 # Test if point given is on the connection connector |
889 def TestConnector(self, pt, direction = None, exclude=True): |
889 def TestConnector(self, pt, direction=None, exclude=True): |
890 if self.Connector and self.Connector.TestPoint(pt, direction, exclude): |
890 if self.Connector and self.Connector.TestPoint(pt, direction, exclude): |
891 return self.Connector |
891 return self.Connector |
892 return None |
892 return None |
893 |
893 |
894 # Returns the connection connector |
894 # Returns the connection connector |
895 def GetConnector(self, position = None, name = None): |
895 def GetConnector(self, position=None, name=None): |
896 return self.Connector |
896 return self.Connector |
897 |
897 |
898 # Returns all the block connectors |
898 # Returns all the block connectors |
899 def GetConnectors(self): |
899 def GetConnectors(self): |
900 connectors = {"inputs": [], "outputs": []} |
900 connectors = {"inputs": [], "outputs": []} |
917 if type != self.Type: |
917 if type != self.Type: |
918 self.Type = type |
918 self.Type = type |
919 self.Clean() |
919 self.Clean() |
920 # Create an input or output connector according to connection type |
920 # Create an input or output connector according to connection type |
921 if self.Type == CONNECTOR: |
921 if self.Type == CONNECTOR: |
922 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
922 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True) |
923 else: |
923 else: |
924 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
924 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
925 self.RefreshConnectors() |
925 self.RefreshConnectors() |
926 self.RefreshBoundingBox() |
926 self.RefreshBoundingBox() |
927 |
927 |