graphics/LD_Objects.py
changeset 550 cfa295862d55
parent 537 a31bf722aa82
child 554 08c26c62f5a7
equal deleted inserted replaced
549:b0d6819119c3 550:cfa295862d55
    36 """
    36 """
    37 
    37 
    38 class LD_PowerRail(Graphic_Element):
    38 class LD_PowerRail(Graphic_Element):
    39     
    39     
    40     # Create a new power rail
    40     # Create a new power rail
    41     def __init__(self, parent, type, id = None, connectors = [True]):
    41     def __init__(self, parent, type, id=None, connectors=1):
    42         Graphic_Element.__init__(self, parent)
    42         Graphic_Element.__init__(self, parent)
    43         self.Type = None
    43         self.Type = None
    44         self.Connectors = []
    44         self.Connectors = []
    45         self.RealConnectors = None
    45         self.RealConnectors = None
    46         self.Id = id
    46         self.Id = id
    47         self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
    47         self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
    48         if len(connectors) < 1:
       
    49             connectors = [True]
       
    50         self.SetType(type, connectors)
    48         self.SetType(type, connectors)
    51         
    49         
    52     def Flush(self):
    50     def Flush(self):
    53         for connector in self.Connectors:
    51         for connector in self.Connectors:
    54             if connector is not None:
    52             connector.Flush()
    55                 connector.Flush()
       
    56         self.Connectors = []
    53         self.Connectors = []
    57     
    54     
    58     # Make a clone of this LD_PowerRail
    55     # Make a clone of this LD_PowerRail
    59     def Clone(self, parent, id = None, pos = None):
    56     def Clone(self, parent, id = None, pos = None):
    60         powerrail = LD_PowerRail(parent, self.Type, id)
    57         powerrail = LD_PowerRail(parent, self.Type, id)
    63             powerrail.SetPosition(pos.x, pos.y)
    60             powerrail.SetPosition(pos.x, pos.y)
    64         else:
    61         else:
    65             powerrail.SetPosition(self.Pos.x, self.Pos.y)
    62             powerrail.SetPosition(self.Pos.x, self.Pos.y)
    66         powerrail.Connectors = []
    63         powerrail.Connectors = []
    67         for connector in self.Connectors:
    64         for connector in self.Connectors:
    68             if connector is not None:
    65             powerrail.Connectors.append(connector.Clone(powerrail))
    69                 powerrail.Connectors.append(connector.Clone(powerrail))
       
    70             else:
       
    71                 powerrail.Connectors.append(None)
       
    72         return powerrail
    66         return powerrail
    73     
    67     
    74     def GetConnectorTranslation(self, element):
    68     def GetConnectorTranslation(self, element):
    75         return dict(zip([connector for connector in self.Connectors if connector is not None],
    69         return dict(zip([connector for connector in self.Connectors],
    76                         [connector for connector in element.Connectors if connector is not None]))
    70                         [connector for connector in element.Connectors]))
    77     
    71     
    78     # Returns the RedrawRect
    72     # Returns the RedrawRect
    79     def GetRedrawRect(self, movex = 0, movey = 0):
    73     def GetRedrawRect(self, movex = 0, movey = 0):
    80         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
    74         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
    81         for connector in self.Connectors:
    75         for connector in self.Connectors:
    82             if connector is not None:
    76             rect = rect.Union(connector.GetRedrawRect(movex, movey))
    83                 rect = rect.Union(connector.GetRedrawRect(movex, movey))
       
    84         if movex != 0 or movey != 0:
    77         if movex != 0 or movey != 0:
    85             for connector in self.Connectors:
    78             for connector in self.Connectors:
    86                 if connector is not None and connector.IsConnected():
    79                 if connector.IsConnected():
    87                     rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
    80                     rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
    88         return rect
    81         return rect
    89     
    82     
    90     # Forbids to change the power rail size
    83     # Forbids to change the power rail size
    91     def SetSize(self, width, height):
    84     def SetSize(self, width, height):
    92         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    85         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    93             Graphic_Element.SetSize(self, width, height)
    86             Graphic_Element.SetSize(self, width, height)
    94             self.RefreshConnectors()
    87         else:
       
    88             Graphic_Element.SetSize(self, LD_POWERRAIL_WIDTH, height)
       
    89         self.RefreshConnectors()
    95     
    90     
    96     # Forbids to select a power rail
    91     # Forbids to select a power rail
    97     def HitTest(self, pt):
    92     def HitTest(self, pt):
    98         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    93         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
    99             return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, exclude=False) != None
    94             return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, exclude=False) != None
   110         self.Parent.DeletePowerRail(self)
   105         self.Parent.DeletePowerRail(self)
   111     
   106     
   112     # Unconnect all connectors
   107     # Unconnect all connectors
   113     def Clean(self):
   108     def Clean(self):
   114         for connector in self.Connectors:
   109         for connector in self.Connectors:
   115             if connector:
   110             connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
   116                 connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
       
   117                 
   111                 
   118     # Refresh the power rail bounding box
   112     # Refresh the power rail bounding box
   119     def RefreshBoundingBox(self):
   113     def RefreshBoundingBox(self):
   120         self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   114         self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
   121     
   115     
   122     # Refresh the power rail size
   116     # Refresh the power rail size
   123     def RefreshSize(self):
   117     def RefreshSize(self):
   124         self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors))
   118         self.Size = wx.Size(LD_POWERRAIL_WIDTH, max(LD_LINE_SIZE * len(self.Connectors), self.Size[1]))
   125         self.RefreshBoundingBox()
   119         self.RefreshBoundingBox()
   126     
   120     
   127     # Returns the block minimum size
   121     # Returns the block minimum size
   128     def GetMinSize(self):
   122     def GetMinSize(self):
   129         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   123         return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
   130             return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
       
   131         else:
       
   132             return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)
       
   133     
   124     
   134     # Add a connector or a blank to this power rail at the last place
   125     # Add a connector or a blank to this power rail at the last place
   135     def AddConnector(self, connector = True):
   126     def AddConnector(self):
   136         self.InsertConnector(len(self.Connectors), connector)
   127         self.InsertConnector(len(self.Connectors))
   137     
   128     
   138     # Add a connector or a blank to this power rail at the place given
   129     # Add a connector or a blank to this power rail at the place given
   139     def InsertConnector(self, idx, connector = True):
   130     def InsertConnector(self, idx):
   140         if connector:
   131         if self.Type == LEFTRAIL:
   141             if self.Type == LEFTRAIL:
   132             connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
   142                 connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
   133         elif self.Type == RIGHTRAIL:
   143             elif self.Type == RIGHTRAIL:
   134             connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
   144                 connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
   135         self.Connectors.insert(idx, connector)
   145             self.Connectors.insert(idx, connector)
       
   146         else:
       
   147             self.Connectors.insert(idx, None)
       
   148         self.RefreshSize()
   136         self.RefreshSize()
   149         self.RefreshConnectors()
   137         self.RefreshConnectors()
   150     
   138     
   151     # Moves the divergence connector given
   139     # Moves the divergence connector given
   152     def MoveConnector(self, connector, movey):
   140     def MoveConnector(self, connector, movey):
   177     def GetConnectorIndex(self, connector):
   165     def GetConnectorIndex(self, connector):
   178         if connector in self.Connectors:
   166         if connector in self.Connectors:
   179             return self.Connectors.index(connector)
   167             return self.Connectors.index(connector)
   180         return None
   168         return None
   181     
   169     
   182     # Returns if there is a connector in connectors list at the index given
       
   183     def IsNullConnector(self, idx):
       
   184         if idx < len(self.Connectors):
       
   185             return self.Connectors[idx] == None
       
   186         return False
       
   187     
       
   188     # Delete the connector or blank from connectors list at the index given
   170     # Delete the connector or blank from connectors list at the index given
   189     def DeleteConnector(self, idx):
   171     def DeleteConnector(self, idx):
   190         self.Connectors.pop(idx)
   172         self.Connectors.pop(idx)
   191         self.RefreshConnectors()
   173         self.RefreshConnectors()
   192         self.RefreshSize()
   174         self.RefreshSize()
   193     
   175     
   194     # Refresh the positions of the power rail connectors
   176     # Refresh the positions of the power rail connectors
   195     def RefreshConnectors(self):
   177     def RefreshConnectors(self):
   196         scaling = self.Parent.GetScaling()
   178         scaling = self.Parent.GetScaling()
   197         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
   179         height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
   198             height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
   180         interval = float(height) / float(max(len(self.Connectors) - 1, 1))
   199             interval = float(height) / float(max(len(self.Connectors) - 1, 1))
   181         for i, connector in enumerate(self.Connectors):
   200             for i, connector in enumerate(self.Connectors):
   182             if self.RealConnectors:
   201                 if self.RealConnectors:
   183                 position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
   202                     position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
   184             else:
   203                 else:
   185                 position = self.Extensions[0] + int(round(i * interval))
   204                     position = self.Extensions[0] + int(round(i * interval))
   186             if scaling is not None:
   205                 if scaling is not None:
   187                 position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
   206                     position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
   188             if self.Type == LEFTRAIL:
   207                 if self.Type == LEFTRAIL:
   189                 connector.SetPosition(wx.Point(self.Size[0], position))
   208                     connector.SetPosition(wx.Point(self.Size[0], position))
   190             elif self.Type == RIGHTRAIL:
   209                 elif self.Type == RIGHTRAIL:
   191                 connector.SetPosition(wx.Point(0, position))
   210                     connector.SetPosition(wx.Point(0, position))
       
   211         else:
       
   212             position = self.Extensions[0]
       
   213             for connector in self.Connectors:
       
   214                 if connector:
       
   215                     if scaling is not None:
       
   216                         ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
       
   217                     else:
       
   218                         ypos = position
       
   219                     if self.Type == LEFTRAIL:
       
   220                         connector.SetPosition(wx.Point(self.Size[0], ypos))
       
   221                     elif self.Type == RIGHTRAIL:
       
   222                         connector.SetPosition(wx.Point(0, ypos))
       
   223                 position += LD_LINE_SIZE
       
   224         self.RefreshConnected()
   192         self.RefreshConnected()
   225     
   193     
   226     # Refresh the position of wires connected to power rail
   194     # Refresh the position of wires connected to power rail
   227     def RefreshConnected(self, exclude = []):
   195     def RefreshConnected(self, exclude = []):
   228         for connector in self.Connectors:
   196         for connector in self.Connectors:
   229             if connector:
   197             connector.MoveConnected(exclude)
   230                 connector.MoveConnected(exclude)
       
   231     
   198     
   232     # Returns the power rail connector that starts with the point given if it exists 
   199     # Returns the power rail connector that starts with the point given if it exists 
   233     def GetConnector(self, position, name = None):
   200     def GetConnector(self, position, name = None):
   234         # if a name is given
   201         # if a name is given
   235         if name:
   202         if name:
   236             # Test each connector if it exists
   203             # Test each connector if it exists
   237             for connector in self.Connectors:
   204             for connector in self.Connectors:
   238                 if connector and name == connector.GetName():
   205                 if name == connector.GetName():
   239                     return connector
   206                     return connector
   240         return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None])
   207         return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None])
   241     
   208     
   242     # Returns all the power rail connectors 
   209     # Returns all the power rail connectors 
   243     def GetConnectors(self):
   210     def GetConnectors(self):
   248             return {"inputs": connectors, "outputs": []}
   215             return {"inputs": connectors, "outputs": []}
   249     
   216     
   250     # Test if point given is on one of the power rail connectors
   217     # Test if point given is on one of the power rail connectors
   251     def TestConnector(self, pt, direction = None, exclude = True):
   218     def TestConnector(self, pt, direction = None, exclude = True):
   252         for connector in self.Connectors:
   219         for connector in self.Connectors:
   253             if connector and connector.TestPoint(pt, direction, exclude):
   220             if connector.TestPoint(pt, direction, exclude):
   254                 return connector
   221                 return connector
   255         return None
   222         return None
   256     
   223     
   257     # Returns the power rail type
   224     # Returns the power rail type
   258     def SetType(self, type, connectors):
   225     def SetType(self, type, connectors):
   259         if type != self.Type or len(self.Connectors) != len(connectors):
   226         if type != self.Type or len(self.Connectors) != connectors:
   260             # Create a connector or a blank according to 'connectors' and add it in
   227             # Create a connector or a blank according to 'connectors' and add it in
   261             # the connectors list
   228             # the connectors list
   262             self.Type = type
   229             self.Type = type
   263             self.Clean()
   230             self.Clean()
   264             self.Connectors = []
   231             self.Connectors = []
   265             for connector in connectors:
   232             for connector in xrange(connectors):
   266                 self.AddConnector(connector)
   233                 self.AddConnector()
   267             self.RefreshSize()
   234             self.RefreshSize()
   268     
   235     
   269     # Returns the power rail type
   236     # Returns the power rail type
   270     def GetType(self):
   237     def GetType(self):
   271         return self.Type
   238         return self.Type
   349         self.Parent.RefreshPowerRailModel(self)
   316         self.Parent.RefreshPowerRailModel(self)
   350         # If power rail has moved and power rail is of type LEFT, refresh the model 
   317         # If power rail has moved and power rail is of type LEFT, refresh the model 
   351         # of wires connected to connectors
   318         # of wires connected to connectors
   352         if move and self.Type == LEFTRAIL:
   319         if move and self.Type == LEFTRAIL:
   353             for connector in self.Connectors:
   320             for connector in self.Connectors:
   354                 if connector:
   321                 connector.RefreshWires()
   355                     connector.RefreshWires()
       
   356     
   322     
   357     # Draws power rail
   323     # Draws power rail
   358     def Draw(self, dc):
   324     def Draw(self, dc):
   359         Graphic_Element.Draw(self, dc)
   325         Graphic_Element.Draw(self, dc)
   360         dc.SetPen(wx.BLACK_PEN)
   326         dc.SetPen(wx.BLACK_PEN)
   364             dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
   330             dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
   365         else:
   331         else:
   366             dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
   332             dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
   367         # Draw connectors
   333         # Draw connectors
   368         for connector in self.Connectors:
   334         for connector in self.Connectors:
   369             if connector:
   335             connector.Draw(dc)
   370                 connector.Draw(dc)
       
   371         
   336         
   372 
   337 
   373 #-------------------------------------------------------------------------------
   338 #-------------------------------------------------------------------------------
   374 #                         Ladder Diagram Contact
   339 #                         Ladder Diagram Contact
   375 #-------------------------------------------------------------------------------
   340 #-------------------------------------------------------------------------------