graphics/SFC_Objects.py
changeset 80 c798a68c5560
parent 71 0578bc212c20
child 108 9aa1fdfb7cb2
equal deleted inserted replaced
79:b22f661cbcfb 80:c798a68c5560
   434 """
   434 """
   435 
   435 
   436 class SFC_Transition(Graphic_Element):
   436 class SFC_Transition(Graphic_Element):
   437     
   437     
   438     # Create a new transition
   438     # Create a new transition
   439     def __init__(self, parent, type = "reference", condition = None, id = None):
   439     def __init__(self, parent, type = "reference", condition = None, priority = 0, id = None):
   440         Graphic_Element.__init__(self, parent)
   440         Graphic_Element.__init__(self, parent)
   441         self.Type = None
   441         self.Type = None
   442         self.Id = id
   442         self.Id = id
       
   443         self.Priority = 0
   443         self.Size = wx.Size(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1])
   444         self.Size = wx.Size(SFC_TRANSITION_SIZE[0], SFC_TRANSITION_SIZE[1])
   444         # Create an input and output connector
   445         # Create an input and output connector
   445         self.Input = Connector(self, "", "ANY", wx.Point(self.Size[0] / 2, 0), NORTH)
   446         self.Input = Connector(self, "", "ANY", wx.Point(self.Size[0] / 2, 0), NORTH)
   446         self.Output = Connector(self, "", "ANY", wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH)
   447         self.Output = Connector(self, "", "ANY", wx.Point(self.Size[0] / 2, self.Size[1]), SOUTH)
   447         self.SetType(type, condition)
   448         self.SetType(type, condition)
       
   449         self.SetPriority(priority)
   448     
   450     
   449     # Destructor
   451     # Destructor
   450     def __del__(self):
   452     def __del__(self):
   451         self.Input = None
   453         self.Input = None
   452         self.Output = None
   454         self.Output = None
   470             if self.Condition != "":
   472             if self.Condition != "":
   471                 self.ConditionSize = dc.GetTextExtent(self.Condition)
   473                 self.ConditionSize = dc.GetTextExtent(self.Condition)
   472             else:
   474             else:
   473                 self.ConditionSize = dc.GetTextExtent("Transition")
   475                 self.ConditionSize = dc.GetTextExtent("Transition")
   474     
   476     
       
   477     # Refresh the size of text for name
       
   478     def RefreshPrioritySize(self):
       
   479         if self.Priority != "":
       
   480             dc = wx.ClientDC(self.Parent)
       
   481             self.PrioritySize = dc.GetTextExtent(str(self.Priority))
       
   482         else:
       
   483             self.PrioritySize = None
       
   484 
   475     # Delete this transition by calling the appropriate method
   485     # Delete this transition by calling the appropriate method
   476     def Delete(self):
   486     def Delete(self):
   477         self.Parent.DeleteTransition(self)
   487         self.Parent.DeleteTransition(self)
   478     
   488     
   479     # Unconnect input and output
   489     # Unconnect input and output
   484             self.Condition.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
   494             self.Condition.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
   485     
   495     
   486     # Refresh the transition bounding box
   496     # Refresh the transition bounding box
   487     def RefreshBoundingBox(self):
   497     def RefreshBoundingBox(self):
   488         dc = wx.ClientDC(self.Parent)
   498         dc = wx.ClientDC(self.Parent)
       
   499         bbx_x, bbx_y, bbx_width, bbx_height = self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]
       
   500         if self.Priority != 0:
       
   501             bbx_y = self.Pos.y - self.PrioritySize[1] - 2
       
   502             bbx_width = max(self.Size[0], self.PrioritySize[0])
       
   503             bbx_height = self.Size[1] + self.PrioritySize[1] + 2
   489         if self.Type == "connection":
   504         if self.Type == "connection":
   490             bbx_x = self.Pos.x - CONNECTOR_SIZE
   505             bbx_x = self.Pos.x - CONNECTOR_SIZE
   491             bbx_width = self.Size[0] + CONNECTOR_SIZE
   506             bbx_width = bbx_width + CONNECTOR_SIZE
   492             bbx_y = self.Pos.y
       
   493             bbx_height = self.Size[1]
       
   494         else:
   507         else:
   495             text_width, text_height = self.ConditionSize
   508             text_width, text_height = self.ConditionSize
   496             # Calculate the bounding box size
   509             # Calculate the bounding box size
   497             bbx_x = self.Pos.x
   510             bbx_width = max(bbx_width, self.Size[0] + 5 + text_width)
   498             bbx_width = self.Size[0] + 5 + text_width
   511             bbx_y = min(bbx_y, self.Pos.y - max(0, (text_height - self.Size[1]) / 2))
   499             bbx_y = self.Pos.y - max(0, (text_height - 5 - self.Size[1]) / 2)
   512             bbx_height = max(bbx_height, self.Pos.y - bbx_y + (self.Size[1] + text_height) / 2)
   500             bbx_height = max(self.Size[1], text_height) - 5
       
   501         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   513         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   502         
   514         
   503     # Returns the connector connected to input
   515     # Returns the connector connected to input
   504     def GetPreviousConnector(self):
   516     def GetPreviousConnector(self):
   505         wires = self.Input.GetWires()
   517         wires = self.Input.GetWires()
   599         
   611         
   600     # Returns the transition type
   612     # Returns the transition type
   601     def GetType(self):
   613     def GetType(self):
   602         return self.Type
   614         return self.Type
   603 
   615 
       
   616     # Changes the transition priority
       
   617     def SetPriority(self, priority):
       
   618         self.Priority = priority
       
   619         self.RefreshPrioritySize()
       
   620         self.RefreshBoundingBox()
       
   621         
       
   622     # Returns the transition type
       
   623     def GetPriority(self):
       
   624         return self.Priority
       
   625 
   604     # Returns the transition condition
   626     # Returns the transition condition
   605     def GetCondition(self):
   627     def GetCondition(self):
   606         if self.Type != "connection":
   628         if self.Type != "connection":
   607             return self.Condition
   629             return self.Condition
   608         return None
   630         return None
   717                 condition = self.Condition
   739                 condition = self.Condition
   718             else:
   740             else:
   719                 condition = "Transition"
   741                 condition = "Transition"
   720             dc.DrawText(condition, self.Pos.x + self.Size[0] + 5,
   742             dc.DrawText(condition, self.Pos.x + self.Size[0] + 5,
   721                         self.Pos.y + (self.Size[1] - text_height) / 2)
   743                         self.Pos.y + (self.Size[1] - text_height) / 2)
       
   744         # Draw priority number
       
   745         if self.Priority != 0:
       
   746             priority_width, priority_height = self.PrioritySize
       
   747             dc.DrawText(str(self.Priority), self.Pos.x, self.Pos.y - self.PrioritySize[1] - 2)
   722         # Draw input and output connectors
   748         # Draw input and output connectors
   723         self.Input.Draw(dc)
   749         self.Input.Draw(dc)
   724         self.Output.Draw(dc)
   750         self.Output.Draw(dc)
   725         if self.Type == "connection":
   751         if self.Type == "connection":
   726             self.Condition.Draw(dc)
   752             self.Condition.Draw(dc)
   813         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
   839         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
   814             if connector in self.Inputs:
   840             if connector in self.Inputs:
   815                 self.Inputs.remove(connector)
   841                 self.Inputs.remove(connector)
   816                 self.MoveConnector(self.Inputs[0], 0)
   842                 self.MoveConnector(self.Inputs[0], 0)
   817     
   843     
       
   844     # Remove the handled branch from the divergence
       
   845     def RemoveHandledBranch(self):
       
   846         handle_type, handle = self.Handle
       
   847         if handle_type == HANDLE_CONNECTOR:
       
   848             handle.UnConnect(delete=True)
       
   849             self.RemoveBranch(handle)
       
   850             
   818     # Return the number of branches for the divergence
   851     # Return the number of branches for the divergence
   819     def GetBranchNumber(self):
   852     def GetBranchNumber(self):
   820         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
   853         if self.Type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
   821             return len(self.Outputs)
   854             return len(self.Outputs)
   822         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
   855         elif self.Type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]: