graphics/SFC_Objects.py
changeset 0 b622defdfd98
child 1 e9d01d824086
equal deleted inserted replaced
-1:000000000000 0:b622defdfd98
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C): Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU Lesser General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #Lesser General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU Lesser General Public
       
    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
       
    24 
       
    25 from wxPython.wx import *
       
    26 import wx
       
    27 
       
    28 from GraphicCommons import *
       
    29 from plcopen.structures import *
       
    30 
       
    31 def GetWireSize(block):
       
    32     if isinstance(block, SFC_Step):
       
    33         return SFC_WIRE_MIN_SIZE + block.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1]
       
    34     else:
       
    35         return SFC_WIRE_MIN_SIZE
       
    36 
       
    37 #-------------------------------------------------------------------------------
       
    38 #                         Sequencial Function Chart Step
       
    39 #-------------------------------------------------------------------------------
       
    40 
       
    41 """
       
    42 Class that implements the graphic representation of a step
       
    43 """
       
    44 
       
    45 class SFC_Step(Graphic_Element):
       
    46     
       
    47     # Create a new step
       
    48     def __init__(self, parent, name, initial = False, id = None):
       
    49         Graphic_Element.__init__(self, parent)
       
    50         self.Name = name
       
    51         self.Initial = initial
       
    52         self.Id = id
       
    53         self.Size = wxSize(SFC_STEP_DEFAULT_SIZE[0], SFC_STEP_DEFAULT_SIZE[1])
       
    54         # Create an input and output connector
       
    55         if not self.Initial:
       
    56             self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
       
    57         else:
       
    58             self.Input = None
       
    59         self.Output = None
       
    60         self.Action = None
       
    61     
       
    62     # Destructor
       
    63     def __del__(self):
       
    64         self.Input = None
       
    65         self.Output = None
       
    66         self.Action = None
       
    67     
       
    68     # Delete this step by calling the appropriate method
       
    69     def Delete(self):
       
    70         self.Parent.DeleteStep(self)
       
    71     
       
    72     # Unconnect input and output
       
    73     def Clean(self):
       
    74         if self.Input:
       
    75             self.Input.UnConnect()
       
    76         if self.Output:
       
    77             self.Output.UnConnect()
       
    78         if self.Action:
       
    79             self.Action.UnConnect()
       
    80     
       
    81     # Add output connector to step
       
    82     def AddOutput(self):
       
    83         if not self.Output:
       
    84             self.Output = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)
       
    85             self.RefreshBoundingBox()
       
    86     
       
    87     # Remove output connector from step
       
    88     def RemoveOutput(self):
       
    89         if self.Output:
       
    90             self.Output.UnConnect()
       
    91             self.Output = None
       
    92             self.RefreshBoundingBox()
       
    93     
       
    94     # Add action connector to step
       
    95     def AddAction(self):
       
    96         if not self.Action:
       
    97             self.Action = Connector(self, "", "ANY", wxPoint(self.Size[0], self.Size[1] / 2), EAST)
       
    98             self.RefreshBoundingBox()
       
    99     
       
   100     # Remove action connector from step
       
   101     def RemoveAction(self):
       
   102         if self.Action:
       
   103             self.Action.UnConnect()
       
   104             self.Action = None
       
   105             self.RefreshBoundingBox()
       
   106     
       
   107     # Refresh the step bounding box
       
   108     def RefreshBoundingBox(self):
       
   109         dc = wxClientDC(self.Parent)
       
   110         # Calculate the bounding box size
       
   111         if self.Action:
       
   112             bbx_width = self.Size[0] + CONNECTOR_SIZE
       
   113         else:
       
   114             bbx_width = self.Size[0]
       
   115         if self.Initial:
       
   116             bbx_y = self.Pos.y
       
   117             bbx_height = self.Size[1]
       
   118             if self.Output:
       
   119                 bbx_height += CONNECTOR_SIZE
       
   120         else:
       
   121             bbx_y = self.Pos.y - CONNECTOR_SIZE
       
   122             bbx_height = self.Size[1] + CONNECTOR_SIZE
       
   123             if self.Output:
       
   124                 bbx_height += CONNECTOR_SIZE
       
   125         #self.BoundingBox = wxRect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height + 1)
       
   126         self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
       
   127         
       
   128     # Refresh the positions of the step connectors
       
   129     def RefreshConnectors(self):
       
   130         # Update input position if it exists
       
   131         if self.Input:
       
   132             self.Input.SetPosition(wxPoint(self.Size[0] / 2, 0))
       
   133         # Update output position
       
   134         if self.Output:
       
   135             self.Output.SetPosition(wxPoint(self.Size[0] / 2, self.Size[1]))
       
   136         # Update action position if it exists
       
   137         if self.Action:
       
   138             self.Action.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2))
       
   139         self.RefreshConnected()
       
   140     
       
   141     # Refresh the position of wires connected to step
       
   142     def RefreshConnected(self, exclude = []):
       
   143         if self.Input:
       
   144             self.Input.MoveConnected(exclude)
       
   145         if self.Output:
       
   146             self.Output.MoveConnected(exclude)
       
   147         if self.Action:
       
   148             self.Action.MoveConnected(exclude)
       
   149     
       
   150     # Returns the step connector that starts with the point given if it exists 
       
   151     def GetConnector(self, position):
       
   152         # Test input connector if it exists
       
   153         if self.Input:
       
   154             input_pos = self.Input.GetRelPosition()
       
   155             if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
       
   156                 return self.Input
       
   157         # Test output connector if it exists
       
   158         if self.Output:
       
   159             output_pos = self.Output.GetRelPosition()
       
   160             if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
       
   161                 return self.Output
       
   162         # Test action connector if it exists
       
   163         if self.Action:
       
   164             action_pos = self.Action.GetRelPosition()
       
   165             if position.x == self.Pos.x + action_pos.x and position.y == self.Pos.y + action_pos.y:
       
   166                 return self.Action
       
   167         return None
       
   168     
       
   169     # Returns input and output step connectors 
       
   170     def GetConnectors(self):
       
   171         return {"input":self.Input,"output":self.Output,"action":self.Action}
       
   172     
       
   173     # Test if point given is on step input or output connector
       
   174     def TestConnector(self, pt, exclude=True):
       
   175         # Test input connector if it exists
       
   176         if self.Input and self.Input.TestPoint(pt, exclude):
       
   177             return self.Input
       
   178         # Test output connector
       
   179         if self.Output and self.Output.TestPoint(pt, exclude):
       
   180             return self.Output
       
   181         return None
       
   182 
       
   183     # Changes the step name
       
   184     def SetName(self, name):
       
   185         self.Name = name
       
   186 
       
   187     # Returns the step name
       
   188     def GetName(self):
       
   189         return self.Name
       
   190 
       
   191     # Returns the step initial property
       
   192     def GetInitial(self):
       
   193         return self.Initial
       
   194     
       
   195     # Returns the connector connected to input
       
   196     def GetPreviousConnector(self):
       
   197         if self.Input:
       
   198             wires = self.Input.GetWires()
       
   199             if len(wires) == 1:
       
   200                 return wires[0][0].EndConnected
       
   201         return None
       
   202     
       
   203     # Returns the connector connected to output
       
   204     def GetNextConnector(self):
       
   205         if self.Output:
       
   206             wires = self.Output.GetWires()
       
   207             if len(wires) == 1:
       
   208                 return wires[0][0].StartConnected
       
   209         return None
       
   210     
       
   211     # Returns the connector connected to action
       
   212     def GetActionConnector(self):
       
   213         if self.Action:
       
   214             wires = self.Action.GetWires()
       
   215             if len(wires) == 1:
       
   216                 return wires[0][0].StartConnected
       
   217         return None
       
   218     
       
   219     # Returns the number of action line
       
   220     def GetActionExtraLineNumber(self):
       
   221         if self.Action:
       
   222             wires = self.Action.GetWires()
       
   223             if len(wires) != 1:
       
   224                 return 0
       
   225             action_block = wires[0][0].StartConnected.GetParentBlock()
       
   226             return max(0, action_block.GetLineNumber() - 1)
       
   227         return 0
       
   228     
       
   229     # Returns the step minimum size
       
   230     def GetMinSize(self):
       
   231         dc = wxClientDC(self.Parent)
       
   232         text_width, text_height = dc.GetTextExtent(self.Name)
       
   233         if self.Initial:
       
   234             return text_width + 14, text_height + 14
       
   235         else:
       
   236             return text_width + 10, text_height + 10
       
   237     
       
   238     # Updates the step size
       
   239     def UpdateSize(self, width, height):
       
   240         diffx = self.Size.GetWidth() / 2 - width / 2
       
   241         diffy = height - self.Size.GetHeight()
       
   242         self.Move(diffx, 0)
       
   243         Graphic_Element.SetSize(self, width, height)
       
   244         self.RefreshOutputPosition((0, diffy))
       
   245     
       
   246     # Align input element with this step
       
   247     def RefreshInputPosition(self):
       
   248         if self.Input:
       
   249             current_pos = self.Input.GetPosition(False)
       
   250             input = self.GetPreviousConnector()
       
   251             if input:
       
   252                 input_pos = input.GetPosition(False)
       
   253                 diffx = current_pos.x - input_pos.x
       
   254                 input_block = input.GetParentBlock()
       
   255                 if isinstance(input_block, SFC_Divergence):
       
   256                     input_block.MoveConnector(input, diffx)
       
   257                 else:
       
   258                     if isinstance(input_block, SFC_Step):
       
   259                         input_block.MoveActionBlock((diffx, 0))
       
   260                     input_block.Move(diffx, 0)
       
   261                     input_block.RefreshInputPosition()
       
   262     
       
   263     # Align output element with this step
       
   264     def RefreshOutputPosition(self, move = None):
       
   265         if self.Output:
       
   266             wires = self.Output.GetWires()
       
   267             if len(wires) != 1:
       
   268                 return
       
   269             current_pos = self.Output.GetPosition(False)
       
   270             output = wires[0][0].StartConnected
       
   271             output_pos = output.GetPosition(False)
       
   272             diffx = current_pos.x - output_pos.x
       
   273             output_block = output.GetParentBlock()
       
   274             wire_size = SFC_WIRE_MIN_SIZE + self.GetActionExtraLineNumber() * SFC_ACTION_MIN_SIZE[1]
       
   275             diffy = wire_size - output_pos.y + current_pos.y
       
   276             if diffy != 0:
       
   277                 if isinstance(output_block, SFC_Step):
       
   278                     output_block.MoveActionBlock((diffx, diffy))
       
   279                 wires[0][0].SetPoints([wxPoint(current_pos.x, current_pos.y + wire_size),
       
   280                     wxPoint(current_pos.x, current_pos.y)])
       
   281                 if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
       
   282                     output_block.Move(diffx, diffy, self.Parent.Wires)
       
   283                     output_block.RefreshOutputPosition((diffx, diffy))
       
   284                 else:
       
   285                     output_block.RefreshPosition()
       
   286             elif move:
       
   287                 if isinstance(output_block, SFC_Step):
       
   288                     output_block.MoveActionBlock(move)
       
   289                 wires[0][0].Move(move[0], move[1], True)
       
   290                 if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
       
   291                     output_block.Move(move[0], move[1], self.Parent.Wires)
       
   292                     output_block.RefreshOutputPosition(move)
       
   293                 else:
       
   294                     output_block.RefreshPosition()
       
   295             elif isinstance(output_block, SFC_Divergence):
       
   296                 output_block.MoveConnector(output, diffx)
       
   297             else:
       
   298                 if isinstance(output_block, SFC_Step):
       
   299                     output_block.MoveActionBlock((diffx, 0))
       
   300                 output_block.Move(diffx, 0)
       
   301                 output_block.RefreshOutputPosition()
       
   302     
       
   303     # Refresh action element with this step
       
   304     def MoveActionBlock(self, move):
       
   305         if self.Action:
       
   306             wires = self.Action.GetWires()
       
   307             if len(wires) != 1:
       
   308                 return
       
   309             action_block = wires[0][0].StartConnected.GetParentBlock()
       
   310             action_block.Move(move[0], move[1], self.Parent.Wires)
       
   311             wires[0][0].Move(move[0], move[1], True)
       
   312     
       
   313     # Resize the divergence from position and size given
       
   314     def Resize(self, x, y, width, height):
       
   315         self.UpdateSize(width, height)
       
   316     
       
   317     # Method called when a LeftDClick event have been generated
       
   318     def OnLeftDClick(self, event, scaling):
       
   319         # Edit the step properties
       
   320         self.Parent.EditStepContent(self)
       
   321     
       
   322     # Method called when a RightUp event have been generated
       
   323     def OnRightUp(self, event, scaling):
       
   324         # Popup the menu with special items for a step
       
   325         self.Parent.PopupDefaultMenu()
       
   326     
       
   327     # Refreshes the step state according to move defined and handle selected
       
   328     def ProcessDragging(self, movex, movey):
       
   329         handle_type, handle = self.Handle
       
   330         if handle_type == HANDLE_MOVE:
       
   331             action_block = None
       
   332             if self.Initial:
       
   333                 self.MoveActionBlock((movex, movey))
       
   334                 self.Move(movex, movey, self.Parent.Wires)
       
   335                 self.RefreshOutputPosition((movex, movey))
       
   336             else:
       
   337                 self.MoveActionBlock((movex, 0))
       
   338                 self.Move(movex, 0)
       
   339                 self.RefreshInputPosition()
       
   340                 self.RefreshOutputPosition()
       
   341         else:
       
   342             Graphic_Element.ProcessDragging(self, movex, movey)
       
   343     
       
   344     # Refresh input element model
       
   345     def RefreshInputModel(self):
       
   346         if self.Input:
       
   347             input = self.GetPreviousConnector()
       
   348             if input:                
       
   349                 input_block = input.GetParentBlock()
       
   350                 input_block.RefreshModel(False)
       
   351                 if not isinstance(input_block, SFC_Divergence):
       
   352                     input_block.RefreshInputModel()
       
   353     
       
   354     # Refresh output element model
       
   355     def RefreshOutputModel(self, move=False):
       
   356         if self.Output:
       
   357             output = self.GetNextConnector()
       
   358             if output:
       
   359                 output_block = output.GetParentBlock()
       
   360                 output_block.RefreshModel(False)
       
   361                 if not isinstance(output_block, SFC_Divergence) or move:
       
   362                     output_block.RefreshOutputModel(move)
       
   363     
       
   364     # Refreshes the step model
       
   365     def RefreshModel(self, move=True):
       
   366         self.Parent.RefreshStepModel(self)
       
   367         if self.Action:
       
   368             action = self.GetActionConnector()
       
   369             if action:
       
   370                 action_block = action.GetParentBlock()
       
   371                 action_block.RefreshModel(False)
       
   372         # If step has moved, refresh the model of wires connected to output
       
   373         if move:
       
   374             self.RefreshInputModel()
       
   375             self.RefreshOutputModel(self.Initial)
       
   376     
       
   377     # Draws step
       
   378     def Draw(self, dc):
       
   379         dc.SetPen(wxBLACK_PEN)
       
   380         dc.SetBrush(wxWHITE_BRUSH)
       
   381         # Draw two rectangles for representing the step
       
   382         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
       
   383         if self.Initial:
       
   384             dc.DrawRectangle(self.Pos.x + 2, self.Pos.y + 2, self.Size[0] - 3, self.Size[1] - 3)
       
   385         # Draw step name
       
   386         namewidth, nameheight = dc.GetTextExtent(self.Name)
       
   387         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
       
   388                     self.Pos.y + (self.Size[1] - nameheight) / 2)
       
   389         # Draw input and output connectors
       
   390         if self.Input:
       
   391             self.Input.Draw(dc)
       
   392         if self.Output:
       
   393             self.Output.Draw(dc)
       
   394         if self.Action:
       
   395             self.Action.Draw(dc)
       
   396         Graphic_Element.Draw(self, dc)
       
   397 
       
   398 #-------------------------------------------------------------------------------
       
   399 #                       Sequencial Function Chart Transition
       
   400 #-------------------------------------------------------------------------------
       
   401 
       
   402 """
       
   403 Class that implements the graphic representation of a transition
       
   404 """
       
   405 
       
   406 class SFC_Transition(Graphic_Element):
       
   407     
       
   408     # Create a new transition
       
   409     def __init__(self, parent, type = "reference", condition = "", id = None):
       
   410         Graphic_Element.__init__(self, parent)
       
   411         self.Type = type
       
   412         self.Condition = condition
       
   413         self.Id = id
       
   414         self.Size = wxSize(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1])
       
   415         # Create an input and output connector
       
   416         self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
       
   417         self.Output = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)
       
   418     
       
   419     # Destructor
       
   420     def __del__(self):
       
   421         self.Input = None
       
   422         self.Output = None
       
   423     
       
   424     # Forbids to change the transition size
       
   425     def SetSize(self, width, height):
       
   426         pass
       
   427     
       
   428     # Forbids to resize the transition
       
   429     def Resize(self, x, y, width, height):
       
   430         pass
       
   431     
       
   432     # Delete this transition by calling the appropriate method
       
   433     def Delete(self):
       
   434         self.Parent.DeleteTransition(self)
       
   435     
       
   436     # Unconnect input and output
       
   437     def Clean(self):
       
   438         self.Input.UnConnect()
       
   439         self.Output.UnConnect()
       
   440     
       
   441     # Refresh the transition bounding box
       
   442     def RefreshBoundingBox(self):
       
   443         dc = wxClientDC(self.Parent)
       
   444         if self.Condition != "":
       
   445             text_width, text_height = dc.GetTextExtent(self.Condition)
       
   446         else:
       
   447             text_width, text_height = dc.GetTextExtent("Transition")
       
   448         # Calculate the bounding box size
       
   449         bbx_width = self.Size[0] + CONNECTOR_SIZE + 2 + text_width
       
   450         bbx_y = self.Pos.y - max(0, (text_height - self.Size[1]) / 2)
       
   451         bbx_height = max(self.Size[1], text_height)
       
   452         self.BoundingBox = wxRect(self.Pos.x, bbx_y, bbx_width + 1, bbx_height + 1)
       
   453         
       
   454     # Returns the connector connected to input
       
   455     def GetPreviousConnector(self):
       
   456         wires = self.Input.GetWires()
       
   457         if len(wires) == 1:
       
   458             return wires[0][0].EndConnected
       
   459         return None
       
   460     
       
   461     # Returns the connector connected to output
       
   462     def GetNextConnector(self):
       
   463         wires = self.Output.GetWires()
       
   464         if len(wires) == 1:
       
   465             return wires[0][0].StartConnected
       
   466         return None
       
   467     
       
   468     # Refresh the positions of the transition connectors
       
   469     def RefreshConnectors(self):
       
   470         # Update input position
       
   471         self.Input.SetPosition(wxPoint(self.Size[0] / 2, 0))
       
   472         # Update output position
       
   473         self.Output.SetPosition(wxPoint(self.Size[0] / 2, self.Size[1]))
       
   474         self.RefreshConnected()
       
   475     
       
   476     # Refresh the position of the wires connected to transition
       
   477     def RefreshConnected(self, exclude = []):
       
   478         self.Input.MoveConnected(exclude)
       
   479         self.Output.MoveConnected(exclude)
       
   480     
       
   481     # Returns the transition connector that starts with the point given if it exists 
       
   482     def GetConnector(self, position):
       
   483         # Test input connector
       
   484         input_pos = self.Input.GetRelPosition()
       
   485         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
       
   486             return self.Input
       
   487         # Test output connector
       
   488         output_pos = self.Output.GetRelPosition()
       
   489         if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
       
   490             return self.Output
       
   491         return None
       
   492     
       
   493     # Returns input and output transition connectors
       
   494     def GetConnectors(self):
       
   495         return {"input":self.Input,"output":self.Output}
       
   496     
       
   497     # Test if point given is on transition input or output connector
       
   498     def TestConnector(self, pt, exclude=True):
       
   499         # Test input connector
       
   500         if self.Input.TestPoint(pt, exclude):
       
   501             return self.Input
       
   502         # Test output connector
       
   503         if self.Output.TestPoint(pt, exclude):
       
   504             return self.Output
       
   505         return None
       
   506 
       
   507     # Changes the transition type
       
   508     def SetType(self, type):
       
   509         self.Type = type
       
   510         
       
   511     # Returns the transition type
       
   512     def GetType(self):
       
   513         return self.Type
       
   514 
       
   515     # Changes the transition condition
       
   516     def SetCondition(self, condition):
       
   517         self.Condition = condition
       
   518         self.RefreshBoundingBox()
       
   519 
       
   520     # Returns the transition condition
       
   521     def GetCondition(self):
       
   522         return self.Condition
       
   523         
       
   524     # Returns the transition minimum size
       
   525     def GetMinSize(self):
       
   526         return SFC_TRANSITION_SIZE
       
   527     
       
   528     # Align input element with this step
       
   529     def RefreshInputPosition(self):
       
   530         wires = self.Input.GetWires()
       
   531         current_pos = self.Input.GetPosition(False)
       
   532         input = self.GetPreviousConnector()
       
   533         if input:
       
   534             input_pos = input.GetPosition(False)
       
   535             diffx = current_pos.x - input_pos.x
       
   536             input_block = input.GetParentBlock()
       
   537             if isinstance(input_block, SFC_Divergence):
       
   538                 input_block.MoveConnector(input, diffx)
       
   539             else:
       
   540                 if isinstance(input_block, SFC_Step):
       
   541                     input_block.MoveActionBlock((diffx, 0))
       
   542                 input_block.Move(diffx, 0)
       
   543                 input_block.RefreshInputPosition()
       
   544     
       
   545     # Align output element with this step
       
   546     def RefreshOutputPosition(self, move = None):
       
   547         wires = self.Output.GetWires()
       
   548         if len(wires) != 1:
       
   549             return
       
   550         current_pos = self.Output.GetPosition(False)
       
   551         output = wires[0][0].StartConnected
       
   552         output_pos = output.GetPosition(False)
       
   553         diffx = current_pos.x - output_pos.x
       
   554         output_block = output.GetParentBlock()
       
   555         if move:
       
   556             if isinstance(output_block, SFC_Step):
       
   557                 output_block.MoveActionBlock(move)
       
   558             wires[0][0].Move(move[0], move[1], True)
       
   559             if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
       
   560                 output_block.Move(move[0], move[1], self.Parent.Wires)
       
   561                 output_block.RefreshOutputPosition(move)
       
   562             else:
       
   563                 output_block.RefreshPosition()
       
   564         elif isinstance(output_block, SFC_Divergence):
       
   565             output_block.MoveConnector(output, diffx)
       
   566         else:
       
   567             if isinstance(output_block, SFC_Step):
       
   568                 output_block.MoveActionBlock((diffx, 0))
       
   569             output_block.Move(diffx, 0)
       
   570             output_block.RefreshOutputPosition()
       
   571     
       
   572     # Method called when a LeftDClick event have been generated
       
   573     def OnLeftDClick(self, event, scaling):
       
   574         # Edit the transition properties
       
   575         self.Parent.EditTransitionContent(self)
       
   576     
       
   577     # Method called when a RightUp event have been generated
       
   578     def OnRightUp(self, event, scaling):
       
   579         # Popup the menu with special items for a step
       
   580         self.Parent.PopupDefaultMenu()
       
   581     
       
   582     # Refreshes the transition state according to move defined and handle selected
       
   583     def ProcessDragging(self, movex, movey):
       
   584         self.Move(movex, 0)
       
   585         self.RefreshInputPosition()
       
   586         self.RefreshOutputPosition()
       
   587     
       
   588     # Refresh input element model
       
   589     def RefreshInputModel(self):
       
   590         input = self.GetPreviousConnector()
       
   591         if input:
       
   592             input_block = input.GetParentBlock()
       
   593             input_block.RefreshModel(False)
       
   594             if not isinstance(input_block, SFC_Divergence):
       
   595                 input_block.RefreshInputModel()
       
   596     
       
   597     # Refresh output element model
       
   598     def RefreshOutputModel(self, move=False):
       
   599         output = self.GetNextConnector()
       
   600         if output:
       
   601             output_block = output.GetParentBlock()
       
   602             output_block.RefreshModel(False)
       
   603             if not isinstance(output_block, SFC_Divergence) or move:
       
   604                 output_block.RefreshOutputModel(move)
       
   605     
       
   606     # Refreshes the transition model
       
   607     def RefreshModel(self, move=True):
       
   608         self.Parent.RefreshTransitionModel(self)
       
   609         # If transition has moved, refresh the model of wires connected to output
       
   610         if move:
       
   611             self.RefreshInputModel()
       
   612             self.RefreshOutputModel()
       
   613     
       
   614     # Draws transition
       
   615     def Draw(self, dc):
       
   616         dc.SetPen(wxBLACK_PEN)
       
   617         dc.SetBrush(wxBLACK_BRUSH)
       
   618         # Draw plain rectangle for representing the transition
       
   619         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
       
   620         # Draw transition condition
       
   621         if self.Condition != "":
       
   622             text_width, text_height = dc.GetTextExtent(self.Condition)
       
   623             condition = self.Condition
       
   624         else:
       
   625             text_width, text_height = dc.GetTextExtent("Transition")
       
   626             condition = "Transition"
       
   627         dc.DrawText(condition, self.Pos.x + self.Size[0] + CONNECTOR_SIZE + 2,
       
   628                     self.Pos.y + (self.Size[1] - text_height) / 2)
       
   629         # Draw input and output connectors
       
   630         self.Input.Draw(dc)
       
   631         self.Output.Draw(dc)
       
   632         Graphic_Element.Draw(self, dc)
       
   633 
       
   634 #-------------------------------------------------------------------------------
       
   635 #                Sequencial Function Chart Divergence and Convergence
       
   636 #-------------------------------------------------------------------------------
       
   637 
       
   638 """
       
   639 Class that implements the graphic representation of a divergence or convergence,
       
   640 selection or simultaneous
       
   641 """
       
   642 
       
   643 class SFC_Divergence(Graphic_Element):
       
   644     
       
   645     # Create a new divergence
       
   646     def __init__(self, parent, type, number = 2, id = None):
       
   647         Graphic_Element.__init__(self, parent)
       
   648         self.Type = type
       
   649         self.Id = id
       
   650         self.RealConnectors = None
       
   651         number = max(2, number)
       
   652         if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
       
   653             self.Size = wxSize((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 1)
       
   654         elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   655             self.Size = wxSize((number - 1) * SFC_DEFAULT_SEQUENCE_INTERVAL, 3)
       
   656         # Create an input and output connector
       
   657         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
       
   658             self.Inputs = [Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)]
       
   659             self.Outputs = []
       
   660             for i in xrange(number):
       
   661                 self.Outputs.append(Connector(self, "", "ANY", wxPoint(i * SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH))
       
   662         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   663             self.Inputs = []
       
   664             for i in xrange(number):
       
   665                 self.Inputs.append(Connector(self, "", "ANY", wxPoint(i * SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH))
       
   666             self.Outputs = [Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, self.Size[1]), SOUTH)]
       
   667     
       
   668     # Destructor
       
   669     def __del__(self):
       
   670         self.Inputs = []
       
   671         self.Outputs = []
       
   672     
       
   673     # Forbids to resize the divergence
       
   674     def Resize(self, x, y, width, height):
       
   675         pass
       
   676     
       
   677     # Delete this divergence by calling the appropriate method
       
   678     def Delete(self):
       
   679         self.Parent.DeleteDivergence(self)
       
   680     
       
   681     # Returns the divergence type
       
   682     def GetType(self):
       
   683         return self.Type
       
   684     
       
   685     # Unconnect input and output
       
   686     def Clean(self):
       
   687         for input in self.Inputs:
       
   688             input.UnConnect()
       
   689         for output in self.Outputs:
       
   690             output.UnConnect()
       
   691     
       
   692     # Add a branch to the divergence
       
   693     def AddBranch(self):
       
   694         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
       
   695             maxx = 0
       
   696             for output in self.Outputs:
       
   697                 pos = output.GetRelPosition()
       
   698                 maxx = max(maxx, pos.x)
       
   699             connector = Connector(self, "", "ANY", wxPoint(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, self.Size[1]), SOUTH)
       
   700             self.Outputs.append(connector)
       
   701             self.MoveConnector(connector, 0)
       
   702         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   703             maxx = 0
       
   704             for input in self.Inputs:
       
   705                 pos = input.GetRelPosition()
       
   706                 maxx = max(maxx, pos.x)
       
   707             connector = Connector(self, "", "ANY", wxPoint(maxx + SFC_DEFAULT_SEQUENCE_INTERVAL, 0), NORTH)
       
   708             self.Inputs.append(connector)
       
   709             self.MoveConnector(connector, SFC_DEFAULT_SEQUENCE_INTERVAL)
       
   710     
       
   711     # Remove a branch from the divergence
       
   712     def RemoveBranch(self, connector):
       
   713         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
       
   714             if connector in self.Outputs:
       
   715                 self.Outputs.remove(connector)
       
   716                 self.MoveConnector(self.Outputs[0], 0)
       
   717         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   718             if connector in self.Inputs:
       
   719                 self.Inputs.remove(connector)
       
   720                 self.MoveConnector(self.Inputs[0], 0)
       
   721     
       
   722     # Return the number of branches for the divergence
       
   723     def GetBranchNumber(self):
       
   724         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
       
   725             return len(self.Outputs)
       
   726         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   727             return len(self.Inputs)
       
   728     
       
   729     # Returns if the point given is in the bounding box
       
   730     def HitTest(self, pt):
       
   731         rect = self.BoundingBox
       
   732         return rect.InsideXY(pt.x, pt.y) or self.TestConnector(pt, False) != None
       
   733     
       
   734     # Refresh the divergence bounding box
       
   735     def RefreshBoundingBox(self):
       
   736         if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
       
   737             self.BoundingBox = wxRect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE, 
       
   738                 self.Size[0] + 1, self.Size[1] + CONNECTOR_SIZE * 2 + 1)
       
   739         elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   740             self.BoundingBox = wxRect(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y - CONNECTOR_SIZE, 
       
   741                 self.Size[0] + 2 * SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Size[1] + CONNECTOR_SIZE * 2 + 1)
       
   742     
       
   743     # Refresh the position of wires connected to divergence
       
   744     def RefreshConnected(self, exclude = []):
       
   745         for input in self.Inputs:
       
   746             input.MoveConnected(exclude)
       
   747         for output in self.Outputs:
       
   748             output.MoveConnected(exclude)
       
   749     
       
   750     # Moves the divergence connector given
       
   751     def MoveConnector(self, connector, movex):
       
   752         position = connector.GetRelPosition()
       
   753         connector.SetPosition(wxPoint(position.x + movex, position.y))
       
   754         minx = self.Size[0]
       
   755         maxx = 0
       
   756         for input in self.Inputs:
       
   757             input_pos = input.GetRelPosition()
       
   758             minx = min(minx, input_pos.x)
       
   759             maxx = max(maxx, input_pos.x)
       
   760         for output in self.Outputs:
       
   761             output_pos = output.GetRelPosition()
       
   762             minx = min(minx, output_pos.x)
       
   763             maxx = max(maxx, output_pos.x)
       
   764         if minx != 0:
       
   765             for input in self.Inputs:
       
   766                 input_pos = input.GetRelPosition()
       
   767                 input.SetPosition(wxPoint(input_pos.x - minx, input_pos.y))
       
   768             for output in self.Outputs:
       
   769                 output_pos = output.GetRelPosition()
       
   770                 output.SetPosition(wxPoint(output_pos.x - minx, output_pos.y))
       
   771         self.Pos.x += minx
       
   772         self.Size[0] = maxx - minx
       
   773         connector.MoveConnected()
       
   774         self.RefreshBoundingBox()
       
   775     
       
   776     # Returns the divergence connector that starts with the point given if it exists 
       
   777     def GetConnector(self, position):
       
   778         # Test input connector
       
   779         for input in self.Inputs:
       
   780             input_pos = input.GetPosition(False)
       
   781             if position.x == input_pos.x and position.y == input_pos.y:
       
   782                 return input
       
   783         # Test output connector
       
   784         for output in self.Outputs:
       
   785             output_pos = output.GetPosition(False)
       
   786             if position.x == output_pos.x and position.y == output_pos.y:
       
   787                 return output
       
   788         return None
       
   789     
       
   790     # Returns input and output divergence connectors 
       
   791     def GetConnectors(self):
       
   792         return {"inputs":self.Inputs,"outputs":self.Outputs}
       
   793     
       
   794     # Test if point given is on divergence input or output connector
       
   795     def TestConnector(self, pt, exclude=True):
       
   796         # Test input connector
       
   797         for input in self.Inputs:
       
   798             if input.TestPoint(pt, exclude):
       
   799                 return input
       
   800         # Test output connector
       
   801         for output in self.Outputs:
       
   802             if output.TestPoint(pt, exclude):
       
   803                 return output
       
   804         return None
       
   805     
       
   806     # Changes the divergence size
       
   807     def SetSize(self, width, height):
       
   808         for i, input in enumerate(self.Inputs):
       
   809             position = input.GetRelPosition()
       
   810             if self.RealConnectors:
       
   811                 input.SetPosition(wxPoint(int(round(self.RealConnectors["Inputs"][i] * width)), position.y))
       
   812             else:
       
   813                 input.SetPosition(wxPoint(int(round(float(position.x)*float(width)/float(self.Size[0]))), position.y))
       
   814             input.MoveConnected()
       
   815         for i, output in enumerate(self.Outputs):
       
   816             position = output.GetRelPosition()
       
   817             if self.RealConnectors:
       
   818                 output.SetPosition(wxPoint(int(round(self.RealConnectors["Outputs"][i] * width)), position.y))
       
   819             else:
       
   820                 output.SetPosition(wxPoint(int(round(float(position.x)*float(width)/float(self.Size[0]))), position.y))
       
   821             output.MoveConnected()
       
   822         self.Size = wxSize(width, self.Size[1])
       
   823         self.RefreshBoundingBox()
       
   824     
       
   825     # Returns the divergence minimum size
       
   826     def GetMinSize(self):
       
   827         return 0, self.Size[1]
       
   828     
       
   829     # Refresh the position of the block connected to connector
       
   830     def RefreshConnectedPosition(self, connector):
       
   831         wires = connector.GetWires()
       
   832         if len(wires) != 1:
       
   833             return
       
   834         current_pos = connector.GetPosition(False)
       
   835         if connector in self.Inputs:
       
   836             next = wires[0][0].EndConnected
       
   837         else:
       
   838             next = wires[0][0].StartConnected
       
   839         next_pos = next.GetPosition(False)
       
   840         diffx = current_pos.x - next_pos.x
       
   841         next_block = next.GetParentBlock()
       
   842         if isinstance(next_block, SFC_Divergence):
       
   843             next_block.MoveConnector(next, diffx)
       
   844         else:
       
   845             next_block.Move(diffx, 0)
       
   846             if connector in self.Inputs:
       
   847                 next_block.RefreshInputPosition()
       
   848             else:
       
   849                 next_block.RefreshOutputPosition()
       
   850     
       
   851     # Refresh the position of this divergence
       
   852     def RefreshPosition(self):
       
   853         y = 0
       
   854         for input in self.Inputs:
       
   855             wires = input.GetWires()
       
   856             if len(wires) != 1:
       
   857                 return
       
   858             previous = wires[0][0].EndConnected
       
   859             previous_pos = previous.GetPosition(False)
       
   860             y = max(y, previous_pos.y + GetWireSize(previous.GetParentBlock()))
       
   861         diffy = y - self.Pos.y
       
   862         if diffy != 0:
       
   863             self.Move(0, diffy, self.Parent.Wires)
       
   864             self.RefreshOutputPosition((0, diffy))
       
   865         for input in self.Inputs:
       
   866             input.MoveConnected()
       
   867     
       
   868     # Align output element with this divergence
       
   869     def RefreshOutputPosition(self, move = None):
       
   870         if move:
       
   871             for output_connector in self.Outputs:
       
   872                 wires = output_connector.GetWires()
       
   873                 if len(wires) != 1:
       
   874                     return
       
   875                 current_pos = output_connector.GetPosition(False)
       
   876                 output = wires[0][0].StartConnected
       
   877                 output_pos = output.GetPosition(False)
       
   878                 diffx = current_pos.x - output_pos.x
       
   879                 output_block = output.GetParentBlock()
       
   880                 if isinstance(output_block, SFC_Step):
       
   881                     output_block.MoveActionBlock(move)
       
   882                 wires[0][0].Move(move[0], move[1], True)
       
   883                 if not isinstance(output_block, SFC_Divergence) or output_block.GetConnectors()["inputs"].index(output) == 0:
       
   884                     output_block.Move(move[0], move[1], self.Parent.Wires)
       
   885                     output_block.RefreshOutputPosition(move)
       
   886     
       
   887     # Method called when a LeftDown event have been generated
       
   888     def OnLeftDown(self, event, scaling):
       
   889         pos = GetScaledEventPosition(event, scaling)
       
   890         # Test if a connector have been handled
       
   891         connector = self.TestConnector(pos, False)
       
   892         if connector:
       
   893             self.Handle = (HANDLE_CONNECTOR, connector)
       
   894             self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND))
       
   895             self.Selected = False
       
   896             # Initializes the last position
       
   897             self.oldPos = GetScaledEventPosition(event, scaling)
       
   898         else:
       
   899             self.RealConnectors = {"Inputs":[],"Outputs":[]}
       
   900             for input in self.Inputs:
       
   901                 position = input.GetRelPosition()
       
   902                 self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
       
   903             for output in self.Outputs:
       
   904                 position = output.GetRelPosition()
       
   905                 self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
       
   906             Graphic_Element.OnLeftDown(self, event, scaling)
       
   907     
       
   908     # Method called when a LeftUp event have been generated
       
   909     def OnLeftUp(self, event, scaling):
       
   910         self.RealConnectors = None
       
   911         handle_type, handle = self.Handle
       
   912         if handle_type == HANDLE_CONNECTOR:
       
   913             wires = handle.GetWires()
       
   914             if len(wires) != 1:
       
   915                 return
       
   916             if handle in self.Inputs:
       
   917                 block = wires[0][0].EndConnected.GetParentBlock()
       
   918             else:
       
   919                 block = wires[0][0].StartConnected.GetParentBlock()
       
   920             block.RefreshModel(False)
       
   921             if not isinstance(block, SFC_Divergence):
       
   922                 if handle in self.Inputs:
       
   923                     block.RefreshInputModel()
       
   924                 else:
       
   925                     block.RefreshOutputModel()
       
   926         Graphic_Element.OnLeftUp(self, event, scaling)
       
   927     
       
   928     # Method called when a RightUp event have been generated
       
   929     def OnRightUp(self, event, scaling):
       
   930         pos = GetScaledEventPosition(event, scaling)
       
   931         # Popup the menu with special items for a block and a connector if one is handled
       
   932         connector = self.TestConnector(pos, False)
       
   933         if connector:
       
   934             self.Handle = (HANDLE_CONNECTOR, connector)
       
   935             self.Parent.PopupDivergenceMenu(True)
       
   936         else:
       
   937             # Popup the divergence menu without delete branch
       
   938             self.Parent.PopupDivergenceMenu(False)
       
   939     
       
   940     # Refreshes the divergence state according to move defined and handle selected
       
   941     def ProcessDragging(self, movex, movey):
       
   942         handle_type, handle = self.Handle
       
   943         # A connector has been handled
       
   944         if handle_type == HANDLE_CONNECTOR:
       
   945             self.MoveConnector(handle, movex)
       
   946             self.RefreshConnectedPosition(handle)
       
   947     
       
   948     # Refresh output element model
       
   949     def RefreshOutputModel(self, move=False):
       
   950         if move:
       
   951             for output in self.Outputs:
       
   952                 wires = output.GetWires()
       
   953                 if len(wires) != 1:
       
   954                     return
       
   955                 output_block = wires[0][0].StartConnected.GetParentBlock()
       
   956                 output_block.RefreshModel(False)
       
   957                 if not isinstance(output_block, SFC_Divergence) or move:
       
   958                     output_block.RefreshOutputModel(move)
       
   959     
       
   960     # Refreshes the divergence model
       
   961     def RefreshModel(self, move=True):
       
   962         self.Parent.RefreshDivergenceModel(self)
       
   963         # If divergence has moved, refresh the model of wires connected to outputs
       
   964         if move:
       
   965             self.RefreshOutputModel()
       
   966     
       
   967     # Draws divergence
       
   968     def Draw(self, dc):
       
   969         dc.SetPen(wxBLACK_PEN)
       
   970         dc.SetBrush(wxBLACK_BRUSH)
       
   971         # Draw plain rectangle for representing the divergence
       
   972         if self.Type in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
       
   973             dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
       
   974         elif self.Type in [SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]:
       
   975             dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y, 
       
   976                         self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y)
       
   977             dc.DrawLine(self.Pos.x - SFC_SIMULTANEOUS_SEQUENCE_EXTRA, self.Pos.y + 3, 
       
   978                         self.Pos.x + self.Size[0] + SFC_SIMULTANEOUS_SEQUENCE_EXTRA + 1, self.Pos.y + 3)
       
   979         # Draw inputs and outputs connectors
       
   980         for input in self.Inputs:
       
   981             input.Draw(dc)
       
   982         for output in self.Outputs:
       
   983             output.Draw(dc)
       
   984         Graphic_Element.Draw(self, dc)
       
   985 
       
   986 #-------------------------------------------------------------------------------
       
   987 #                   Sequencial Function Chart Jump to Step
       
   988 #-------------------------------------------------------------------------------
       
   989 
       
   990 """
       
   991 Class that implements the graphic representation of a jump to step
       
   992 """
       
   993 
       
   994 class SFC_Jump(Graphic_Element):
       
   995     
       
   996     # Create a new jump
       
   997     def __init__(self, parent, target, id = None):
       
   998         Graphic_Element.__init__(self, parent)
       
   999         self.Target = target
       
  1000         self.Id = id
       
  1001         self.Size = wxSize(SFC_JUMP_SIZE[0], SFC_JUMP_SIZE[1])
       
  1002         # Create an input and output connector
       
  1003         self.Input = Connector(self, "", "ANY", wxPoint(self.Size[0] / 2, 0), NORTH)
       
  1004         
       
  1005     # Destructor
       
  1006     def __del__(self):
       
  1007         self.Inputs = None
       
  1008     
       
  1009     # Forbids to change the jump size
       
  1010     def SetSize(self, width, height):
       
  1011         pass
       
  1012     
       
  1013     # Forbids to resize jump
       
  1014     def Resize(self, x, y, width, height):
       
  1015         pass
       
  1016     
       
  1017     # Delete this jump by calling the appropriate method
       
  1018     def Delete(self):
       
  1019         self.Parent.DeleteJump(self)
       
  1020     
       
  1021     # Unconnect input
       
  1022     def Clean(self):
       
  1023         if self.Input:
       
  1024             self.Input.UnConnect()
       
  1025     
       
  1026     # Refresh the jump bounding box
       
  1027     def RefreshBoundingBox(self):
       
  1028         dc = wxClientDC(self.Parent)
       
  1029         text_width, text_height = dc.GetTextExtent(self.Target)
       
  1030         # Calculate the bounding box size
       
  1031         bbx_width = self.Size[0] + 2 + text_width
       
  1032         self.BoundingBox = wxRect(self.Pos.x, self.Pos.y - CONNECTOR_SIZE, 
       
  1033                 bbx_width + 1, self.Size[1] + CONNECTOR_SIZE + 1)
       
  1034     
       
  1035     # Returns the connector connected to input
       
  1036     def GetPreviousConnector(self):
       
  1037         if self.Input:
       
  1038             wires = self.Input.GetWires()
       
  1039             if len(wires) == 1:
       
  1040                 return wires[0][0].EndConnected
       
  1041         return None
       
  1042     
       
  1043     # Refresh the position of wires connected to jump
       
  1044     def RefreshConnected(self, exclude = []):
       
  1045         if self.Input:
       
  1046             self.Input.MoveConnected(exclude)
       
  1047     
       
  1048     # Returns input jump connector 
       
  1049     def GetConnector(self, position = None):
       
  1050         return self.Input
       
  1051     
       
  1052     # Test if point given is on jump input connector
       
  1053     def TestConnector(self, pt, exclude = True):
       
  1054         # Test input connector
       
  1055         if self.Input and self.Input.TestPoint(pt, exclude):
       
  1056             return self.Input
       
  1057         return None
       
  1058     
       
  1059     # Changes the jump target
       
  1060     def SetTarget(self, target):
       
  1061         self.Target = target
       
  1062         self.RefreshBoundingBox()
       
  1063     
       
  1064     # Returns the jump target
       
  1065     def GetTarget(self):
       
  1066         return self.Target
       
  1067     
       
  1068     # Returns the jump minimum size
       
  1069     def GetMinSize(self):
       
  1070         return SFC_JUMP_SIZE
       
  1071     
       
  1072     # Align input element with this jump
       
  1073     def RefreshInputPosition(self):
       
  1074         if self.Input:
       
  1075             current_pos = self.Input.GetPosition(False)
       
  1076             input = self.GetPreviousConnector()
       
  1077             if input:
       
  1078                 input_pos = input.GetPosition(False)
       
  1079                 diffx = current_pos.x - input_pos.x
       
  1080                 input_block = input.GetParentBlock()
       
  1081                 if isinstance(input_block, SFC_Divergence):
       
  1082                     input_block.MoveConnector(input, diffx)
       
  1083                 else:
       
  1084                     if isinstance(input_block, SFC_Step):
       
  1085                         input_block.MoveActionBlock((diffx, 0))
       
  1086                     input_block.Move(diffx, 0)
       
  1087                     input_block.RefreshInputPosition()
       
  1088     
       
  1089     # Can't align output element, because there is no output
       
  1090     def RefreshOutputPosition(self, move = None):
       
  1091         pass
       
  1092     
       
  1093     # Method called when a LeftDClick event have been generated
       
  1094     def OnLeftDClick(self, event, scaling):
       
  1095         # Edit the jump properties
       
  1096         self.Parent.EditJumpContent(self)
       
  1097     
       
  1098     # Method called when a RightUp event have been generated
       
  1099     def OnRightUp(self, event, scaling):
       
  1100         # Popup the default menu
       
  1101         self.Parent.PopupDefaultMenu()
       
  1102     
       
  1103     # Refreshes the jump state according to move defined and handle selected
       
  1104     def ProcessDragging(self, movex, movey):
       
  1105         self.Move(movex, 0)
       
  1106         self.RefreshInputPosition()
       
  1107     
       
  1108     # Refresh input element model
       
  1109     def RefreshInputModel(self):
       
  1110         input = self.GetPreviousConnector()
       
  1111         if input:
       
  1112             input_block = input.GetParentBlock()
       
  1113             input_block.RefreshModel(False)
       
  1114             if not isinstance(input_block, SFC_Divergence):
       
  1115                 input_block.RefreshInputModel()
       
  1116     
       
  1117     # Refresh output element model
       
  1118     def RefreshOutputModel(self, move=False):
       
  1119         pass
       
  1120     
       
  1121     # Refreshes the jump model
       
  1122     def RefreshModel(self, move=True):
       
  1123         self.Parent.RefreshJumpModel(self)
       
  1124         if move:
       
  1125             self.RefreshInputModel()
       
  1126     
       
  1127     # Draws divergence
       
  1128     def Draw(self, dc):
       
  1129         dc.SetPen(wxBLACK_PEN)
       
  1130         dc.SetBrush(wxBLACK_BRUSH)
       
  1131         # Draw plain rectangle for representing the divergence
       
  1132         dc.DrawLine(self.Pos.x + self.Size[0] / 2, self.Pos.y, self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])
       
  1133         points = [wxPoint(self.Pos.x, self.Pos.y),
       
  1134                   wxPoint(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1] / 3),
       
  1135                   wxPoint(self.Pos.x + self.Size[0], self.Pos.y),
       
  1136                   wxPoint(self.Pos.x + self.Size[0] / 2, self.Pos.y + self.Size[1])]
       
  1137         dc.DrawPolygon(points)
       
  1138         text_width, text_height = dc.GetTextExtent(self.Target)
       
  1139         dc.DrawText(self.Target, self.Pos.x + self.Size[0] + 2,
       
  1140                     self.Pos.y + (self.Size[1] - text_height) / 2)
       
  1141         # Draw input connector
       
  1142         if self.Input:
       
  1143             self.Input.Draw(dc)
       
  1144         Graphic_Element.Draw(self, dc)
       
  1145 
       
  1146 
       
  1147 #-------------------------------------------------------------------------------
       
  1148 #                   Sequencial Function Chart Action Block
       
  1149 #-------------------------------------------------------------------------------
       
  1150 
       
  1151 """
       
  1152 Class that implements the graphic representation of an action block
       
  1153 """
       
  1154 
       
  1155 class SFC_ActionBlock(Graphic_Element):
       
  1156     
       
  1157     # Create a new action block
       
  1158     def __init__(self, parent, actions = [], id = None):
       
  1159         Graphic_Element.__init__(self, parent)
       
  1160         self.Id = id
       
  1161         self.Size = wxSize(SFC_ACTION_MIN_SIZE[0], SFC_ACTION_MIN_SIZE[1])
       
  1162         # Create an input and output connector
       
  1163         self.Input = Connector(self, "", "ANY", wxPoint(0, SFC_ACTION_MIN_SIZE[1] / 2), WEST)
       
  1164         self.SetActions(actions)
       
  1165     
       
  1166     # Destructor
       
  1167     def __del__(self):
       
  1168         self.Input = None
       
  1169     
       
  1170     # Returns the number of action lines
       
  1171     def GetLineNumber(self):
       
  1172         return len(self.Actions)
       
  1173     
       
  1174     # Forbids to resize the action block
       
  1175     def Resize(self, x, y, width, height):
       
  1176         if x == 0:
       
  1177             self.SetSize(width, self.Size[1])
       
  1178     
       
  1179     # Delete this action block by calling the appropriate method
       
  1180     def Delete(self):
       
  1181         self.Parent.DeleteActionBlock(self)
       
  1182     
       
  1183     # Unconnect input and output
       
  1184     def Clean(self):
       
  1185         self.Input.UnConnect()
       
  1186         
       
  1187     # Refresh the action block bounding box
       
  1188     def RefreshBoundingBox(self):
       
  1189         self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
       
  1190     
       
  1191     # Refresh the position of wires connected to action block
       
  1192     def RefreshConnected(self, exclude = []):
       
  1193         self.Input.MoveConnected(exclude)
       
  1194     
       
  1195     # Returns input action block connector 
       
  1196     def GetConnector(self, position = None):
       
  1197         return self.Input
       
  1198     
       
  1199     # Test if point given is on action block input connector
       
  1200     def TestConnector(self, pt, exclude = True):
       
  1201         # Test input connector
       
  1202         if self.Input.TestPoint(pt, exclude):
       
  1203             return self.Input
       
  1204         return None
       
  1205     
       
  1206     # Changes the action block actions
       
  1207     def SetActions(self, actions):
       
  1208         dc = wxClientDC(self.Parent)
       
  1209         self.Actions = actions
       
  1210         self.ColSize = [0, 0, 0]
       
  1211         for action in self.Actions:
       
  1212             width, height = dc.GetTextExtent(action["qualifier"])
       
  1213             self.ColSize[0] = max(self.ColSize[0], width + 10)
       
  1214             if "duration" in action:
       
  1215                 width, height = dc.GetTextExtent("T#%s"%action["duration"])
       
  1216                 self.ColSize[0] = max(self.ColSize[0], width + 10)
       
  1217             width, height = dc.GetTextExtent(action["value"])
       
  1218             self.ColSize[1] = max(self.ColSize[1], width + 10)
       
  1219             if "indicator" in action and action["indicator"] != "":
       
  1220                 width, height = dc.GetTextExtent(action["indicator"])
       
  1221                 self.ColSize[2] = max(self.ColSize[2], width + 10)
       
  1222         self.Size = wxSize(max(self.ColSize[0] + self.ColSize[1] + self.ColSize[2],
       
  1223             self.Size[0]), len(self.Actions) * SFC_ACTION_MIN_SIZE[1])
       
  1224         self.RefreshBoundingBox()
       
  1225         if self.Input:
       
  1226             wires = self.Input.GetWires()
       
  1227             if len(wires) == 1:
       
  1228                 input_block = wires[0][0].EndConnected.GetParentBlock()
       
  1229                 input_block.RefreshOutputPosition()
       
  1230                 input_block.RefreshOutputModel(True)
       
  1231     
       
  1232     # Returns the action block actions
       
  1233     def GetActions(self):
       
  1234         return self.Actions
       
  1235     
       
  1236     # Returns the action block minimum size
       
  1237     def GetMinSize(self):
       
  1238         return SFC_ACTION_MIN_SIZE[0], len(self.Actions) * SFC_ACTION_MIN_SIZE[1]
       
  1239     
       
  1240     # Method called when a LeftDClick event have been generated
       
  1241     def OnLeftDClick(self, event, scaling):
       
  1242         # Edit the action block properties
       
  1243         self.Parent.EditActionBlockContent(self)
       
  1244     
       
  1245     # Refreshes the action block state according to move defined and handle selected
       
  1246     def ProcessDragging(self, movex, movey):
       
  1247         handle_type, handle = self.Handle
       
  1248         if handle_type == HANDLE_MOVE:
       
  1249             wires = self.Input.GetWires()
       
  1250             if len(wires) == 1:
       
  1251                 input_pos = wires[0][0].EndConnected.GetPosition(False)
       
  1252                 if self.Pos.x - input_pos.x + movex >= SFC_WIRE_MIN_SIZE:
       
  1253                     self.Move(movex, 0)
       
  1254         else:
       
  1255             Graphic_Element.ProcessDragging(self, movex, movey)
       
  1256     
       
  1257    # Refreshes the action block model
       
  1258     def RefreshModel(self, move=True):
       
  1259         self.Parent.RefreshActionBlockModel(self)
       
  1260     
       
  1261     # Draws divergence
       
  1262     def Draw(self, dc):
       
  1263         dc.SetPen(wxBLACK_PEN)
       
  1264         dc.SetBrush(wxWHITE_BRUSH)
       
  1265         colsize = [self.ColSize[0], self.Size[0] - self.ColSize[0] - self.ColSize[2], self.ColSize[2]]
       
  1266         # Draw plain rectangle for representing the action block
       
  1267         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
       
  1268         dc.DrawLine(self.Pos.x + colsize[0], self.Pos.y, 
       
  1269                 self.Pos.x + colsize[0], self.Pos.y + self.Size[1])
       
  1270         dc.DrawLine(self.Pos.x + colsize[0] + colsize[1], self.Pos.y, 
       
  1271                 self.Pos.x + colsize[0] + colsize[1], self.Pos.y + self.Size[1])
       
  1272         for i, action in enumerate(self.Actions):
       
  1273             if i != 0:
       
  1274                 dc.DrawLine(self.Pos.x, self.Pos.y + i * SFC_ACTION_MIN_SIZE[1], 
       
  1275                     self.Pos.x + self.Size[0], self.Pos.y + i * SFC_ACTION_MIN_SIZE[1])
       
  1276             text_width, text_height = dc.GetTextExtent(action["qualifier"])
       
  1277             if "duration" in action:
       
  1278                 dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2,
       
  1279                     self.Pos.y + i * SFC_ACTION_MIN_SIZE[1] + SFC_ACTION_MIN_SIZE[1] / 2 - text_height)
       
  1280                 text_width, text_height = dc.GetTextExtent("T#%s"%action["duration"])
       
  1281                 dc.DrawText("T#%s"%action["duration"], self.Pos.x + (colsize[0] - text_width) / 2,
       
  1282                     self.Pos.y + i * SFC_ACTION_MIN_SIZE[1] + SFC_ACTION_MIN_SIZE[1] / 2)
       
  1283             else:
       
  1284                 dc.DrawText(action["qualifier"], self.Pos.x + (colsize[0] - text_width) / 2,
       
  1285                         self.Pos.y + i * SFC_ACTION_MIN_SIZE[1] + (SFC_ACTION_MIN_SIZE[1] - text_height) / 2)
       
  1286             text_width, text_height = dc.GetTextExtent(action["value"])
       
  1287             dc.DrawText(action["value"], self.Pos.x + colsize[0] + (colsize[1] - text_width) / 2,
       
  1288                     self.Pos.y + i * SFC_ACTION_MIN_SIZE[1] + (SFC_ACTION_MIN_SIZE[1] - text_height) / 2)
       
  1289             if "indicator" in action:
       
  1290                 text_width, text_height = dc.GetTextExtent(action["indicator"])
       
  1291                 dc.DrawText(action["indicator"], self.Pos.x + colsize[0] + colsize[1] + (colsize[2] - text_width) / 2,
       
  1292                         self.Pos.y + i * SFC_ACTION_MIN_SIZE[1] + (SFC_ACTION_MIN_SIZE[1] - text_height) / 2)
       
  1293         # Draw input connector
       
  1294         self.Input.Draw(dc)
       
  1295         Graphic_Element.Draw(self, dc)