graphics/FBD_Objects.py
changeset 118 0c53d6a36013
parent 112 317148fc1225
child 140 06d28f03f6f4
equal deleted inserted replaced
117:bbe0697cf1ea 118:0c53d6a36013
    37 """
    37 """
    38 
    38 
    39 class FBD_Block(Graphic_Element):
    39 class FBD_Block(Graphic_Element):
    40     
    40     
    41     # Create a new block
    41     # Create a new block
    42     def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}):
    42     def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}, executionOrder = 0):
    43         Graphic_Element.__init__(self, parent)
    43         Graphic_Element.__init__(self, parent)
    44         self.Type = None
    44         self.Type = None
    45         self.Extension = None
    45         self.Extension = None
    46         self.Name = name
       
    47         self.Id = id
    46         self.Id = id
       
    47         self.SetName(name)
       
    48         self.SetExecutionOrder(executionOrder)
    48         self.Inputs = []
    49         self.Inputs = []
    49         self.Outputs = []
    50         self.Outputs = []
    50         self.RefreshNameSize()
       
    51         self.Colour = wx.BLACK
    51         self.Colour = wx.BLACK
    52         self.Pen = wx.BLACK_PEN
    52         self.Pen = wx.BLACK_PEN
    53         self.SetType(type, extension, inputs, connectors)
    53         self.SetType(type, extension, inputs, connectors)
    54     
    54     
    55     # Make a clone of this FBD_Block
    55     # Make a clone of this FBD_Block
    80     
    80     
    81     # Refresh the size of text for name
    81     # Refresh the size of text for name
    82     def RefreshNameSize(self):
    82     def RefreshNameSize(self):
    83         dc = wx.ClientDC(self.Parent)
    83         dc = wx.ClientDC(self.Parent)
    84         self.NameSize = dc.GetTextExtent(self.Name)
    84         self.NameSize = dc.GetTextExtent(self.Name)
       
    85     
       
    86     # Refresh the size of text for execution order
       
    87     def RefreshExecutionOrderSize(self):
       
    88         dc = wx.ClientDC(self.Parent)
       
    89         self.ExecutionOrderSize = dc.GetTextExtent(str(self.ExecutionOrder))
    85     
    90     
    86     # Refresh the block bounding box
    91     # Refresh the block bounding box
    87     def RefreshBoundingBox(self):
    92     def RefreshBoundingBox(self):
    88         # Calculate the size of the name outside the block
    93         # Calculate the size of the name outside the block
    89         text_width, text_height = self.NameSize
    94         text_width, text_height = self.NameSize
    94             bbx_y = self.Pos.y - (text_height + 2)
    99             bbx_y = self.Pos.y - (text_height + 2)
    95             bbx_height = self.Size[1] + (text_height + 2)
   100             bbx_height = self.Size[1] + (text_height + 2)
    96         else:
   101         else:
    97             bbx_y = self.Pos.y
   102             bbx_y = self.Pos.y
    98             bbx_height = self.Size[1]
   103             bbx_height = self.Size[1]
       
   104         if self.ExecutionOrder != 0:
       
   105             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
       
   106             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
       
   107             bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
    99         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   108         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   100     
   109     
   101     # Refresh the positions of the block connectors
   110     # Refresh the positions of the block connectors
   102     def RefreshConnectors(self):
   111     def RefreshConnectors(self):
   103         # Calculate the size for the connector lines
   112         # Calculate the size for the connector lines
   258     
   267     
   259     # Returs the extension name
   268     # Returs the extension name
   260     def GetExtension(self):
   269     def GetExtension(self):
   261         return self.Extension
   270         return self.Extension
   262     
   271     
       
   272     # Changes the execution order
       
   273     def SetExecutionOrder(self, executionOrder):
       
   274         self.ExecutionOrder = executionOrder
       
   275         self.RefreshExecutionOrderSize()
       
   276     
       
   277     # Returs the execution order
       
   278     def GetExecutionOrder(self):
       
   279         return self.ExecutionOrder
       
   280     
   263     # Refresh the block minimum size
   281     # Refresh the block minimum size
   264     def RefreshMinSize(self):
   282     def RefreshMinSize(self):
   265         # Calculate the inputs maximum width
   283         # Calculate the inputs maximum width
   266         max_input = 0
   284         max_input = 0
   267         for input in self.Inputs:
   285         for input in self.Inputs:
   333         # Draw inputs and outputs connectors
   351         # Draw inputs and outputs connectors
   334         for input in self.Inputs:
   352         for input in self.Inputs:
   335             input.Draw(dc)
   353             input.Draw(dc)
   336         for output in self.Outputs:
   354         for output in self.Outputs:
   337             output.Draw(dc)
   355             output.Draw(dc)
       
   356         if self.ExecutionOrder != 0:
       
   357             # Draw block execution order
       
   358             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0],
       
   359                     self.Pos.y + self.Size[1] + 2)
   338         Graphic_Element.Draw(self, dc)
   360         Graphic_Element.Draw(self, dc)
   339         dc.SetTextForeground(wx.BLACK)
   361         dc.SetTextForeground(wx.BLACK)
   340 
   362 
   341 
   363 
   342 #-------------------------------------------------------------------------------
   364 #-------------------------------------------------------------------------------
   348 """
   370 """
   349 
   371 
   350 class FBD_Variable(Graphic_Element):
   372 class FBD_Variable(Graphic_Element):
   351 
   373 
   352     # Create a new variable
   374     # Create a new variable
   353     def __init__(self, parent, type, name, value_type, id = None):
   375     def __init__(self, parent, type, name, value_type, id = None, executionOrder = 0):
   354         Graphic_Element.__init__(self, parent)
   376         Graphic_Element.__init__(self, parent)
   355         self.Type = None
   377         self.Type = None
   356         self.ValueType = None
   378         self.ValueType = None
   357         self.Name = name
       
   358         self.Id = id
   379         self.Id = id
       
   380         self.SetName(name)
       
   381         self.SetExecutionOrder(executionOrder)
   359         self.Input = None
   382         self.Input = None
   360         self.Output = None
   383         self.Output = None
   361         self.RefreshNameSize()
       
   362         self.SetType(type, value_type)
   384         self.SetType(type, value_type)
   363     
   385     
   364     # Make a clone of this FBD_Variable
   386     # Make a clone of this FBD_Variable
   365     def Clone(self, id = None, pos = None):
   387     def Clone(self, id = None, pos = None):
   366         variable = FBD_Variable(self.Parent, self.Type, self.Name, self.ValueType, id)
   388         variable = FBD_Variable(self.Parent, self.Type, self.Name, self.ValueType, id)
   389     
   411     
   390     # Refresh the size of text for name
   412     # Refresh the size of text for name
   391     def RefreshNameSize(self):
   413     def RefreshNameSize(self):
   392         dc = wx.ClientDC(self.Parent)
   414         dc = wx.ClientDC(self.Parent)
   393         self.NameSize = dc.GetTextExtent(self.Name)
   415         self.NameSize = dc.GetTextExtent(self.Name)
       
   416     
       
   417     # Refresh the size of text for execution order
       
   418     def RefreshExecutionOrderSize(self):
       
   419         dc = wx.ClientDC(self.Parent)
       
   420         self.ExecutionOrderSize = dc.GetTextExtent(str(self.ExecutionOrder))
   394     
   421     
   395     # Refresh the variable bounding box
   422     # Refresh the variable bounding box
   396     def RefreshBoundingBox(self):
   423     def RefreshBoundingBox(self):
   397         if self.Type in (OUTPUT, INOUT):
   424         if self.Type in (OUTPUT, INOUT):
   398             bbx_x = self.Pos.x - CONNECTOR_SIZE
   425             bbx_x = self.Pos.x - CONNECTOR_SIZE
   400             bbx_x = self.Pos.x
   427             bbx_x = self.Pos.x
   401         if self.Type == INOUT:
   428         if self.Type == INOUT:
   402             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   429             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   403         else:
   430         else:
   404             bbx_width = self.Size[0] + CONNECTOR_SIZE
   431             bbx_width = self.Size[0] + CONNECTOR_SIZE
   405         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1)
   432         bbx_height = self.Size[1]
       
   433         if self.ExecutionOrder != 0:
       
   434             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
       
   435             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
       
   436             bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2)
       
   437         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1)
   406     
   438     
   407     # Refresh the position of the variable connector
   439     # Refresh the position of the variable connector
   408     def RefreshConnectors(self):
   440     def RefreshConnectors(self):
   409         if self.Input:
   441         if self.Input:
   410             self.Input.SetPosition(wx.Point(0, self.Size[1] / 2))
   442             self.Input.SetPosition(wx.Point(0, self.Size[1] / 2))
   490     
   522     
   491     # Returns the variable name
   523     # Returns the variable name
   492     def GetName(self):
   524     def GetName(self):
   493         return self.Name
   525         return self.Name
   494     
   526     
       
   527     # Changes the execution order
       
   528     def SetExecutionOrder(self, executionOrder):
       
   529         self.ExecutionOrder = executionOrder
       
   530         self.RefreshExecutionOrderSize()
       
   531     
       
   532     # Returs the execution order
       
   533     def GetExecutionOrder(self):
       
   534         return self.ExecutionOrder
       
   535     
   495     # Returns the variable minimum size
   536     # Returns the variable minimum size
   496     def GetMinSize(self):
   537     def GetMinSize(self):
   497         return self.NameSize[0] + 10, self.NameSize[1] + 10
   538         return self.NameSize[0] + 10, self.NameSize[1] + 10
   498     
   539     
   499     # Method called when a LeftDClick event have been generated
   540     # Method called when a LeftDClick event have been generated
   526         # Draw connectors
   567         # Draw connectors
   527         if self.Input:
   568         if self.Input:
   528             self.Input.Draw(dc)
   569             self.Input.Draw(dc)
   529         if self.Output:    
   570         if self.Output:    
   530             self.Output.Draw(dc)
   571             self.Output.Draw(dc)
       
   572         if self.ExecutionOrder != 0:
       
   573             # Draw variable execution order
       
   574             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0],
       
   575                     self.Pos.y + self.Size[1] + 2)
   531         Graphic_Element.Draw(self, dc)
   576         Graphic_Element.Draw(self, dc)
   532 
   577 
   533 
   578 
   534 #-------------------------------------------------------------------------------
   579 #-------------------------------------------------------------------------------
   535 #                        Function Block Diagram Connector
   580 #                        Function Block Diagram Connector
   543 
   588 
   544     # Create a new connection
   589     # Create a new connection
   545     def __init__(self, parent, type, name, id = None):
   590     def __init__(self, parent, type, name, id = None):
   546         Graphic_Element.__init__(self, parent)
   591         Graphic_Element.__init__(self, parent)
   547         self.Type = type
   592         self.Type = type
   548         self.Name = name
       
   549         self.Id = id
   593         self.Id = id
       
   594         self.SetName(name)
   550         self.Pos = wx.Point(0, 0)
   595         self.Pos = wx.Point(0, 0)
   551         self.Size = wx.Size(0, 0)
   596         self.Size = wx.Size(0, 0)
   552         # Create an input or output connector according to connection type
   597         # Create an input or output connector according to connection type
   553         if self.Type == CONNECTOR:
   598         if self.Type == CONNECTOR:
   554             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   599             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   680                 self.Pos.x, self.Pos.y + self.Size[1])
   725                 self.Pos.x, self.Pos.y + self.Size[1])
   681         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   726         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   682                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   727                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   683         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   728         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   684                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   729                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   685         # Draw variable name
   730         # Draw connection name
   686         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   731         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   687                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   732                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   688         # Draw connector
   733         # Draw connector
   689         if self.Connector:
   734         if self.Connector:
   690             self.Connector.Draw(dc)
   735             self.Connector.Draw(dc)