graphics/FBD_Objects.py
changeset 2459 21164625b393
parent 2437 105c20fdeb19
child 3303 0ffb41625592
equal deleted inserted replaced
2458:2a70d5240300 2459:21164625b393
    22 # along with this program; if not, write to the Free Software
    22 # along with this program; if not, write to the Free Software
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    24 
    24 
    25 
    25 
    26 from __future__ import absolute_import
    26 from __future__ import absolute_import
       
    27 from __future__ import division
    27 import wx
    28 import wx
       
    29 from six.moves import xrange
    28 
    30 
    29 from graphics.GraphicCommons import *
    31 from graphics.GraphicCommons import *
    30 from plcopen.structures import *
    32 from plcopen.structures import *
    31 
    33 
    32 
    34 
   128 
   130 
   129     # Returns the bounding box of the name outside the block
   131     # Returns the bounding box of the name outside the block
   130     def GetTextBoundingBox(self):
   132     def GetTextBoundingBox(self):
   131         # Calculate the size of the name outside the block
   133         # Calculate the size of the name outside the block
   132         text_width, text_height = self.NameSize
   134         text_width, text_height = self.NameSize
   133         return wx.Rect(self.Pos.x + (self.Size[0] - text_width) / 2,
   135         return wx.Rect(self.Pos.x + (self.Size[0] - text_width) // 2,
   134                        self.Pos.y - (text_height + 2),
   136                        self.Pos.y - (text_height + 2),
   135                        text_width,
   137                        text_width,
   136                        text_height)
   138                        text_height)
   137 
   139 
   138     # Returns the bounding box of function block without name outside
   140     # Returns the bounding box of function block without name outside
   158     def RefreshConnectors(self):
   160     def RefreshConnectors(self):
   159         scaling = self.Parent.GetScaling()
   161         scaling = self.Parent.GetScaling()
   160         # Calculate the size for the connector lines
   162         # Calculate the size for the connector lines
   161         lines = max(len(self.Inputs), len(self.Outputs))
   163         lines = max(len(self.Inputs), len(self.Outputs))
   162         if lines > 0:
   164         if lines > 0:
   163             linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE)
   165             linesize = max((self.Size[1] - BLOCK_LINE_SIZE) // lines, BLOCK_LINE_SIZE)
   164             # Update inputs and outputs positions
   166             # Update inputs and outputs positions
   165             position = BLOCK_LINE_SIZE + linesize / 2
   167             position = BLOCK_LINE_SIZE + linesize // 2
   166             for i in xrange(lines):
   168             for i in xrange(lines):
   167                 if scaling is not None:
   169                 if scaling is not None:
   168                     ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
   170                     ypos = round_scaling(self.Pos.y + position, scaling[1]) - self.Pos.y
   169                 else:
   171                 else:
   170                     ypos = position
   172                     ypos = position
   476             executionorder_size = self.ExecutionOrderSize
   478             executionorder_size = self.ExecutionOrderSize
   477 
   479 
   478         # Draw a rectangle with the block size
   480         # Draw a rectangle with the block size
   479         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   481         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   480         # Draw block name and block type
   482         # Draw block name and block type
   481         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
   483         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
   482                     self.Pos.y - (name_size[1] + 2))
   484                     self.Pos.y - (name_size[1] + 2))
   483         type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2,
   485         type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) // 2,
   484                     self.Pos.y + 5)
   486                     self.Pos.y + 5)
   485         dc.DrawText(self.Name, name_pos[0], name_pos[1])
   487         dc.DrawText(self.Name, name_pos[0], name_pos[1])
   486         dc.DrawText(self.Type, type_pos[0], type_pos[1])
   488         dc.DrawText(self.Type, type_pos[0], type_pos[1])
   487         # Draw inputs and outputs connectors
   489         # Draw inputs and outputs connectors
   488         for input in self.Inputs:
   490         for input in self.Inputs:
   589             bbx_x = self.Pos.x
   591             bbx_x = self.Pos.x
   590         if self.Type == INOUT:
   592         if self.Type == INOUT:
   591             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   593             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   592         else:
   594         else:
   593             bbx_width = self.Size[0] + CONNECTOR_SIZE
   595             bbx_width = self.Size[0] + CONNECTOR_SIZE
   594         bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2)
   596         bbx_x = min(bbx_x, self.Pos.x + (self.Size[0] - self.NameSize[0]) // 2)
   595         bbx_width = max(bbx_width, self.NameSize[0])
   597         bbx_width = max(bbx_width, self.NameSize[0])
   596         bbx_height = self.Size[1]
   598         bbx_height = self.Size[1]
   597         if self.ExecutionOrder != 0:
   599         if self.ExecutionOrder != 0:
   598             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
   600             bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0])
   599             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
   601             bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0])
   602 
   604 
   603     # Refresh the position of the variable connector
   605     # Refresh the position of the variable connector
   604     def RefreshConnectors(self):
   606     def RefreshConnectors(self):
   605         scaling = self.Parent.GetScaling()
   607         scaling = self.Parent.GetScaling()
   606         if scaling is not None:
   608         if scaling is not None:
   607             position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
   609             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
   608         else:
   610         else:
   609             position = self.Size[1] / 2
   611             position = self.Size[1] // 2
   610         if self.Input:
   612         if self.Input:
   611             self.Input.SetPosition(wx.Point(0, position))
   613             self.Input.SetPosition(wx.Point(0, position))
   612         if self.Output:
   614         if self.Output:
   613             self.Output.SetPosition(wx.Point(self.Size[0], position))
   615             self.Output.SetPosition(wx.Point(self.Size[0], position))
   614         self.RefreshConnected()
   616         self.RefreshConnected()
   775             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
   777             executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder))
   776         else:
   778         else:
   777             name_size = self.NameSize
   779             name_size = self.NameSize
   778             executionorder_size = self.ExecutionOrderSize
   780             executionorder_size = self.ExecutionOrderSize
   779 
   781 
   780         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
   782         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
   781                     self.Pos.y + (self.Size[1] - name_size[1]) / 2)
   783                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
   782         # Draw a rectangle with the variable size
   784         # Draw a rectangle with the variable size
   783         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   785         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   784         # Draw variable name
   786         # Draw variable name
   785         dc.DrawText(self.Name, text_pos[0], text_pos[1])
   787         dc.DrawText(self.Name, text_pos[0], text_pos[1])
   786         # Draw connectors
   788         # Draw connectors
   874 
   876 
   875     # Refresh the position of the connection connector
   877     # Refresh the position of the connection connector
   876     def RefreshConnectors(self):
   878     def RefreshConnectors(self):
   877         scaling = self.Parent.GetScaling()
   879         scaling = self.Parent.GetScaling()
   878         if scaling is not None:
   880         if scaling is not None:
   879             position = round_scaling(self.Pos.y + self.Size[1] / 2, scaling[1]) - self.Pos.y
   881             position = round_scaling(self.Pos.y + self.Size[1] // 2, scaling[1]) - self.Pos.y
   880         else:
   882         else:
   881             position = self.Size[1] / 2
   883             position = self.Size[1] // 2
   882         if self.Type == CONNECTOR:
   884         if self.Type == CONNECTOR:
   883             self.Connector.SetPosition(wx.Point(0, position))
   885             self.Connector.SetPosition(wx.Point(0, position))
   884         else:
   886         else:
   885             self.Connector.SetPosition(wx.Point(self.Size[0], position))
   887             self.Connector.SetPosition(wx.Point(self.Size[0], position))
   886         self.RefreshConnected()
   888         self.RefreshConnected()
  1015         else:
  1017         else:
  1016             name_size = self.NameSize
  1018             name_size = self.NameSize
  1017 
  1019 
  1018         # Draw a rectangle with the connection size with arrows inside
  1020         # Draw a rectangle with the connection size with arrows inside
  1019         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
  1021         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
  1020         arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2)
  1022         arrowsize = min(self.Size[1] // 2, (self.Size[0] - name_size[0] - 10) // 2)
  1021         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
  1023         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize,
  1022                     self.Pos.y + self.Size[1] / 2)
  1024                     self.Pos.y + self.Size[1] // 2)
  1023         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2,
  1025         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] // 2,
  1024                     self.Pos.x, self.Pos.y + self.Size[1])
  1026                     self.Pos.x, self.Pos.y + self.Size[1])
  1025         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
  1027         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y,
  1026                     self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
  1028                     self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2)
  1027         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2,
  1029         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] // 2,
  1028                     self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
  1030                     self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
  1029         # Draw connection name
  1031         # Draw connection name
  1030         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
  1032         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) // 2,
  1031                     self.Pos.y + (self.Size[1] - name_size[1]) / 2)
  1033                     self.Pos.y + (self.Size[1] - name_size[1]) // 2)
  1032         dc.DrawText(self.Name, text_pos[0], text_pos[1])
  1034         dc.DrawText(self.Name, text_pos[0], text_pos[1])
  1033         # Draw connector
  1035         # Draw connector
  1034         if self.Connector:
  1036         if self.Connector:
  1035             self.Connector.Draw(dc)
  1037             self.Connector.Draw(dc)
  1036 
  1038