graphics/FBD_Objects.py
changeset 64 dd6f693e46a1
parent 58 39cd981ff242
child 90 2245e8776086
equal deleted inserted replaced
63:04a02b4b2a57 64:dd6f693e46a1
    20 #
    20 #
    21 #You should have received a copy of the GNU General Public
    21 #You should have received a copy of the GNU General Public
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 from wxPython.wx import *
       
    26 import wx
    25 import wx
    27 
    26 
    28 from GraphicCommons import *
    27 from GraphicCommons import *
    29 from plcopen.structures import *
    28 from plcopen.structures import *
    30 
    29 
    67         for output in self.Outputs:
    66         for output in self.Outputs:
    68             output.UnConnect(delete = True)
    67             output.UnConnect(delete = True)
    69     
    68     
    70     # Refresh the size of text for name
    69     # Refresh the size of text for name
    71     def RefreshNameSize(self):
    70     def RefreshNameSize(self):
    72         dc = wxClientDC(self.Parent)
    71         dc = wx.ClientDC(self.Parent)
    73         self.NameSize = dc.GetTextExtent(self.Name)
    72         self.NameSize = dc.GetTextExtent(self.Name)
    74     
    73     
    75     # Refresh the block bounding box
    74     # Refresh the block bounding box
    76     def RefreshBoundingBox(self):
    75     def RefreshBoundingBox(self):
    77         # Calculate the size of the name outside the block
    76         # Calculate the size of the name outside the block
    83             bbx_y = self.Pos.y - (text_height + 2)
    82             bbx_y = self.Pos.y - (text_height + 2)
    84             bbx_height = self.Size[1] + (text_height + 2)
    83             bbx_height = self.Size[1] + (text_height + 2)
    85         else:
    84         else:
    86             bbx_y = self.Pos.y
    85             bbx_y = self.Pos.y
    87             bbx_height = self.Size[1]
    86             bbx_height = self.Size[1]
    88         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
    87         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
    89     
    88     
    90     # Refresh the positions of the block connectors
    89     # Refresh the positions of the block connectors
    91     def RefreshConnectors(self):
    90     def RefreshConnectors(self):
    92         # Calculate the size for the connector lines
    91         # Calculate the size for the connector lines
    93         lines = max(len(self.Inputs), len(self.Outputs))
    92         lines = max(len(self.Inputs), len(self.Outputs))
    94         linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE)
    93         linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE)
    95         # Update inputs positions
    94         # Update inputs positions
    96         position = BLOCK_LINE_SIZE + linesize / 2
    95         position = BLOCK_LINE_SIZE + linesize / 2
    97         for input in self.Inputs:
    96         for input in self.Inputs:
    98             input.SetPosition(wxPoint(0, position))
    97             input.SetPosition(wx.Point(0, position))
    99             position += linesize
    98             position += linesize
   100         # Update outputs positions
    99         # Update outputs positions
   101         position = BLOCK_LINE_SIZE + linesize / 2
   100         position = BLOCK_LINE_SIZE + linesize / 2
   102         for output in self.Outputs:
   101         for output in self.Outputs:
   103             output.SetPosition(wxPoint(self.Size[0], position))
   102             output.SetPosition(wx.Point(self.Size[0], position))
   104             position += linesize
   103             position += linesize
   105         self.RefreshConnected()
   104         self.RefreshConnected()
   106     
   105     
   107     # Refresh the positions of wires connected to inputs and outputs
   106     # Refresh the positions of wires connected to inputs and outputs
   108     def RefreshConnected(self, exclude = []):
   107     def RefreshConnected(self, exclude = []):
   156     # Changes the block type
   155     # Changes the block type
   157     def SetType(self, type, extension, inputs = None):
   156     def SetType(self, type, extension, inputs = None):
   158         if type != self.Type or self.Extension != extension: 
   157         if type != self.Type or self.Extension != extension: 
   159             if type != self.Type:
   158             if type != self.Type:
   160                 self.Type = type
   159                 self.Type = type
   161                 dc = wxClientDC(self.Parent)
   160                 dc = wx.ClientDC(self.Parent)
   162                 self.TypeSize = dc.GetTextExtent(self.Type)
   161                 self.TypeSize = dc.GetTextExtent(self.Type)
   163             self.Extension = extension
   162             self.Extension = extension
   164             # Find the block definition from type given and create the corresponding
   163             # Find the block definition from type given and create the corresponding
   165             # inputs and outputs
   164             # inputs and outputs
   166             blocktype = GetBlockType(type, inputs)
   165             blocktype = GetBlockType(type, inputs)
   176                 raise ValueError, "This block type isn't defined"
   175                 raise ValueError, "This block type isn't defined"
   177             self.Clean()
   176             self.Clean()
   178             # Extract the inputs properties and create the corresponding connector
   177             # Extract the inputs properties and create the corresponding connector
   179             self.Inputs = []
   178             self.Inputs = []
   180             for input_name, input_type, input_modifier in inputs:
   179             for input_name, input_type, input_modifier in inputs:
   181                 connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST, onlyone = True)
   180                 connector = Connector(self, input_name, input_type, wx.Point(0, 0), WEST, onlyone = True)
   182                 if input_modifier == "negated":
   181                 if input_modifier == "negated":
   183                     connector.SetNegated(True)
   182                     connector.SetNegated(True)
   184                 elif input_modifier != "none":
   183                 elif input_modifier != "none":
   185                     connector.SetEdge(input_modifier)
   184                     connector.SetEdge(input_modifier)
   186                 self.Inputs.append(connector)
   185                 self.Inputs.append(connector)
   187             # Extract the outputs properties and create the corresponding connector
   186             # Extract the outputs properties and create the corresponding connector
   188             self.Outputs = []
   187             self.Outputs = []
   189             for output_name, output_type, output_modifier in outputs:
   188             for output_name, output_type, output_modifier in outputs:
   190                 connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST)
   189                 connector = Connector(self, output_name, output_type, wx.Point(0, 0), EAST)
   191                 if output_modifier == "negated":
   190                 if output_modifier == "negated":
   192                     connector.SetNegated(True)
   191                     connector.SetNegated(True)
   193                 elif output_modifier != "none":
   192                 elif output_modifier != "none":
   194                     connector.SetEdge(output_modifier)
   193                     connector.SetEdge(output_modifier)
   195                 self.Outputs.append(connector)
   194                 self.Outputs.append(connector)
   276             for output in self.Outputs:
   275             for output in self.Outputs:
   277                 output.RefreshWires()
   276                 output.RefreshWires()
   278     
   277     
   279     # Draws block
   278     # Draws block
   280     def Draw(self, dc):
   279     def Draw(self, dc):
   281         dc.SetPen(wxBLACK_PEN)
   280         dc.SetPen(wx.BLACK_PEN)
   282         dc.SetBrush(wxWHITE_BRUSH)
   281         dc.SetBrush(wx.WHITE_BRUSH)
   283         # Draw a rectangle with the block size
   282         # Draw a rectangle with the block size
   284         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   283         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   285         # Draw block name and block type
   284         # Draw block name and block type
   286         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   285         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   287                 self.Pos.y - (self.NameSize[1] + 2))
   286                 self.Pos.y - (self.NameSize[1] + 2))
   333     def Delete(self):
   332     def Delete(self):
   334         self.Parent.DeleteVariable(self)
   333         self.Parent.DeleteVariable(self)
   335     
   334     
   336     # Refresh the size of text for name
   335     # Refresh the size of text for name
   337     def RefreshNameSize(self):
   336     def RefreshNameSize(self):
   338         dc = wxClientDC(self.Parent)
   337         dc = wx.ClientDC(self.Parent)
   339         self.NameSize = dc.GetTextExtent(self.Name)
   338         self.NameSize = dc.GetTextExtent(self.Name)
   340     
   339     
   341     # Refresh the variable bounding box
   340     # Refresh the variable bounding box
   342     def RefreshBoundingBox(self):
   341     def RefreshBoundingBox(self):
   343         if self.Type in (OUTPUT, INOUT):
   342         if self.Type in (OUTPUT, INOUT):
   346             bbx_x = self.Pos.x
   345             bbx_x = self.Pos.x
   347         if self.Type == INOUT:
   346         if self.Type == INOUT:
   348             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   347             bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE
   349         else:
   348         else:
   350             bbx_width = self.Size[0] + CONNECTOR_SIZE
   349             bbx_width = self.Size[0] + CONNECTOR_SIZE
   351         self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1)
   350         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1)
   352     
   351     
   353     # Refresh the position of the variable connector
   352     # Refresh the position of the variable connector
   354     def RefreshConnectors(self):
   353     def RefreshConnectors(self):
   355         if self.Input:
   354         if self.Input:
   356             self.Input.SetPosition(wxPoint(0, self.Size[1] / 2))
   355             self.Input.SetPosition(wx.Point(0, self.Size[1] / 2))
   357         if self.Output:
   356         if self.Output:
   358             self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2))
   357             self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2))
   359         self.RefreshConnected()
   358         self.RefreshConnected()
   360     
   359     
   361     # Refresh the position of wires connected to connector
   360     # Refresh the position of wires connected to connector
   362     def RefreshConnected(self, exclude = []):
   361     def RefreshConnected(self, exclude = []):
   363         if self.Input:
   362         if self.Input:
   412             self.Clean()
   411             self.Clean()
   413             self.Input = None
   412             self.Input = None
   414             self.Output = None
   413             self.Output = None
   415             # Create an input or output connector according to variable type
   414             # Create an input or output connector according to variable type
   416             if self.Type != INPUT:
   415             if self.Type != INPUT:
   417                 self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST, onlyone = True)
   416                 self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True)
   418             if self.Type != OUTPUT:
   417             if self.Type != OUTPUT:
   419                 self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST)
   418                 self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST)
   420             self.RefreshConnectors()
   419             self.RefreshConnectors()
   421         elif value_type != self.ValueType:
   420         elif value_type != self.ValueType:
   422             if self.Input:
   421             if self.Input:
   423                 self.Input.SetType(value_type)
   422                 self.Input.SetType(value_type)
   424             if self.Output:
   423             if self.Output:
   467             if self.Output:
   466             if self.Output:
   468                 self.Output.RefreshWires()
   467                 self.Output.RefreshWires()
   469     
   468     
   470     # Draws variable
   469     # Draws variable
   471     def Draw(self, dc):
   470     def Draw(self, dc):
   472         dc.SetPen(wxBLACK_PEN)
   471         dc.SetPen(wx.BLACK_PEN)
   473         dc.SetBrush(wxWHITE_BRUSH)
   472         dc.SetBrush(wx.WHITE_BRUSH)
   474         # Draw a rectangle with the variable size
   473         # Draw a rectangle with the variable size
   475         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   474         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   476         # Draw variable name
   475         # Draw variable name
   477         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   476         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   478                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   477                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   498     def __init__(self, parent, type, name, id = None):
   497     def __init__(self, parent, type, name, id = None):
   499         Graphic_Element.__init__(self, parent)
   498         Graphic_Element.__init__(self, parent)
   500         self.Type = type
   499         self.Type = type
   501         self.Name = name
   500         self.Name = name
   502         self.Id = id
   501         self.Id = id
   503         self.Pos = wxPoint(0, 0)
   502         self.Pos = wx.Point(0, 0)
   504         self.Size = wxSize(0, 0)
   503         self.Size = wx.Size(0, 0)
   505         # Create an input or output connector according to connection type
   504         # Create an input or output connector according to connection type
   506         if self.Type == CONNECTOR:
   505         if self.Type == CONNECTOR:
   507             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True)
   506             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   508         else:
   507         else:
   509             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   508             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
   510         self.RefreshConnectors()
   509         self.RefreshConnectors()
   511         self.RefreshNameSize()
   510         self.RefreshNameSize()
   512     
   511     
   513     # Destructor
   512     # Destructor
   514     def __del__(self):
   513     def __del__(self):
   523     def Delete(self):
   522     def Delete(self):
   524         self.Parent.DeleteConnection(self)
   523         self.Parent.DeleteConnection(self)
   525     
   524     
   526     # Refresh the size of text for name
   525     # Refresh the size of text for name
   527     def RefreshNameSize(self):
   526     def RefreshNameSize(self):
   528         dc = wxClientDC(self.Parent)
   527         dc = wx.ClientDC(self.Parent)
   529         self.NameSize = dc.GetTextExtent(self.Name)
   528         self.NameSize = dc.GetTextExtent(self.Name)
   530     
   529     
   531     # Refresh the connection bounding box
   530     # Refresh the connection bounding box
   532     def RefreshBoundingBox(self):
   531     def RefreshBoundingBox(self):
   533         if self.Type == CONNECTOR:
   532         if self.Type == CONNECTOR:
   534             bbx_x = self.Pos.x - CONNECTOR_SIZE
   533             bbx_x = self.Pos.x - CONNECTOR_SIZE
   535         else:
   534         else:
   536             bbx_x = self.Pos.x
   535             bbx_x = self.Pos.x
   537         bbx_width = self.Size[0] + CONNECTOR_SIZE
   536         bbx_width = self.Size[0] + CONNECTOR_SIZE
   538         self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width, self.Size[1])
   537         self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1])
   539     
   538     
   540     # Refresh the position of the connection connector
   539     # Refresh the position of the connection connector
   541     def RefreshConnectors(self):
   540     def RefreshConnectors(self):
   542         if self.Type == CONNECTOR:
   541         if self.Type == CONNECTOR:
   543             self.Connector.SetPosition(wxPoint(0, self.Size[1] / 2))
   542             self.Connector.SetPosition(wx.Point(0, self.Size[1] / 2))
   544         else:
   543         else:
   545             self.Connector.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2))
   544             self.Connector.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2))
   546         self.RefreshConnected()
   545         self.RefreshConnected()
   547     
   546     
   548     # Refresh the position of wires connected to connector
   547     # Refresh the position of wires connected to connector
   549     def RefreshConnected(self, exclude = []):
   548     def RefreshConnected(self, exclude = []):
   550         if self.Connector:
   549         if self.Connector:
   565         if type != self.Type:
   564         if type != self.Type:
   566             self.Type = type
   565             self.Type = type
   567             self.Clean()
   566             self.Clean()
   568             # Create an input or output connector according to connection type
   567             # Create an input or output connector according to connection type
   569             if self.Type == CONNECTOR:
   568             if self.Type == CONNECTOR:
   570                 self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True)
   569                 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   571             else:
   570             else:
   572                 self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   571                 self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
   573             self.RefreshConnectors()
   572             self.RefreshConnectors()
   574     
   573     
   575     # Returns the connection type
   574     # Returns the connection type
   576     def GetType(self):
   575     def GetType(self):
   577         return self.Type
   576         return self.Type
   611             if self.Connector:
   610             if self.Connector:
   612                 self.Connector.RefreshWires()
   611                 self.Connector.RefreshWires()
   613     
   612     
   614     # Draws connection
   613     # Draws connection
   615     def Draw(self, dc):
   614     def Draw(self, dc):
   616         dc.SetPen(wxBLACK_PEN)
   615         dc.SetPen(wx.BLACK_PEN)
   617         dc.SetBrush(wxWHITE_BRUSH)
   616         dc.SetBrush(wx.WHITE_BRUSH)
   618         # Draw a rectangle with the connection size with arrows in 
   617         # Draw a rectangle with the connection size with arrows in 
   619         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   618         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   620         arrowsize = min(self.Size[1] / 2, (self.Size[0] - self.NameSize[0] - 10) / 2)
   619         arrowsize = min(self.Size[1] / 2, (self.Size[0] - self.NameSize[0] - 10) / 2)
   621         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, 
   620         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, 
   622                 self.Pos.y + self.Size[1] / 2)
   621                 self.Pos.y + self.Size[1] / 2)