graphics/FBD_Objects.py
changeset 566 6014ef82a98a
parent 563 3f92a5e18804
child 625 b7062a7018ec
equal deleted inserted replaced
565:94c11207aa6f 566:6014ef82a98a
    49         self.Inputs = []
    49         self.Inputs = []
    50         self.Outputs = []
    50         self.Outputs = []
    51         self.Colour = wx.BLACK
    51         self.Colour = wx.BLACK
    52         self.Pen = MiterPen(wx.BLACK)
    52         self.Pen = MiterPen(wx.BLACK)
    53         self.SetType(type, extension, inputs, connectors, executionControl)
    53         self.SetType(type, extension, inputs, connectors, executionControl)
    54         self.Errors = {}
    54         self.Highlights = {}
    55     
    55     
    56     # Make a clone of this FBD_Block
    56     # Make a clone of this FBD_Block
    57     def Clone(self, parent, id = None, name = "", pos = None):
    57     def Clone(self, parent, id = None, name = "", pos = None):
    58         if self.Name != "" and name == "":
    58         if self.Name != "" and name == "":
    59             name = self.Name
    59             name = self.Name
   366         # If block has moved, refresh the model of wires connected to outputs
   366         # If block has moved, refresh the model of wires connected to outputs
   367         if move:
   367         if move:
   368             for output in self.Outputs:
   368             for output in self.Outputs:
   369                 output.RefreshWires()
   369                 output.RefreshWires()
   370     
   370     
   371     def AddError(self, infos, start, end):
   371     # Adds an highlight to the block
       
   372     def AddHighlight(self, infos, start, end ,highlight_type):
   372         if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0:
   373         if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0:
   373             self.Errors[infos[0]] = (start, end)
   374             highlights = self.Highlights.setdefault(infos[0], [])
       
   375             AddHighlight(highlights, (start, end, highlight_type))
   374         elif infos[0] == "input" and infos[1] < len(self.Inputs):
   376         elif infos[0] == "input" and infos[1] < len(self.Inputs):
   375             self.Inputs[infos[1]].AddError(infos[2:], start, end)
   377             self.Inputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
   376         elif infos[0] == "output" and infos[1] < len(self.Outputs):
   378         elif infos[0] == "output" and infos[1] < len(self.Outputs):
   377             self.Outputs[infos[1]].AddError(infos[2:], start, end)
   379             self.Outputs[infos[1]].AddHighlight(infos[2:], start, end, highlight_type)
       
   380     
       
   381     # Removes an highlight from the block
       
   382     def RemoveHighlight(self, infos, start, end, highlight_type):
       
   383         if infos[0] in ["type", "name"]:
       
   384             highlights = self.Highlights.get(infos[0], [])
       
   385             if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0:
       
   386                 self.Highlights.pop(infos[0])
       
   387         elif infos[0] == "input" and infos[1] < len(self.Inputs):
       
   388             self.Inputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
       
   389         elif infos[0] == "output" and infos[1] < len(self.Outputs):
       
   390             self.Outputs[infos[1]].RemoveHighlight(infos[2:], start, end, highlight_type)
       
   391             
       
   392     # Removes all the highlights of one particular type from the block
       
   393     def ClearHighlight(self, highlight_type=None):
       
   394         if highlight_type is None:
       
   395             self.Highlights = {}
       
   396         else:
       
   397             highlight_items = self.Highlights.items()
       
   398             for name, highlights in highlight_items:
       
   399                 highlights = ClearHighlights(highlights, highlight_type)
       
   400                 if len(highlights) == 0:
       
   401                     self.Highlights.pop(name)
       
   402         for input in self.Inputs:
       
   403             input.ClearHighlights(highlight_type)
       
   404         for output in self.Outputs:
       
   405             output.ClearHighlights(highlight_type)
   378     
   406     
   379     # Draws block
   407     # Draws block
   380     def Draw(self, dc):
   408     def Draw(self, dc):
   381         Graphic_Element.Draw(self, dc)
   409         Graphic_Element.Draw(self, dc)
   382         dc.SetPen(self.Pen)
   410         dc.SetPen(self.Pen)
   408             output.Draw(dc)
   436             output.Draw(dc)
   409         if self.ExecutionOrder != 0:
   437         if self.ExecutionOrder != 0:
   410             # Draw block execution order
   438             # Draw block execution order
   411             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
   439             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
   412                     self.Pos.y + self.Size[1] + 2)
   440                     self.Pos.y + self.Size[1] + 2)
   413         if self.Errors.has_key("name"):
   441         
   414             HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
   442         if not getattr(dc, "printing", False):
   415         if self.Errors.has_key("type"):
   443             DrawHighlightedText(dc, self.Name, self.Highlights.get("name", []), name_pos[0], name_pos[1])
   416             HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])    
   444             DrawHighlightedText(dc, self.Type, self.Highlights.get("type", []), type_pos[0], type_pos[1])
   417         dc.SetTextForeground(wx.BLACK)
       
   418         
   445         
   419 
   446 
   420 #-------------------------------------------------------------------------------
   447 #-------------------------------------------------------------------------------
   421 #                        Function Block Diagram Variable
   448 #                        Function Block Diagram Variable
   422 #-------------------------------------------------------------------------------
   449 #-------------------------------------------------------------------------------
   436         self.SetName(name)
   463         self.SetName(name)
   437         self.SetExecutionOrder(executionOrder)
   464         self.SetExecutionOrder(executionOrder)
   438         self.Input = None
   465         self.Input = None
   439         self.Output = None
   466         self.Output = None
   440         self.SetType(type, value_type)
   467         self.SetType(type, value_type)
   441         self.Errors = []
   468         self.Highlights = []
   442     
   469     
   443     # Make a clone of this FBD_Variable
   470     # Make a clone of this FBD_Variable
   444     def Clone(self, parent, id = None, pos = None):
   471     def Clone(self, parent, id = None, pos = None):
   445         variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id)
   472         variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id)
   446         variable.SetSize(self.Size[0], self.Size[1])
   473         variable.SetSize(self.Size[0], self.Size[1])
   642         # of wires connected to output connector
   669         # of wires connected to output connector
   643         if move and self.Type != OUTPUT:
   670         if move and self.Type != OUTPUT:
   644             if self.Output:
   671             if self.Output:
   645                 self.Output.RefreshWires()
   672                 self.Output.RefreshWires()
   646     
   673     
   647     def AddError(self, infos, start, end):
   674     # Adds an highlight to the variable
       
   675     def AddHighlight(self, infos, start, end, highlight_type):
   648         if infos[0] == "expression" and start[0] == 0 and end[0] == 0:
   676         if infos[0] == "expression" and start[0] == 0 and end[0] == 0:
   649             self.Errors.append((start[1], end[1]))
   677             AddHighlight(self.Highlights, (start, end, highlight_type))
       
   678     
       
   679     # Removes an highlight from the variable
       
   680     def RemoveHighlight(self, infos, start, end, highlight_type):
       
   681         if infos[0] == "expression":
       
   682             RemoveHighlight(self.Highlights, (start, end, highlight_type))
       
   683     
       
   684     # Removes all the highlights of one particular type from the variable
       
   685     def ClearHighlight(self, highlight_type=None):
       
   686         ClearHighlights(self.Highlights, highlight_type)
   650     
   687     
   651     # Draws variable
   688     # Draws variable
   652     def Draw(self, dc):
   689     def Draw(self, dc):
   653         Graphic_Element.Draw(self, dc)
   690         Graphic_Element.Draw(self, dc)
   654         dc.SetPen(MiterPen(wx.BLACK))
   691         dc.SetPen(MiterPen(wx.BLACK))
   674             self.Output.Draw(dc)
   711             self.Output.Draw(dc)
   675         if self.ExecutionOrder != 0:
   712         if self.ExecutionOrder != 0:
   676             # Draw variable execution order
   713             # Draw variable execution order
   677             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
   714             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
   678                     self.Pos.y + self.Size[1] + 2)
   715                     self.Pos.y + self.Size[1] + 2)
   679         for start, end in self.Errors:
   716         if not getattr(dc, "printing", False):
   680             offset = dc.GetTextExtent(self.Name[:start])
   717             DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])
   681             size = dc.GetTextExtent(self.Name[start:end + 1])
   718             
   682             HighlightErrorZone(dc, text_pos[0] + offset[0], text_pos[1], size[0], size[1])
       
   683 
       
   684 #-------------------------------------------------------------------------------
   719 #-------------------------------------------------------------------------------
   685 #                        Function Block Diagram Connector
   720 #                        Function Block Diagram Connector
   686 #-------------------------------------------------------------------------------
   721 #-------------------------------------------------------------------------------
   687 
   722 
   688 """
   723 """
   697         self.Type = type
   732         self.Type = type
   698         self.Id = id
   733         self.Id = id
   699         self.SetName(name)
   734         self.SetName(name)
   700         self.Pos = wx.Point(0, 0)
   735         self.Pos = wx.Point(0, 0)
   701         self.Size = wx.Size(0, 0)
   736         self.Size = wx.Size(0, 0)
       
   737         self.Highlights = []
   702         # Create an input or output connector according to connection type
   738         # Create an input or output connector according to connection type
   703         if self.Type == CONNECTOR:
   739         if self.Type == CONNECTOR:
   704             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   740             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True)
   705         else:
   741         else:
   706             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
   742             self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST)
   848         # If connection has moved and connection is of type CONTINUATION, refresh
   884         # If connection has moved and connection is of type CONTINUATION, refresh
   849         # the model of wires connected to connector
   885         # the model of wires connected to connector
   850         if move and self.Type == CONTINUATION:
   886         if move and self.Type == CONTINUATION:
   851             if self.Connector:
   887             if self.Connector:
   852                 self.Connector.RefreshWires()
   888                 self.Connector.RefreshWires()
       
   889     
       
   890     # Adds an highlight to the connection
       
   891     def AddHighlight(self, infos, start, end, highlight_type):
       
   892         if infos[0] == "name" and start[0] == 0 and end[0] == 0:
       
   893             AddHighlight(self.Highlights, (start, end, highlight_type))
       
   894     
       
   895     # Removes an highlight from the connection
       
   896     def RemoveHighlight(self, infos, start, end, highlight_type):
       
   897         if infos[0] == "name":
       
   898             RemoveHighlight(self.Highlights, (start, end, highlight_type))
       
   899     
       
   900     # Removes all the highlights of one particular type from the connection
       
   901     def ClearHighlight(self, highlight_type=None):
       
   902         ClearHighlights(self.Highlights, highlight_type)
   853     
   903     
   854     # Draws connection
   904     # Draws connection
   855     def Draw(self, dc):
   905     def Draw(self, dc):
   856         Graphic_Element.Draw(self, dc)
   906         Graphic_Element.Draw(self, dc)
   857         dc.SetPen(MiterPen(wx.BLACK))
   907         dc.SetPen(MiterPen(wx.BLACK))
   872         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   922         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   873                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   923                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   874         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   924         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   875                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   925                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   876         # Draw connection name
   926         # Draw connection name
   877         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - name_size[0]) / 2,
   927         text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, 
   878                 self.Pos.y + (self.Size[1] - name_size[1]) / 2)
   928                     self.Pos.y + (self.Size[1] - name_size[1]) / 2)
       
   929         dc.DrawText(self.Name, text_pos[0], text_pos[1])
   879         # Draw connector
   930         # Draw connector
   880         if self.Connector:
   931         if self.Connector:
   881             self.Connector.Draw(dc)
   932             self.Connector.Draw(dc)
   882         
   933         
       
   934         if not getattr(dc, "printing", False):
       
   935             DrawHighlightedText(dc, self.Name, self.Highlights, text_pos[0], text_pos[1])
       
   936