graphics/LD_Objects.py
changeset 42 4a8400732001
parent 28 fc23e1f415d8
child 50 4610aafc884e
equal deleted inserted replaced
41:0688db995ddf 42:4a8400732001
    57     def __del__(self):
    57     def __del__(self):
    58         self.Connectors = []
    58         self.Connectors = []
    59     
    59     
    60     # Forbids to change the power rail size
    60     # Forbids to change the power rail size
    61     def SetSize(self, width, height):
    61     def SetSize(self, width, height):
    62         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    62         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    63             Graphic_Element.SetSize(self, width, height)
    63             Graphic_Element.SetSize(self, width, height)
    64             self.RefreshConnectors()
    64             self.RefreshConnectors()
    65     
    65     
    66     # Forbids to select a power rail
    66     # Forbids to select a power rail
    67     def HitTest(self, pt):
    67     def HitTest(self, pt):
    68         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    68         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    69             return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
    69             return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
       
    70         return False
       
    71     
       
    72     # Forbids to select a power rail
       
    73     def IsInSelection(self, rect):
       
    74         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
       
    75             return Graphic_Element.IsInSelection(rect)
    70         return False
    76         return False
    71     
    77     
    72     # Deletes this power rail by calling the appropriate method
    78     # Deletes this power rail by calling the appropriate method
    73     def Delete(self):
    79     def Delete(self):
    74         self.Parent.DeletePowerRail(self)
    80         self.Parent.DeletePowerRail(self)
   299         self.Id = id
   305         self.Id = id
   300         self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
   306         self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
   301         # Create an input and output connector
   307         # Create an input and output connector
   302         self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
   308         self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
   303         self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
   309         self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
       
   310         self.RefreshNameSize()
       
   311         self.RefreshTypeSize()
   304     
   312     
   305     # Destructor
   313     # Destructor
   306     def __del__(self):
   314     def __del__(self):
   307         self.Input = None
   315         self.Input = None
   308         self.Output = None
   316         self.Output = None
   309     
   317     
   310     # Forbids to change the contact size
   318     # Forbids to change the contact size
   311     def SetSize(self, width, height):
   319     def SetSize(self, width, height):
   312         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   320         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   313             Graphic_Element.SetSize(self, width, height)
   321             Graphic_Element.SetSize(self, width, height)
   314             self.RefreshConnectors()
   322             self.RefreshConnectors()
   315     
   323     
   316     # Delete this contact by calling the appropriate method
   324     # Delete this contact by calling the appropriate method
   317     def Delete(self):
   325     def Delete(self):
   319     
   327     
   320     # Unconnect input and output
   328     # Unconnect input and output
   321     def Clean(self):
   329     def Clean(self):
   322         self.Input.UnConnect()
   330         self.Input.UnConnect()
   323         self.Output.UnConnect()
   331         self.Output.UnConnect()
       
   332     
       
   333     # Refresh the size of text for name
       
   334     def RefreshNameSize(self):
       
   335         dc = wxClientDC(self.Parent)
       
   336         if self.Name != "":
       
   337             self.NameSize = dc.GetTextExtent(self.Name)
       
   338         else:
       
   339             self.NameSize = 0, 0
       
   340     
       
   341     # Refresh the size of text for type
       
   342     def RefreshTypeSize(self):
       
   343         dc = wxClientDC(self.Parent)
       
   344         typetext = ""
       
   345         if self.Type == CONTACT_REVERSE:
       
   346             typetext = "/"
       
   347         elif self.Type == CONTACT_RISING:
       
   348             typetext = "P"
       
   349         elif self.Type == CONTACT_FALLING:
       
   350             typetext = "N"
       
   351         if typetext != "":
       
   352             self.TypeSize = dc.GetTextExtent(typetext)
       
   353         else:
       
   354             self.TypeSize = 0, 0
   324     
   355     
   325     # Refresh the contact bounding box
   356     # Refresh the contact bounding box
   326     def RefreshBoundingBox(self):
   357     def RefreshBoundingBox(self):
   327         dc = wxClientDC(self.Parent)
   358         dc = wxClientDC(self.Parent)
   328         # Calculate the size of the name outside the contact
   359         # Calculate the size of the name outside the contact
   389         self.RefreshConnected()
   420         self.RefreshConnected()
   390 
   421 
   391     # Changes the contact name
   422     # Changes the contact name
   392     def SetName(self, name):
   423     def SetName(self, name):
   393         self.Name = name
   424         self.Name = name
       
   425         self.RefreshNameSize()
   394 
   426 
   395     # Returns the contact name
   427     # Returns the contact name
   396     def GetName(self):
   428     def GetName(self):
   397         return self.Name
   429         return self.Name
   398 
   430 
   422         dc.SetBrush(wxBLACK_BRUSH)
   454         dc.SetBrush(wxBLACK_BRUSH)
   423         # Draw two rectangles for representing the contact
   455         # Draw two rectangles for representing the contact
   424         dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
   456         dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
   425         dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
   457         dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
   426         # Draw contact name
   458         # Draw contact name
   427         namewidth, nameheight = dc.GetTextExtent(self.Name)
   459         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   428         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
   460                 self.Pos.y - (self.NameSize[1] + 2))
   429                 self.Pos.y - (nameheight + 2))
       
   430         # Draw the modifier symbol in the middle of contact
   461         # Draw the modifier symbol in the middle of contact
   431         typetext = ""
   462         typetext = ""
   432         if self.Type == CONTACT_REVERSE:
   463         if self.Type == CONTACT_REVERSE:
   433             typetext = "/"
   464             typetext = "/"
   434         elif self.Type == CONTACT_RISING:
   465         elif self.Type == CONTACT_RISING:
   435             typetext = "P"
   466             typetext = "P"
   436         elif self.Type == CONTACT_FALLING:
   467         elif self.Type == CONTACT_FALLING:
   437             typetext = "N"
   468             typetext = "N"
   438         if typetext != "":
   469         if typetext != "":
   439             typewidth, typeheight = dc.GetTextExtent(typetext)
   470             dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
   440             dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
   471                     self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
   441                     self.Pos.y + (self.Size[1] - typeheight) / 2)
       
   442         # Draw input and output connectors
   472         # Draw input and output connectors
   443         self.Input.Draw(dc)
   473         self.Input.Draw(dc)
   444         self.Output.Draw(dc)
   474         self.Output.Draw(dc)
   445         Graphic_Element.Draw(self, dc)
   475         Graphic_Element.Draw(self, dc)
   446 
   476 
   463         self.Id = id
   493         self.Id = id
   464         self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
   494         self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
   465         # Create an input and output connector
   495         # Create an input and output connector
   466         self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
   496         self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
   467         self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
   497         self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
       
   498         self.RefreshNameSize()
       
   499         self.RefreshTypeSize()
   468         
   500         
   469     # Destructor
   501     # Destructor
   470     def __del__(self):
   502     def __del__(self):
   471         self.Input = None
   503         self.Input = None
   472         self.Output = None
   504         self.Output = None
   473     
   505     
   474     # Forbids to change the contact size
   506     # Forbids to change the contact size
   475     def SetSize(self, width, height):
   507     def SetSize(self, width, height):
   476         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   508         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   477             Graphic_Element.SetSize(self, width, height)
   509             Graphic_Element.SetSize(self, width, height)
   478             self.RefreshConnectors()
   510             self.RefreshConnectors()
   479     
   511     
   480     # Delete this coil by calling the appropriate method
   512     # Delete this coil by calling the appropriate method
   481     def Delete(self):
   513     def Delete(self):
   484     # Unconnect input and output
   516     # Unconnect input and output
   485     def Clean(self):
   517     def Clean(self):
   486         self.Input.UnConnect()
   518         self.Input.UnConnect()
   487         self.Output.UnConnect()
   519         self.Output.UnConnect()
   488                 
   520                 
       
   521     # Refresh the size of text for name
       
   522     def RefreshNameSize(self):
       
   523         dc = wxClientDC(self.Parent)
       
   524         if self.Name != "":
       
   525             self.NameSize = dc.GetTextExtent(self.Name)
       
   526         else:
       
   527             self.NameSize = 0, 0
       
   528     
       
   529     # Refresh the size of text for type
       
   530     def RefreshTypeSize(self):
       
   531         dc = wxClientDC(self.Parent)
       
   532         typetext = ""
       
   533         if self.Type == COIL_REVERSE:
       
   534             typetext = "/"
       
   535         elif self.Type == COIL_SET:
       
   536             typetext = "S"
       
   537         elif self.Type == COIL_RESET:
       
   538             typetext = "R"
       
   539         if typetext != "":
       
   540             self.TypeSize = dc.GetTextExtent(typetext)
       
   541         else:
       
   542             self.TypeSize = 0, 0
       
   543     
   489     # Refresh the coil bounding box
   544     # Refresh the coil bounding box
   490     def RefreshBoundingBox(self):
   545     def RefreshBoundingBox(self):
   491         dc = wxClientDC(self.Parent)
   546         dc = wxClientDC(self.Parent)
   492         # Calculate the size of the name outside the coil
   547         # Calculate the size of the name outside the coil
   493         text_width, text_height = dc.GetTextExtent(self.Name)
   548         text_width, text_height = dc.GetTextExtent(self.Name)
   553         self.RefreshConnected()
   608         self.RefreshConnected()
   554     
   609     
   555     # Changes the coil name
   610     # Changes the coil name
   556     def SetName(self, name):
   611     def SetName(self, name):
   557         self.Name = name
   612         self.Name = name
       
   613         self.RefreshNameSize()
   558 
   614 
   559     # Returns the coil name
   615     # Returns the coil name
   560     def GetName(self):
   616     def GetName(self):
   561         return self.Name
   617         return self.Name
   562     
   618     
   563     # Changes the coil type
   619     # Changes the coil type
   564     def SetType(self, type):
   620     def SetType(self, type):
   565         self.Type = type
   621         self.Type = type
       
   622         self.RefreshTypeSize()
   566     
   623     
   567     # Returns the coil type
   624     # Returns the coil type
   568     def GetType(self):
   625     def GetType(self):
   569         return self.Type
   626         return self.Type
   570     
   627     
   588         dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
   645         dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
   589         dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
   646         dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
   590         dc.SetPen(wxBLACK_PEN)
   647         dc.SetPen(wxBLACK_PEN)
   591         dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
   648         dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
   592         # Draw coil name
   649         # Draw coil name
   593         namewidth, nameheight = dc.GetTextExtent(self.Name)
   650         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
   594         dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
   651                 self.Pos.y - (self.NameSize[1] + 2))
   595                 self.Pos.y - (nameheight + 2))
       
   596         # Draw the modifier symbol in the middle of coil
   652         # Draw the modifier symbol in the middle of coil
   597         typetext = ""
   653         typetext = ""
   598         if self.Type == COIL_REVERSE:
   654         if self.Type == COIL_REVERSE:
   599             typetext = "/"
   655             typetext = "/"
   600         elif self.Type == COIL_SET:
   656         elif self.Type == COIL_SET:
   601             typetext = "S"
   657             typetext = "S"
   602         elif self.Type == COIL_RESET:
   658         elif self.Type == COIL_RESET:
   603             typetext = "R"
   659             typetext = "R"
   604         if typetext != "":
   660         if typetext != "":
   605             typewidth, typeheight = dc.GetTextExtent(typetext)
   661             dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
   606             dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
   662                     self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
   607                     self.Pos.y + (self.Size[1] - typeheight) / 2)
       
   608         # Draw input and output connectors
   663         # Draw input and output connectors
   609         self.Input.Draw(dc)
   664         self.Input.Draw(dc)
   610         self.Output.Draw(dc)
   665         self.Output.Draw(dc)
   611         Graphic_Element.Draw(self, dc)
   666         Graphic_Element.Draw(self, dc)