68 else: |
70 else: |
69 block.SetPosition(self.Pos.x, self.Pos.y) |
71 block.SetPosition(self.Pos.x, self.Pos.y) |
70 block.Inputs = [input.Clone(block) for input in self.Inputs] |
72 block.Inputs = [input.Clone(block) for input in self.Inputs] |
71 block.Outputs = [output.Clone(block) for output in self.Outputs] |
73 block.Outputs = [output.Clone(block) for output in self.Outputs] |
72 return block |
74 return block |
73 |
75 |
74 def GetConnectorTranslation(self, element): |
76 def GetConnectorTranslation(self, element): |
75 return dict(zip(self.Inputs + self.Outputs, element.Inputs + element.Outputs)) |
77 return dict(zip(self.Inputs + self.Outputs, element.Inputs + element.Outputs)) |
76 |
78 |
77 def Flush(self): |
79 def Flush(self): |
78 for input in self.Inputs: |
80 for input in self.Inputs: |
79 input.Flush() |
81 input.Flush() |
80 self.Inputs = [] |
82 self.Inputs = [] |
81 for output in self.Outputs: |
83 for output in self.Outputs: |
82 output.Flush() |
84 output.Flush() |
83 self.Outputs = [] |
85 self.Outputs = [] |
84 |
86 |
85 # Returns the RedrawRect |
87 # Returns the RedrawRect |
86 def GetRedrawRect(self, movex = 0, movey = 0): |
88 def GetRedrawRect(self, movex=0, movey=0): |
87 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
89 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
88 if movex != 0 or movey != 0: |
90 if movex != 0 or movey != 0: |
89 for input in self.Inputs: |
91 for input in self.Inputs: |
90 if input.IsConnected(): |
92 if input.IsConnected(): |
91 rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
93 rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
92 for output in self.Outputs: |
94 for output in self.Outputs: |
93 if output.IsConnected(): |
95 if output.IsConnected(): |
94 rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) |
96 rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) |
95 return rect |
97 return rect |
96 |
98 |
97 # Delete this block by calling the appropriate method |
99 # Delete this block by calling the appropriate method |
98 def Delete(self): |
100 def Delete(self): |
99 self.Parent.DeleteBlock(self) |
101 self.Parent.DeleteBlock(self) |
100 |
102 |
101 # Unconnect all inputs and outputs |
103 # Unconnect all inputs and outputs |
102 def Clean(self): |
104 def Clean(self): |
103 for input in self.Inputs: |
105 for input in self.Inputs: |
104 input.UnConnect(delete = True) |
106 input.UnConnect(delete=True) |
105 for output in self.Outputs: |
107 for output in self.Outputs: |
106 output.UnConnect(delete = True) |
108 output.UnConnect(delete=True) |
107 |
109 |
108 # Refresh the size of text for name |
110 # Refresh the size of text for name |
109 def RefreshNameSize(self): |
111 def RefreshNameSize(self): |
110 self.NameSize = self.Parent.GetTextExtent(self.Name) |
112 self.NameSize = self.Parent.GetTextExtent(self.Name) |
111 |
113 |
112 # Refresh the size of text for execution order |
114 # Refresh the size of text for execution order |
113 def RefreshExecutionOrderSize(self): |
115 def RefreshExecutionOrderSize(self): |
114 self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) |
116 self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) |
115 |
117 |
116 # Returns if the point given is in the bounding box |
118 # Returns if the point given is in the bounding box |
117 def HitTest(self, pt, connectors=True): |
119 def HitTest(self, pt, connectors=True): |
118 if self.Name != "": |
120 if self.Name != "": |
119 test_text = self.GetTextBoundingBox().InsideXY(pt.x, pt.y) |
121 test_text = self.GetTextBoundingBox().InsideXY(pt.x, pt.y) |
120 else: |
122 else: |
121 test_text = False |
123 test_text = False |
122 test_block = self.GetBlockBoundingBox(connectors).InsideXY(pt.x, pt.y) |
124 test_block = self.GetBlockBoundingBox(connectors).InsideXY(pt.x, pt.y) |
123 return test_text or test_block |
125 return test_text or test_block |
124 |
126 |
125 # Returns the bounding box of the name outside the block |
127 # Returns the bounding box of the name outside the block |
126 def GetTextBoundingBox(self): |
128 def GetTextBoundingBox(self): |
127 # Calculate the size of the name outside the block |
129 # Calculate the size of the name outside the block |
128 text_width, text_height = self.NameSize |
130 text_width, text_height = self.NameSize |
129 return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2, |
131 return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2, |
130 self.Pos.y - (text_height + 2), |
132 self.Pos.y - (text_height + 2), |
131 text_width, |
133 text_width, |
132 text_height) |
134 text_height) |
133 |
135 |
134 # Returns the bounding box of function block without name outside |
136 # Returns the bounding box of function block without name outside |
135 def GetBlockBoundingBox(self, connectors=True): |
137 def GetBlockBoundingBox(self, connectors=True): |
136 bbx_x, bbx_y = self.Pos.x, self.Pos.y |
138 bbx_x, bbx_y = self.Pos.x, self.Pos.y |
137 bbx_width, bbx_height = self.Size |
139 bbx_width, bbx_height = self.Size |
138 if connectors: |
140 if connectors: |
358 w, h = output.GetNameSize() |
360 w, h = output.GetNameSize() |
359 max_output = max(max_output, w) |
361 max_output = max(max_output, w) |
360 width = max(self.TypeSize[0] + 10, max_input + max_output + 15) |
362 width = max(self.TypeSize[0] + 10, max_input + max_output + 15) |
361 height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
363 height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
362 self.MinSize = width, height |
364 self.MinSize = width, height |
363 |
365 |
364 # Returns the block minimum size |
366 # Returns the block minimum size |
365 def GetMinSize(self): |
367 def GetMinSize(self): |
366 return self.MinSize |
368 return self.MinSize |
367 |
369 |
368 # Changes the negated property of the connector handled |
370 # Changes the negated property of the connector handled |
369 def SetConnectorNegated(self, negated): |
371 def SetConnectorNegated(self, negated): |
370 handle_type, handle = self.Handle |
372 handle_type, handle = self.Handle |
371 if handle_type == HANDLE_CONNECTOR: |
373 if handle_type == HANDLE_CONNECTOR: |
372 handle.SetNegated(negated) |
374 handle.SetNegated(negated) |
373 self.RefreshModel(False) |
375 self.RefreshModel(False) |
374 |
376 |
375 # Changes the edge property of the connector handled |
377 # Changes the edge property of the connector handled |
376 def SetConnectorEdge(self, edge): |
378 def SetConnectorEdge(self, edge): |
377 handle_type, handle = self.Handle |
379 handle_type, handle = self.Handle |
378 if handle_type == HANDLE_CONNECTOR: |
380 if handle_type == HANDLE_CONNECTOR: |
379 handle.SetEdge(edge) |
381 handle.SetEdge(edge) |
380 self.RefreshModel(False) |
382 self.RefreshModel(False) |
381 |
383 |
382 ## # Method called when a Motion event have been generated |
384 # # Method called when a Motion event have been generated |
383 ## def OnMotion(self, event, dc, scaling): |
385 # def OnMotion(self, event, dc, scaling): |
384 ## if not event.Dragging(): |
386 # if not event.Dragging(): |
385 ## pos = event.GetLogicalPosition(dc) |
387 # pos = event.GetLogicalPosition(dc) |
386 ## for input in self.Inputs: |
388 # for input in self.Inputs: |
387 ## rect = input.GetRedrawRect() |
389 # rect = input.GetRedrawRect() |
388 ## if rect.InsideXY(pos.x, pos.y): |
390 # if rect.InsideXY(pos.x, pos.y): |
389 ## print "Find input" |
391 # print "Find input" |
390 ## tip = wx.TipWindow(self.Parent, "Test") |
392 # tip = wx.TipWindow(self.Parent, "Test") |
391 ## tip.SetBoundingRect(rect) |
393 # tip.SetBoundingRect(rect) |
392 ## return Graphic_Element.OnMotion(self, event, dc, scaling) |
394 # return Graphic_Element.OnMotion(self, event, dc, scaling) |
393 |
395 |
394 # Method called when a LeftDClick event have been generated |
396 # Method called when a LeftDClick event have been generated |
395 def OnLeftDClick(self, event, dc, scaling): |
397 def OnLeftDClick(self, event, dc, scaling): |
396 # Edit the block properties |
398 # Edit the block properties |
397 self.Parent.EditBlockContent(self) |
399 self.Parent.EditBlockContent(self) |
398 |
400 |
399 # Method called when a RightUp event have been generated |
401 # Method called when a RightUp event have been generated |
400 def OnRightUp(self, event, dc, scaling): |
402 def OnRightUp(self, event, dc, scaling): |
401 pos = GetScaledEventPosition(event, dc, scaling) |
403 pos = GetScaledEventPosition(event, dc, scaling) |
402 # Popup the menu with special items for a block and a connector if one is handled |
404 # Popup the menu with special items for a block and a connector if one is handled |
403 connector = self.TestConnector(pos, exclude=False) |
405 connector = self.TestConnector(pos, exclude=False) |
404 if connector: |
406 if connector: |
405 self.Handle = (HANDLE_CONNECTOR, connector) |
407 self.Handle = (HANDLE_CONNECTOR, connector) |
406 self.Parent.PopupBlockMenu(connector) |
408 self.Parent.PopupBlockMenu(connector) |
407 else: |
409 else: |
408 self.Parent.PopupBlockMenu() |
410 self.Parent.PopupBlockMenu() |
409 |
411 |
410 # Refreshes the block model |
412 # Refreshes the block model |
411 def RefreshModel(self, move=True): |
413 def RefreshModel(self, move=True): |
412 self.Parent.RefreshBlockModel(self) |
414 self.Parent.RefreshBlockModel(self) |
413 # If block has moved, refresh the model of wires connected to outputs |
415 # If block has moved, refresh the model of wires connected to outputs |
414 if move: |
416 if move: |
415 for output in self.Outputs: |
417 for output in self.Outputs: |
416 output.RefreshWires() |
418 output.RefreshWires() |
417 |
419 |
418 def GetToolTipValue(self): |
420 def GetToolTipValue(self): |
419 return self.Description |
421 return self.Description |
420 |
422 |
421 # Adds an highlight to the block |
423 # Adds an highlight to the block |
422 def AddHighlight(self, infos, start, end ,highlight_type): |
424 def AddHighlight(self, infos, start, end, highlight_type): |
423 if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0: |
425 if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0: |
424 highlights = self.Highlights.setdefault(infos[0], []) |
426 highlights = self.Highlights.setdefault(infos[0], []) |
425 AddHighlight(highlights, (start, end, highlight_type)) |
427 AddHighlight(highlights, (start, end, highlight_type)) |
426 elif infos[0] == "input" and infos[1] < len(self.Inputs): |
428 elif infos[0] == "input" and infos[1] < len(self.Inputs): |
427 self.Inputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type) |
429 self.Inputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type) |
428 elif infos[0] == "output" and infos[1] < len(self.Outputs): |
430 elif infos[0] == "output" and infos[1] < len(self.Outputs): |
429 self.Outputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type) |
431 self.Outputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type) |
430 |
432 |
431 # Removes an highlight from the block |
433 # Removes an highlight from the block |
432 def RemoveHighlight(self, infos, start, end, highlight_type): |
434 def RemoveHighlight(self, infos, start, end, highlight_type): |
433 if infos[0] in ["type", "name"]: |
435 if infos[0] in ["type", "name"]: |
434 highlights = self.Highlights.get(infos[0], []) |
436 highlights = self.Highlights.get(infos[0], []) |
435 if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: |
437 if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0: |
436 self.Highlights.pop(infos[0]) |
438 self.Highlights.pop(infos[0]) |
437 elif infos[0] == "input" and infos[1] < len(self.Inputs): |
439 elif infos[0] == "input" and infos[1] < len(self.Inputs): |
438 self.Inputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type) |
440 self.Inputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type) |
439 elif infos[0] == "output" and infos[1] < len(self.Outputs): |
441 elif infos[0] == "output" and infos[1] < len(self.Outputs): |
440 self.Outputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type) |
442 self.Outputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type) |
441 |
443 |
442 # Removes all the highlights of one particular type from the block |
444 # Removes all the highlights of one particular type from the block |
443 def ClearHighlight(self, highlight_type=None): |
445 def ClearHighlight(self, highlight_type=None): |
444 if highlight_type is None: |
446 if highlight_type is None: |
445 self.Highlights = {} |
447 self.Highlights = {} |
446 else: |
448 else: |
638 connectors.append(self.Input) |
640 connectors.append(self.Input) |
639 # Test output connector if it exists |
641 # Test output connector if it exists |
640 if self.Output: |
642 if self.Output: |
641 connectors.append(self.Output) |
643 connectors.append(self.Output) |
642 return self.FindNearestConnector(position, connectors) |
644 return self.FindNearestConnector(position, connectors) |
643 |
645 |
644 # Returns all the block connectors |
646 # Returns all the block connectors |
645 def GetConnectors(self): |
647 def GetConnectors(self): |
646 connectors = {"inputs": [], "outputs": []} |
648 connectors = {"inputs": [], "outputs": []} |
647 if self.Input: |
649 if self.Input: |
648 connectors["inputs"].append(self.Input) |
650 connectors["inputs"].append(self.Input) |
649 if self.Output: |
651 if self.Output: |
650 connectors["outputs"].append(self.Output) |
652 connectors["outputs"].append(self.Output) |
651 return connectors |
653 return connectors |
652 |
654 |
653 # Changes the negated property of the variable connector if handled |
655 # Changes the negated property of the variable connector if handled |
654 def SetConnectorNegated(self, negated): |
656 def SetConnectorNegated(self, negated): |
655 handle_type, handle = self.Handle |
657 handle_type, handle = self.Handle |
656 if handle_type == HANDLE_CONNECTOR: |
658 if handle_type == HANDLE_CONNECTOR: |
657 handle.SetNegated(negated) |
659 handle.SetNegated(negated) |
658 self.RefreshModel(False) |
660 self.RefreshModel(False) |
659 |
661 |
660 # Changes the variable type |
662 # Changes the variable type |
661 def SetType(self, type, value_type): |
663 def SetType(self, type, value_type): |
662 if type != self.Type: |
664 if type != self.Type: |
663 self.Type = type |
665 self.Type = type |
664 # Create an input or output connector according to variable type |
666 # Create an input or output connector according to variable type |
665 if self.Type != INPUT: |
667 if self.Type != INPUT: |
666 if self.Input is None: |
668 if self.Input is None: |
667 self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True) |
669 self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone=True) |
668 elif self.Input: |
670 elif self.Input: |
669 self.Input.UnConnect(delete = True) |
671 self.Input.UnConnect(delete=True) |
670 self.Input = None |
672 self.Input = None |
671 if self.Type != OUTPUT: |
673 if self.Type != OUTPUT: |
672 if self.Output is None: |
674 if self.Output is None: |
673 self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) |
675 self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) |
674 elif self.Output: |
676 elif self.Output: |
675 self.Output.UnConnect(delete = True) |
677 self.Output.UnConnect(delete=True) |
676 self.Output = None |
678 self.Output = None |
677 self.RefreshConnectors() |
679 self.RefreshConnectors() |
678 self.RefreshBoundingBox() |
680 self.RefreshBoundingBox() |
679 elif value_type != self.ValueType: |
681 elif value_type != self.ValueType: |
680 if self.Input: |
682 if self.Input: |
681 self.Input.SetType(value_type) |
683 self.Input.SetType(value_type) |
682 if self.Output: |
684 if self.Output: |
683 self.Output.SetType(value_type) |
685 self.Output.SetType(value_type) |
684 |
686 |
685 # Returns the variable type |
687 # Returns the variable type |
686 def GetType(self): |
688 def GetType(self): |
687 return self.Type |
689 return self.Type |
688 |
690 |
689 # Returns the variable value type |
691 # Returns the variable value type |
690 def GetValueType(self): |
692 def GetValueType(self): |
691 return self.ValueType |
693 return self.ValueType |
692 |
694 |
693 # Changes the variable name |
695 # Changes the variable name |
694 def SetName(self, name): |
696 def SetName(self, name): |
695 self.Name = name |
697 self.Name = name |
696 self.RefreshNameSize() |
698 self.RefreshNameSize() |
697 |
699 |
698 # Returns the variable name |
700 # Returns the variable name |
699 def GetName(self): |
701 def GetName(self): |
700 return self.Name |
702 return self.Name |
701 |
703 |
702 # Changes the execution order |
704 # Changes the execution order |
703 def SetExecutionOrder(self, executionOrder): |
705 def SetExecutionOrder(self, executionOrder): |
704 self.ExecutionOrder = executionOrder |
706 self.ExecutionOrder = executionOrder |
705 self.RefreshExecutionOrderSize() |
707 self.RefreshExecutionOrderSize() |
706 |
708 |
707 # Returs the execution order |
709 # Returs the execution order |
708 def GetExecutionOrder(self): |
710 def GetExecutionOrder(self): |
709 return self.ExecutionOrder |
711 return self.ExecutionOrder |
710 |
712 |
711 # Returns the variable minimum size |
713 # Returns the variable minimum size |
712 def GetMinSize(self): |
714 def GetMinSize(self): |
713 return self.NameSize[0] + 10, self.NameSize[1] + 10 |
715 return self.NameSize[0] + 10, self.NameSize[1] + 10 |
714 |
716 |
715 # Set size of the variable to the minimum size |
717 # Set size of the variable to the minimum size |
716 def SetBestSize(self, scaling): |
718 def SetBestSize(self, scaling): |
717 if self.Type == INPUT: |
719 if self.Type == INPUT: |
718 return Graphic_Element.SetBestSize(self, scaling, x_factor=1.) |
720 return Graphic_Element.SetBestSize(self, scaling, x_factor=1.) |
719 elif self.Type == OUTPUT: |
721 elif self.Type == OUTPUT: |
720 return Graphic_Element.SetBestSize(self, scaling, x_factor=0.) |
722 return Graphic_Element.SetBestSize(self, scaling, x_factor=0.) |
721 else: |
723 else: |
722 return Graphic_Element.SetBestSize(self, scaling) |
724 return Graphic_Element.SetBestSize(self, scaling) |
723 |
725 |
724 # Method called when a LeftDClick event have been generated |
726 # Method called when a LeftDClick event have been generated |
725 def OnLeftDClick(self, event, dc, scaling): |
727 def OnLeftDClick(self, event, dc, scaling): |
726 if event.ControlDown(): |
728 if event.ControlDown(): |
727 # Change variable type |
729 # Change variable type |
728 types = [INPUT, OUTPUT, INOUT] |
730 types = [INPUT, OUTPUT, INOUT] |
729 self.Parent.ChangeVariableType(self, |
731 self.Parent.ChangeVariableType( |
730 types[(types.index(self.Type) + 1) % len(types)]) |
732 self, types[(types.index(self.Type) + 1) % len(types)]) |
731 else: |
733 else: |
732 # Edit the variable properties |
734 # Edit the variable properties |
733 self.Parent.EditVariableContent(self) |
735 self.Parent.EditVariableContent(self) |
734 |
736 |
735 # Method called when a RightUp event have been generated |
737 # Method called when a RightUp event have been generated |
736 def OnRightUp(self, event, dc, scaling): |
738 def OnRightUp(self, event, dc, scaling): |
737 self.Parent.PopupVariableMenu() |
739 self.Parent.PopupVariableMenu() |
738 |
740 |
739 # Refreshes the variable model |
741 # Refreshes the variable model |
740 def RefreshModel(self, move=True): |
742 def RefreshModel(self, move=True): |
741 self.Parent.RefreshVariableModel(self) |
743 self.Parent.RefreshVariableModel(self) |
742 # If variable has moved and variable is not of type OUTPUT, refresh the model |
744 # If variable has moved and variable is not of type OUTPUT, refresh the model |
743 # of wires connected to output connector |
745 # of wires connected to output connector |
744 if move and self.Type != OUTPUT: |
746 if move and self.Type != OUTPUT: |
745 if self.Output: |
747 if self.Output: |
746 self.Output.RefreshWires() |
748 self.Output.RefreshWires() |
747 |
749 |
748 # Adds an highlight to the variable |
750 # Adds an highlight to the variable |
749 def AddHighlight(self, infos, start, end, highlight_type): |
751 def AddHighlight(self, infos, start, end, highlight_type): |
750 if infos[0] == "expression" and start[0] == 0 and end[0] == 0: |
752 if infos[0] == "expression" and start[0] == 0 and end[0] == 0: |
751 AddHighlight(self.Highlights, (start, end, highlight_type)) |
753 AddHighlight(self.Highlights, (start, end, highlight_type)) |
752 |
754 |
753 # Removes an highlight from the variable |
755 # Removes an highlight from the variable |
754 def RemoveHighlight(self, infos, start, end, highlight_type): |
756 def RemoveHighlight(self, infos, start, end, highlight_type): |
755 if infos[0] == "expression": |
757 if infos[0] == "expression": |
756 RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
758 RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
757 |
759 |
758 # Removes all the highlights of one particular type from the variable |
760 # Removes all the highlights of one particular type from the variable |
759 def ClearHighlight(self, highlight_type=None): |
761 def ClearHighlight(self, highlight_type=None): |
760 ClearHighlights(self.Highlights, highlight_type) |
762 ClearHighlights(self.Highlights, highlight_type) |
761 |
763 |
762 # Draws variable |
764 # Draws variable |
763 def Draw(self, dc): |
765 def Draw(self, dc): |
764 Graphic_Element.Draw(self, dc) |
766 Graphic_Element.Draw(self, dc) |
765 dc.SetPen(MiterPen(wx.BLACK)) |
767 dc.SetPen(MiterPen(wx.BLACK)) |
766 dc.SetBrush(wx.WHITE_BRUSH) |
768 dc.SetBrush(wx.WHITE_BRUSH) |
767 |
769 |
768 if getattr(dc, "printing", False): |
770 if getattr(dc, "printing", False): |
769 name_size = dc.GetTextExtent(self.Name) |
771 name_size = dc.GetTextExtent(self.Name) |
770 executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) |
772 executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) |
771 else: |
773 else: |
772 name_size = self.NameSize |
774 name_size = self.NameSize |
773 executionorder_size = self.ExecutionOrderSize |
775 executionorder_size = self.ExecutionOrderSize |
774 |
776 |
775 text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
777 text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
776 self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
778 self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
777 # Draw a rectangle with the variable size |
779 # Draw a rectangle with the variable size |
778 dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
780 dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
779 # Draw variable name |
781 # Draw variable name |
780 dc.DrawText(self.Name, text_pos[0], text_pos[1]) |
782 dc.DrawText(self.Name, text_pos[0], text_pos[1]) |
781 # Draw connectors |
783 # Draw connectors |
782 if self.Input: |
784 if self.Input: |
783 self.Input.Draw(dc) |
785 self.Input.Draw(dc) |
784 if self.Output: |
786 if self.Output: |
785 self.Output.Draw(dc) |
787 self.Output.Draw(dc) |
786 if self.ExecutionOrder != 0: |
788 if self.ExecutionOrder != 0: |
787 # Draw variable execution order |
789 # Draw variable execution order |
788 dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], |
790 dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], |
789 self.Pos.y + self.Size[1] + 2) |
791 self.Pos.y + self.Size[1] + 2) |
790 if not getattr(dc, "printing", False): |
792 if not getattr(dc, "printing", False): |
791 DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1]) |
793 DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1]) |
792 |
794 |
793 #------------------------------------------------------------------------------- |
795 |
|
796 # ------------------------------------------------------------------------------- |
794 # Function Block Diagram Connector |
797 # Function Block Diagram Connector |
795 #------------------------------------------------------------------------------- |
798 # ------------------------------------------------------------------------------- |
796 |
799 |
797 """ |
|
798 Class that implements the graphic representation of a connection |
|
799 """ |
|
800 |
800 |
801 class FBD_Connector(Graphic_Element): |
801 class FBD_Connector(Graphic_Element): |
|
802 """ |
|
803 Class that implements the graphic representation of a connection |
|
804 """ |
802 |
805 |
803 # Create a new connection |
806 # Create a new connection |
804 def __init__(self, parent, type, name, id = None): |
807 def __init__(self, parent, type, name, id=None): |
805 Graphic_Element.__init__(self, parent) |
808 Graphic_Element.__init__(self, parent) |
806 self.Type = type |
809 self.Type = type |
807 self.Id = id |
810 self.Id = id |
808 self.SetName(name) |
811 self.SetName(name) |
809 self.Pos = wx.Point(0, 0) |
812 self.Pos = wx.Point(0, 0) |
810 self.Size = wx.Size(0, 0) |
813 self.Size = wx.Size(0, 0) |
811 self.Highlights = [] |
814 self.Highlights = [] |
812 # Create an input or output connector according to connection type |
815 # Create an input or output connector according to connection type |
813 if self.Type == CONNECTOR: |
816 if self.Type == CONNECTOR: |
814 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
817 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True) |
815 else: |
818 else: |
816 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
819 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
817 self.RefreshConnectors() |
820 self.RefreshConnectors() |
818 self.RefreshNameSize() |
821 self.RefreshNameSize() |
819 |
822 |
820 def Flush(self): |
823 def Flush(self): |
821 if self.Connector: |
824 if self.Connector: |
822 self.Connector.Flush() |
825 self.Connector.Flush() |
823 self.Connector = None |
826 self.Connector = None |
824 |
827 |
825 # Returns the RedrawRect |
828 # Returns the RedrawRect |
826 def GetRedrawRect(self, movex = 0, movey = 0): |
829 def GetRedrawRect(self, movex=0, movey=0): |
827 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
830 rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
828 if movex != 0 or movey != 0: |
831 if movex != 0 or movey != 0: |
829 if self.Connector and self.Connector.IsConnected(): |
832 if self.Connector and self.Connector.IsConnected(): |
830 rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
833 rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
831 return rect |
834 return rect |
832 |
835 |
833 # Make a clone of this FBD_Connector |
836 # Make a clone of this FBD_Connector |
834 def Clone(self, parent, id = None, pos = None): |
837 def Clone(self, parent, id=None, pos=None): |
835 connection = FBD_Connector(parent, self.Type, self.Name, id) |
838 connection = FBD_Connector(parent, self.Type, self.Name, id) |
836 connection.SetSize(self.Size[0], self.Size[1]) |
839 connection.SetSize(self.Size[0], self.Size[1]) |
837 if pos is not None: |
840 if pos is not None: |
838 connection.SetPosition(pos.x, pos.y) |
841 connection.SetPosition(pos.x, pos.y) |
839 else: |
842 else: |
840 connection.SetPosition(self.Pos.x, self.Pos.y) |
843 connection.SetPosition(self.Pos.x, self.Pos.y) |
841 connection.Connector = self.Connector.Clone(connection) |
844 connection.Connector = self.Connector.Clone(connection) |
842 return connection |
845 return connection |
843 |
846 |
844 def GetConnectorTranslation(self, element): |
847 def GetConnectorTranslation(self, element): |
845 return {self.Connector : element.Connector} |
848 return {self.Connector: element.Connector} |
846 |
849 |
847 # Unconnect connector |
850 # Unconnect connector |
848 def Clean(self): |
851 def Clean(self): |
849 if self.Connector: |
852 if self.Connector: |
850 self.Connector.UnConnect(delete = True) |
853 self.Connector.UnConnect(delete=True) |
851 |
854 |
852 # Delete this connection by calling the appropriate method |
855 # Delete this connection by calling the appropriate method |
853 def Delete(self): |
856 def Delete(self): |
854 self.Parent.DeleteConnection(self) |
857 self.Parent.DeleteConnection(self) |
855 |
858 |
856 # Refresh the size of text for name |
859 # Refresh the size of text for name |
857 def RefreshNameSize(self): |
860 def RefreshNameSize(self): |
858 self.NameSize = self.Parent.GetTextExtent(self.Name) |
861 self.NameSize = self.Parent.GetTextExtent(self.Name) |
859 |
862 |
860 # Refresh the connection bounding box |
863 # Refresh the connection bounding box |
861 def RefreshBoundingBox(self): |
864 def RefreshBoundingBox(self): |
862 if self.Type == CONNECTOR: |
865 if self.Type == CONNECTOR: |
863 bbx_x = self.Pos.x - CONNECTOR_SIZE |
866 bbx_x = self.Pos.x - CONNECTOR_SIZE |
864 else: |
867 else: |
865 bbx_x = self.Pos.x |
868 bbx_x = self.Pos.x |
866 bbx_width = self.Size[0] + CONNECTOR_SIZE |
869 bbx_width = self.Size[0] + CONNECTOR_SIZE |
867 self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
870 self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
868 |
871 |
869 # Refresh the position of the connection connector |
872 # Refresh the position of the connection connector |
870 def RefreshConnectors(self): |
873 def RefreshConnectors(self): |
871 scaling = self.Parent.GetScaling() |
874 scaling = self.Parent.GetScaling() |
872 if scaling is not None: |
875 if scaling is not None: |
873 position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y |
876 position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y |
916 if type != self.Type: |
919 if type != self.Type: |
917 self.Type = type |
920 self.Type = type |
918 self.Clean() |
921 self.Clean() |
919 # Create an input or output connector according to connection type |
922 # Create an input or output connector according to connection type |
920 if self.Type == CONNECTOR: |
923 if self.Type == CONNECTOR: |
921 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
924 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True) |
922 else: |
925 else: |
923 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
926 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
924 self.RefreshConnectors() |
927 self.RefreshConnectors() |
925 self.RefreshBoundingBox() |
928 self.RefreshBoundingBox() |
926 |
929 |
927 # Returns the connection type |
930 # Returns the connection type |
928 def GetType(self): |
931 def GetType(self): |
929 return self.Type |
932 return self.Type |
930 |
933 |
931 def GetConnectionResultType(self, connector, connectortype): |
934 def GetConnectionResultType(self, connector, connectortype): |
932 if self.Type == CONTINUATION: |
935 if self.Type == CONTINUATION: |
933 connector = self.Parent.GetConnectorByName(self.Name) |
936 connector = self.Parent.GetConnectorByName(self.Name) |
934 if connector is not None: |
937 if connector is not None: |
935 return connector.Connector.GetConnectedType() |
938 return connector.Connector.GetConnectedType() |
936 return connectortype |
939 return connectortype |
937 |
940 |
938 # Changes the connection name |
941 # Changes the connection name |
939 def SetName(self, name): |
942 def SetName(self, name): |
940 self.Name = name |
943 self.Name = name |
941 self.RefreshNameSize() |
944 self.RefreshNameSize() |
942 |
945 |
943 # Returns the connection name |
946 # Returns the connection name |
944 def GetName(self): |
947 def GetName(self): |
945 return self.Name |
948 return self.Name |
946 |
949 |
947 # Set size of the variable to the minimum size |
950 # Set size of the variable to the minimum size |
948 def SetBestSize(self, scaling): |
951 def SetBestSize(self, scaling): |
949 if self.Type == CONTINUATION: |
952 if self.Type == CONTINUATION: |
950 return Graphic_Element.SetBestSize(self, scaling, x_factor=1.) |
953 return Graphic_Element.SetBestSize(self, scaling, x_factor=1.) |
951 else: |
954 else: |
952 return Graphic_Element.SetBestSize(self, scaling, x_factor=0.) |
955 return Graphic_Element.SetBestSize(self, scaling, x_factor=0.) |
953 |
956 |
954 # Returns the connection minimum size |
957 # Returns the connection minimum size |
955 def GetMinSize(self): |
958 def GetMinSize(self): |
956 text_width, text_height = self.NameSize |
959 text_width, text_height = self.NameSize |
957 if text_height % 2 == 1: |
960 if text_height % 2 == 1: |
958 text_height += 1 |
961 text_height += 1 |
959 return text_width + text_height + 20, text_height + 10 |
962 return text_width + text_height + 20, text_height + 10 |
960 |
963 |
961 # Method called when a LeftDClick event have been generated |
964 # Method called when a LeftDClick event have been generated |
962 def OnLeftDClick(self, event, dc, scaling): |
965 def OnLeftDClick(self, event, dc, scaling): |
963 if event.ControlDown(): |
966 if event.ControlDown(): |
964 # Change connection type |
967 # Change connection type |
965 if self.Type == CONNECTOR: |
968 if self.Type == CONNECTOR: |
966 self.Parent.ChangeConnectionType(self, CONTINUATION) |
969 self.Parent.ChangeConnectionType(self, CONTINUATION) |
967 else: |
970 else: |
968 self.Parent.ChangeConnectionType(self, CONNECTOR) |
971 self.Parent.ChangeConnectionType(self, CONNECTOR) |
969 else: |
972 else: |
970 # Edit the connection properties |
973 # Edit the connection properties |
971 self.Parent.EditConnectionContent(self) |
974 self.Parent.EditConnectionContent(self) |
972 |
975 |
973 # Method called when a RightUp event have been generated |
976 # Method called when a RightUp event have been generated |
974 def OnRightUp(self, event, dc, scaling): |
977 def OnRightUp(self, event, dc, scaling): |
975 # Popup the default menu |
978 # Popup the default menu |
976 self.Parent.PopupConnectionMenu() |
979 self.Parent.PopupConnectionMenu() |
977 |
980 |
978 # Refreshes the connection model |
981 # Refreshes the connection model |
979 def RefreshModel(self, move=True): |
982 def RefreshModel(self, move=True): |
980 self.Parent.RefreshConnectionModel(self) |
983 self.Parent.RefreshConnectionModel(self) |
981 # If connection has moved and connection is of type CONTINUATION, refresh |
984 # If connection has moved and connection is of type CONTINUATION, refresh |
982 # the model of wires connected to connector |
985 # the model of wires connected to connector |
983 if move and self.Type == CONTINUATION: |
986 if move and self.Type == CONTINUATION: |
984 if self.Connector: |
987 if self.Connector: |
985 self.Connector.RefreshWires() |
988 self.Connector.RefreshWires() |
986 |
989 |
987 # Adds an highlight to the connection |
990 # Adds an highlight to the connection |
988 def AddHighlight(self, infos, start, end, highlight_type): |
991 def AddHighlight(self, infos, start, end, highlight_type): |
989 if infos[0] == "name" and start[0] == 0 and end[0] == 0: |
992 if infos[0] == "name" and start[0] == 0 and end[0] == 0: |
990 AddHighlight(self.Highlights, (start, end, highlight_type)) |
993 AddHighlight(self.Highlights, (start, end, highlight_type)) |
991 |
994 |
992 # Removes an highlight from the connection |
995 # Removes an highlight from the connection |
993 def RemoveHighlight(self, infos, start, end, highlight_type): |
996 def RemoveHighlight(self, infos, start, end, highlight_type): |
994 if infos[0] == "name": |
997 if infos[0] == "name": |
995 RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
998 RemoveHighlight(self.Highlights, (start, end, highlight_type)) |
996 |
999 |
997 # Removes all the highlights of one particular type from the connection |
1000 # Removes all the highlights of one particular type from the connection |
998 def ClearHighlight(self, highlight_type=None): |
1001 def ClearHighlight(self, highlight_type=None): |
999 ClearHighlights(self.Highlights, highlight_type) |
1002 ClearHighlights(self.Highlights, highlight_type) |
1000 |
1003 |
1001 # Draws connection |
1004 # Draws connection |
1002 def Draw(self, dc): |
1005 def Draw(self, dc): |
1003 Graphic_Element.Draw(self, dc) |
1006 Graphic_Element.Draw(self, dc) |
1004 dc.SetPen(MiterPen(wx.BLACK)) |
1007 dc.SetPen(MiterPen(wx.BLACK)) |
1005 dc.SetBrush(wx.WHITE_BRUSH) |
1008 dc.SetBrush(wx.WHITE_BRUSH) |
1006 |
1009 |
1007 if getattr(dc, "printing", False): |
1010 if getattr(dc, "printing", False): |
1008 name_size = dc.GetTextExtent(self.Name) |
1011 name_size = dc.GetTextExtent(self.Name) |
1009 else: |
1012 else: |
1010 name_size = self.NameSize |
1013 name_size = self.NameSize |
1011 |
1014 |
1012 # Draw a rectangle with the connection size with arrows inside |
1015 # Draw a rectangle with the connection size with arrows inside |
1013 dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
1016 dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
1014 arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2) |
1017 arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2) |
1015 dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
1018 dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
1016 self.Pos.y + self.Size[1] / 2) |
1019 self.Pos.y + self.Size[1] / 2) |
1017 dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
1020 dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
1018 self.Pos.x, self.Pos.y + self.Size[1]) |
1021 self.Pos.x, self.Pos.y + self.Size[1]) |
1019 dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
1022 dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
1020 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
1023 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
1021 dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
1024 dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
1022 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
1025 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
1023 # Draw connection name |
1026 # Draw connection name |
1024 text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
1027 text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
1025 self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
1028 self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
1026 dc.DrawText(self.Name, text_pos[0], text_pos[1]) |
1029 dc.DrawText(self.Name, text_pos[0], text_pos[1]) |
1027 # Draw connector |
1030 # Draw connector |
1028 if self.Connector: |
1031 if self.Connector: |
1029 self.Connector.Draw(dc) |
1032 self.Connector.Draw(dc) |
1030 |
1033 |
1031 if not getattr(dc, "printing", False): |
1034 if not getattr(dc, "printing", False): |
1032 DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1]) |
1035 DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1]) |
1033 |
|