etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- etisserant@0: etisserant@0: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor etisserant@0: #based on the plcopen standard. etisserant@0: # lbessard@58: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@0: # etisserant@0: #See COPYING file for copyrights details. etisserant@0: # etisserant@0: #This library is free software; you can redistribute it and/or etisserant@5: #modify it under the terms of the GNU General Public etisserant@0: #License as published by the Free Software Foundation; either etisserant@0: #version 2.1 of the License, or (at your option) any later version. etisserant@0: # etisserant@0: #This library is distributed in the hope that it will be useful, etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@58: #General Public License for more details. etisserant@0: # etisserant@5: #You should have received a copy of the GNU General Public etisserant@0: #License along with this library; if not, write to the Free Software etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: etisserant@0: import wx etisserant@0: etisserant@0: from GraphicCommons import * etisserant@0: from plcopen.structures import * etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Function Block Diagram Block etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a function block etisserant@0: """ etisserant@0: etisserant@0: class FBD_Block(Graphic_Element): etisserant@0: etisserant@0: # Create a new block lbessard@118: def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}, executionOrder = 0): etisserant@0: Graphic_Element.__init__(self, parent) lbessard@3: self.Type = None lbessard@27: self.Extension = None etisserant@0: self.Id = id lbessard@118: self.SetName(name) lbessard@118: self.SetExecutionOrder(executionOrder) lbessard@2: self.Inputs = [] lbessard@2: self.Outputs = [] lbessard@90: self.Colour = wx.BLACK lbessard@90: self.Pen = wx.BLACK_PEN lbessard@90: self.SetType(type, extension, inputs, connectors) lbessard@231: self.Errors = {} etisserant@0: lbessard@112: # Make a clone of this FBD_Block lbessard@162: def Clone(self, parent, id = None, name = "", pos = None): lbessard@144: if self.Name != "" and name == "": lbessard@144: name = self.Name lbessard@162: block = FBD_Block(parent, self.Type, name, id, self.Extension) lbessard@112: block.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: block.SetPosition(pos.x, pos.y) lbessard@112: block.Inputs = [input.Clone(block) for input in self.Inputs] lbessard@112: block.Outputs = [output.Clone(block) for output in self.Outputs] lbessard@112: return block lbessard@112: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Inputs = [] etisserant@0: self.Outputs = [] etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: if movex != 0 or movey != 0: lbessard@144: for input in self.Inputs: lbessard@144: if input.IsConnected(): lbessard@144: rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) lbessard@144: for output in self.Outputs: lbessard@144: if output.IsConnected(): lbessard@144: rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@0: # Delete this block by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteBlock(self) etisserant@0: etisserant@0: # Unconnect all inputs and outputs etisserant@0: def Clean(self): etisserant@0: for input in self.Inputs: lbessard@2: input.UnConnect(delete = True) lbessard@2: for output in self.Outputs: lbessard@2: output.UnConnect(delete = True) etisserant@0: lbessard@42: # Refresh the size of text for name lbessard@42: def RefreshNameSize(self): lbessard@165: self.NameSize = self.Parent.GetTextExtent(self.Name) lbessard@42: lbessard@118: # Refresh the size of text for execution order lbessard@118: def RefreshExecutionOrderSize(self): lbessard@165: self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) lbessard@118: etisserant@0: # Refresh the block bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: # Calculate the size of the name outside the block lbessard@42: text_width, text_height = self.NameSize etisserant@0: # Calculate the bounding box size etisserant@0: bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) etisserant@0: bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE etisserant@0: if self.Name != "": etisserant@0: bbx_y = self.Pos.y - (text_height + 2) etisserant@0: bbx_height = self.Size[1] + (text_height + 2) etisserant@0: else: etisserant@0: bbx_y = self.Pos.y etisserant@0: bbx_height = self.Size[1] lbessard@118: if self.ExecutionOrder != 0: lbessard@118: bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0]) lbessard@118: bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0]) lbessard@118: bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2) lbessard@64: self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) etisserant@0: etisserant@0: # Refresh the positions of the block connectors etisserant@0: def RefreshConnectors(self): lbessard@145: scaling = self.Parent.GetScaling() etisserant@0: # Calculate the size for the connector lines etisserant@0: lines = max(len(self.Inputs), len(self.Outputs)) lbessard@103: if lines > 0: lbessard@103: linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE) lbessard@145: # Update inputs and outputs positions lbessard@103: position = BLOCK_LINE_SIZE + linesize / 2 lbessard@145: for i in xrange(lines): lbessard@145: if scaling is not None: lbessard@145: ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: else: lbessard@145: ypos = position lbessard@145: if i < len(self.Inputs): lbessard@145: self.Inputs[i].SetPosition(wx.Point(0, ypos)) lbessard@145: if i < len(self.Outputs): lbessard@145: self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos)) lbessard@103: position += linesize etisserant@0: self.RefreshConnected() etisserant@0: etisserant@0: # Refresh the positions of wires connected to inputs and outputs etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: for input in self.Inputs: etisserant@0: input.MoveConnected(exclude) etisserant@0: for output in self.Outputs: etisserant@0: output.MoveConnected(exclude) etisserant@0: etisserant@0: # Returns the block connector that starts with the point given if it exists lbessard@27: def GetConnector(self, position, name = None): lbessard@27: # if a name is given lbessard@27: if name: lbessard@27: # Test each input and output connector lbessard@27: for input in self.Inputs: lbessard@27: if name == input.GetName(): lbessard@27: return input lbessard@27: for output in self.Outputs: lbessard@27: if name == output.GetName(): lbessard@27: return output etisserant@0: # Test each input connector etisserant@0: for input in self.Inputs: etisserant@0: input_pos = input.GetRelPosition() etisserant@0: if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: etisserant@0: return input etisserant@0: # Test each output connector etisserant@0: for output in self.Outputs: etisserant@0: output_pos = output.GetRelPosition() etisserant@0: if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: etisserant@0: return output etisserant@0: return None etisserant@0: lbessard@28: def GetInputTypes(self): lbessard@112: return tuple([input.GetType(True) for input in self.Inputs]) lbessard@28: lbessard@102: def SetOutputValues(self, values): lbessard@102: for output in self.Outputs: lbessard@102: output.SetValue(values.get(ouput.getName(), None)) lbessard@102: lbessard@102: def GetConnectionResultType(self, connector, connectortype): lbessard@102: resulttype = connectortype lbessard@99: for input in self.Inputs: lbessard@99: name = input.GetName() lbessard@99: if input != connector and (name.startswith("IN") or name in ["MN", "MX"]): lbessard@99: inputtype = input.GetConnectedType() lbessard@231: if resulttype is None or inputtype is not None and self.IsOfType(inputtype, resulttype): lbessard@99: resulttype = inputtype lbessard@99: for output in self.Outputs: lbessard@99: name = output.GetName() lbessard@231: if output != connector and name == "OUT" and not self.IsEndType(output.GetType()): lbessard@99: outputtype = output.GetConnectedType() lbessard@231: if resulttype is None or outputtype is not None and self.IsOfType(outputtype, resulttype): lbessard@99: resulttype = outputtype lbessard@99: return resulttype lbessard@99: lbessard@145: # Returns all the block connectors etisserant@0: def GetConnectors(self): etisserant@0: return {"inputs" : self.Inputs, "outputs" : self.Outputs} etisserant@0: etisserant@0: # Test if point given is on one of the block connectors lbessard@243: def TestConnector(self, pt, direction = None, exclude = True): etisserant@0: # Test each input connector etisserant@0: for input in self.Inputs: lbessard@243: if input.TestPoint(pt, direction, exclude): etisserant@0: return input etisserant@0: # Test each output connector etisserant@0: for output in self.Outputs: lbessard@243: if output.TestPoint(pt, direction, exclude): etisserant@0: return output etisserant@0: return None etisserant@0: lbessard@2: # Changes the block type lbessard@90: def SetType(self, type, extension, inputs = None, connectors = {}): lbessard@27: if type != self.Type or self.Extension != extension: lbessard@42: if type != self.Type: lbessard@42: self.Type = type lbessard@165: self.TypeSize = self.Parent.GetTextExtent(self.Type) lbessard@27: self.Extension = extension lbessard@3: # Find the block definition from type given and create the corresponding lbessard@3: # inputs and outputs lbessard@231: blocktype = self.Parent.GetBlockType(type, inputs) lbessard@3: if blocktype: lbessard@90: self.Colour = wx.BLACK lbessard@3: inputs = [input for input in blocktype["inputs"]] lbessard@3: outputs = [output for output in blocktype["outputs"]] lbessard@3: if blocktype["extensible"]: lbessard@3: start = int(inputs[-1][0].replace("IN", "")) lbessard@3: for i in xrange(self.Extension - len(blocktype["inputs"])): lbessard@3: start += 1 lbessard@27: inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2])) lbessard@16: else: lbessard@90: self.Colour = wx.RED lbessard@90: if "inputs" in connectors: lbessard@90: inputs = connectors["inputs"] lbessard@90: else: lbessard@90: inputs = [] lbessard@90: if "outputs" in connectors: lbessard@90: outputs = connectors["outputs"] lbessard@90: else: lbessard@90: outputs = [] lbessard@90: self.Pen = wx.Pen(self.Colour) lbessard@3: self.Clean() lbessard@3: # Extract the inputs properties and create the corresponding connector lbessard@3: self.Inputs = [] lbessard@3: for input_name, input_type, input_modifier in inputs: lbessard@64: connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone = True) lbessard@3: if input_modifier == "negated": lbessard@3: connector.SetNegated(True) lbessard@3: elif input_modifier != "none": lbessard@3: connector.SetEdge(input_modifier) lbessard@3: self.Inputs.append(connector) lbessard@3: # Extract the outputs properties and create the corresponding connector lbessard@3: self.Outputs = [] lbessard@3: for output_name, output_type, output_modifier in outputs: lbessard@64: connector = Connector(self, output_name, output_type, wx.Point(0, 0), EAST) lbessard@3: if output_modifier == "negated": lbessard@3: connector.SetNegated(True) lbessard@3: elif output_modifier != "none": lbessard@3: connector.SetEdge(output_modifier) lbessard@3: self.Outputs.append(connector) lbessard@42: self.RefreshMinSize() lbessard@3: self.RefreshConnectors() lbessard@3: self.RefreshBoundingBox() lbessard@2: etisserant@0: # Returns the block type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: etisserant@0: # Changes the block name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() etisserant@0: etisserant@0: # Returs the block name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: lbessard@2: # Changes the extension name lbessard@2: def SetExtension(self, extension): lbessard@2: self.Extension = extension lbessard@2: lbessard@2: # Returs the extension name lbessard@2: def GetExtension(self): lbessard@2: return self.Extension lbessard@2: lbessard@118: # Changes the execution order lbessard@118: def SetExecutionOrder(self, executionOrder): lbessard@118: self.ExecutionOrder = executionOrder lbessard@118: self.RefreshExecutionOrderSize() lbessard@118: lbessard@118: # Returs the execution order lbessard@118: def GetExecutionOrder(self): lbessard@118: return self.ExecutionOrder lbessard@118: lbessard@42: # Refresh the block minimum size lbessard@42: def RefreshMinSize(self): etisserant@0: # Calculate the inputs maximum width etisserant@0: max_input = 0 etisserant@0: for input in self.Inputs: lbessard@42: w, h = input.GetNameSize() etisserant@0: max_input = max(max_input, w) etisserant@0: # Calculate the outputs maximum width etisserant@0: max_output = 0 etisserant@0: for output in self.Outputs: lbessard@42: w, h = output.GetNameSize() etisserant@0: max_output = max(max_output, w) lbessard@42: width = max(self.TypeSize[0] + 10, max_input + max_output + 15) etisserant@0: height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE lbessard@42: self.MinSize = width, height lbessard@42: lbessard@42: # Returns the block minimum size lbessard@42: def GetMinSize(self): lbessard@42: return self.MinSize etisserant@0: etisserant@0: # Changes the negated property of the connector handled etisserant@0: def SetConnectorNegated(self, negated): etisserant@0: handle_type, handle = self.Handle etisserant@0: if handle_type == HANDLE_CONNECTOR: etisserant@0: handle.SetNegated(negated) etisserant@0: self.RefreshModel(False) etisserant@0: etisserant@0: # Changes the edge property of the connector handled etisserant@0: def SetConnectorEdge(self, edge): etisserant@0: handle_type, handle = self.Handle etisserant@0: if handle_type == HANDLE_CONNECTOR: etisserant@0: handle.SetEdge(edge) etisserant@0: self.RefreshModel(False) etisserant@0: lbessard@2: # Method called when a LeftDClick event have been generated lbessard@27: def OnLeftDClick(self, event, dc, scaling): lbessard@3: # Edit the block properties lbessard@2: self.Parent.EditBlockContent(self) lbessard@2: etisserant@0: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: # Popup the menu with special items for a block and a connector if one is handled lbessard@243: connector = self.TestConnector(pos, exclude=False) etisserant@0: if connector: etisserant@0: self.Handle = (HANDLE_CONNECTOR, connector) etisserant@0: self.Parent.PopupBlockMenu(connector) etisserant@0: else: etisserant@0: self.Parent.PopupBlockMenu() etisserant@0: etisserant@0: # Refreshes the block model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshBlockModel(self) etisserant@0: # If block has moved, refresh the model of wires connected to outputs etisserant@0: if move: etisserant@0: for output in self.Outputs: etisserant@0: output.RefreshWires() etisserant@0: lbessard@231: def AddError(self, infos, start, end): lbessard@231: if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0: lbessard@231: self.Errors[infos[0]] = (start, end) lbessard@231: elif infos[0] == "input" and infos[1] < len(self.Inputs): lbessard@231: self.Inputs[infos[1]].AddError(infos[2:], start, end) lbessard@231: elif infos[0] == "output" and infos[1] < len(self.Outputs): lbessard@231: self.Outputs[infos[1]].AddError(infos[2:], start, end) lbessard@231: etisserant@0: # Draws block etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@90: dc.SetPen(self.Pen) lbessard@64: dc.SetBrush(wx.WHITE_BRUSH) lbessard@90: dc.SetTextForeground(self.Colour) lbessard@213: lbessard@213: if getattr(dc, "printing", False): lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: type_size = dc.GetTextExtent(self.Type) lbessard@213: executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) lbessard@213: else: lbessard@213: name_size = self.NameSize lbessard@213: type_size = self.TypeSize lbessard@213: executionorder_size = self.ExecutionOrderSize lbessard@213: etisserant@0: # Draw a rectangle with the block size etisserant@0: dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) etisserant@0: # Draw block name and block type lbessard@231: name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, lbessard@231: self.Pos.y - (name_size[1] + 2)) lbessard@231: type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2, lbessard@231: self.Pos.y + 5) lbessard@231: dc.DrawText(self.Name, name_pos[0], name_pos[1]) lbessard@231: dc.DrawText(self.Type, type_pos[0], type_pos[1]) etisserant@0: # Draw inputs and outputs connectors etisserant@0: for input in self.Inputs: etisserant@0: input.Draw(dc) etisserant@0: for output in self.Outputs: etisserant@0: output.Draw(dc) lbessard@118: if self.ExecutionOrder != 0: lbessard@118: # Draw block execution order lbessard@213: dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], lbessard@118: self.Pos.y + self.Size[1] + 2) lbessard@231: if "name" in self.Errors: lbessard@231: HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) lbessard@231: if "type" in self.Errors: lbessard@231: HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) lbessard@140: dc.SetTextForeground(wx.BLACK) lbessard@140: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Function Block Diagram Variable etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a variable etisserant@0: """ etisserant@0: etisserant@0: class FBD_Variable(Graphic_Element): etisserant@0: etisserant@0: # Create a new variable lbessard@118: def __init__(self, parent, type, name, value_type, id = None, executionOrder = 0): etisserant@0: Graphic_Element.__init__(self, parent) lbessard@3: self.Type = None lbessard@3: self.ValueType = None etisserant@0: self.Id = id lbessard@118: self.SetName(name) lbessard@118: self.SetExecutionOrder(executionOrder) etisserant@0: self.Input = None etisserant@0: self.Output = None lbessard@3: self.SetType(type, value_type) lbessard@231: self.Errors = [] etisserant@0: lbessard@112: # Make a clone of this FBD_Variable lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id) lbessard@112: variable.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: variable.SetPosition(pos.x, pos.y) lbessard@144: if self.Input: lbessard@144: variable.Input = self.Input.Clone(variable) lbessard@144: if self.Output: lbessard@144: variable.Output = self.Output.Clone(variable) lbessard@112: return variable lbessard@112: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Input = None etisserant@0: self.Output = None etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: if movex != 0 or movey != 0: lbessard@144: if self.Input and self.Input.IsConnected(): lbessard@144: rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) lbessard@144: if self.Output and self.Output.IsConnected(): lbessard@144: rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@0: # Unconnect connector etisserant@0: def Clean(self): etisserant@0: if self.Input: lbessard@3: self.Input.UnConnect(delete = True) etisserant@0: if self.Output: lbessard@3: self.Output.UnConnect(delete = True) etisserant@0: etisserant@0: # Delete this variable by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteVariable(self) etisserant@0: lbessard@42: # Refresh the size of text for name lbessard@42: def RefreshNameSize(self): lbessard@165: self.NameSize = self.Parent.GetTextExtent(self.Name) lbessard@42: lbessard@118: # Refresh the size of text for execution order lbessard@118: def RefreshExecutionOrderSize(self): lbessard@165: self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) lbessard@118: etisserant@0: # Refresh the variable bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: if self.Type in (OUTPUT, INOUT): etisserant@0: bbx_x = self.Pos.x - CONNECTOR_SIZE etisserant@0: else: etisserant@0: bbx_x = self.Pos.x etisserant@0: if self.Type == INOUT: etisserant@0: bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE etisserant@0: else: etisserant@0: bbx_width = self.Size[0] + CONNECTOR_SIZE lbessard@118: bbx_height = self.Size[1] lbessard@118: if self.ExecutionOrder != 0: lbessard@118: bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0]) lbessard@118: bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0]) lbessard@118: bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2) lbessard@118: self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1) etisserant@0: etisserant@0: # Refresh the position of the variable connector etisserant@0: def RefreshConnectors(self): lbessard@145: scaling = self.Parent.GetScaling() lbessard@145: if scaling is not None: lbessard@145: position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: else: lbessard@145: position = self.Size[1] / 2 etisserant@0: if self.Input: lbessard@145: self.Input.SetPosition(wx.Point(0, position)) etisserant@0: if self.Output: lbessard@145: self.Output.SetPosition(wx.Point(self.Size[0], position)) lbessard@28: self.RefreshConnected() etisserant@0: etisserant@0: # Refresh the position of wires connected to connector etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: if self.Input: etisserant@0: self.Input.MoveConnected(exclude) etisserant@0: if self.Output: etisserant@0: self.Output.MoveConnected(exclude) etisserant@0: etisserant@0: # Test if point given is on the variable connector lbessard@243: def TestConnector(self, pt, direction = None, exclude=True): lbessard@243: if self.Input and self.Input.TestPoint(pt, direction, exclude): etisserant@0: return self.Input lbessard@243: if self.Output and self.Output.TestPoint(pt, direction, exclude): etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Returns the block connector that starts with the point given if it exists lbessard@27: def GetConnector(self, position, name = None): lbessard@27: # if a name is given lbessard@27: if name: lbessard@27: # Test input and output connector if they exists lbessard@27: if self.Input and name == self.Input.GetName(): lbessard@27: return self.Input lbessard@27: if self.Output and name == self.Output.GetName(): lbessard@27: return self.Output etisserant@0: # Test input connector if it exists etisserant@0: if self.Input: etisserant@0: input_pos = self.Input.GetRelPosition() etisserant@0: if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: etisserant@0: return self.Input etisserant@0: # Test output connector if it exists etisserant@0: if self.Output: etisserant@0: output_pos = self.Output.GetRelPosition() etisserant@0: if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: etisserant@0: return self.Output etisserant@0: return None etisserant@0: etisserant@0: # Returns all the block connectors etisserant@0: def GetConnectors(self): etisserant@0: return {"input" : self.Input, "output" : self.Output} etisserant@0: etisserant@0: # Changes the negated property of the variable connector if handled etisserant@0: def SetConnectorNegated(self, negated): etisserant@0: handle_type, handle = self.Handle etisserant@0: if handle_type == HANDLE_CONNECTOR: etisserant@0: handle.SetNegated(negated) etisserant@0: self.RefreshModel(False) etisserant@0: lbessard@3: # Changes the variable type lbessard@3: def SetType(self, type, value_type): lbessard@32: if type != self.Type: lbessard@3: self.Type = type lbessard@3: self.Clean() lbessard@3: self.Input = None lbessard@3: self.Output = None lbessard@3: # Create an input or output connector according to variable type lbessard@3: if self.Type != INPUT: lbessard@64: self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True) lbessard@3: if self.Type != OUTPUT: lbessard@64: self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) lbessard@3: self.RefreshConnectors() lbessard@32: elif value_type != self.ValueType: lbessard@32: if self.Input: lbessard@32: self.Input.SetType(value_type) lbessard@32: if self.Output: lbessard@32: self.Output.SetType(value_type) lbessard@32: self.RefreshConnectors() lbessard@3: etisserant@0: # Returns the variable type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: etisserant@0: # Changes the variable name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() etisserant@0: etisserant@0: # Returns the variable name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: lbessard@118: # Changes the execution order lbessard@118: def SetExecutionOrder(self, executionOrder): lbessard@118: self.ExecutionOrder = executionOrder lbessard@118: self.RefreshExecutionOrderSize() lbessard@118: lbessard@118: # Returs the execution order lbessard@118: def GetExecutionOrder(self): lbessard@118: return self.ExecutionOrder lbessard@118: etisserant@0: # Returns the variable minimum size etisserant@0: def GetMinSize(self): lbessard@42: return self.NameSize[0] + 10, self.NameSize[1] + 10 etisserant@0: lbessard@3: # Method called when a LeftDClick event have been generated lbessard@27: def OnLeftDClick(self, event, dc, scaling): lbessard@3: # Edit the variable properties lbessard@3: self.Parent.EditVariableContent(self) lbessard@3: etisserant@0: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): lbessard@102: self.Parent.PopupDefaultMenu() etisserant@0: etisserant@0: # Refreshes the variable model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshVariableModel(self) etisserant@0: # If variable has moved and variable is not of type OUTPUT, refresh the model etisserant@0: # of wires connected to output connector etisserant@0: if move and self.Type != OUTPUT: etisserant@0: if self.Output: etisserant@0: self.Output.RefreshWires() etisserant@0: lbessard@231: def AddError(self, infos, start, end): lbessard@231: if infos[0] == "expression" and start[0] == 0 and end[0] == 0: lbessard@231: self.Errors.append((start[1], end[1])) lbessard@231: etisserant@0: # Draws variable etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.WHITE_BRUSH) lbessard@213: lbessard@213: if getattr(dc, "printing", False): lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) lbessard@213: else: lbessard@213: name_size = self.NameSize lbessard@213: executionorder_size = self.ExecutionOrderSize lbessard@213: lbessard@231: text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, lbessard@231: self.Pos.y + (self.Size[1] - name_size[1]) / 2) etisserant@0: # Draw a rectangle with the variable size etisserant@0: dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) etisserant@0: # Draw variable name lbessard@231: dc.DrawText(self.Name, text_pos[0], text_pos[1]) etisserant@0: # Draw connectors etisserant@0: if self.Input: etisserant@0: self.Input.Draw(dc) etisserant@0: if self.Output: etisserant@0: self.Output.Draw(dc) lbessard@118: if self.ExecutionOrder != 0: lbessard@118: # Draw variable execution order lbessard@213: dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], lbessard@118: self.Pos.y + self.Size[1] + 2) lbessard@231: for start, end in self.Errors: lbessard@231: offset = dc.GetTextExtent(self.Name[:start]) lbessard@231: size = dc.GetTextExtent(self.Name[start:end + 1]) lbessard@231: HighlightErrorZone(dc, text_pos[0] + offset[0], text_pos[1], size[0], size[1]) etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Function Block Diagram Connector etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements the graphic representation of a connection etisserant@0: """ etisserant@0: etisserant@0: class FBD_Connector(Graphic_Element): etisserant@0: etisserant@0: # Create a new connection etisserant@0: def __init__(self, parent, type, name, id = None): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Type = type etisserant@0: self.Id = id lbessard@118: self.SetName(name) lbessard@64: self.Pos = wx.Point(0, 0) lbessard@64: self.Size = wx.Size(0, 0) etisserant@0: # Create an input or output connector according to connection type etisserant@0: if self.Type == CONNECTOR: lbessard@64: self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) lbessard@64: else: lbessard@64: self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) etisserant@0: self.RefreshConnectors() lbessard@42: self.RefreshNameSize() etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Connector = None etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = Graphic_Element.GetRedrawRect(self, movex, movey) lbessard@144: if movex != 0 or movey != 0: lbessard@144: if self.Connector and self.Connector.IsConnected(): lbessard@144: rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: lbessard@112: # Make a clone of this FBD_Connector lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: connection = FBD_Connector(parent, self.Type, self.Name, id) lbessard@112: connection.SetSize(self.Size[0], self.Size[1]) lbessard@112: if pos is not None: lbessard@112: connection.SetPosition(pos.x, pos.y) lbessard@112: connection.Connector = self.Connector.Clone(connection) lbessard@112: return connection lbessard@112: etisserant@0: # Unconnect connector etisserant@0: def Clean(self): etisserant@0: if self.Connector: lbessard@28: self.Connector.UnConnect(delete = True) etisserant@0: etisserant@0: # Delete this connection by calling the appropriate method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteConnection(self) etisserant@0: lbessard@42: # Refresh the size of text for name lbessard@42: def RefreshNameSize(self): lbessard@165: self.NameSize = self.Parent.GetTextExtent(self.Name) lbessard@42: etisserant@0: # Refresh the connection bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: if self.Type == CONNECTOR: etisserant@0: bbx_x = self.Pos.x - CONNECTOR_SIZE etisserant@0: else: etisserant@0: bbx_x = self.Pos.x etisserant@0: bbx_width = self.Size[0] + CONNECTOR_SIZE lbessard@64: self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) etisserant@0: etisserant@0: # Refresh the position of the connection connector etisserant@0: def RefreshConnectors(self): lbessard@145: scaling = self.Parent.GetScaling() lbessard@145: if scaling is not None: lbessard@145: position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y lbessard@145: else: lbessard@145: position = self.Size[1] / 2 etisserant@0: if self.Type == CONNECTOR: lbessard@145: self.Connector.SetPosition(wx.Point(0, position)) lbessard@145: else: lbessard@145: self.Connector.SetPosition(wx.Point(self.Size[0], position)) lbessard@28: self.RefreshConnected() etisserant@0: etisserant@0: # Refresh the position of wires connected to connector etisserant@0: def RefreshConnected(self, exclude = []): etisserant@0: if self.Connector: etisserant@0: self.Connector.MoveConnected(exclude) etisserant@0: etisserant@0: # Test if point given is on the connection connector lbessard@243: def TestConnector(self, pt, direction = None, exclude=True): lbessard@243: if self.Connector and self.Connector.TestPoint(pt, direction, exclude): etisserant@0: return self.Connector etisserant@0: return None etisserant@0: etisserant@0: # Returns the connection connector lbessard@27: def GetConnector(self, position = None, name = None): etisserant@0: return self.Connector etisserant@0: lbessard@28: # Changes the variable type lbessard@28: def SetType(self, type): lbessard@28: if type != self.Type: lbessard@28: self.Type = type lbessard@28: self.Clean() lbessard@28: # Create an input or output connector according to connection type lbessard@28: if self.Type == CONNECTOR: lbessard@64: self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) lbessard@28: else: lbessard@64: self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) lbessard@28: self.RefreshConnectors() lbessard@28: etisserant@0: # Returns the connection type etisserant@0: def GetType(self): etisserant@0: return self.Type etisserant@0: lbessard@145: def GetConnectionResultType(self, connector, connectortype): lbessard@145: return connectortype lbessard@145: etisserant@0: # Changes the connection name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() etisserant@0: etisserant@0: # Returns the connection name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: etisserant@0: # Returns the connection minimum size etisserant@0: def GetMinSize(self): lbessard@42: text_width, text_height = self.NameSize etisserant@0: if text_height % 2 == 1: etisserant@0: text_height += 1 etisserant@0: return text_width + text_height + 20, text_height + 10 etisserant@0: lbessard@28: # Method called when a LeftDClick event have been generated lbessard@28: def OnLeftDClick(self, event, dc, scaling): lbessard@28: # Edit the connection properties lbessard@28: self.Parent.EditConnectionContent(self) lbessard@28: etisserant@0: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): etisserant@0: # Popup the default menu etisserant@0: self.Parent.PopupDefaultMenu() etisserant@0: etisserant@0: # Refreshes the connection model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshConnectionModel(self) etisserant@0: # If connection has moved and connection is of type CONTINUATION, refresh etisserant@0: # the model of wires connected to connector etisserant@0: if move and self.Type == CONTINUATION: etisserant@0: if self.Connector: etisserant@0: self.Connector.RefreshWires() etisserant@0: etisserant@0: # Draws connection etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.WHITE_BRUSH) lbessard@213: lbessard@213: if getattr(dc, "printing", False): lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: else: lbessard@213: name_size = self.NameSize lbessard@213: etisserant@0: # Draw a rectangle with the connection size with arrows in etisserant@0: dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) lbessard@213: arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2) etisserant@0: dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, etisserant@0: self.Pos.y + self.Size[1] / 2) etisserant@0: dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, etisserant@0: self.Pos.x, self.Pos.y + self.Size[1]) etisserant@0: dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, etisserant@0: self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) etisserant@0: dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, etisserant@0: self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) lbessard@118: # Draw connection name lbessard@213: dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - name_size[0]) / 2, lbessard@213: self.Pos.y + (self.Size[1] - name_size[1]) / 2) etisserant@0: # Draw connector etisserant@0: if self.Connector: etisserant@0: self.Connector.Draw(dc) lbessard@140: