graphics/LD_Objects.py
changeset 550 cfa295862d55
parent 537 a31bf722aa82
child 554 08c26c62f5a7
--- a/graphics/LD_Objects.py	Mon Jul 04 15:24:44 2011 +0200
+++ b/graphics/LD_Objects.py	Fri Aug 12 17:04:55 2011 +0200
@@ -38,21 +38,18 @@
 class LD_PowerRail(Graphic_Element):
     
     # Create a new power rail
-    def __init__(self, parent, type, id = None, connectors = [True]):
+    def __init__(self, parent, type, id=None, connectors=1):
         Graphic_Element.__init__(self, parent)
         self.Type = None
         self.Connectors = []
         self.RealConnectors = None
         self.Id = id
         self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
-        if len(connectors) < 1:
-            connectors = [True]
         self.SetType(type, connectors)
         
     def Flush(self):
         for connector in self.Connectors:
-            if connector is not None:
-                connector.Flush()
+            connector.Flush()
         self.Connectors = []
     
     # Make a clone of this LD_PowerRail
@@ -65,25 +62,21 @@
             powerrail.SetPosition(self.Pos.x, self.Pos.y)
         powerrail.Connectors = []
         for connector in self.Connectors:
-            if connector is not None:
-                powerrail.Connectors.append(connector.Clone(powerrail))
-            else:
-                powerrail.Connectors.append(None)
+            powerrail.Connectors.append(connector.Clone(powerrail))
         return powerrail
     
     def GetConnectorTranslation(self, element):
-        return dict(zip([connector for connector in self.Connectors if connector is not None],
-                        [connector for connector in element.Connectors if connector is not None]))
+        return dict(zip([connector for connector in self.Connectors],
+                        [connector for connector in element.Connectors]))
     
     # Returns the RedrawRect
     def GetRedrawRect(self, movex = 0, movey = 0):
         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
         for connector in self.Connectors:
-            if connector is not None:
-                rect = rect.Union(connector.GetRedrawRect(movex, movey))
+            rect = rect.Union(connector.GetRedrawRect(movex, movey))
         if movex != 0 or movey != 0:
             for connector in self.Connectors:
-                if connector is not None and connector.IsConnected():
+                if connector.IsConnected():
                     rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
         return rect
     
@@ -91,7 +84,9 @@
     def SetSize(self, width, height):
         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
             Graphic_Element.SetSize(self, width, height)
-            self.RefreshConnectors()
+        else:
+            Graphic_Element.SetSize(self, LD_POWERRAIL_WIDTH, height)
+        self.RefreshConnectors()
     
     # Forbids to select a power rail
     def HitTest(self, pt):
@@ -112,8 +107,7 @@
     # Unconnect all connectors
     def Clean(self):
         for connector in self.Connectors:
-            if connector:
-                connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
+            connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
                 
     # Refresh the power rail bounding box
     def RefreshBoundingBox(self):
@@ -121,30 +115,24 @@
     
     # Refresh the power rail size
     def RefreshSize(self):
-        self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors))
+        self.Size = wx.Size(LD_POWERRAIL_WIDTH, max(LD_LINE_SIZE * len(self.Connectors), self.Size[1]))
         self.RefreshBoundingBox()
     
     # Returns the block minimum size
     def GetMinSize(self):
-        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
-            return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
-        else:
-            return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)
+        return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
     
     # Add a connector or a blank to this power rail at the last place
-    def AddConnector(self, connector = True):
-        self.InsertConnector(len(self.Connectors), connector)
+    def AddConnector(self):
+        self.InsertConnector(len(self.Connectors))
     
     # Add a connector or a blank to this power rail at the place given
-    def InsertConnector(self, idx, connector = True):
-        if connector:
-            if self.Type == LEFTRAIL:
-                connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
-            elif self.Type == RIGHTRAIL:
-                connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
-            self.Connectors.insert(idx, connector)
-        else:
-            self.Connectors.insert(idx, None)
+    def InsertConnector(self, idx):
+        if self.Type == LEFTRAIL:
+            connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
+        elif self.Type == RIGHTRAIL:
+            connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
+        self.Connectors.insert(idx, connector)
         self.RefreshSize()
         self.RefreshConnectors()
     
@@ -179,12 +167,6 @@
             return self.Connectors.index(connector)
         return None
     
-    # Returns if there is a connector in connectors list at the index given
-    def IsNullConnector(self, idx):
-        if idx < len(self.Connectors):
-            return self.Connectors[idx] == None
-        return False
-    
     # Delete the connector or blank from connectors list at the index given
     def DeleteConnector(self, idx):
         self.Connectors.pop(idx)
@@ -194,40 +176,25 @@
     # Refresh the positions of the power rail connectors
     def RefreshConnectors(self):
         scaling = self.Parent.GetScaling()
-        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
-            height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
-            interval = float(height) / float(max(len(self.Connectors) - 1, 1))
-            for i, connector in enumerate(self.Connectors):
-                if self.RealConnectors:
-                    position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
-                else:
-                    position = self.Extensions[0] + int(round(i * interval))
-                if scaling is not None:
-                    position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
-                if self.Type == LEFTRAIL:
-                    connector.SetPosition(wx.Point(self.Size[0], position))
-                elif self.Type == RIGHTRAIL:
-                    connector.SetPosition(wx.Point(0, position))
-        else:
-            position = self.Extensions[0]
-            for connector in self.Connectors:
-                if connector:
-                    if scaling is not None:
-                        ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
-                    else:
-                        ypos = position
-                    if self.Type == LEFTRAIL:
-                        connector.SetPosition(wx.Point(self.Size[0], ypos))
-                    elif self.Type == RIGHTRAIL:
-                        connector.SetPosition(wx.Point(0, ypos))
-                position += LD_LINE_SIZE
+        height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
+        interval = float(height) / float(max(len(self.Connectors) - 1, 1))
+        for i, connector in enumerate(self.Connectors):
+            if self.RealConnectors:
+                position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
+            else:
+                position = self.Extensions[0] + int(round(i * interval))
+            if scaling is not None:
+                position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
+            if self.Type == LEFTRAIL:
+                connector.SetPosition(wx.Point(self.Size[0], position))
+            elif self.Type == RIGHTRAIL:
+                connector.SetPosition(wx.Point(0, position))
         self.RefreshConnected()
     
     # Refresh the position of wires connected to power rail
     def RefreshConnected(self, exclude = []):
         for connector in self.Connectors:
-            if connector:
-                connector.MoveConnected(exclude)
+            connector.MoveConnected(exclude)
     
     # Returns the power rail connector that starts with the point given if it exists 
     def GetConnector(self, position, name = None):
@@ -235,7 +202,7 @@
         if name:
             # Test each connector if it exists
             for connector in self.Connectors:
-                if connector and name == connector.GetName():
+                if name == connector.GetName():
                     return connector
         return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None])
     
@@ -250,20 +217,20 @@
     # Test if point given is on one of the power rail connectors
     def TestConnector(self, pt, direction = None, exclude = True):
         for connector in self.Connectors:
-            if connector and connector.TestPoint(pt, direction, exclude):
+            if connector.TestPoint(pt, direction, exclude):
                 return connector
         return None
     
     # Returns the power rail type
     def SetType(self, type, connectors):
-        if type != self.Type or len(self.Connectors) != len(connectors):
+        if type != self.Type or len(self.Connectors) != connectors:
             # Create a connector or a blank according to 'connectors' and add it in
             # the connectors list
             self.Type = type
             self.Clean()
             self.Connectors = []
-            for connector in connectors:
-                self.AddConnector(connector)
+            for connector in xrange(connectors):
+                self.AddConnector()
             self.RefreshSize()
     
     # Returns the power rail type
@@ -351,8 +318,7 @@
         # of wires connected to connectors
         if move and self.Type == LEFTRAIL:
             for connector in self.Connectors:
-                if connector:
-                    connector.RefreshWires()
+                connector.RefreshWires()
     
     # Draws power rail
     def Draw(self, dc):
@@ -366,8 +332,7 @@
             dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
         # Draw connectors
         for connector in self.Connectors:
-            if connector:
-                connector.Draw(dc)
+            connector.Draw(dc)
         
 
 #-------------------------------------------------------------------------------