graphics/FBD_Objects.py
changeset 27 dae55dd9ee14
parent 16 20dcc0dce64b
child 28 fc23e1f415d8
equal deleted inserted replaced
26:36d378bd852e 27:dae55dd9ee14
    38 """
    38 """
    39 
    39 
    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):
    44         Graphic_Element.__init__(self, parent)
    44         Graphic_Element.__init__(self, parent)
    45         self.Type = None
    45         self.Type = None
       
    46         self.Extension = None
    46         self.Name = name
    47         self.Name = name
    47         self.Id = id
    48         self.Id = id
    48         self.Extension = extension
       
    49         self.Inputs = []
    49         self.Inputs = []
    50         self.Outputs = []
    50         self.Outputs = []
    51         self.SetType(type)
    51         self.SetType(type, extension)
    52     
    52     
    53     # Destructor
    53     # Destructor
    54     def __del__(self):
    54     def __del__(self):
    55         self.Inputs = []
    55         self.Inputs = []
    56         self.Outputs = []
    56         self.Outputs = []
   105             input.MoveConnected(exclude)
   105             input.MoveConnected(exclude)
   106         for output in self.Outputs:
   106         for output in self.Outputs:
   107             output.MoveConnected(exclude)
   107             output.MoveConnected(exclude)
   108     
   108     
   109     # Returns the block connector that starts with the point given if it exists 
   109     # Returns the block connector that starts with the point given if it exists 
   110     def GetConnector(self, position):
   110     def GetConnector(self, position, name = None):
       
   111         # if a name is given
       
   112         if name:
       
   113             # Test each input and output connector
       
   114             for input in self.Inputs:
       
   115                 if name == input.GetName():
       
   116                     return input
       
   117             for output in self.Outputs:
       
   118                 if name == output.GetName():
       
   119                     return output
   111         # Test each input connector
   120         # Test each input connector
   112         for input in self.Inputs:
   121         for input in self.Inputs:
   113             input_pos = input.GetRelPosition()
   122             input_pos = input.GetRelPosition()
   114             if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   123             if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   115                 return input
   124                 return input
   135             if output.TestPoint(pt, exclude):
   144             if output.TestPoint(pt, exclude):
   136                 return output
   145                 return output
   137         return None
   146         return None
   138     
   147     
   139     # Changes the block type
   148     # Changes the block type
   140     def SetType(self, type):
   149     def SetType(self, type, extension):
   141         if type != self.Type: 
   150         if type != self.Type or self.Extension != extension: 
   142             self.Type = type
   151             self.Type = type
       
   152             self.Extension = extension
   143             # Find the block definition from type given and create the corresponding
   153             # Find the block definition from type given and create the corresponding
   144             # inputs and outputs
   154             # inputs and outputs
   145             blocktype = GetBlockType(type)
   155             blocktype = GetBlockType(type)
   146             if blocktype:
   156             if blocktype:
   147                 inputs = [input for input in blocktype["inputs"]]
   157                 inputs = [input for input in blocktype["inputs"]]
   148                 outputs = [output for output in blocktype["outputs"]]
   158                 outputs = [output for output in blocktype["outputs"]]
   149                 if blocktype["extensible"]:
   159                 if blocktype["extensible"]:
   150                     start = int(inputs[-1][0].replace("IN", ""))
   160                     start = int(inputs[-1][0].replace("IN", ""))
   151                     for i in xrange(self.Extension - len(blocktype["inputs"])):
   161                     for i in xrange(self.Extension - len(blocktype["inputs"])):
   152                         start += 1
   162                         start += 1
   153                         inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2]))
   163                         inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2]))
   154             else:
   164             else:
   155                 raise ValueError, "This block type isn't defined"
   165                 raise ValueError, "This block type isn't defined"
   156             self.Clean()
   166             self.Clean()
   157             # Extract the inputs properties and create the corresponding connector
   167             # Extract the inputs properties and create the corresponding connector
   158             self.Inputs = []
   168             self.Inputs = []
   159             for input_name, input_type, input_modifier in inputs:
   169             for input_name, input_type, input_modifier in inputs:
   160                 connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST)
   170                 connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST, onlyone = True)
   161                 if input_modifier == "negated":
   171                 if input_modifier == "negated":
   162                     connector.SetNegated(True)
   172                     connector.SetNegated(True)
   163                 elif input_modifier != "none":
   173                 elif input_modifier != "none":
   164                     connector.SetEdge(input_modifier)
   174                     connector.SetEdge(input_modifier)
   165                 self.Inputs.append(connector)
   175                 self.Inputs.append(connector)
   226         if handle_type == HANDLE_CONNECTOR:
   236         if handle_type == HANDLE_CONNECTOR:
   227             handle.SetEdge(edge)
   237             handle.SetEdge(edge)
   228             self.RefreshModel(False)
   238             self.RefreshModel(False)
   229     
   239     
   230     # Method called when a LeftDClick event have been generated
   240     # Method called when a LeftDClick event have been generated
   231     def OnLeftDClick(self, event, scaling):
   241     def OnLeftDClick(self, event, dc, scaling):
   232         # Edit the block properties
   242         # Edit the block properties
   233         self.Parent.EditBlockContent(self)
   243         self.Parent.EditBlockContent(self)
   234     
   244     
   235     # Method called when a RightUp event have been generated
   245     # Method called when a RightUp event have been generated
   236     def OnRightUp(self, event, scaling):
   246     def OnRightUp(self, event, dc, scaling):
   237         pos = GetScaledEventPosition(event, scaling)
   247         pos = GetScaledEventPosition(event, dc, scaling)
   238         # Popup the menu with special items for a block and a connector if one is handled
   248         # Popup the menu with special items for a block and a connector if one is handled
   239         connector = self.TestConnector(pos, False)
   249         connector = self.TestConnector(pos, False)
   240         if connector:
   250         if connector:
   241             self.Handle = (HANDLE_CONNECTOR, connector)
   251             self.Handle = (HANDLE_CONNECTOR, connector)
   242             self.Parent.PopupBlockMenu(connector)
   252             self.Parent.PopupBlockMenu(connector)
   344         if self.Output and self.Output.TestPoint(pt, exclude):
   354         if self.Output and self.Output.TestPoint(pt, exclude):
   345             return self.Output
   355             return self.Output
   346         return None
   356         return None
   347     
   357     
   348     # Returns the block connector that starts with the point given if it exists 
   358     # Returns the block connector that starts with the point given if it exists 
   349     def GetConnector(self, position):
   359     def GetConnector(self, position, name = None):
       
   360         # if a name is given
       
   361         if name:
       
   362             # Test input and output connector if they exists
       
   363             if self.Input and name == self.Input.GetName():
       
   364                 return self.Input
       
   365             if self.Output and name == self.Output.GetName():
       
   366                 return self.Output
   350         # Test input connector if it exists
   367         # Test input connector if it exists
   351         if self.Input:
   368         if self.Input:
   352             input_pos = self.Input.GetRelPosition()
   369             input_pos = self.Input.GetRelPosition()
   353             if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   370             if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   354                 return self.Input
   371                 return self.Input
   378             self.Clean()
   395             self.Clean()
   379             self.Input = None
   396             self.Input = None
   380             self.Output = None
   397             self.Output = None
   381             # Create an input or output connector according to variable type
   398             # Create an input or output connector according to variable type
   382             if self.Type != INPUT:
   399             if self.Type != INPUT:
   383                 self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST)
   400                 self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST, onlyone = True)
   384             if self.Type != OUTPUT:
   401             if self.Type != OUTPUT:
   385                 self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST)
   402                 self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST)
   386             self.RefreshConnectors()
   403             self.RefreshConnectors()
   387     
   404     
   388     # Returns the variable type
   405     # Returns the variable type
   402         dc = wxClientDC(self.Parent)
   419         dc = wxClientDC(self.Parent)
   403         text_width, text_height = dc.GetTextExtent(self.Name)
   420         text_width, text_height = dc.GetTextExtent(self.Name)
   404         return text_width + 10, text_height + 10
   421         return text_width + 10, text_height + 10
   405     
   422     
   406     # Method called when a LeftDClick event have been generated
   423     # Method called when a LeftDClick event have been generated
   407     def OnLeftDClick(self, event, scaling):
   424     def OnLeftDClick(self, event, dc, scaling):
   408         # Edit the variable properties
   425         # Edit the variable properties
   409         self.Parent.EditVariableContent(self)
   426         self.Parent.EditVariableContent(self)
   410     
   427     
   411     # Method called when a RightUp event have been generated
   428     # Method called when a RightUp event have been generated
   412     def OnRightUp(self, event, scaling):
   429     def OnRightUp(self, event, dc, scaling):
   413         pos = GetScaledEventPosition(event, scaling)
   430         pos = GetScaledEventPosition(event, dc, scaling)
   414         # Popup the menu with special items for a variable and a connector if it's handled
   431         # Popup the menu with special items for a variable and a connector if it's handled
   415         connector = self.TestConnectors(pos, False)
   432         connector = self.TestConnector(pos, False)
   416         if connector:
   433         if connector:
   417             self.Handle = (HANDLE_CONNECTOR, connector)
   434             self.Handle = (HANDLE_CONNECTOR, connector)
   418             self.Parent.PopupVariableMenu(connector)
   435             self.Parent.PopupVariableMenu(connector)
   419         else:
   436         else:
   420             self.Parent.PopupVariableMenu()
   437             self.Parent.PopupVariableMenu()
   464         self.Id = id
   481         self.Id = id
   465         self.Pos = wxPoint(0, 0)
   482         self.Pos = wxPoint(0, 0)
   466         self.Size = wxSize(0, 0)
   483         self.Size = wxSize(0, 0)
   467         # Create an input or output connector according to connection type
   484         # Create an input or output connector according to connection type
   468         if self.Type == CONNECTOR:
   485         if self.Type == CONNECTOR:
   469             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST)
   486             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True)
   470         else:
   487         else:
   471             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   488             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   472         self.RefreshConnectors()
   489         self.RefreshConnectors()
   473     
   490     
   474     # Destructor
   491     # Destructor
   512         if self.Connector and self.Connector.TestPoint(pt, exclude):
   529         if self.Connector and self.Connector.TestPoint(pt, exclude):
   513             return self.Connector
   530             return self.Connector
   514         return None
   531         return None
   515     
   532     
   516     # Returns the connection connector
   533     # Returns the connection connector
   517     def GetConnector(self, position = None):
   534     def GetConnector(self, position = None, name = None):
   518         return self.Connector
   535         return self.Connector
   519     
   536     
   520     # Returns the connection type
   537     # Returns the connection type
   521     def GetType(self):
   538     def GetType(self):
   522         return self.Type
   539         return self.Type
   536         if text_height % 2 == 1:
   553         if text_height % 2 == 1:
   537             text_height += 1
   554             text_height += 1
   538         return text_width + text_height + 20, text_height + 10
   555         return text_width + text_height + 20, text_height + 10
   539     
   556     
   540     # Method called when a RightUp event have been generated
   557     # Method called when a RightUp event have been generated
   541     def OnRightUp(self, event, scaling):
   558     def OnRightUp(self, event, dc, scaling):
   542         # Popup the default menu
   559         # Popup the default menu
   543         self.Parent.PopupDefaultMenu()
   560         self.Parent.PopupDefaultMenu()
   544     
   561     
   545     # Refreshes the connection model
   562     # Refreshes the connection model
   546     def RefreshModel(self, move=True):
   563     def RefreshModel(self, move=True):