graphics/FBD_Objects.py
changeset 2 93bc4c2cf376
parent 0 b622defdfd98
child 3 86ccc89d7b0b
equal deleted inserted replaced
1:e9d01d824086 2:93bc4c2cf376
    40 class FBD_Block(Graphic_Element):
    40 class FBD_Block(Graphic_Element):
    41     
    41     
    42     # Create a new block
    42     # Create a new block
    43     def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []):
    43     def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []):
    44         Graphic_Element.__init__(self, parent)
    44         Graphic_Element.__init__(self, parent)
    45         self.Type = type
       
    46         self.Name = name
    45         self.Name = name
    47         self.Id = id
    46         self.Id = id
    48         # Find the block definition from type given and create the corresponding
    47         self.Extension = extension
    49         # inputs and outputs
    48         self.Inputs = []
    50         blocktype = GetBlockType(type)
    49         self.Outputs = []
    51         if blocktype:
    50         self.SetType(type)
    52             inputs = [input for input in blocktype["inputs"]]
       
    53             outputs = [output for output in blocktype["outputs"]]
       
    54             if blocktype["extensible"]:
       
    55                 start = int(inputs[-1][0].replace("IN", ""))
       
    56                 for i in xrange(extension - len(blocktype["inputs"])):
       
    57                     start += 1
       
    58                     inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2]))
       
    59         self.SetConnectors(inputs, outputs)
       
    60     
    51     
    61     # Destructor
    52     # Destructor
    62     def __del__(self):
    53     def __del__(self):
    63         self.Inputs = []
    54         self.Inputs = []
    64         self.Outputs = []
    55         self.Outputs = []
    68         self.Parent.DeleteBlock(self)
    59         self.Parent.DeleteBlock(self)
    69     
    60     
    70     # Unconnect all inputs and outputs
    61     # Unconnect all inputs and outputs
    71     def Clean(self):
    62     def Clean(self):
    72         for input in self.Inputs:
    63         for input in self.Inputs:
    73             input.UnConnect()
    64             input.UnConnect(delete = True)
    74         for output in self.Outputs:
    65         for output in self.Outputs:
    75             output.UnConnect()
    66             output.UnConnect(delete = True)
    76     
    67     
    77     # Refresh the block bounding box
    68     # Refresh the block bounding box
    78     def RefreshBoundingBox(self):
    69     def RefreshBoundingBox(self):
    79         dc = wxClientDC(self.Parent)
    70         dc = wxClientDC(self.Parent)
    80         # Calculate the size of the name outside the block
    71         # Calculate the size of the name outside the block
   142         for output in self.Outputs:
   133         for output in self.Outputs:
   143             if output.TestPoint(pt, exclude):
   134             if output.TestPoint(pt, exclude):
   144                 return output
   135                 return output
   145         return None
   136         return None
   146     
   137     
       
   138     # Changes the block type
       
   139     def SetType(self, type):
       
   140         self.Type = type
       
   141         # Find the block definition from type given and create the corresponding
       
   142         # inputs and outputs
       
   143         blocktype = GetBlockType(type)
       
   144         if blocktype:
       
   145             inputs = [input for input in blocktype["inputs"]]
       
   146             outputs = [output for output in blocktype["outputs"]]
       
   147             if blocktype["extensible"]:
       
   148                 start = int(inputs[-1][0].replace("IN", ""))
       
   149                 for i in xrange(self.Extension - len(blocktype["inputs"])):
       
   150                     start += 1
       
   151                     inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2]))
       
   152         self.SetConnectors(inputs, outputs)
       
   153     
   147     # Returns the block type
   154     # Returns the block type
   148     def GetType(self):
   155     def GetType(self):
   149         return self.Type
   156         return self.Type
   150     
   157     
   151     # Changes the block name
   158     # Changes the block name
   153         self.Name = name
   160         self.Name = name
   154     
   161     
   155     # Returs the block name
   162     # Returs the block name
   156     def GetName(self):
   163     def GetName(self):
   157         return self.Name
   164         return self.Name
       
   165     
       
   166     # Changes the extension name
       
   167     def SetExtension(self, extension):
       
   168         self.Extension = extension
       
   169     
       
   170     # Returs the extension name
       
   171     def GetExtension(self):
       
   172         return self.Extension
   158     
   173     
   159     # Returns the block minimum size
   174     # Returns the block minimum size
   160     def GetMinSize(self):
   175     def GetMinSize(self):
   161         dc = wxClientDC(self.Parent)
   176         dc = wxClientDC(self.Parent)
   162         text_width, text_height = dc.GetTextExtent(self.Type)
   177         text_width, text_height = dc.GetTextExtent(self.Type)
   174         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
   189         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
   175         return width, height
   190         return width, height
   176     
   191     
   177     # Changes the block connectors
   192     # Changes the block connectors
   178     def SetConnectors(self, inputs, outputs):
   193     def SetConnectors(self, inputs, outputs):
       
   194         self.Clean()
   179         # Extract the inputs properties and create the corresponding connector
   195         # Extract the inputs properties and create the corresponding connector
   180         self.Inputs = []
   196         self.Inputs = []
   181         for input_name, input_type, input_modifier in inputs:
   197         for input_name, input_type, input_modifier in inputs:
   182             connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST)
   198             connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST)
   183             if input_modifier == "negated":
   199             if input_modifier == "negated":
   192             if output_modifier == "negated":
   208             if output_modifier == "negated":
   193                 connector.SetNegated(True)
   209                 connector.SetNegated(True)
   194             elif output_modifier != "none":
   210             elif output_modifier != "none":
   195                 connector.SetEdge(output_modifier)
   211                 connector.SetEdge(output_modifier)
   196             self.Outputs.append(connector)
   212             self.Outputs.append(connector)
       
   213         self.RefreshConnectors()
   197         self.RefreshBoundingBox()
   214         self.RefreshBoundingBox()
   198     
   215     
   199     # Changes the negated property of the connector handled
   216     # Changes the negated property of the connector handled
   200     def SetConnectorNegated(self, negated):
   217     def SetConnectorNegated(self, negated):
   201         handle_type, handle = self.Handle
   218         handle_type, handle = self.Handle
   207     def SetConnectorEdge(self, edge):
   224     def SetConnectorEdge(self, edge):
   208         handle_type, handle = self.Handle
   225         handle_type, handle = self.Handle
   209         if handle_type == HANDLE_CONNECTOR:
   226         if handle_type == HANDLE_CONNECTOR:
   210             handle.SetEdge(edge)
   227             handle.SetEdge(edge)
   211             self.RefreshModel(False)
   228             self.RefreshModel(False)
       
   229     
       
   230     # Method called when a LeftDClick event have been generated
       
   231     def OnLeftDClick(self, event, scaling):
       
   232         # Edit the step properties
       
   233         self.Parent.EditBlockContent(self)
   212     
   234     
   213     # Method called when a RightUp event have been generated
   235     # Method called when a RightUp event have been generated
   214     def OnRightUp(self, event, scaling):
   236     def OnRightUp(self, event, scaling):
   215         pos = GetScaledEventPosition(event, scaling)
   237         pos = GetScaledEventPosition(event, scaling)
   216         # Popup the menu with special items for a block and a connector if one is handled
   238         # Popup the menu with special items for a block and a connector if one is handled