Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
andrej@2437: from __future__ import division
Laurent@814: import wx
andrej@2432: from six.moves import xrange
Laurent@814: 
Laurent@1176: from graphics.GraphicCommons import *
Laurent@814: from plcopen.structures import *
Laurent@814: 
andrej@1782: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                         Function Block Diagram Block
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: 
Laurent@814: def TestConnectorName(name, block_type):
Laurent@814:     return name in ["OUT", "MN", "MX"] or name.startswith("IN") and (block_type, name) != ("EXPT", "IN2")
Laurent@814: 
andrej@1736: 
Laurent@814: class FBD_Block(Graphic_Element):
andrej@1736:     """
andrej@1736:     Class that implements the graphic representation of a function block
andrej@1736:     """
andrej@1730: 
Laurent@814:     # Create a new block
andrej@1852:     def __init__(self, parent, type, name, id=None, extension=0, inputs=None, connectors=None, executionControl=False, executionOrder=0):
Laurent@814:         Graphic_Element.__init__(self, parent)
Laurent@814:         self.Type = None
Laurent@814:         self.Description = None
Laurent@814:         self.Extension = None
Laurent@814:         self.ExecutionControl = False
Laurent@814:         self.Id = id
Laurent@814:         self.SetName(name)
Laurent@814:         self.SetExecutionOrder(executionOrder)
Laurent@814:         self.Inputs = []
Laurent@814:         self.Outputs = []
Laurent@814:         self.Colour = wx.BLACK
Laurent@814:         self.Pen = MiterPen(wx.BLACK)
Laurent@814:         self.SetType(type, extension, inputs, connectors, executionControl)
Laurent@814:         self.Highlights = {}
andrej@1730: 
Laurent@814:     # Make a clone of this FBD_Block
andrej@1744:     def Clone(self, parent, id=None, name="", pos=None):
Laurent@814:         if self.Name != "" and name == "":
Laurent@814:             name = self.Name
Laurent@814:         block = FBD_Block(parent, self.Type, name, id, self.Extension)
Laurent@814:         block.SetSize(self.Size[0], self.Size[1])
Laurent@814:         if pos is not None:
Laurent@814:             block.SetPosition(pos.x, pos.y)
Laurent@814:         else:
Laurent@814:             block.SetPosition(self.Pos.x, self.Pos.y)
Laurent@814:         block.Inputs = [input.Clone(block) for input in self.Inputs]
Laurent@814:         block.Outputs = [output.Clone(block) for output in self.Outputs]
Laurent@814:         return block
andrej@1730: 
Laurent@814:     def GetConnectorTranslation(self, element):
Laurent@814:         return dict(zip(self.Inputs + self.Outputs, element.Inputs + element.Outputs))
andrej@1730: 
Laurent@814:     def Flush(self):
Laurent@814:         for input in self.Inputs:
Laurent@814:             input.Flush()
Laurent@814:         self.Inputs = []
Laurent@814:         for output in self.Outputs:
Laurent@814:             output.Flush()
Laurent@814:         self.Outputs = []
andrej@1730: 
Laurent@814:     # Returns the RedrawRect
andrej@1744:     def GetRedrawRect(self, movex=0, movey=0):
Laurent@814:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
Laurent@814:         if movex != 0 or movey != 0:
Laurent@814:             for input in self.Inputs:
Laurent@814:                 if input.IsConnected():
Laurent@814:                     rect = rect.Union(input.GetConnectedRedrawRect(movex, movey))
Laurent@814:             for output in self.Outputs:
Laurent@814:                 if output.IsConnected():
Laurent@814:                     rect = rect.Union(output.GetConnectedRedrawRect(movex, movey))
Laurent@814:         return rect
andrej@1730: 
Laurent@814:     # Delete this block by calling the appropriate method
Laurent@814:     def Delete(self):
Laurent@814:         self.Parent.DeleteBlock(self)
andrej@1730: 
Laurent@814:     # Unconnect all inputs and outputs
Laurent@814:     def Clean(self):
Laurent@814:         for input in self.Inputs:
andrej@1744:             input.UnConnect(delete=True)
Laurent@814:         for output in self.Outputs:
andrej@1744:             output.UnConnect(delete=True)
andrej@1730: 
Laurent@814:     # Refresh the size of text for name
Laurent@814:     def RefreshNameSize(self):
Laurent@814:         self.NameSize = self.Parent.GetTextExtent(self.Name)
andrej@1730: 
Laurent@814:     # Refresh the size of text for execution order
Laurent@814:     def RefreshExecutionOrderSize(self):
Laurent@814:         self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder))
andrej@1730: 
Laurent@814:     # Returns if the point given is in the bounding box
Laurent@814:     def HitTest(self, pt, connectors=True):
Laurent@814:         if self.Name != "":
Laurent@814:             test_text = self.GetTextBoundingBox().InsideXY(pt.x, pt.y)
Laurent@814:         else:
Laurent@814:             test_text = False
Laurent@814:         test_block = self.GetBlockBoundingBox(connectors).InsideXY(pt.x, pt.y)
Laurent@814:         return test_text or test_block
andrej@1730: 
Laurent@814:     # Returns the bounding box of the name outside the block
Laurent@814:     def GetTextBoundingBox(self):
Laurent@814:         # Calculate the size of the name outside the block
Laurent@814:         text_width, text_height = self.NameSize
andrej@2437:         return wx.Rect(self.Pos.x + (self.Size[0] - text_width) // 2,
Laurent@814:                        self.Pos.y - (text_height + 2),
Laurent@814:                        text_width,
Laurent@814:                        text_height)
andrej@1730: 
Laurent@814:     # Returns the bounding box of function block without name outside
Laurent@814:     def GetBlockBoundingBox(self, connectors=True):
Laurent@814:         bbx_x, bbx_y = self.Pos.x, self.Pos.y
Laurent@814:         bbx_width, bbx_height = self.Size
Laurent@814:         if connectors:
Laurent@814:             bbx_x -= min(1, len(self.Inputs)) * CONNECTOR_SIZE
Laurent@814:             bbx_width += (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE
Laurent@814:         if self.ExecutionOrder != 0:
Laurent@814:             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
Laurent@814:             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
Laurent@814:             bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
Laurent@814:         return wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
andrej@1730: 
Laurent@814:     # Refresh the block bounding box
Laurent@814:     def RefreshBoundingBox(self):
Laurent@814:         self.BoundingBox = self.GetBlockBoundingBox()
Laurent@814:         if self.Name != "":
Laurent@814:             self.BoundingBox.Union(self.GetTextBoundingBox())
andrej@1730: 
Laurent@814:     # Refresh the positions of the block connectors
Laurent@814:     def RefreshConnectors(self):
Laurent@814:         scaling = self.Parent.GetScaling()
Laurent@814:         # Calculate the size for the connector lines
Laurent@814:         lines = max(len(self.Inputs), len(self.Outputs))
Laurent@814:         if lines > 0:
andrej@2437:             linesize = max((self.Size[1] - BLOCK_LINE_SIZE) // lines, BLOCK_LINE_SIZE)
Laurent@814:             # Update inputs and outputs positions
andrej@2437:             position = BLOCK_LINE_SIZE + linesize // 2
Laurent@814:             for i in xrange(lines):
Laurent@814:                 if scaling is not None:
Laurent@814:                     ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
Laurent@814:                 else:
Laurent@814:                     ypos = position
Laurent@814:                 if i < len(self.Inputs):
Laurent@814:                     self.Inputs[i].SetPosition(wx.Point(0, ypos))
Laurent@814:                 if i < len(self.Outputs):
Laurent@814:                     self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos))
Laurent@814:                 position += linesize
Laurent@814:         self.RefreshConnected()
andrej@1730: 
Laurent@814:     # Refresh the positions of wires connected to inputs and outputs
andrej@1852:     def RefreshConnected(self, exclude=None):
Laurent@814:         for input in self.Inputs:
Laurent@814:             input.MoveConnected(exclude)
Laurent@814:         for output in self.Outputs:
Laurent@814:             output.MoveConnected(exclude)
andrej@1730: 
andrej@1730:     # Returns the block connector that starts with the point given if it exists
andrej@1744:     def GetConnector(self, position, output_name=None, input_name=None):
Laurent@1130:         if input_name is not None:
Laurent@1130:             # Test each input connector
Laurent@1130:             for input in self.Inputs:
Laurent@1130:                 if input_name == input.GetName():
Laurent@1130:                     return input
Laurent@1130:         if output_name is not None:
Laurent@1130:             # Test each output connector
Laurent@814:             for output in self.Outputs:
Laurent@1130:                 if output_name == output.GetName():
Laurent@814:                     return output
Laurent@1133:         if input_name is None and output_name is None:
Laurent@1133:             return self.FindNearestConnector(position, self.Inputs + self.Outputs)
Laurent@1133:         return None
andrej@1730: 
Laurent@814:     def GetInputTypes(self):
Laurent@814:         return tuple([input.GetType(True) for input in self.Inputs if input.GetName() != "EN"])
andrej@1730: 
Laurent@814:     def SetOutputValues(self, values):
Laurent@814:         for output in self.Outputs:
andrej@1872:             output.SetValue(values.get(output.getName(), None))
andrej@1730: 
Laurent@814:     def GetConnectionResultType(self, connector, connectortype):
Laurent@814:         if not TestConnectorName(connector.GetName(), self.Type):
Laurent@814:             return connectortype
Laurent@814:         resulttype = connectortype
Laurent@814:         for input in self.Inputs:
Laurent@814:             if input != connector and input.GetType(True) == "ANY" and TestConnectorName(input.GetName(), self.Type):
Laurent@814:                 inputtype = input.GetConnectedType()
Laurent@814:                 if resulttype is None or inputtype is not None and self.IsOfType(inputtype, resulttype):
Laurent@814:                     resulttype = inputtype
Laurent@814:         for output in self.Outputs:
Laurent@814:             if output != connector and output.GetType(True) == "ANY" and TestConnectorName(output.GetName(), self.Type):
Laurent@814:                 outputtype = output.GetConnectedType()
Laurent@814:                 if resulttype is None or outputtype is not None and self.IsOfType(outputtype, resulttype):
Laurent@814:                     resulttype = outputtype
Laurent@814:         return resulttype
andrej@1730: 
Laurent@814:     # Returns all the block connectors
Laurent@814:     def GetConnectors(self):
andrej@1739:         return {"inputs": self.Inputs, "outputs": self.Outputs}
andrej@1730: 
Laurent@814:     # Test if point given is on one of the block connectors
andrej@1744:     def TestConnector(self, pt, direction=None, exclude=True):
Laurent@814:         # Test each input connector
Laurent@814:         for input in self.Inputs:
Laurent@814:             if input.TestPoint(pt, direction, exclude):
Laurent@814:                 return input
Laurent@814:         # Test each output connector
Laurent@814:         for output in self.Outputs:
Laurent@814:             if output.TestPoint(pt, direction, exclude):
Laurent@814:                 return output
Laurent@814:         return None
andrej@1730: 
Laurent@814:     # Changes the block type
andrej@1852:     def SetType(self, type, extension, inputs=None, connectors=None, executionControl=False):
andrej@1730:         if type != self.Type or self.Extension != extension or executionControl != self.ExecutionControl:
Laurent@814:             if type != self.Type:
Laurent@814:                 self.Type = type
Laurent@814:                 self.TypeSize = self.Parent.GetTextExtent(self.Type)
Laurent@814:             self.Extension = extension
Laurent@814:             self.ExecutionControl = executionControl
Laurent@814:             # Find the block definition from type given and create the corresponding
Laurent@814:             # inputs and outputs
Laurent@814:             blocktype = self.Parent.GetBlockType(type, inputs)
Laurent@814:             if blocktype:
Laurent@814:                 self.Colour = wx.BLACK
Laurent@814:                 inputs = [input for input in blocktype["inputs"]]
Laurent@814:                 outputs = [output for output in blocktype["outputs"]]
Laurent@814:                 if blocktype["extensible"]:
Laurent@814:                     start = int(inputs[-1][0].replace("IN", ""))
andrej@1847:                     for dummy in xrange(self.Extension - len(blocktype["inputs"])):
Laurent@814:                         start += 1
andrej@1734:                         inputs.append(("IN%d" % start, inputs[-1][1], inputs[-1][2]))
Laurent@814:                 comment = blocktype["comment"]
Laurent@814:                 self.Description = _(comment) + blocktype.get("usage", "")
Laurent@814:             else:
Laurent@814:                 self.Colour = wx.RED
andrej@1852:                 connectors = {} if connectors is None else connectors
Laurent@814:                 inputs = connectors.get("inputs", [])
Laurent@814:                 outputs = connectors.get("outputs", [])
Laurent@814:                 self.Description = None
Laurent@814:             if self.ExecutionControl:
andrej@1740:                 inputs.insert(0,  ("EN",   "BOOL", "none"))
andrej@1761:                 outputs.insert(0, ("ENO",  "BOOL", "none"))
Laurent@814:             self.Pen = MiterPen(self.Colour)
andrej@1730: 
Laurent@814:             # Extract the inputs properties and create or modify the corresponding connector
Laurent@1054:             input_connectors = []
Laurent@1054:             for input_name, input_type, input_modifier in inputs:
andrej@1744:                 connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone=True)
Laurent@814:                 if input_modifier == "negated":
Laurent@814:                     connector.SetNegated(True)
Laurent@814:                 elif input_modifier != "none":
Laurent@814:                     connector.SetEdge(input_modifier)
Laurent@1054:                 for input in self.Inputs:
Laurent@1054:                     if input.GetName() == input_name:
Laurent@1054:                         wires = input.GetWires()[:]
Laurent@1054:                         input.UnConnect()
Laurent@1054:                         for wire in wires:
Laurent@1054:                             connector.Connect(wire)
Laurent@1054:                         break
Laurent@1054:                 input_connectors.append(connector)
Laurent@1054:             for input in self.Inputs:
andrej@1744:                 input.UnConnect(delete=True)
Laurent@1054:             self.Inputs = input_connectors
andrej@1730: 
Laurent@814:             # Extract the outputs properties and create or modify the corresponding connector
Laurent@1054:             output_connectors = []
Laurent@1054:             for output_name, output_type, output_modifier in outputs:
Laurent@1054:                 connector = Connector(self, output_name, output_type, wx.Point(0, 0), EAST)
Laurent@814:                 if output_modifier == "negated":
Laurent@814:                     connector.SetNegated(True)
Laurent@814:                 elif output_modifier != "none":
Laurent@814:                     connector.SetEdge(output_modifier)
Laurent@1054:                 for output in self.Outputs:
Laurent@1054:                     if output.GetName() == output_name:
Laurent@1054:                         wires = output.GetWires()[:]
Laurent@1054:                         output.UnConnect()
Laurent@1054:                         for wire in wires:
Laurent@1054:                             connector.Connect(wire)
Laurent@1054:                         break
Laurent@1054:                 output_connectors.append(connector)
Laurent@1054:             for output in self.Outputs:
andrej@1744:                 output.UnConnect(delete=True)
Laurent@1054:             self.Outputs = output_connectors
andrej@1730: 
Laurent@814:             self.RefreshMinSize()
Laurent@814:             self.RefreshConnectors()
Laurent@1054:             for output in self.Outputs:
Laurent@1054:                 output.RefreshWires()
Laurent@814:             self.RefreshBoundingBox()
andrej@1730: 
Laurent@814:     # Returns the block type
Laurent@814:     def GetType(self):
Laurent@814:         return self.Type
andrej@1730: 
Laurent@814:     # Changes the block name
Laurent@814:     def SetName(self, name):
Laurent@814:         self.Name = name
Laurent@814:         self.RefreshNameSize()
andrej@1730: 
Laurent@814:     # Returs the block name
Laurent@814:     def GetName(self):
Laurent@814:         return self.Name
andrej@1730: 
Laurent@814:     # Changes the extension name
Laurent@814:     def SetExtension(self, extension):
Laurent@814:         self.Extension = extension
andrej@1730: 
Laurent@814:     # Returs the extension name
Laurent@814:     def GetExtension(self):
Laurent@814:         return self.Extension
andrej@1730: 
Laurent@814:     # Changes the execution order
Laurent@814:     def SetExecutionOrder(self, executionOrder):
Laurent@814:         self.ExecutionOrder = executionOrder
Laurent@814:         self.RefreshExecutionOrderSize()
andrej@1730: 
Laurent@814:     # Returs the execution order
Laurent@814:     def GetExecutionOrder(self):
Laurent@814:         return self.ExecutionOrder
andrej@1730: 
Laurent@814:     # Returs the execution order
Laurent@814:     def GetExecutionControl(self):
Laurent@814:         return self.ExecutionControl
andrej@1730: 
Laurent@814:     # Refresh the block minimum size
Laurent@814:     def RefreshMinSize(self):
Laurent@814:         # Calculate the inputs maximum width
Laurent@814:         max_input = 0
Laurent@814:         for input in self.Inputs:
andrej@1847:             w, _h = input.GetNameSize()
Laurent@814:             max_input = max(max_input, w)
Laurent@814:         # Calculate the outputs maximum width
Laurent@814:         max_output = 0
Laurent@814:         for output in self.Outputs:
andrej@1847:             w, _h = output.GetNameSize()
Laurent@814:             max_output = max(max_output, w)
Laurent@814:         width = max(self.TypeSize[0] + 10, max_input + max_output + 15)
Laurent@814:         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
Laurent@814:         self.MinSize = width, height
andrej@1730: 
Laurent@814:     # Returns the block minimum size
Laurent@814:     def GetMinSize(self):
Laurent@814:         return self.MinSize
andrej@1730: 
Laurent@814:     # Changes the negated property of the connector handled
Laurent@814:     def SetConnectorNegated(self, negated):
Laurent@814:         handle_type, handle = self.Handle
Laurent@814:         if handle_type == HANDLE_CONNECTOR:
Laurent@814:             handle.SetNegated(negated)
Laurent@814:             self.RefreshModel(False)
andrej@1730: 
Laurent@814:     # Changes the edge property of the connector handled
Laurent@814:     def SetConnectorEdge(self, edge):
Laurent@814:         handle_type, handle = self.Handle
Laurent@814:         if handle_type == HANDLE_CONNECTOR:
Laurent@814:             handle.SetEdge(edge)
Laurent@814:             self.RefreshModel(False)
andrej@1730: 
andrej@1753: #    # Method called when a Motion event have been generated
andrej@1753: #    def OnMotion(self, event, dc, scaling):
andrej@1753: #        if not event.Dragging():
andrej@1753: #            pos = event.GetLogicalPosition(dc)
andrej@1753: #            for input in self.Inputs:
andrej@1753: #                rect = input.GetRedrawRect()
andrej@1753: #                if rect.InsideXY(pos.x, pos.y):
andrej@1753: #                    print "Find input"
andrej@1753: #                    tip = wx.TipWindow(self.Parent, "Test")
andrej@1753: #                    tip.SetBoundingRect(rect)
andrej@1753: #        return Graphic_Element.OnMotion(self, event, dc, scaling)
andrej@1730: 
Laurent@814:     # Method called when a LeftDClick event have been generated
Laurent@814:     def OnLeftDClick(self, event, dc, scaling):
Laurent@814:         # Edit the block properties
Laurent@814:         self.Parent.EditBlockContent(self)
andrej@1730: 
Laurent@814:     # Method called when a RightUp event have been generated
Laurent@814:     def OnRightUp(self, event, dc, scaling):
Laurent@814:         pos = GetScaledEventPosition(event, dc, scaling)
Laurent@814:         # Popup the menu with special items for a block and a connector if one is handled
Laurent@814:         connector = self.TestConnector(pos, exclude=False)
Laurent@814:         if connector:
Laurent@814:             self.Handle = (HANDLE_CONNECTOR, connector)
Laurent@814:             self.Parent.PopupBlockMenu(connector)
Laurent@814:         else:
Laurent@814:             self.Parent.PopupBlockMenu()
andrej@1730: 
Laurent@814:     # Refreshes the block model
Laurent@814:     def RefreshModel(self, move=True):
Laurent@814:         self.Parent.RefreshBlockModel(self)
Laurent@814:         # If block has moved, refresh the model of wires connected to outputs
Laurent@814:         if move:
Laurent@814:             for output in self.Outputs:
Laurent@814:                 output.RefreshWires()
andrej@1730: 
Laurent@814:     def GetToolTipValue(self):
Laurent@814:         return self.Description
andrej@1730: 
Laurent@814:     # Adds an highlight to the block
andrej@1739:     def AddHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0:
Laurent@814:             highlights = self.Highlights.setdefault(infos[0], [])
Laurent@814:             AddHighlight(highlights, (start, end, highlight_type))
Laurent@814:         elif infos[0] == "input" and infos[1] < len(self.Inputs):
Laurent@814:             self.Inputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
Laurent@814:         elif infos[0] == "output" and infos[1] < len(self.Outputs):
Laurent@814:             self.Outputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
andrej@1730: 
Laurent@814:     # Removes an highlight from the block
Laurent@814:     def RemoveHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] in ["type", "name"]:
Laurent@814:             highlights = self.Highlights.get(infos[0], [])
Laurent@814:             if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0:
Laurent@814:                 self.Highlights.pop(infos[0])
Laurent@814:         elif infos[0] == "input" and infos[1] < len(self.Inputs):
Laurent@814:             self.Inputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
Laurent@814:         elif infos[0] == "output" and infos[1] < len(self.Outputs):
Laurent@814:             self.Outputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
andrej@1730: 
Laurent@814:     # Removes all the highlights of one particular type from the block
Laurent@814:     def ClearHighlight(self, highlight_type=None):
Laurent@814:         if highlight_type is None:
Laurent@814:             self.Highlights = {}
Laurent@814:         else:
Laurent@814:             highlight_items = self.Highlights.items()
Laurent@814:             for name, highlights in highlight_items:
Laurent@814:                 highlights = ClearHighlights(highlights, highlight_type)
Laurent@814:                 if len(highlights) == 0:
Laurent@814:                     self.Highlights.pop(name)
Laurent@814:         for input in self.Inputs:
Laurent@814:             input.ClearHighlights(highlight_type)
Laurent@814:         for output in self.Outputs:
Laurent@814:             output.ClearHighlights(highlight_type)
andrej@1730: 
Laurent@814:     # Draws block
Laurent@814:     def Draw(self, dc):
Laurent@814:         Graphic_Element.Draw(self, dc)
Laurent@814:         dc.SetPen(self.Pen)
Laurent@814:         dc.SetBrush(wx.WHITE_BRUSH)
Laurent@814:         dc.SetTextForeground(self.Colour)
andrej@1730: 
Laurent@814:         if getattr(dc, "printing", False):
Laurent@814:             name_size = dc.GetTextExtent(self.Name)
Laurent@814:             type_size = dc.GetTextExtent(self.Type)
Laurent@814:             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
Laurent@814:         else:
Laurent@814:             name_size = self.NameSize
Laurent@814:             type_size = self.TypeSize
Laurent@814:             executionorder_size = self.ExecutionOrderSize
andrej@1730: 
Laurent@814:         # Draw a rectangle with the block size
Laurent@814:         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
Laurent@814:         # Draw block name and block type
andrej@2437:         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
Laurent@814:                     self.Pos.y - (name_size[1] + 2))
andrej@2437:         type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) // 2,
Laurent@814:                     self.Pos.y + 5)
Laurent@814:         dc.DrawText(self.Name, name_pos[0], name_pos[1])
Laurent@814:         dc.DrawText(self.Type, type_pos[0], type_pos[1])
Laurent@814:         # Draw inputs and outputs connectors
Laurent@814:         for input in self.Inputs:
Laurent@814:             input.Draw(dc)
Laurent@814:         for output in self.Outputs:
Laurent@814:             output.Draw(dc)
Laurent@814:         if self.ExecutionOrder != 0:
Laurent@814:             # Draw block execution order
Laurent@814:             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
andrej@1768:                         self.Pos.y + self.Size[1] + 2)
andrej@1730: 
Laurent@814:         if not getattr(dc, "printing", False):
Laurent@814:             DrawHighlightedText(dc, self.Name, self.Highlights.get("name", []), name_pos[0], name_pos[1])
Laurent@814:             DrawHighlightedText(dc, self.Type, self.Highlights.get("type", []), type_pos[0], type_pos[1])
andrej@1730: 
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                        Function Block Diagram Variable
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: 
Laurent@814: class FBD_Variable(Graphic_Element):
andrej@1736:     """
andrej@1736:     Class that implements the graphic representation of a variable
andrej@1736:     """
Laurent@814: 
Laurent@814:     # Create a new variable
andrej@1744:     def __init__(self, parent, type, name, value_type, id=None, executionOrder=0):
Laurent@814:         Graphic_Element.__init__(self, parent)
Laurent@814:         self.Type = None
Laurent@814:         self.ValueType = None
Laurent@814:         self.Id = id
Laurent@814:         self.SetName(name)
Laurent@814:         self.SetExecutionOrder(executionOrder)
Laurent@814:         self.Input = None
Laurent@814:         self.Output = None
Laurent@814:         self.SetType(type, value_type)
Laurent@814:         self.Highlights = []
andrej@1730: 
Laurent@814:     # Make a clone of this FBD_Variable
andrej@1744:     def Clone(self, parent, id=None, pos=None):
Laurent@814:         variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id)
Laurent@814:         variable.SetSize(self.Size[0], self.Size[1])
Laurent@814:         if pos is not None:
Laurent@814:             variable.SetPosition(pos.x, pos.y)
Laurent@814:         else:
Laurent@814:             variable.SetPosition(self.Pos.x, self.Pos.y)
Laurent@814:         if self.Input:
Laurent@814:             variable.Input = self.Input.Clone(variable)
Laurent@814:         if self.Output:
Laurent@814:             variable.Output = self.Output.Clone(variable)
Laurent@814:         return variable
andrej@1730: 
Laurent@814:     def GetConnectorTranslation(self, element):
Laurent@814:         connectors = {}
Laurent@814:         if self.Input is not None:
Laurent@814:             connectors[self.Input] = element.Input
Laurent@814:         if self.Output is not None:
Laurent@814:             connectors[self.Output] = element.Output
Laurent@814:         return connectors
andrej@1730: 
Laurent@814:     def Flush(self):
Laurent@814:         if self.Input is not None:
Laurent@814:             self.Input.Flush()
Laurent@814:             self.Input = None
Laurent@814:         if self.Output is not None:
Laurent@814:             self.Output.Flush()
Laurent@814:             self.Output = None
andrej@1730: 
Laurent@814:     # Returns the RedrawRect
andrej@1744:     def GetRedrawRect(self, movex=0, movey=0):
Laurent@814:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
Laurent@814:         if movex != 0 or movey != 0:
Laurent@814:             if self.Input and self.Input.IsConnected():
Laurent@814:                 rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
Laurent@814:             if self.Output and self.Output.IsConnected():
Laurent@814:                 rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
Laurent@814:         return rect
andrej@1730: 
Laurent@814:     # Unconnect connector
Laurent@814:     def Clean(self):
Laurent@814:         if self.Input:
andrej@1744:             self.Input.UnConnect(delete=True)
Laurent@814:         if self.Output:
andrej@1744:             self.Output.UnConnect(delete=True)
andrej@1730: 
Laurent@814:     # Delete this variable by calling the appropriate method
Laurent@814:     def Delete(self):
Laurent@814:         self.Parent.DeleteVariable(self)
andrej@1730: 
Laurent@814:     # Refresh the size of text for name
Laurent@814:     def RefreshNameSize(self):
Laurent@814:         self.NameSize = self.Parent.GetTextExtent(self.Name)
andrej@1730: 
Laurent@814:     # Refresh the size of text for execution order
Laurent@814:     def RefreshExecutionOrderSize(self):
Laurent@814:         self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder))
andrej@1730: 
Laurent@814:     # Refresh the variable bounding box
Laurent@814:     def RefreshBoundingBox(self):
Laurent@814:         if self.Type in (OUTPUT, INOUT):
Laurent@814:             bbx_x = self.Pos.x - CONNECTOR_SIZE
Laurent@814:         else:
Laurent@814:             bbx_x = self.Pos.x
Laurent@814:         if self.Type == INOUT:
Laurent@814:             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
Laurent@814:         else:
Laurent@814:             bbx_width = self.Size[0] + CONNECTOR_SIZE
andrej@2437:         bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) // 2)
Laurent@814:         bbx_width = max(bbx_width, self.NameSize[0])
Laurent@814:         bbx_height = self.Size[1]
Laurent@814:         if self.ExecutionOrder != 0:
Laurent@814:             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
Laurent@814:             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
Laurent@814:             bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
Laurent@814:         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1)
andrej@1730: 
Laurent@814:     # Refresh the position of the variable connector
Laurent@814:     def RefreshConnectors(self):
Laurent@814:         scaling = self.Parent.GetScaling()
Laurent@814:         if scaling is not None:
andrej@2437:             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
andrej@2437:         else:
andrej@2437:             position = self.Size[1] // 2
Laurent@814:         if self.Input:
Laurent@814:             self.Input.SetPosition(wx.Point(0, position))
Laurent@814:         if self.Output:
Laurent@814:             self.Output.SetPosition(wx.Point(self.Size[0], position))
Laurent@814:         self.RefreshConnected()
andrej@1730: 
Laurent@814:     # Refresh the position of wires connected to connector
andrej@1852:     def RefreshConnected(self, exclude=None):
Laurent@814:         if self.Input:
Laurent@814:             self.Input.MoveConnected(exclude)
Laurent@814:         if self.Output:
Laurent@814:             self.Output.MoveConnected(exclude)
andrej@1730: 
Laurent@814:     # Test if point given is on the variable connector
andrej@1744:     def TestConnector(self, pt, direction=None, exclude=True):
Laurent@814:         if self.Input and self.Input.TestPoint(pt, direction, exclude):
Laurent@814:             return self.Input
Laurent@814:         if self.Output and self.Output.TestPoint(pt, direction, exclude):
Laurent@814:             return self.Output
Laurent@814:         return None
andrej@1730: 
andrej@1730:     # Returns the block connector that starts with the point given if it exists
andrej@1744:     def GetConnector(self, position, name=None):
Laurent@814:         # if a name is given
Laurent@814:         if name is not None:
Laurent@814:             # Test input and output connector if they exists
andrej@1782:             # if self.Input and name == self.Input.GetName():
Laurent@814:             #    return self.Input
Laurent@814:             if self.Output and name == self.Output.GetName():
Laurent@814:                 return self.Output
Laurent@814:         connectors = []
Laurent@814:         # Test input connector if it exists
Laurent@814:         if self.Input:
Laurent@814:             connectors.append(self.Input)
Laurent@814:         # Test output connector if it exists
Laurent@814:         if self.Output:
Laurent@814:             connectors.append(self.Output)
Laurent@814:         return self.FindNearestConnector(position, connectors)
andrej@1730: 
andrej@1730:     # Returns all the block connectors
Laurent@814:     def GetConnectors(self):
Laurent@814:         connectors = {"inputs": [], "outputs": []}
Laurent@814:         if self.Input:
Laurent@814:             connectors["inputs"].append(self.Input)
Laurent@814:         if self.Output:
Laurent@814:             connectors["outputs"].append(self.Output)
Laurent@814:         return connectors
andrej@1730: 
Laurent@814:     # Changes the negated property of the variable connector if handled
Laurent@814:     def SetConnectorNegated(self, negated):
Laurent@814:         handle_type, handle = self.Handle
Laurent@814:         if handle_type == HANDLE_CONNECTOR:
Laurent@814:             handle.SetNegated(negated)
Laurent@814:             self.RefreshModel(False)
andrej@1730: 
Laurent@814:     # Changes the variable type
Laurent@814:     def SetType(self, type, value_type):
Laurent@814:         if type != self.Type:
Laurent@814:             self.Type = type
Laurent@814:             # Create an input or output connector according to variable type
Laurent@814:             if self.Type != INPUT:
Laurent@857:                 if self.Input is None:
andrej@1744:                     self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone=True)
Laurent@857:             elif self.Input:
andrej@1744:                 self.Input.UnConnect(delete=True)
Laurent@857:                 self.Input = None
Laurent@814:             if self.Type != OUTPUT:
Laurent@857:                 if self.Output is None:
Laurent@857:                     self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST)
Laurent@857:             elif self.Output:
andrej@1744:                 self.Output.UnConnect(delete=True)
Laurent@857:                 self.Output = None
Laurent@814:             self.RefreshConnectors()
Laurent@873:             self.RefreshBoundingBox()
Laurent@814:         elif value_type != self.ValueType:
Laurent@814:             if self.Input:
Laurent@814:                 self.Input.SetType(value_type)
Laurent@814:             if self.Output:
andrej@1730:                 self.Output.SetType(value_type)
andrej@1730: 
Laurent@814:     # Returns the variable type
Laurent@814:     def GetType(self):
Laurent@814:         return self.Type
andrej@1730: 
Laurent@857:     # Returns the variable value type
Laurent@857:     def GetValueType(self):
Laurent@857:         return self.ValueType
andrej@1730: 
Laurent@814:     # Changes the variable name
Laurent@814:     def SetName(self, name):
Laurent@814:         self.Name = name
Laurent@814:         self.RefreshNameSize()
andrej@1730: 
Laurent@814:     # Returns the variable name
Laurent@814:     def GetName(self):
Laurent@814:         return self.Name
andrej@1730: 
Laurent@814:     # Changes the execution order
Laurent@814:     def SetExecutionOrder(self, executionOrder):
Laurent@814:         self.ExecutionOrder = executionOrder
Laurent@814:         self.RefreshExecutionOrderSize()
andrej@1730: 
Laurent@814:     # Returs the execution order
Laurent@814:     def GetExecutionOrder(self):
Laurent@814:         return self.ExecutionOrder
andrej@1730: 
Laurent@814:     # Returns the variable minimum size
Laurent@814:     def GetMinSize(self):
Laurent@814:         return self.NameSize[0] + 10, self.NameSize[1] + 10
andrej@1730: 
Laurent@852:     # Set size of the variable to the minimum size
Laurent@852:     def SetBestSize(self, scaling):
Laurent@852:         if self.Type == INPUT:
Laurent@852:             return Graphic_Element.SetBestSize(self, scaling, x_factor=1.)
Laurent@852:         elif self.Type == OUTPUT:
Laurent@852:             return Graphic_Element.SetBestSize(self, scaling, x_factor=0.)
Laurent@852:         else:
Laurent@852:             return Graphic_Element.SetBestSize(self, scaling)
andrej@1730: 
Laurent@814:     # Method called when a LeftDClick event have been generated
Laurent@814:     def OnLeftDClick(self, event, dc, scaling):
Laurent@857:         if event.ControlDown():
andrej@1730:             # Change variable type
Laurent@857:             types = [INPUT, OUTPUT, INOUT]
andrej@1768:             self.Parent.ChangeVariableType(
andrej@1768:                 self, types[(types.index(self.Type) + 1) % len(types)])
Laurent@857:         else:
Laurent@857:             # Edit the variable properties
Laurent@857:             self.Parent.EditVariableContent(self)
andrej@1730: 
Laurent@814:     # Method called when a RightUp event have been generated
Laurent@814:     def OnRightUp(self, event, dc, scaling):
Laurent@857:         self.Parent.PopupVariableMenu()
andrej@1730: 
Laurent@814:     # Refreshes the variable model
Laurent@814:     def RefreshModel(self, move=True):
Laurent@814:         self.Parent.RefreshVariableModel(self)
Laurent@814:         # If variable has moved and variable is not of type OUTPUT, refresh the model
Laurent@814:         # of wires connected to output connector
Laurent@814:         if move and self.Type != OUTPUT:
Laurent@814:             if self.Output:
Laurent@814:                 self.Output.RefreshWires()
andrej@1730: 
Laurent@814:     # Adds an highlight to the variable
Laurent@814:     def AddHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] == "expression" and start[0] == 0 and end[0] == 0:
Laurent@814:             AddHighlight(self.Highlights, (start, end, highlight_type))
andrej@1730: 
Laurent@814:     # Removes an highlight from the variable
Laurent@814:     def RemoveHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] == "expression":
Laurent@814:             RemoveHighlight(self.Highlights, (start, end, highlight_type))
andrej@1730: 
Laurent@814:     # Removes all the highlights of one particular type from the variable
Laurent@814:     def ClearHighlight(self, highlight_type=None):
Laurent@814:         ClearHighlights(self.Highlights, highlight_type)
andrej@1730: 
Laurent@814:     # Draws variable
Laurent@814:     def Draw(self, dc):
Laurent@814:         Graphic_Element.Draw(self, dc)
Laurent@814:         dc.SetPen(MiterPen(wx.BLACK))
Laurent@814:         dc.SetBrush(wx.WHITE_BRUSH)
andrej@1730: 
Laurent@814:         if getattr(dc, "printing", False):
Laurent@814:             name_size = dc.GetTextExtent(self.Name)
Laurent@814:             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
Laurent@814:         else:
Laurent@814:             name_size = self.NameSize
Laurent@814:             executionorder_size = self.ExecutionOrderSize
andrej@1730: 
andrej@2437:         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
andrej@2437:                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
Laurent@814:         # Draw a rectangle with the variable size
Laurent@814:         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
Laurent@814:         # Draw variable name
Laurent@814:         dc.DrawText(self.Name, text_pos[0], text_pos[1])
Laurent@814:         # Draw connectors
Laurent@814:         if self.Input:
Laurent@814:             self.Input.Draw(dc)
andrej@1730:         if self.Output:
Laurent@814:             self.Output.Draw(dc)
Laurent@814:         if self.ExecutionOrder != 0:
Laurent@814:             # Draw variable execution order
Laurent@814:             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
andrej@1768:                         self.Pos.y + self.Size[1] + 2)
Laurent@814:         if not getattr(dc, "printing", False):
Laurent@814:             DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])
andrej@1730: 
andrej@1782: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                        Function Block Diagram Connector
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: 
Laurent@814: class FBD_Connector(Graphic_Element):
andrej@1736:     """
andrej@1736:     Class that implements the graphic representation of a connection
andrej@1736:     """
Laurent@814: 
Laurent@814:     # Create a new connection
andrej@1744:     def __init__(self, parent, type, name, id=None):
Laurent@814:         Graphic_Element.__init__(self, parent)
Laurent@814:         self.Type = type
Laurent@814:         self.Id = id
Laurent@814:         self.SetName(name)
Laurent@814:         self.Pos = wx.Point(0, 0)
Laurent@814:         self.Size = wx.Size(0, 0)
Laurent@814:         self.Highlights = []
Laurent@814:         # Create an input or output connector according to connection type
Laurent@814:         if self.Type == CONNECTOR:
andrej@1744:             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True)
Laurent@814:         else:
Laurent@814:             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
Laurent@814:         self.RefreshConnectors()
Laurent@814:         self.RefreshNameSize()
andrej@1730: 
Laurent@814:     def Flush(self):
Laurent@814:         if self.Connector:
Laurent@814:             self.Connector.Flush()
Laurent@814:             self.Connector = None
andrej@1730: 
Laurent@814:     # Returns the RedrawRect
andrej@1744:     def GetRedrawRect(self, movex=0, movey=0):
Laurent@814:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
Laurent@814:         if movex != 0 or movey != 0:
Laurent@814:             if self.Connector and self.Connector.IsConnected():
Laurent@814:                 rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey))
Laurent@814:         return rect
andrej@1730: 
Laurent@814:     # Make a clone of this FBD_Connector
andrej@1744:     def Clone(self, parent, id=None, pos=None):
Laurent@814:         connection = FBD_Connector(parent, self.Type, self.Name, id)
Laurent@814:         connection.SetSize(self.Size[0], self.Size[1])
Laurent@814:         if pos is not None:
Laurent@814:             connection.SetPosition(pos.x, pos.y)
Laurent@814:         else:
Laurent@814:             connection.SetPosition(self.Pos.x, self.Pos.y)
Laurent@814:         connection.Connector = self.Connector.Clone(connection)
Laurent@814:         return connection
andrej@1730: 
Laurent@814:     def GetConnectorTranslation(self, element):
andrej@1739:         return {self.Connector: element.Connector}
Laurent@814: 
Laurent@814:     # Unconnect connector
Laurent@814:     def Clean(self):
Laurent@814:         if self.Connector:
andrej@1744:             self.Connector.UnConnect(delete=True)
andrej@1730: 
Laurent@814:     # Delete this connection by calling the appropriate method
Laurent@814:     def Delete(self):
Laurent@814:         self.Parent.DeleteConnection(self)
andrej@1730: 
Laurent@814:     # Refresh the size of text for name
Laurent@814:     def RefreshNameSize(self):
Laurent@814:         self.NameSize = self.Parent.GetTextExtent(self.Name)
andrej@1730: 
Laurent@814:     # Refresh the connection bounding box
Laurent@814:     def RefreshBoundingBox(self):
Laurent@814:         if self.Type == CONNECTOR:
Laurent@814:             bbx_x = self.Pos.x - CONNECTOR_SIZE
Laurent@814:         else:
Laurent@814:             bbx_x = self.Pos.x
Laurent@814:         bbx_width = self.Size[0] + CONNECTOR_SIZE
Laurent@814:         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1])
andrej@1730: 
Laurent@814:     # Refresh the position of the connection connector
Laurent@814:     def RefreshConnectors(self):
Laurent@814:         scaling = self.Parent.GetScaling()
Laurent@814:         if scaling is not None:
andrej@2437:             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
andrej@2437:         else:
andrej@2437:             position = self.Size[1] // 2
Laurent@814:         if self.Type == CONNECTOR:
Laurent@814:             self.Connector.SetPosition(wx.Point(0, position))
Laurent@814:         else:
Laurent@814:             self.Connector.SetPosition(wx.Point(self.Size[0], position))
Laurent@814:         self.RefreshConnected()
andrej@1730: 
Laurent@814:     # Refresh the position of wires connected to connector
andrej@1852:     def RefreshConnected(self, exclude=None):
Laurent@814:         if self.Connector:
Laurent@814:             self.Connector.MoveConnected(exclude)
andrej@1730: 
Laurent@814:     # Test if point given is on the connection connector
andrej@1744:     def TestConnector(self, pt, direction=None, exclude=True):
Laurent@814:         if self.Connector and self.Connector.TestPoint(pt, direction, exclude):
Laurent@814:             return self.Connector
Laurent@814:         return None
andrej@1730: 
Laurent@814:     # Returns the connection connector
andrej@1744:     def GetConnector(self, position=None, name=None):
Laurent@814:         return self.Connector
andrej@1730: 
andrej@1730:         # Returns all the block connectors
Laurent@814:     def GetConnectors(self):
Laurent@814:         connectors = {"inputs": [], "outputs": []}
Laurent@814:         if self.Type == CONNECTOR:
Laurent@814:             connectors["inputs"].append(self.Connector)
Laurent@814:         else:
Laurent@814:             connectors["outputs"].append(self.Connector)
Laurent@814:         return connectors
andrej@1605: 
andrej@1605:     def SpreadCurrent(self):
andrej@1605:         if self.Type == CONNECTOR:
andrej@1605:             continuations = self.Parent.GetContinuationByName(self.Name)
andrej@1605:             if continuations is not None:
andrej@1605:                 value = self.Connector.ReceivingCurrent()
andrej@1605:                 for cont in continuations:
andrej@1605:                     cont.Connector.SpreadCurrent(value)
andrej@1605: 
Laurent@814:     # Changes the variable type
Laurent@814:     def SetType(self, type):
Laurent@814:         if type != self.Type:
Laurent@814:             self.Type = type
Laurent@814:             self.Clean()
Laurent@814:             # Create an input or output connector according to connection type
Laurent@814:             if self.Type == CONNECTOR:
andrej@1744:                 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone=True)
Laurent@814:             else:
Laurent@814:                 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
Laurent@814:             self.RefreshConnectors()
Laurent@873:             self.RefreshBoundingBox()
andrej@1730: 
Laurent@814:     # Returns the connection type
Laurent@814:     def GetType(self):
Laurent@814:         return self.Type
andrej@1730: 
Laurent@814:     def GetConnectionResultType(self, connector, connectortype):
Laurent@814:         if self.Type == CONTINUATION:
Laurent@814:             connector = self.Parent.GetConnectorByName(self.Name)
Laurent@814:             if connector is not None:
Laurent@814:                 return connector.Connector.GetConnectedType()
Laurent@814:         return connectortype
andrej@1730: 
Laurent@814:     # Changes the connection name
Laurent@814:     def SetName(self, name):
Laurent@814:         self.Name = name
Laurent@814:         self.RefreshNameSize()
andrej@1730: 
Laurent@814:     # Returns the connection name
Laurent@814:     def GetName(self):
Laurent@814:         return self.Name
andrej@1730: 
Laurent@852:     # Set size of the variable to the minimum size
Laurent@852:     def SetBestSize(self, scaling):
Laurent@852:         if self.Type == CONTINUATION:
Laurent@852:             return Graphic_Element.SetBestSize(self, scaling, x_factor=1.)
Laurent@852:         else:
Laurent@852:             return Graphic_Element.SetBestSize(self, scaling, x_factor=0.)
andrej@1730: 
Laurent@814:     # Returns the connection minimum size
Laurent@814:     def GetMinSize(self):
Laurent@814:         text_width, text_height = self.NameSize
Laurent@814:         if text_height % 2 == 1:
Laurent@814:             text_height += 1
Laurent@814:         return text_width + text_height + 20, text_height + 10
andrej@1730: 
Laurent@814:     # Method called when a LeftDClick event have been generated
Laurent@814:     def OnLeftDClick(self, event, dc, scaling):
Laurent@857:         if event.ControlDown():
andrej@1730:             # Change connection type
Laurent@857:             if self.Type == CONNECTOR:
Laurent@857:                 self.Parent.ChangeConnectionType(self, CONTINUATION)
Laurent@857:             else:
Laurent@857:                 self.Parent.ChangeConnectionType(self, CONNECTOR)
Laurent@857:         else:
Laurent@857:             # Edit the connection properties
Laurent@857:             self.Parent.EditConnectionContent(self)
andrej@1730: 
Laurent@814:     # Method called when a RightUp event have been generated
Laurent@814:     def OnRightUp(self, event, dc, scaling):
Laurent@814:         # Popup the default menu
Laurent@857:         self.Parent.PopupConnectionMenu()
andrej@1730: 
Laurent@814:     # Refreshes the connection model
Laurent@814:     def RefreshModel(self, move=True):
Laurent@814:         self.Parent.RefreshConnectionModel(self)
Laurent@814:         # If connection has moved and connection is of type CONTINUATION, refresh
Laurent@814:         # the model of wires connected to connector
Laurent@814:         if move and self.Type == CONTINUATION:
Laurent@814:             if self.Connector:
Laurent@814:                 self.Connector.RefreshWires()
andrej@1730: 
Laurent@814:     # Adds an highlight to the connection
Laurent@814:     def AddHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] == "name" and start[0] == 0 and end[0] == 0:
Laurent@814:             AddHighlight(self.Highlights, (start, end, highlight_type))
andrej@1730: 
Laurent@814:     # Removes an highlight from the connection
Laurent@814:     def RemoveHighlight(self, infos, start, end, highlight_type):
Laurent@814:         if infos[0] == "name":
Laurent@814:             RemoveHighlight(self.Highlights, (start, end, highlight_type))
andrej@1730: 
Laurent@814:     # Removes all the highlights of one particular type from the connection
Laurent@814:     def ClearHighlight(self, highlight_type=None):
Laurent@814:         ClearHighlights(self.Highlights, highlight_type)
andrej@1730: 
Laurent@814:     # Draws connection
Laurent@814:     def Draw(self, dc):
Laurent@814:         Graphic_Element.Draw(self, dc)
Laurent@814:         dc.SetPen(MiterPen(wx.BLACK))
Laurent@814:         dc.SetBrush(wx.WHITE_BRUSH)
andrej@1730: 
Laurent@814:         if getattr(dc, "printing", False):
Laurent@814:             name_size = dc.GetTextExtent(self.Name)
Laurent@814:         else:
Laurent@814:             name_size = self.NameSize
andrej@1730: 
Laurent@814:         # Draw a rectangle with the connection size with arrows inside
Laurent@814:         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
andrej@2437:         arrowsize = min(self.Size[1] // 2, (self.Size[0] - name_size[0] - 10) // 2)
andrej@1730:         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
andrej@2437:                     self.Pos.y + self.Size[1] // 2)
andrej@2437:         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] // 2,
andrej@1768:                     self.Pos.x, self.Pos.y + self.Size[1])
andrej@1730:         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
andrej@2437:                     self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2)
andrej@2437:         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2,
andrej@1768:                     self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
Laurent@814:         # Draw connection name
andrej@2437:         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
andrej@2437:                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
Laurent@814:         dc.DrawText(self.Name, text_pos[0], text_pos[1])
Laurent@814:         # Draw connector
Laurent@814:         if self.Connector:
Laurent@814:             self.Connector.Draw(dc)
andrej@1730: 
Laurent@814:         if not getattr(dc, "printing", False):
Laurent@814:             DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])