graphics/FBD_Objects.py
changeset 42 4a8400732001
parent 32 cf9efccff009
child 58 39cd981ff242
equal deleted inserted replaced
41:0688db995ddf 42:4a8400732001
    46         self.Extension = None
    46         self.Extension = None
    47         self.Name = name
    47         self.Name = name
    48         self.Id = id
    48         self.Id = id
    49         self.Inputs = []
    49         self.Inputs = []
    50         self.Outputs = []
    50         self.Outputs = []
       
    51         self.RefreshNameSize()
    51         self.SetType(type, extension, inputs)
    52         self.SetType(type, extension, inputs)
    52     
    53     
    53     # Destructor
    54     # Destructor
    54     def __del__(self):
    55     def __del__(self):
    55         self.Inputs = []
    56         self.Inputs = []
    64         for input in self.Inputs:
    65         for input in self.Inputs:
    65             input.UnConnect(delete = True)
    66             input.UnConnect(delete = True)
    66         for output in self.Outputs:
    67         for output in self.Outputs:
    67             output.UnConnect(delete = True)
    68             output.UnConnect(delete = True)
    68     
    69     
       
    70     # Refresh the size of text for name
       
    71     def RefreshNameSize(self):
       
    72         dc = wxClientDC(self.Parent)
       
    73         self.NameSize = dc.GetTextExtent(self.Name)
       
    74     
    69     # Refresh the block bounding box
    75     # Refresh the block bounding box
    70     def RefreshBoundingBox(self):
    76     def RefreshBoundingBox(self):
    71         dc = wxClientDC(self.Parent)
       
    72         # Calculate the size of the name outside the block
    77         # Calculate the size of the name outside the block
    73         text_width, text_height = dc.GetTextExtent(self.Name)
    78         text_width, text_height = self.NameSize
    74         # Calculate the bounding box size
    79         # Calculate the bounding box size
    75         bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2)
    80         bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2)
    76         bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE
    81         bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE
    77         if self.Name != "":
    82         if self.Name != "":
    78             bbx_y = self.Pos.y - (text_height + 2)
    83             bbx_y = self.Pos.y - (text_height + 2)
   149         return None
   154         return None
   150     
   155     
   151     # Changes the block type
   156     # Changes the block type
   152     def SetType(self, type, extension, inputs = None):
   157     def SetType(self, type, extension, inputs = None):
   153         if type != self.Type or self.Extension != extension: 
   158         if type != self.Type or self.Extension != extension: 
   154             self.Type = type
   159             if type != self.Type:
       
   160                 self.Type = type
       
   161                 dc = wxClientDC(self.Parent)
       
   162                 self.TypeSize = dc.GetTextExtent(self.Type)
   155             self.Extension = extension
   163             self.Extension = extension
   156             # Find the block definition from type given and create the corresponding
   164             # Find the block definition from type given and create the corresponding
   157             # inputs and outputs
   165             # inputs and outputs
   158             blocktype = GetBlockType(type, inputs)
   166             blocktype = GetBlockType(type, inputs)
   159             if blocktype:
   167             if blocktype:
   183                 if output_modifier == "negated":
   191                 if output_modifier == "negated":
   184                     connector.SetNegated(True)
   192                     connector.SetNegated(True)
   185                 elif output_modifier != "none":
   193                 elif output_modifier != "none":
   186                     connector.SetEdge(output_modifier)
   194                     connector.SetEdge(output_modifier)
   187                 self.Outputs.append(connector)
   195                 self.Outputs.append(connector)
       
   196             self.RefreshMinSize()
   188             self.RefreshConnectors()
   197             self.RefreshConnectors()
   189             self.RefreshBoundingBox()
   198             self.RefreshBoundingBox()
   190     
   199     
   191     # Returns the block type
   200     # Returns the block type
   192     def GetType(self):
   201     def GetType(self):
   193         return self.Type
   202         return self.Type
   194     
   203     
   195     # Changes the block name
   204     # Changes the block name
   196     def SetName(self, name):
   205     def SetName(self, name):
   197         self.Name = name
   206         self.Name = name
       
   207         self.RefreshNameSize()
   198     
   208     
   199     # Returs the block name
   209     # Returs the block name
   200     def GetName(self):
   210     def GetName(self):
   201         return self.Name
   211         return self.Name
   202     
   212     
   206     
   216     
   207     # Returs the extension name
   217     # Returs the extension name
   208     def GetExtension(self):
   218     def GetExtension(self):
   209         return self.Extension
   219         return self.Extension
   210     
   220     
   211     # Returns the block minimum size
   221     # Refresh the block minimum size
   212     def GetMinSize(self):
   222     def RefreshMinSize(self):
   213         dc = wxClientDC(self.Parent)
       
   214         text_width, text_height = dc.GetTextExtent(self.Type)
       
   215         # Calculate the inputs maximum width
   223         # Calculate the inputs maximum width
   216         max_input = 0
   224         max_input = 0
   217         for input in self.Inputs:
   225         for input in self.Inputs:
   218             w, h = dc.GetTextExtent(input.GetName())
   226             w, h = input.GetNameSize()
   219             max_input = max(max_input, w)
   227             max_input = max(max_input, w)
   220         # Calculate the outputs maximum width
   228         # Calculate the outputs maximum width
   221         max_output = 0
   229         max_output = 0
   222         for output in self.Outputs:
   230         for output in self.Outputs:
   223             w, h = dc.GetTextExtent(output.GetName())
   231             w, h = output.GetNameSize()
   224             max_output = max(max_output, w)
   232             max_output = max(max_output, w)
   225         width = max(text_width + 10, max_input + max_output + 15)
   233         width = max(self.TypeSize[0] + 10, max_input + max_output + 15)
   226         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
   234         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
   227         return width, height
   235         self.MinSize = width, height
       
   236     
       
   237     # Returns the block minimum size
       
   238     def GetMinSize(self):
       
   239         return self.MinSize
   228     
   240     
   229     # Changes the negated property of the connector handled
   241     # Changes the negated property of the connector handled
   230     def SetConnectorNegated(self, negated):
   242     def SetConnectorNegated(self, negated):
   231         handle_type, handle = self.Handle
   243         handle_type, handle = self.Handle
   232         if handle_type == HANDLE_CONNECTOR:
   244         if handle_type == HANDLE_CONNECTOR:
   269         dc.SetPen(wxBLACK_PEN)
   281         dc.SetPen(wxBLACK_PEN)
   270         dc.SetBrush(wxWHITE_BRUSH)
   282         dc.SetBrush(wxWHITE_BRUSH)
   271         # Draw a rectangle with the block size
   283         # Draw a rectangle with the block size
   272         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   284         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   273         # Draw block name and block type
   285         # Draw block name and block type
   274         namewidth, nameheight = dc.GetTextExtent(self.Name)
   286         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   275         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
   287                 self.Pos.y - (self.NameSize[1] + 2))
   276                 self.Pos.y - (nameheight + 2))
   288         dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2,
   277         typewidth, typeheight = dc.GetTextExtent(self.Type)
       
   278         dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - typewidth) / 2,
       
   279                 self.Pos.y + 5)
   289                 self.Pos.y + 5)
   280         # Draw inputs and outputs connectors
   290         # Draw inputs and outputs connectors
   281         for input in self.Inputs:
   291         for input in self.Inputs:
   282             input.Draw(dc)
   292             input.Draw(dc)
   283         for output in self.Outputs:
   293         for output in self.Outputs:
   302         self.ValueType = None
   312         self.ValueType = None
   303         self.Name = name
   313         self.Name = name
   304         self.Id = id
   314         self.Id = id
   305         self.Input = None
   315         self.Input = None
   306         self.Output = None
   316         self.Output = None
       
   317         self.RefreshNameSize()
   307         self.SetType(type, value_type)
   318         self.SetType(type, value_type)
   308     
   319     
   309     # Destructor
   320     # Destructor
   310     def __del__(self):
   321     def __del__(self):
   311         self.Input = None
   322         self.Input = None
   320     
   331     
   321     # Delete this variable by calling the appropriate method
   332     # Delete this variable by calling the appropriate method
   322     def Delete(self):
   333     def Delete(self):
   323         self.Parent.DeleteVariable(self)
   334         self.Parent.DeleteVariable(self)
   324     
   335     
       
   336     # Refresh the size of text for name
       
   337     def RefreshNameSize(self):
       
   338         dc = wxClientDC(self.Parent)
       
   339         self.NameSize = dc.GetTextExtent(self.Name)
       
   340     
   325     # Refresh the variable bounding box
   341     # Refresh the variable bounding box
   326     def RefreshBoundingBox(self):
   342     def RefreshBoundingBox(self):
   327         dc = wxClientDC(self.Parent)
       
   328         if self.Type in (OUTPUT, INOUT):
   343         if self.Type in (OUTPUT, INOUT):
   329             bbx_x = self.Pos.x - CONNECTOR_SIZE
   344             bbx_x = self.Pos.x - CONNECTOR_SIZE
   330         else:
   345         else:
   331             bbx_x = self.Pos.x
   346             bbx_x = self.Pos.x
   332         if self.Type == INOUT:
   347         if self.Type == INOUT:
   415         return self.Type
   430         return self.Type
   416     
   431     
   417     # Changes the variable name
   432     # Changes the variable name
   418     def SetName(self, name):
   433     def SetName(self, name):
   419         self.Name = name
   434         self.Name = name
       
   435         self.RefreshNameSize()
   420     
   436     
   421     # Returns the variable name
   437     # Returns the variable name
   422     def GetName(self):
   438     def GetName(self):
   423         return self.Name
   439         return self.Name
   424     
   440     
   425     # Returns the variable minimum size
   441     # Returns the variable minimum size
   426     def GetMinSize(self):
   442     def GetMinSize(self):
   427         dc = wxClientDC(self.Parent)
   443         return self.NameSize[0] + 10, self.NameSize[1] + 10
   428         text_width, text_height = dc.GetTextExtent(self.Name)
       
   429         return text_width + 10, text_height + 10
       
   430     
   444     
   431     # Method called when a LeftDClick event have been generated
   445     # Method called when a LeftDClick event have been generated
   432     def OnLeftDClick(self, event, dc, scaling):
   446     def OnLeftDClick(self, event, dc, scaling):
   433         # Edit the variable properties
   447         # Edit the variable properties
   434         self.Parent.EditVariableContent(self)
   448         self.Parent.EditVariableContent(self)
   458         dc.SetPen(wxBLACK_PEN)
   472         dc.SetPen(wxBLACK_PEN)
   459         dc.SetBrush(wxWHITE_BRUSH)
   473         dc.SetBrush(wxWHITE_BRUSH)
   460         # Draw a rectangle with the variable size
   474         # Draw a rectangle with the variable size
   461         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   475         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   462         # Draw variable name
   476         # Draw variable name
   463         namewidth, nameheight = dc.GetTextExtent(self.Name)
   477         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   464         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
   478                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   465                 self.Pos.y + (self.Size[1] - nameheight) / 2)
       
   466         # Draw connectors
   479         # Draw connectors
   467         if self.Input:
   480         if self.Input:
   468             self.Input.Draw(dc)
   481             self.Input.Draw(dc)
   469         if self.Output:    
   482         if self.Output:    
   470             self.Output.Draw(dc)
   483             self.Output.Draw(dc)
   493         if self.Type == CONNECTOR:
   506         if self.Type == CONNECTOR:
   494             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True)
   507             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True)
   495         else:
   508         else:
   496             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   509             self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST)
   497         self.RefreshConnectors()
   510         self.RefreshConnectors()
       
   511         self.RefreshNameSize()
   498     
   512     
   499     # Destructor
   513     # Destructor
   500     def __del__(self):
   514     def __del__(self):
   501         self.Connector = None
   515         self.Connector = None
   502     
   516     
   507     
   521     
   508     # Delete this connection by calling the appropriate method
   522     # Delete this connection by calling the appropriate method
   509     def Delete(self):
   523     def Delete(self):
   510         self.Parent.DeleteConnection(self)
   524         self.Parent.DeleteConnection(self)
   511     
   525     
       
   526     # Refresh the size of text for name
       
   527     def RefreshNameSize(self):
       
   528         dc = wxClientDC(self.Parent)
       
   529         self.NameSize = dc.GetTextExtent(self.Name)
       
   530     
   512     # Refresh the connection bounding box
   531     # Refresh the connection bounding box
   513     def RefreshBoundingBox(self):
   532     def RefreshBoundingBox(self):
   514         dc = wxClientDC(self.Parent)
       
   515         if self.Type == CONNECTOR:
   533         if self.Type == CONNECTOR:
   516             bbx_x = self.Pos.x - CONNECTOR_SIZE
   534             bbx_x = self.Pos.x - CONNECTOR_SIZE
   517         else:
   535         else:
   518             bbx_x = self.Pos.x
   536             bbx_x = self.Pos.x
   519         bbx_width = self.Size[0] + CONNECTOR_SIZE
   537         bbx_width = self.Size[0] + CONNECTOR_SIZE
   559         return self.Type
   577         return self.Type
   560     
   578     
   561     # Changes the connection name
   579     # Changes the connection name
   562     def SetName(self, name):
   580     def SetName(self, name):
   563         self.Name = name
   581         self.Name = name
       
   582         self.RefreshNameSize()
   564         
   583         
   565     # Returns the connection name
   584     # Returns the connection name
   566     def GetName(self):
   585     def GetName(self):
   567         return self.Name
   586         return self.Name
   568     
   587     
   569     # Returns the connection minimum size
   588     # Returns the connection minimum size
   570     def GetMinSize(self):
   589     def GetMinSize(self):
   571         dc = wxClientDC(self.Parent)
   590         text_width, text_height = self.NameSize
   572         text_width, text_height = dc.GetTextExtent(self.Name)
       
   573         if text_height % 2 == 1:
   591         if text_height % 2 == 1:
   574             text_height += 1
   592             text_height += 1
   575         return text_width + text_height + 20, text_height + 10
   593         return text_width + text_height + 20, text_height + 10
   576     
   594     
   577     # Method called when a LeftDClick event have been generated
   595     # Method called when a LeftDClick event have been generated
   597     def Draw(self, dc):
   615     def Draw(self, dc):
   598         dc.SetPen(wxBLACK_PEN)
   616         dc.SetPen(wxBLACK_PEN)
   599         dc.SetBrush(wxWHITE_BRUSH)
   617         dc.SetBrush(wxWHITE_BRUSH)
   600         # Draw a rectangle with the connection size with arrows in 
   618         # Draw a rectangle with the connection size with arrows in 
   601         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   619         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   602         namewidth, nameheight = dc.GetTextExtent(self.Name)
   620         arrowsize = min(self.Size[1] / 2, (self.Size[0] - self.NameSize[0] - 10) / 2)
   603         arrowsize = min(self.Size[1] / 2, (self.Size[0] - namewidth - 10) / 2)
       
   604         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, 
   621         dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, 
   605                 self.Pos.y + self.Size[1] / 2)
   622                 self.Pos.y + self.Size[1] / 2)
   606         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, 
   623         dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, 
   607                 self.Pos.x, self.Pos.y + self.Size[1])
   624                 self.Pos.x, self.Pos.y + self.Size[1])
   608         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   625         dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, 
   609                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   626                 self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2)
   610         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   627         dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, 
   611                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   628                 self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1])
   612         # Draw variable name
   629         # Draw variable name
   613         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
   630         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   614                 self.Pos.y + (self.Size[1] - nameheight) / 2)
   631                 self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2)
   615         # Draw connector
   632         # Draw connector
   616         if self.Connector:
   633         if self.Connector:
   617             self.Connector.Draw(dc)
   634             self.Connector.Draw(dc)
   618         Graphic_Element.Draw(self, dc)
   635         Graphic_Element.Draw(self, dc)